482
390
def test_email(self):
483
391
tree_a, tree_b, branch_c = self.make_trees()
484
md = self.from_objects(tree_a.branch.repository, 'rev2a', 476, 60,
485
tree_b.branch.base, patch_type=None,
486
public_branch=tree_a.branch.base)
392
md = merge_directive.MergeDirective.from_objects(
393
tree_a.branch.repository, 'rev2a', 476, 60, tree_b.branch.base,
394
patch_type=None, public_branch=tree_a.branch.base)
487
395
message = md.to_email('pqm@example.com', tree_a.branch)
488
self.assertContainsRe(message.as_string(), self.EMAIL1)
396
self.assertContainsRe(message.as_string(), EMAIL1)
489
397
md.message = 'Commit of rev2a with special message'
490
398
message = md.to_email('pqm@example.com', tree_a.branch)
491
self.assertContainsRe(message.as_string(), self.EMAIL2)
399
self.assertContainsRe(message.as_string(), EMAIL2)
493
401
def test_install_revisions_branch(self):
494
402
tree_a, tree_b, branch_c = self.make_trees()
495
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
496
tree_b.branch.base, patch_type=None,
497
public_branch=tree_a.branch.base)
403
md = merge_directive.MergeDirective.from_objects(
404
tree_a.branch.repository, 'rev2a', 500, 36, tree_b.branch.base,
405
patch_type=None, public_branch=tree_a.branch.base)
498
406
self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
499
407
revision = md.install_revisions(tree_b.branch.repository)
500
408
self.assertEqual('rev2a', revision)
501
409
self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
503
def test_get_merge_request(self):
504
tree_a, tree_b, branch_c = self.make_trees()
505
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
506
tree_b.branch.base, patch_type='bundle',
507
public_branch=tree_a.branch.base)
508
self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
509
md.install_revisions(tree_b.branch.repository)
510
base, revision, verified = md.get_merge_request(
511
tree_b.branch.repository)
512
if isinstance(md, merge_directive.MergeDirective):
513
self.assertIs(None, base)
514
self.assertEqual('inapplicable', verified)
516
self.assertEqual('rev1', base)
517
self.assertEqual('verified', verified)
518
self.assertEqual('rev2a', revision)
519
self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
520
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
521
tree_b.branch.base, patch_type=None,
522
public_branch=tree_a.branch.base)
523
base, revision, verified = md.get_merge_request(
524
tree_b.branch.repository)
525
if isinstance(md, merge_directive.MergeDirective):
526
self.assertIs(None, base)
527
self.assertEqual('inapplicable', verified)
529
self.assertEqual('rev1', base)
530
self.assertEqual('inapplicable', verified)
531
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
532
tree_b.branch.base, patch_type='diff',
533
public_branch=tree_a.branch.base)
534
base, revision, verified = md.get_merge_request(
535
tree_b.branch.repository)
536
if isinstance(md, merge_directive.MergeDirective):
537
self.assertIs(None, base)
538
self.assertEqual('inapplicable', verified)
540
self.assertEqual('rev1', base)
541
self.assertEqual('verified', verified)
543
base, revision, verified = md.get_merge_request(
544
tree_b.branch.repository)
545
if isinstance(md, merge_directive.MergeDirective):
546
self.assertIs(None, base)
547
self.assertEqual('inapplicable', verified)
549
self.assertEqual('rev1', base)
550
self.assertEqual('failed', verified)
552
411
def test_install_revisions_bundle(self):
553
412
tree_a, tree_b, branch_c = self.make_trees()
554
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
555
tree_b.branch.base, patch_type='bundle',
556
public_branch=tree_a.branch.base)
413
md = merge_directive.MergeDirective.from_objects(
414
tree_a.branch.repository, 'rev2a', 500, 36, tree_b.branch.base,
415
patch_type='bundle', public_branch=tree_a.branch.base)
557
416
self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
558
417
revision = md.install_revisions(tree_b.branch.repository)
559
418
self.assertEqual('rev2a', revision)
562
421
def test_get_target_revision_nofetch(self):
563
422
tree_a, tree_b, branch_c = self.make_trees()
564
423
tree_b.branch.fetch(tree_a.branch)
565
md = self.from_objects( tree_a.branch.repository, 'rev2a', 500, 36,
566
tree_b.branch.base, patch_type=None,
567
public_branch=tree_a.branch.base)
424
md = merge_directive.MergeDirective.from_objects(
425
tree_a.branch.repository, 'rev2a', 500, 36, tree_b.branch.base,
426
patch_type=None, public_branch=tree_a.branch.base)
568
427
md.source_branch = '/dev/null'
569
428
revision = md.install_revisions(tree_b.branch.repository)
570
429
self.assertEqual('rev2a', revision)
572
def test_use_submit_for_missing_dependency(self):
573
tree_a, tree_b, branch_c = self.make_trees()
574
branch_c.pull(tree_a.branch)
575
self.build_tree_contents([('tree_a/file', 'content_q\ncontent_r\n')])
576
tree_a.commit('rev3a', rev_id='rev3a')
577
md = self.from_objects(tree_a.branch.repository, 'rev3a', 500, 36,
578
branch_c.base, base_revision_id='rev2a')
579
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
432
class TestMergeDirective1Branch(tests.TestCaseWithTransport,
594
433
TestMergeDirectiveBranch):
595
434
"""Test merge directive format 1 with a branch"""
601
435
def from_objects(self, repository, revision_id, time, timezone,
602
436
target_branch, patch_type='bundle', local_target_branch=None,
603
public_branch=None, message=None, base_revision_id=None):
604
if base_revision_id is not None:
605
raise tests.TestNotApplicable('This format does not support'
607
repository.lock_write()
609
return merge_directive.MergeDirective.from_objects( repository,
610
revision_id, time, timezone, target_branch, patch_type,
611
local_target_branch, public_branch, message)
615
def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
616
target_branch, patch=None, patch_type=None,
617
source_branch=None, message=None):
618
return merge_directive.MergeDirective(revision_id, testament_sha1,
619
time, timezone, target_branch, patch, patch_type,
620
source_branch, message)
437
public_branch=None, message=None):
438
return merge_directive.MergeDirective.from_objects(
439
repository, revision_id, time, timezone, target_branch,
440
patch_type, local_target_branch, public_branch, message)
623
443
class TestMergeDirective2Branch(tests.TestCaseWithTransport,
624
444
TestMergeDirectiveBranch):
625
445
"""Test merge directive format 2 with a branch"""
631
447
def from_objects(self, repository, revision_id, time, timezone,
632
448
target_branch, patch_type='bundle', local_target_branch=None,
633
public_branch=None, message=None, base_revision_id=None):
634
include_patch = (patch_type in ('bundle', 'diff'))
635
include_bundle = (patch_type == 'bundle')
636
self.assertTrue(patch_type in ('bundle', 'diff', None))
449
public_branch=None, message=None):
637
450
return merge_directive.MergeDirective2.from_objects(
638
451
repository, revision_id, time, timezone, target_branch,
639
include_patch, include_bundle, local_target_branch, public_branch,
640
message, base_revision_id)
642
def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
643
target_branch, patch=None, patch_type=None,
644
source_branch=None, message=None, base_revision_id='null:'):
645
if patch_type == 'bundle':
650
return merge_directive.MergeDirective2(revision_id, testament_sha1,
651
time, timezone, target_branch, patch, source_branch, message,
652
bundle, base_revision_id)
654
def test_base_revision(self):
655
tree_a, tree_b, branch_c = self.make_trees()
656
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 60,
657
tree_b.branch.base, patch_type='bundle',
658
public_branch=tree_a.branch.base, base_revision_id=None)
659
self.assertEqual('rev1', md.base_revision_id)
660
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 60,
661
tree_b.branch.base, patch_type='bundle',
662
public_branch=tree_a.branch.base, base_revision_id='null:')
663
self.assertEqual('null:', md.base_revision_id)
664
lines = md.to_lines()
665
md2 = merge_directive.MergeDirective.from_lines(lines)
666
self.assertEqual(md2.base_revision_id, md.base_revision_id)
668
def test_patch_verification(self):
669
tree_a, tree_b, branch_c = self.make_trees()
670
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 60,
671
tree_b.branch.base, patch_type='bundle',
672
public_branch=tree_a.branch.base)
673
lines = md.to_lines()
674
md2 = merge_directive.MergeDirective.from_lines(lines)
675
md2._verify_patch(tree_a.branch.repository)
676
# Strip trailing whitespace
677
md2.patch = md2.patch.replace(' \n', '\n')
678
md2._verify_patch(tree_a.branch.repository)
679
# Convert to Mac line-endings
680
md2.patch = re.sub('(\r\n|\r|\n)', '\r', md2.patch)
681
self.assertTrue(md2._verify_patch(tree_a.branch.repository))
682
# Convert to DOS line-endings
683
md2.patch = re.sub('(\r\n|\r|\n)', '\r\n', md2.patch)
684
self.assertTrue(md2._verify_patch(tree_a.branch.repository))
685
md2.patch = md2.patch.replace('content_c', 'content_d')
686
self.assertFalse(md2._verify_patch(tree_a.branch.repository))
689
class TestParseOldMergeDirective2(tests.TestCase):
691
def test_parse_old_merge_directive(self):
692
md = merge_directive.MergeDirective.from_lines(INPUT1_2_OLD)
693
self.assertEqual('example:', md.revision_id)
694
self.assertEqual('sha', md.testament_sha1)
695
self.assertEqual('http://example.com', md.target_branch)
696
self.assertEqual('http://example.org', md.source_branch)
697
self.assertEqual(453, md.time)
698
self.assertEqual(120, md.timezone)
699
self.assertEqual('booga', md.patch)
700
self.assertEqual('diff', md.patch_type)
701
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)
452
patch_type, local_target_branch, public_branch, message)