/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: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

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
 
25
27
from .. import (
 
28
    bedding,
26
29
    branch as _mod_branch,
27
30
    config,
28
31
    controldir,
39
42
    BzrBranch5,
40
43
    BzrBranchFormat5,
41
44
    )
42
 
from ..sixish import (
43
 
    BytesIO,
44
 
    )
45
45
 
46
46
 
47
47
class TestErrors(tests.TestCase):
61
61
    def test_default_format(self):
62
62
        # update this if you change the default branch format
63
63
        self.assertIsInstance(_mod_branch.format_registry.get_default(),
64
 
                _mod_bzrbranch.BzrBranchFormat7)
 
64
                              _mod_bzrbranch.BzrBranchFormat7)
65
65
 
66
66
    def test_default_format_is_same_as_bzrdir_default(self):
67
67
        # XXX: it might be nice if there was only one place the default was
112
112
        branch = self.make_branch('.', format='knit')
113
113
        branch.set_push_location('foo')
114
114
        local_path = urlutils.local_path_from_url(branch.base[:-1])
115
 
        self.assertFileEqual("# comment\n"
116
 
                             "[%s]\n"
117
 
                             "push_location = foo\n"
118
 
                             "push_location:policy = norecurse\n" % local_path,
119
 
                             config.locations_config_filename())
 
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())
120
121
 
121
122
    # TODO RBC 20051029 test getting a push location from a branch in a
122
123
    # recursive section - that is, it appends the branch name.
132
133
    @classmethod
133
134
    def get_format_string(cls):
134
135
        """See BzrBranchFormat.get_format_string()."""
135
 
        return "Sample branch format."
 
136
        return b"Sample branch format."
136
137
 
137
138
    def initialize(self, a_controldir, name=None, repository=None,
138
139
                   append_revisions_only=None):
151
152
 
152
153
# Demonstrating how lazy loading is often implemented:
153
154
# A constant string is created.
154
 
SampleSupportedBranchFormatString = "Sample supported branch format."
 
155
SampleSupportedBranchFormatString = b"Sample supported branch format."
155
156
 
156
157
# And the format class can then reference the constant to avoid skew.
 
158
 
 
159
 
157
160
class SampleSupportedBranchFormat(_mod_bzrbranch.BranchFormatMetadir):
158
161
    """A sample supported format."""
159
162
 
199
202
        # create a branch with a few known format objects.
200
203
        # this is not quite the same as
201
204
        self.build_tree(["foo/", "bar/"])
 
205
 
202
206
        def check_format(format, url):
203
207
            dir = format._matchingcontroldir.initialize(url)
204
208
            dir.create_repository()
209
213
 
210
214
    def test_from_string(self):
211
215
        self.assertIsInstance(
212
 
            SampleBranchFormat.from_string("Sample branch format."),
 
216
            SampleBranchFormat.from_string(b"Sample branch format."),
213
217
            SampleBranchFormat)
214
218
        self.assertRaises(AssertionError,
215
 
            SampleBranchFormat.from_string, "Different branch format.")
 
219
                          SampleBranchFormat.from_string, b"Different branch format.")
216
220
 
217
221
    def test_find_format_not_branch(self):
218
222
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
229
233
 
230
234
    def test_find_format_with_features(self):
231
235
        tree = self.make_branch_and_tree('.', format='2a')
232
 
        tree.branch.update_feature_flags({"name": "optional"})
233
 
        found_format = _mod_bzrbranch.BranchFormatMetadir.find_format(tree.controldir)
 
236
        tree.branch.update_feature_flags({b"name": b"optional"})
 
237
        found_format = _mod_bzrbranch.BranchFormatMetadir.find_format(
 
238
            tree.controldir)
234
239
        self.assertIsInstance(found_format, _mod_bzrbranch.BranchFormatMetadir)
235
 
        self.assertEqual(found_format.features.get("name"), "optional")
236
 
        tree.branch.update_feature_flags({"name": None})
 
240
        self.assertEqual(found_format.features.get(b"name"), b"optional")
 
241
        tree.branch.update_feature_flags({b"name": None})
237
242
        branch = _mod_branch.Branch.open('.')
238
243
        self.assertEqual(branch._format.features, {})
239
244
 
254
259
        format = SampleBranchFormat()
255
260
        self.registry.register(format)
256
261
        self.assertEqual(format,
257
 
            self.registry.get("Sample branch format."))
 
262
                         self.registry.get(b"Sample branch format."))
258
263
        self.registry.remove(format)
259
264
        self.assertRaises(KeyError, self.registry.get,
260
 
            "Sample branch format.")
 
265
                          b"Sample branch format.")
261
266
 
262
267
    def test_get_all(self):
263
268
        format = SampleBranchFormat()
274
279
    def test_register_extra_lazy(self):
275
280
        self.assertEqual([], self.registry._get_all())
276
281
        self.registry.register_extra_lazy("breezy.tests.test_branch",
277
 
            "SampleExtraBranchFormat")
 
282
                                          "SampleExtraBranchFormat")
278
283
        formats = self.registry._get_all()
279
284
        self.assertEqual(1, len(formats))
280
285
        self.assertIsInstance(formats[0], SampleExtraBranchFormat)
321
326
        self.assertPathDoesNotExist('a/.bzr/branch/bound')
322
327
        self.assertEqual('ftp://example.com', branch.get_bound_location())
323
328
 
324
 
    def do_checkout_test(self, lightweight=False):
 
329
    def do_checkout_test(self, lightweight):
325
330
        tree = self.make_branch_and_tree('source',
326
 
            format=self.get_format_name_subtree())
 
331
                                         format=self.get_format_name_subtree())
327
332
        subtree = self.make_branch_and_tree('source/subtree',
328
 
            format=self.get_format_name_subtree())
 
333
                                            format=self.get_format_name_subtree())
329
334
        subsubtree = self.make_branch_and_tree('source/subtree/subsubtree',
330
 
            format=self.get_format_name_subtree())
 
335
                                               format=self.get_format_name_subtree())
331
336
        self.build_tree(['source/subtree/file',
332
337
                         'source/subtree/subsubtree/file'])
333
338
        subsubtree.add('file')
334
339
        subtree.add('file')
335
340
        subtree.add_reference(subsubtree)
 
341
        subtree.set_reference_info('subsubtree', subsubtree.branch.user_url)
336
342
        tree.add_reference(subtree)
 
343
        tree.set_reference_info('subtree', subtree.branch.user_url)
337
344
        tree.commit('a revision')
338
345
        subtree.commit('a subtree file')
339
346
        subsubtree.commit('a subsubtree file')
349
356
            self.assertEndsWith(subbranch.base, 'target/subtree/subsubtree/')
350
357
 
351
358
    def test_checkout_with_references(self):
352
 
        self.do_checkout_test()
 
359
        self.do_checkout_test(lightweight=False)
353
360
 
354
361
    def test_light_checkout_with_references(self):
355
362
        self.do_checkout_test(lightweight=True)
369
376
    def test_set_stacked_on_url_errors(self):
370
377
        branch = self.make_branch('a', format=self.get_format_name())
371
378
        self.assertRaises(_mod_branch.UnstackableBranchFormat,
372
 
            branch.set_stacked_on_url, None)
 
379
                          branch.set_stacked_on_url, None)
373
380
 
374
381
    def test_default_stacked_location(self):
375
382
        branch = self.make_branch('a', format=self.get_format_name())
376
 
        self.assertRaises(_mod_branch.UnstackableBranchFormat, branch.get_stacked_on_url)
 
383
        self.assertRaises(_mod_branch.UnstackableBranchFormat,
 
384
                          branch.get_stacked_on_url)
377
385
 
378
386
 
379
387
class TestBranch7(TestBranch67, tests.TestCaseWithTransport):
393
401
        branch = _mod_bzrbranch.BzrBranchFormat7().initialize(control)
394
402
        target = self.make_branch('b')
395
403
        self.assertRaises(errors.UnstackableRepositoryFormat,
396
 
            branch.set_stacked_on_url, target.base)
 
404
                          branch.set_stacked_on_url, target.base)
397
405
 
398
406
    def test_clone_stacked_on_unstackable_repo(self):
399
407
        repo = self.make_repository('a', format='dirstate-tags')
437
445
 
438
446
    def create_branch_with_reference(self):
439
447
        branch = self.make_branch('branch')
440
 
        branch._set_all_reference_info({'file-id': ('path', 'location')})
 
448
        branch._set_all_reference_info({'path': ('location', b'file-id')})
441
449
        return branch
442
450
 
443
451
    @staticmethod
444
452
    def instrument_branch(branch, gets):
445
453
        old_get = branch._transport.get
 
454
 
446
455
        def get(*args, **kwargs):
447
456
            gets.append((args, kwargs))
448
457
            return old_get(*args, **kwargs)
454
463
        branch.lock_read()
455
464
        self.addCleanup(branch.unlock)
456
465
        self.instrument_branch(branch, gets)
457
 
        branch.get_reference_info('file-id')
458
 
        branch.get_reference_info('file-id')
 
466
        branch.get_reference_info('path')
 
467
        branch.get_reference_info('path')
459
468
        self.assertEqual(1, len(gets))
460
469
 
461
470
    def test_reference_info_caching_read_unlocked(self):
462
471
        gets = []
463
472
        branch = self.create_branch_with_reference()
464
473
        self.instrument_branch(branch, gets)
465
 
        branch.get_reference_info('file-id')
466
 
        branch.get_reference_info('file-id')
 
474
        branch.get_reference_info('path')
 
475
        branch.get_reference_info('path')
467
476
        self.assertEqual(2, len(gets))
468
477
 
469
478
    def test_reference_info_caching_write_locked(self):
472
481
        branch.lock_write()
473
482
        self.instrument_branch(branch, gets)
474
483
        self.addCleanup(branch.unlock)
475
 
        branch._set_all_reference_info({'file-id': ('path2', 'location2')})
476
 
        path, location = branch.get_reference_info('file-id')
 
484
        branch._set_all_reference_info({'path2': ('location2', b'file-id')})
 
485
        location, file_id = branch.get_reference_info('path2')
477
486
        self.assertEqual(0, len(gets))
478
 
        self.assertEqual('path2', path)
 
487
        self.assertEqual(b'file-id', file_id)
479
488
        self.assertEqual('location2', location)
480
489
 
481
490
    def test_reference_info_caches_cleared(self):
482
491
        branch = self.make_branch('branch')
483
 
        branch.lock_write()
484
 
        branch.set_reference_info('file-id', 'path2', 'location2')
485
 
        branch.unlock()
 
492
        with branch.lock_write():
 
493
            branch.set_reference_info(b'file-id', 'location2', 'path2')
486
494
        doppelganger = _mod_branch.Branch.open('branch')
487
 
        doppelganger.set_reference_info('file-id', 'path3', 'location3')
488
 
        self.assertEqual(('path3', 'location3'),
489
 
                         branch.get_reference_info('file-id'))
 
495
        doppelganger.set_reference_info(b'file-id', 'location3', 'path3')
 
496
        self.assertEqual(('location3', 'path3'),
 
497
                         branch.get_reference_info(b'file-id'))
490
498
 
491
499
    def _recordParentMapCalls(self, repo):
492
500
        self._parent_map_calls = []
493
501
        orig_get_parent_map = repo.revisions.get_parent_map
 
502
 
494
503
        def get_parent_map(q):
495
504
            q = list(q)
496
505
            self._parent_map_calls.extend([e[0] for e in q])
523
532
        reference_url = branch.controldir.root_transport.abspath('') + '/'
524
533
        # if the api for create_checkout changes to return different checkout types
525
534
        # then this file read will fail.
526
 
        self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
 
535
        self.assertFileEqual(reference_url.encode('utf-8'),
 
536
                             'checkout/.bzr/branch/location')
527
537
        self.assertEqual(reference_url,
528
 
            _mod_bzrbranch.BranchReferenceFormat().get_reference(checkout.controldir))
 
538
                         _mod_bzrbranch.BranchReferenceFormat().get_reference(checkout.controldir))
529
539
 
530
540
 
531
541
class TestHooks(tests.TestCaseWithTransport):
534
544
        """Check that creating a BranchHooks instance has the right defaults."""
535
545
        hooks = _mod_branch.BranchHooks()
536
546
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
537
 
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
 
547
        self.assertTrue("post_commit" in hooks,
 
548
                        "post_commit not in %s" % hooks)
538
549
        self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
539
550
        self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
540
551
        self.assertTrue("post_uncommit" in hooks,
555
566
    def test_post_branch_init_hook(self):
556
567
        calls = []
557
568
        _mod_branch.Branch.hooks.install_named_hook('post_branch_init',
558
 
            calls.append, None)
 
569
                                                    calls.append, None)
559
570
        self.assertLength(0, calls)
560
571
        branch = self.make_branch('a')
561
572
        self.assertLength(1, calls)
567
578
    def test_post_branch_init_hook_repr(self):
568
579
        param_reprs = []
569
580
        _mod_branch.Branch.hooks.install_named_hook('post_branch_init',
570
 
            lambda params: param_reprs.append(repr(params)), None)
 
581
                                                    lambda params: param_reprs.append(repr(params)), None)
571
582
        branch = self.make_branch('a')
572
583
        self.assertLength(1, param_reprs)
573
584
        param_repr = param_reprs[0]
577
588
        from .. import switch
578
589
        calls = []
579
590
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
580
 
            calls.append, None)
 
591
                                                    calls.append, None)
581
592
        tree = self.make_branch_and_tree('branch-1')
582
593
        self.build_tree(['branch-1/file-1'])
583
594
        tree.add('file-1')
613
624
 
614
625
    def test_valid_append_revisions_only(self):
615
626
        self.assertEqual(None,
616
 
                          self.config_stack.get('append_revisions_only'))
 
627
                         self.config_stack.get('append_revisions_only'))
617
628
        self.check_append_revisions_only(None)
618
629
        self.check_append_revisions_only(False, 'False')
619
630
        self.check_append_revisions_only(True, 'True')
625
636
    def test_invalid_append_revisions_only(self):
626
637
        """Ensure warning is noted on invalid settings"""
627
638
        self.warnings = []
 
639
 
628
640
        def warning(*args):
629
641
            self.warnings.append(args[0] % args[1:])
630
642
        self.overrideAttr(trace, 'warning', warning)
684
696
 
685
697
    def test_report_changed(self):
686
698
        r = _mod_branch.PullResult()
687
 
        r.old_revid = "old-revid"
 
699
        r.old_revid = b"old-revid"
688
700
        r.old_revno = 10
689
 
        r.new_revid = "new-revid"
 
701
        r.new_revid = b"new-revid"
690
702
        r.new_revno = 20
691
 
        f = BytesIO()
 
703
        f = StringIO()
692
704
        r.report(f)
693
705
        self.assertEqual("Now on revision 20.\n", f.getvalue())
694
 
        self.assertEqual("Now on revision 20.\n", f.getvalue())
695
706
 
696
707
    def test_report_unchanged(self):
697
708
        r = _mod_branch.PullResult()
698
 
        r.old_revid = "same-revid"
699
 
        r.new_revid = "same-revid"
700
 
        f = BytesIO()
 
709
        r.old_revid = b"same-revid"
 
710
        r.new_revid = b"same-revid"
 
711
        f = StringIO()
701
712
        r.report(f)
702
713
        self.assertEqual("No revisions or tags to pull.\n", f.getvalue())