~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge_directive.py

  • Committer: Florent Gallaire
  • Date: 2017-03-17 10:39:02 UTC
  • mto: This revision was merged to the branch mainline in revision 6622.
  • Revision ID: fgallaire@gmail.com-20170317103902-xsmafws9vn8rczx9
Fix for Windows and 32-bit platforms buggy gmtime().

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
import re
17
18
 
18
19
from bzrlib import (
19
20
    errors,
20
21
    gpg,
 
22
    mail_client,
21
23
    merge_directive,
22
24
    tests,
 
25
    trace,
23
26
    )
24
27
 
25
28
 
31
34
#\x20
32
35
booga"""
33
36
 
34
 
OUTPUT1_2 = """# Bazaar merge directive format 2 (Bazaar 0.19)
 
37
OUTPUT1_2 = """# Bazaar merge directive format 2 (Bazaar 0.90)
35
38
# revision_id: example:
36
39
# target_branch: http://example.com
37
40
# testament_sha1: sha
51
54
#\x20
52
55
booga"""
53
56
 
54
 
OUTPUT2_2 = """# Bazaar merge directive format 2 (Bazaar 0.19)
 
57
OUTPUT2_2 = """# Bazaar merge directive format 2 (Bazaar 0.90)
55
58
# revision_id: example:
56
59
# target_branch: http://example.com
57
60
# testament_sha1: sha
96
99
 
97
100
Aaron
98
101
 
 
102
# Bazaar merge directive format 2 (Bazaar 0.90)\r
 
103
# revision_id: example:
 
104
# target_branch: http://example.com
 
105
# testament_sha1: sha
 
106
# timestamp: 1970-01-01 00:09:33 +0002
 
107
# source_branch: http://example.org
 
108
# base_revision_id: null:
 
109
# message: Hi mom!
 
110
#\x20
 
111
# Begin patch
 
112
booga""".splitlines(True)
 
113
 
 
114
 
 
115
INPUT1_2_OLD = """
 
116
I was thinking today about creating a merge directive.
 
117
 
 
118
So I did.
 
119
 
 
120
Here it is.
 
121
 
 
122
(I've pasted it in the body of this message)
 
123
 
 
124
Aaron
 
125
 
99
126
# Bazaar merge directive format 2 (Bazaar 0.19)\r
100
127
# revision_id: example:
101
128
# target_branch: http://example.com
109
136
booga""".splitlines(True)
110
137
 
111
138
 
 
139
OLD_DIRECTIVE_2 = """# Bazaar merge directive format 2 (Bazaar 0.19)
 
140
# revision_id: abentley@panoramicfeedback.com-20070807234458-\
 
141
#   nzhkoyza56lan7z5
 
142
# target_branch: http://panoramicfeedback.com/opensource/bzr/repo\
 
143
#   /bzr.ab
 
144
# testament_sha1: d825a5cdb267a90ec2ba86b00895f3d8a9bed6bf
 
145
# timestamp: 2007-08-10 16:15:02 -0400
 
146
# source_branch: http://panoramicfeedback.com/opensource/bzr/repo\
 
147
#   /bzr.ab
 
148
# base_revision_id: abentley@panoramicfeedback.com-20070731163346-\
 
149
#   623xwcycwij91xen
 
150
#
 
151
""".splitlines(True)
 
152
 
 
153
 
112
154
class TestMergeDirective(object):
113
155
 
114
156
    def test_merge_source(self):
270
312
To: pqm@example.com
271
313
User-Agent: Bazaar \(.*\)
272
314
 
273
 
# Bazaar merge directive format 2 \\(Bazaar 0.19\\)
 
315
# Bazaar merge directive format 2 \\(Bazaar 0.90\\)
274
316
# revision_id: rev2a
275
317
# target_branch: (.|\n)*
276
318
# testament_sha1: .*
298
340
To: pqm@example.com
299
341
User-Agent: Bazaar \(.*\)
300
342
 
301
 
# Bazaar merge directive format 2 \\(Bazaar 0.19\\)
 
343
# Bazaar merge directive format 2 \\(Bazaar 0.90\\)
302
344
# revision_id: rev2a
303
345
# target_branch: (.|\n)*
304
346
# testament_sha1: .*
311
353
 
312
354
    def make_trees(self):
313
355
        tree_a = self.make_branch_and_tree('tree_a')
314
 
        tree_a.branch.get_config().set_user_option('email',
315
 
            'J. Random Hacker <jrandom@example.com>')
316
 
        self.build_tree_contents([('tree_a/file', 'content_a\ncontent_b\n')])
317
 
        tree_a.add('file')
 
356
        tree_a.branch.get_config_stack().set(
 
357
            'email', 'J. Random Hacker <jrandom@example.com>')
 
358
        self.build_tree_contents([('tree_a/file', 'content_a\ncontent_b\n'),
 
359
                                  ('tree_a/file_2', 'content_x\rcontent_y\r')])
 
360
        tree_a.add(['file', 'file_2'])
318
361
        tree_a.commit('message', rev_id='rev1')
319
362
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
320
363
        branch_c = tree_a.bzrdir.sprout('branch_c').open_branch()
321
364
        tree_b.commit('message', rev_id='rev2b')
322
 
        self.build_tree_contents([('tree_a/file', 'content_a\ncontent_c \n')])
 
365
        self.build_tree_contents([('tree_a/file', 'content_a\ncontent_c \n'),
 
366
                                  ('tree_a/file_2', 'content_x\rcontent_z\r')])
323
367
        tree_a.commit('Commit of rev2a', rev_id='rev2a')
324
368
        return tree_a, tree_b, branch_c
325
369
 
330
374
            tree_d.branch.base, patch_type='diff',
331
375
            public_branch=tree_a.branch.base)
332
376
 
 
377
    def test_disk_name(self):
 
378
        tree_a, tree_b, branch_c = self.make_trees()
 
379
        tree_a.branch.nick = 'fancy <name>'
 
380
        md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
 
381
            tree_b.branch.base)
 
382
        self.assertEqual('fancy-name-2', md.get_disk_name(tree_a.branch))
 
383
 
 
384
    def test_disk_name_old_revno(self):
 
385
        tree_a, tree_b, branch_c = self.make_trees()
 
386
        tree_a.branch.nick = 'fancy-name'
 
387
        md = self.from_objects(tree_a.branch.repository, 'rev1', 500, 120,
 
388
            tree_b.branch.base)
 
389
        self.assertEqual('fancy-name-1', md.get_disk_name(tree_a.branch))
 
390
 
333
391
    def test_generate_patch(self):
334
392
        tree_a, tree_b, branch_c = self.make_trees()
335
393
        md2 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
404
462
        time = 453
405
463
        timezone = 7200
406
464
        class FakeBranch(object):
407
 
            def get_config(self):
 
465
            def get_config_stack(self):
408
466
                return self
409
467
            def gpg_signing_command(self):
410
468
                return 'loopback'
511
569
        revision = md.install_revisions(tree_b.branch.repository)
512
570
        self.assertEqual('rev2a', revision)
513
571
 
 
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)
 
580
 
 
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)
 
591
 
514
592
 
515
593
class TestMergeDirective1Branch(tests.TestCaseWithTransport,
516
594
    TestMergeDirectiveBranch):
522
600
 
523
601
    def from_objects(self, repository, revision_id, time, timezone,
524
602
        target_branch, patch_type='bundle', local_target_branch=None,
525
 
        public_branch=None, message=None):
526
 
        return merge_directive.MergeDirective.from_objects(
527
 
            repository, revision_id, time, timezone, target_branch,
528
 
            patch_type, local_target_branch, public_branch, message)
 
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'
 
606
                                          ' explicit bases.')
 
607
        repository.lock_write()
 
608
        try:
 
609
            return merge_directive.MergeDirective.from_objects( repository,
 
610
                revision_id, time, timezone, target_branch, patch_type,
 
611
                local_target_branch, public_branch, message)
 
612
        finally:
 
613
            repository.unlock()
529
614
 
530
615
    def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
531
616
                 target_branch, patch=None, patch_type=None,
548
633
        public_branch=None, message=None, base_revision_id=None):
549
634
        include_patch = (patch_type in ('bundle', 'diff'))
550
635
        include_bundle = (patch_type == 'bundle')
551
 
        assert patch_type in ('bundle', 'diff', None)
 
636
        self.assertTrue(patch_type in ('bundle', 'diff', None))
552
637
        return merge_directive.MergeDirective2.from_objects(
553
638
            repository, revision_id, time, timezone, target_branch,
554
639
            include_patch, include_bundle, local_target_branch, public_branch,
588
673
        lines = md.to_lines()
589
674
        md2 = merge_directive.MergeDirective.from_lines(lines)
590
675
        md2._verify_patch(tree_a.branch.repository)
591
 
        # Stript trailing whitespace
 
676
        # Strip trailing whitespace
592
677
        md2.patch = md2.patch.replace(' \n', '\n')
593
678
        md2._verify_patch(tree_a.branch.repository)
594
679
        # Convert to Mac line-endings
595
 
        md2.patch = md2.patch.replace('\n', '\r')
 
680
        md2.patch = re.sub('(\r\n|\r|\n)', '\r', md2.patch)
596
681
        self.assertTrue(md2._verify_patch(tree_a.branch.repository))
597
682
        # Convert to DOS line-endings
598
 
        md2.patch = md2.patch.replace('\r', '\r\n')
 
683
        md2.patch = re.sub('(\r\n|\r|\n)', '\r\n', md2.patch)
599
684
        self.assertTrue(md2._verify_patch(tree_a.branch.repository))
600
685
        md2.patch = md2.patch.replace('content_c', 'content_d')
601
686
        self.assertFalse(md2._verify_patch(tree_a.branch.repository))
 
687
 
 
688
 
 
689
class TestParseOldMergeDirective2(tests.TestCase):
 
690
 
 
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)
 
702
 
 
703
 
 
704
class TestHook(object):
 
705
    """Hook callback for test purposes."""
 
706
 
 
707
    def __init__(self, result=None):
 
708
        self.calls = []
 
709
        self.result = result
 
710
 
 
711
    def __call__(self, params):
 
712
        self.calls.append(params)
 
713
        return self.result
 
714
 
 
715
 
 
716
class HookMailClient(mail_client.MailClient):
 
717
    """Mail client for testing hooks."""
 
718
 
 
719
    def __init__(self, config):
 
720
        self.body = None
 
721
        self.config = config
 
722
 
 
723
    def compose(self, prompt, to, subject, attachment, mime_subtype,
 
724
                extension, basename=None, body=None):
 
725
        self.body = body
 
726
 
 
727
 
 
728
class TestBodyHook(tests.TestCaseWithTransport):
 
729
 
 
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')
 
737
        tree.commit('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',
 
744
            None, tree.branch)
 
745
        return client, directive
 
746
 
 
747
    def test_no_supports_body(self):
 
748
        test_hook = TestHook('foo')
 
749
        old_warn = trace.warning
 
750
        warnings = []
 
751
        def warn(*args):
 
752
            warnings.append(args)
 
753
        trace.warning = warn
 
754
        try:
 
755
            client, directive = self.compose_with_hooks([test_hook],
 
756
                supports_body=False)
 
757
        finally:
 
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])
 
763
 
 
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)
 
778
 
 
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)