/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_branch.py

  • Committer: Jelmer Vernooij
  • Date: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
also see this file.
23
23
"""
24
24
 
25
 
from io import StringIO
26
 
 
27
25
from .. import (
28
 
    bedding,
29
26
    branch as _mod_branch,
30
27
    config,
31
28
    controldir,
42
39
    BzrBranch5,
43
40
    BzrBranchFormat5,
44
41
    )
45
 
 
46
 
 
47
 
class TestErrors(tests.TestCase):
48
 
 
49
 
    def test_unstackable_branch_format(self):
50
 
        format = u'foo'
51
 
        url = "/foo"
52
 
        error = _mod_branch.UnstackableBranchFormat(format, url)
53
 
        self.assertEqualDiff(
54
 
            "The branch '/foo'(foo) is not a stackable format. "
55
 
            "You will need to upgrade the branch to permit branch stacking.",
56
 
            str(error))
 
42
from ..sixish import (
 
43
    BytesIO,
 
44
    )
57
45
 
58
46
 
59
47
class TestDefaultFormat(tests.TestCase):
61
49
    def test_default_format(self):
62
50
        # update this if you change the default branch format
63
51
        self.assertIsInstance(_mod_branch.format_registry.get_default(),
64
 
                              _mod_bzrbranch.BzrBranchFormat7)
 
52
                _mod_bzrbranch.BzrBranchFormat7)
65
53
 
66
54
    def test_default_format_is_same_as_bzrdir_default(self):
67
55
        # XXX: it might be nice if there was only one place the default was
112
100
        branch = self.make_branch('.', format='knit')
113
101
        branch.set_push_location('foo')
114
102
        local_path = urlutils.local_path_from_url(branch.base[:-1])
115
 
        self.assertFileEqual(b"# comment\n"
116
 
                             b"[%s]\n"
117
 
                             b"push_location = foo\n"
118
 
                             b"push_location:policy = norecurse\n" % local_path.encode(
119
 
                                 'utf-8'),
120
 
                             bedding.locations_config_path())
 
103
        self.assertFileEqual("# comment\n"
 
104
                             "[%s]\n"
 
105
                             "push_location = foo\n"
 
106
                             "push_location:policy = norecurse\n" % local_path,
 
107
                             config.locations_config_filename())
121
108
 
122
109
    # TODO RBC 20051029 test getting a push location from a branch in a
123
110
    # recursive section - that is, it appends the branch name.
133
120
    @classmethod
134
121
    def get_format_string(cls):
135
122
        """See BzrBranchFormat.get_format_string()."""
136
 
        return b"Sample branch format."
 
123
        return "Sample branch format."
137
124
 
138
125
    def initialize(self, a_controldir, name=None, repository=None,
139
126
                   append_revisions_only=None):
152
139
 
153
140
# Demonstrating how lazy loading is often implemented:
154
141
# A constant string is created.
155
 
SampleSupportedBranchFormatString = b"Sample supported branch format."
 
142
SampleSupportedBranchFormatString = "Sample supported branch format."
156
143
 
157
144
# And the format class can then reference the constant to avoid skew.
158
 
 
159
 
 
160
145
class SampleSupportedBranchFormat(_mod_bzrbranch.BranchFormatMetadir):
161
146
    """A sample supported format."""
162
147
 
202
187
        # create a branch with a few known format objects.
203
188
        # this is not quite the same as
204
189
        self.build_tree(["foo/", "bar/"])
205
 
 
206
190
        def check_format(format, url):
207
 
            dir = format._matchingcontroldir.initialize(url)
 
191
            dir = format._matchingbzrdir.initialize(url)
208
192
            dir.create_repository()
209
193
            format.initialize(dir)
210
194
            found_format = _mod_bzrbranch.BranchFormatMetadir.find_format(dir)
213
197
 
214
198
    def test_from_string(self):
215
199
        self.assertIsInstance(
216
 
            SampleBranchFormat.from_string(b"Sample branch format."),
 
200
            SampleBranchFormat.from_string("Sample branch format."),
217
201
            SampleBranchFormat)
218
202
        self.assertRaises(AssertionError,
219
 
                          SampleBranchFormat.from_string, b"Different branch format.")
 
203
            SampleBranchFormat.from_string, "Different branch format.")
220
204
 
221
205
    def test_find_format_not_branch(self):
222
206
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
233
217
 
234
218
    def test_find_format_with_features(self):
235
219
        tree = self.make_branch_and_tree('.', format='2a')
236
 
        tree.branch.update_feature_flags({b"name": b"optional"})
237
 
        found_format = _mod_bzrbranch.BranchFormatMetadir.find_format(
238
 
            tree.controldir)
 
220
        tree.branch.update_feature_flags({"name": "optional"})
 
221
        found_format = _mod_bzrbranch.BranchFormatMetadir.find_format(tree.controldir)
239
222
        self.assertIsInstance(found_format, _mod_bzrbranch.BranchFormatMetadir)
240
 
        self.assertEqual(found_format.features.get(b"name"), b"optional")
241
 
        tree.branch.update_feature_flags({b"name": None})
 
223
        self.assertEqual(found_format.features.get("name"), "optional")
 
224
        tree.branch.update_feature_flags({"name": None})
242
225
        branch = _mod_branch.Branch.open('.')
243
226
        self.assertEqual(branch._format.features, {})
244
227
 
259
242
        format = SampleBranchFormat()
260
243
        self.registry.register(format)
261
244
        self.assertEqual(format,
262
 
                         self.registry.get(b"Sample branch format."))
 
245
            self.registry.get("Sample branch format."))
263
246
        self.registry.remove(format)
264
247
        self.assertRaises(KeyError, self.registry.get,
265
 
                          b"Sample branch format.")
 
248
            "Sample branch format.")
266
249
 
267
250
    def test_get_all(self):
268
251
        format = SampleBranchFormat()
279
262
    def test_register_extra_lazy(self):
280
263
        self.assertEqual([], self.registry._get_all())
281
264
        self.registry.register_extra_lazy("breezy.tests.test_branch",
282
 
                                          "SampleExtraBranchFormat")
 
265
            "SampleExtraBranchFormat")
283
266
        formats = self.registry._get_all()
284
267
        self.assertEqual(1, len(formats))
285
268
        self.assertIsInstance(formats[0], SampleExtraBranchFormat)
326
309
        self.assertPathDoesNotExist('a/.bzr/branch/bound')
327
310
        self.assertEqual('ftp://example.com', branch.get_bound_location())
328
311
 
329
 
    def do_checkout_test(self, lightweight):
 
312
    def do_checkout_test(self, lightweight=False):
330
313
        tree = self.make_branch_and_tree('source',
331
 
                                         format=self.get_format_name_subtree())
 
314
            format=self.get_format_name_subtree())
332
315
        subtree = self.make_branch_and_tree('source/subtree',
333
 
                                            format=self.get_format_name_subtree())
 
316
            format=self.get_format_name_subtree())
334
317
        subsubtree = self.make_branch_and_tree('source/subtree/subsubtree',
335
 
                                               format=self.get_format_name_subtree())
 
318
            format=self.get_format_name_subtree())
336
319
        self.build_tree(['source/subtree/file',
337
320
                         'source/subtree/subsubtree/file'])
338
321
        subsubtree.add('file')
339
322
        subtree.add('file')
340
323
        subtree.add_reference(subsubtree)
341
 
        subtree.set_reference_info('subsubtree', subsubtree.branch.user_url)
342
324
        tree.add_reference(subtree)
343
 
        tree.set_reference_info('subtree', subtree.branch.user_url)
344
325
        tree.commit('a revision')
345
326
        subtree.commit('a subtree file')
346
327
        subsubtree.commit('a subsubtree file')
356
337
            self.assertEndsWith(subbranch.base, 'target/subtree/subsubtree/')
357
338
 
358
339
    def test_checkout_with_references(self):
359
 
        self.do_checkout_test(lightweight=False)
 
340
        self.do_checkout_test()
360
341
 
361
342
    def test_light_checkout_with_references(self):
362
343
        self.do_checkout_test(lightweight=True)
375
356
 
376
357
    def test_set_stacked_on_url_errors(self):
377
358
        branch = self.make_branch('a', format=self.get_format_name())
378
 
        self.assertRaises(_mod_branch.UnstackableBranchFormat,
379
 
                          branch.set_stacked_on_url, None)
 
359
        self.assertRaises(errors.UnstackableBranchFormat,
 
360
            branch.set_stacked_on_url, None)
380
361
 
381
362
    def test_default_stacked_location(self):
382
363
        branch = self.make_branch('a', format=self.get_format_name())
383
 
        self.assertRaises(_mod_branch.UnstackableBranchFormat,
384
 
                          branch.get_stacked_on_url)
 
364
        self.assertRaises(errors.UnstackableBranchFormat, branch.get_stacked_on_url)
385
365
 
386
366
 
387
367
class TestBranch7(TestBranch67, tests.TestCaseWithTransport):
401
381
        branch = _mod_bzrbranch.BzrBranchFormat7().initialize(control)
402
382
        target = self.make_branch('b')
403
383
        self.assertRaises(errors.UnstackableRepositoryFormat,
404
 
                          branch.set_stacked_on_url, target.base)
 
384
            branch.set_stacked_on_url, target.base)
405
385
 
406
386
    def test_clone_stacked_on_unstackable_repo(self):
407
387
        repo = self.make_repository('a', format='dirstate-tags')
445
425
 
446
426
    def create_branch_with_reference(self):
447
427
        branch = self.make_branch('branch')
448
 
        branch._set_all_reference_info({'path': ('location', b'file-id')})
 
428
        branch._set_all_reference_info({'file-id': ('path', 'location')})
449
429
        return branch
450
430
 
451
431
    @staticmethod
452
432
    def instrument_branch(branch, gets):
453
433
        old_get = branch._transport.get
454
 
 
455
434
        def get(*args, **kwargs):
456
435
            gets.append((args, kwargs))
457
436
            return old_get(*args, **kwargs)
463
442
        branch.lock_read()
464
443
        self.addCleanup(branch.unlock)
465
444
        self.instrument_branch(branch, gets)
466
 
        branch.get_reference_info('path')
467
 
        branch.get_reference_info('path')
 
445
        branch.get_reference_info('file-id')
 
446
        branch.get_reference_info('file-id')
468
447
        self.assertEqual(1, len(gets))
469
448
 
470
449
    def test_reference_info_caching_read_unlocked(self):
471
450
        gets = []
472
451
        branch = self.create_branch_with_reference()
473
452
        self.instrument_branch(branch, gets)
474
 
        branch.get_reference_info('path')
475
 
        branch.get_reference_info('path')
 
453
        branch.get_reference_info('file-id')
 
454
        branch.get_reference_info('file-id')
476
455
        self.assertEqual(2, len(gets))
477
456
 
478
457
    def test_reference_info_caching_write_locked(self):
481
460
        branch.lock_write()
482
461
        self.instrument_branch(branch, gets)
483
462
        self.addCleanup(branch.unlock)
484
 
        branch._set_all_reference_info({'path2': ('location2', b'file-id')})
485
 
        location, file_id = branch.get_reference_info('path2')
 
463
        branch._set_all_reference_info({'file-id': ('path2', 'location2')})
 
464
        path, location = branch.get_reference_info('file-id')
486
465
        self.assertEqual(0, len(gets))
487
 
        self.assertEqual(b'file-id', file_id)
 
466
        self.assertEqual('path2', path)
488
467
        self.assertEqual('location2', location)
489
468
 
490
469
    def test_reference_info_caches_cleared(self):
491
470
        branch = self.make_branch('branch')
492
 
        with branch.lock_write():
493
 
            branch.set_reference_info(b'file-id', 'location2', 'path2')
 
471
        branch.lock_write()
 
472
        branch.set_reference_info('file-id', 'path2', 'location2')
 
473
        branch.unlock()
494
474
        doppelganger = _mod_branch.Branch.open('branch')
495
 
        doppelganger.set_reference_info(b'file-id', 'location3', 'path3')
496
 
        self.assertEqual(('location3', 'path3'),
497
 
                         branch.get_reference_info(b'file-id'))
 
475
        doppelganger.set_reference_info('file-id', 'path3', 'location3')
 
476
        self.assertEqual(('path3', 'location3'),
 
477
                         branch.get_reference_info('file-id'))
498
478
 
499
479
    def _recordParentMapCalls(self, repo):
500
480
        self._parent_map_calls = []
501
481
        orig_get_parent_map = repo.revisions.get_parent_map
502
 
 
503
482
        def get_parent_map(q):
504
483
            q = list(q)
505
484
            self._parent_map_calls.extend([e[0] for e in q])
532
511
        reference_url = branch.controldir.root_transport.abspath('') + '/'
533
512
        # if the api for create_checkout changes to return different checkout types
534
513
        # then this file read will fail.
535
 
        self.assertFileEqual(reference_url.encode('utf-8'),
536
 
                             'checkout/.bzr/branch/location')
 
514
        self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
537
515
        self.assertEqual(reference_url,
538
 
                         _mod_bzrbranch.BranchReferenceFormat().get_reference(checkout.controldir))
 
516
            _mod_bzrbranch.BranchReferenceFormat().get_reference(checkout.controldir))
539
517
 
540
518
 
541
519
class TestHooks(tests.TestCaseWithTransport):
544
522
        """Check that creating a BranchHooks instance has the right defaults."""
545
523
        hooks = _mod_branch.BranchHooks()
546
524
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
547
 
        self.assertTrue("post_commit" in hooks,
548
 
                        "post_commit not in %s" % hooks)
 
525
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
549
526
        self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
550
527
        self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
551
528
        self.assertTrue("post_uncommit" in hooks,
566
543
    def test_post_branch_init_hook(self):
567
544
        calls = []
568
545
        _mod_branch.Branch.hooks.install_named_hook('post_branch_init',
569
 
                                                    calls.append, None)
 
546
            calls.append, None)
570
547
        self.assertLength(0, calls)
571
548
        branch = self.make_branch('a')
572
549
        self.assertLength(1, calls)
578
555
    def test_post_branch_init_hook_repr(self):
579
556
        param_reprs = []
580
557
        _mod_branch.Branch.hooks.install_named_hook('post_branch_init',
581
 
                                                    lambda params: param_reprs.append(repr(params)), None)
 
558
            lambda params: param_reprs.append(repr(params)), None)
582
559
        branch = self.make_branch('a')
583
560
        self.assertLength(1, param_reprs)
584
561
        param_repr = param_reprs[0]
588
565
        from .. import switch
589
566
        calls = []
590
567
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
591
 
                                                    calls.append, None)
 
568
            calls.append, None)
592
569
        tree = self.make_branch_and_tree('branch-1')
593
570
        self.build_tree(['branch-1/file-1'])
594
571
        tree.add('file-1')
624
601
 
625
602
    def test_valid_append_revisions_only(self):
626
603
        self.assertEqual(None,
627
 
                         self.config_stack.get('append_revisions_only'))
 
604
                          self.config_stack.get('append_revisions_only'))
628
605
        self.check_append_revisions_only(None)
629
606
        self.check_append_revisions_only(False, 'False')
630
607
        self.check_append_revisions_only(True, 'True')
636
613
    def test_invalid_append_revisions_only(self):
637
614
        """Ensure warning is noted on invalid settings"""
638
615
        self.warnings = []
639
 
 
640
616
        def warning(*args):
641
617
            self.warnings.append(args[0] % args[1:])
642
618
        self.overrideAttr(trace, 'warning', warning)
696
672
 
697
673
    def test_report_changed(self):
698
674
        r = _mod_branch.PullResult()
699
 
        r.old_revid = b"old-revid"
 
675
        r.old_revid = "old-revid"
700
676
        r.old_revno = 10
701
 
        r.new_revid = b"new-revid"
 
677
        r.new_revid = "new-revid"
702
678
        r.new_revno = 20
703
 
        f = StringIO()
 
679
        f = BytesIO()
704
680
        r.report(f)
705
681
        self.assertEqual("Now on revision 20.\n", f.getvalue())
 
682
        self.assertEqual("Now on revision 20.\n", f.getvalue())
706
683
 
707
684
    def test_report_unchanged(self):
708
685
        r = _mod_branch.PullResult()
709
 
        r.old_revid = b"same-revid"
710
 
        r.new_revid = b"same-revid"
711
 
        f = StringIO()
 
686
        r.old_revid = "same-revid"
 
687
        r.new_revid = "same-revid"
 
688
        f = BytesIO()
712
689
        r.report(f)
713
690
        self.assertEqual("No revisions or tags to pull.\n", f.getvalue())