/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: 2018-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

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