578
576
branch_c.base, base_revision_id='rev2a')
579
577
revision = md.install_revisions(tree_b.branch.repository)
581
def test_handle_target_not_a_branch(self):
582
tree_a, tree_b, branch_c = self.make_trees()
583
branch_c.pull(tree_a.branch)
584
self.build_tree_contents([('tree_a/file', 'content_q\ncontent_r\n')])
585
tree_a.commit('rev3a', rev_id='rev3a')
586
md = self.from_objects(tree_a.branch.repository, 'rev3a', 500, 36,
587
branch_c.base, base_revision_id='rev2a')
588
md.target_branch = self.get_url('not-a-branch')
589
self.assertRaises(errors.TargetNotBranch, md.install_revisions,
590
tree_b.branch.repository)
593
580
class TestMergeDirective1Branch(tests.TestCaseWithTransport,
594
581
TestMergeDirectiveBranch):
633
620
public_branch=None, message=None, base_revision_id=None):
634
621
include_patch = (patch_type in ('bundle', 'diff'))
635
622
include_bundle = (patch_type == 'bundle')
636
self.assertTrue(patch_type in ('bundle', 'diff', None))
623
assert patch_type in ('bundle', 'diff', None)
637
624
return merge_directive.MergeDirective2.from_objects(
638
625
repository, revision_id, time, timezone, target_branch,
639
626
include_patch, include_bundle, local_target_branch, public_branch,
699
686
self.assertEqual('booga', md.patch)
700
687
self.assertEqual('diff', md.patch_type)
701
688
self.assertEqual('Hi mom!', md.message)
704
class TestHook(object):
705
"""Hook callback for test purposes."""
707
def __init__(self, result=None):
711
def __call__(self, params):
712
self.calls.append(params)
716
class HookMailClient(mail_client.MailClient):
717
"""Mail client for testing hooks."""
719
def __init__(self, config):
723
def compose(self, prompt, to, subject, attachment, mime_subtype,
724
extension, basename=None, body=None):
728
class TestBodyHook(tests.TestCaseWithTransport):
730
def compose_with_hooks(self, test_hooks, supports_body=True):
731
client = HookMailClient({})
732
client.supports_body = supports_body
733
for test_hook in test_hooks:
734
merge_directive.MergeDirective.hooks.install_named_hook(
735
'merge_request_body', test_hook, 'test')
736
tree = self.make_branch_and_tree('foo')
738
directive = merge_directive.MergeDirective2(
739
tree.branch.last_revision(), 'sha', 0, 0, 'sha',
740
source_branch=tree.branch.base,
741
base_revision_id=tree.branch.last_revision(),
742
message='This code rox')
743
directive.compose_merge_request(client, 'jrandom@example.com',
745
return client, directive
747
def test_no_supports_body(self):
748
test_hook = TestHook('foo')
749
old_warn = trace.warning
752
warnings.append(args)
755
client, directive = self.compose_with_hooks([test_hook],
758
trace.warning = old_warn
759
self.assertEqual(0, len(test_hook.calls))
760
self.assertEqual(('Cannot run merge_request_body hooks because mail'
761
' client %s does not support message bodies.',
762
'HookMailClient'), warnings[0])
764
def test_body_hook(self):
765
test_hook = TestHook('foo')
766
client, directive = self.compose_with_hooks([test_hook])
767
self.assertEqual(1, len(test_hook.calls))
768
self.assertEqual('foo', client.body)
769
params = test_hook.calls[0]
770
self.assertIsInstance(params,
771
merge_directive.MergeRequestBodyParams)
772
self.assertIs(None, params.body)
773
self.assertIs(None, params.orig_body)
774
self.assertEqual('jrandom@example.com', params.to)
775
self.assertEqual('[MERGE] This code rox', params.subject)
776
self.assertEqual(directive, params.directive)
777
self.assertEqual('foo-1', params.basename)
779
def test_body_hook_chaining(self):
780
test_hook1 = TestHook('foo')
781
test_hook2 = TestHook('bar')
782
client = self.compose_with_hooks([test_hook1, test_hook2])[0]
783
self.assertEqual(None, test_hook1.calls[0].body)
784
self.assertEqual(None, test_hook1.calls[0].orig_body)
785
self.assertEqual('foo', test_hook2.calls[0].body)
786
self.assertEqual(None, test_hook2.calls[0].orig_body)
787
self.assertEqual('bar', client.body)