/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_bzrdir.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:
60
60
    TestCaseWithTransport,
61
61
    TestSkipped,
62
62
    )
63
 
from . import(
 
63
from . import (
64
64
    http_server,
65
65
    http_utils,
66
66
    )
68
68
    memory,
69
69
    pathfilter,
70
70
    )
71
 
from ..transport.http._urllib import HttpTransport_urllib
 
71
from ..transport.http import HttpTransport
72
72
from ..transport.nosmart import NoSmartTransportDecorator
73
73
from ..transport.readonly import ReadonlyTransportDecorator
74
74
from ..bzr import knitrepo, knitpack_repo
99
99
    def make_format_registry(self):
100
100
        my_format_registry = controldir.ControlDirFormatRegistry()
101
101
        my_format_registry.register('deprecated', DeprecatedBzrDirFormat,
102
 
            'Some format.  Slower and unawesome and deprecated.',
103
 
            deprecated=True)
 
102
                                    'Some format.  Slower and unawesome and deprecated.',
 
103
                                    deprecated=True)
104
104
        my_format_registry.register_lazy('lazy', 'breezy.tests.test_bzrdir',
105
 
            'DeprecatedBzrDirFormat', 'Format registered lazily',
106
 
            deprecated=True)
 
105
                                         'DeprecatedBzrDirFormat', 'Format registered lazily',
 
106
                                         deprecated=True)
107
107
        bzr.register_metadir(my_format_registry, 'knit',
108
 
            'breezy.bzr.knitrepo.RepositoryFormatKnit1',
109
 
            'Format using knits',
110
 
            )
 
108
                             'breezy.bzr.knitrepo.RepositoryFormatKnit1',
 
109
                             'Format using knits',
 
110
                             )
111
111
        my_format_registry.set_default('knit')
112
112
        bzr.register_metadir(my_format_registry,
113
 
            'branch6',
114
 
            'breezy.bzr.knitrepo.RepositoryFormatKnit3',
115
 
            'Experimental successor to knit.  Use at your own risk.',
116
 
            branch_format='breezy.bzr.branch.BzrBranchFormat6',
117
 
            experimental=True)
 
113
                             'branch6',
 
114
                             'breezy.bzr.knitrepo.RepositoryFormatKnit3',
 
115
                             'Experimental successor to knit.  Use at your own risk.',
 
116
                             branch_format='breezy.bzr.branch.BzrBranchFormat6',
 
117
                             experimental=True)
118
118
        bzr.register_metadir(my_format_registry,
119
 
            'hidden format',
120
 
            'breezy.bzr.knitrepo.RepositoryFormatKnit3',
121
 
            'Experimental successor to knit.  Use at your own risk.',
122
 
            branch_format='breezy.bzr.branch.BzrBranchFormat6', hidden=True)
 
119
                             'hidden format',
 
120
                             'breezy.bzr.knitrepo.RepositoryFormatKnit3',
 
121
                             'Experimental successor to knit.  Use at your own risk.',
 
122
                             branch_format='breezy.bzr.branch.BzrBranchFormat6', hidden=True)
123
123
        my_format_registry.register('hiddendeprecated', DeprecatedBzrDirFormat,
124
 
            'Old format.  Slower and does not support things. ', hidden=True)
 
124
                                    'Old format.  Slower and does not support things. ', hidden=True)
125
125
        my_format_registry.register_lazy('hiddenlazy', 'breezy.tests.test_bzrdir',
126
 
            'DeprecatedBzrDirFormat', 'Format registered lazily',
127
 
            deprecated=True, hidden=True)
 
126
                                         'DeprecatedBzrDirFormat', 'Format registered lazily',
 
127
                                         deprecated=True, hidden=True)
128
128
        return my_format_registry
129
129
 
130
130
    def test_format_registry(self):
135
135
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
136
136
        my_bzrdir = my_format_registry.make_controldir('default')
137
137
        self.assertIsInstance(my_bzrdir.repository_format,
138
 
            knitrepo.RepositoryFormatKnit1)
 
138
                              knitrepo.RepositoryFormatKnit1)
139
139
        my_bzrdir = my_format_registry.make_controldir('knit')
140
140
        self.assertIsInstance(my_bzrdir.repository_format,
141
 
            knitrepo.RepositoryFormatKnit1)
 
141
                              knitrepo.RepositoryFormatKnit1)
142
142
        my_bzrdir = my_format_registry.make_controldir('branch6')
143
143
        self.assertIsInstance(my_bzrdir.get_branch_format(),
144
144
                              breezy.bzr.branch.BzrBranchFormat6)
166
166
        experimental, deprecated = rest.split('Deprecated formats')
167
167
        self.assertContainsRe(new, 'formats-help')
168
168
        self.assertContainsRe(new,
169
 
                ':knit:\n    \\(native\\) \\(default\\) Format using knits\n')
 
169
                              ':knit:\n    \\(native\\) \\(default\\) Format using knits\n')
170
170
        self.assertContainsRe(experimental,
171
 
                ':branch6:\n    \\(native\\) Experimental successor to knit')
 
171
                              ':branch6:\n    \\(native\\) Experimental successor to knit')
172
172
        self.assertContainsRe(deprecated,
173
 
                ':lazy:\n    \\(native\\) Format registered lazily\n')
 
173
                              ':lazy:\n    \\(native\\) Format registered lazily\n')
174
174
        self.assertNotContainsRe(new, 'hidden')
175
175
 
176
176
    def test_set_default_repository(self):
177
177
        default_factory = controldir.format_registry.get('default')
178
178
        old_default = [k for k, v in controldir.format_registry.iteritems()
179
179
                       if v == default_factory and k != 'default'][0]
180
 
        controldir.format_registry.set_default_repository('dirstate-with-subtree')
 
180
        controldir.format_registry.set_default_repository(
 
181
            'dirstate-with-subtree')
181
182
        try:
182
183
            self.assertIs(controldir.format_registry.get('dirstate-with-subtree'),
183
184
                          controldir.format_registry.get('default'))
190
191
    def test_aliases(self):
191
192
        a_registry = controldir.ControlDirFormatRegistry()
192
193
        a_registry.register('deprecated', DeprecatedBzrDirFormat,
193
 
            'Old format.  Slower and does not support stuff',
194
 
            deprecated=True)
195
 
        a_registry.register('deprecatedalias', DeprecatedBzrDirFormat,
196
 
            'Old format.  Slower and does not support stuff',
197
 
            deprecated=True, alias=True)
198
 
        self.assertEqual(frozenset(['deprecatedalias']), a_registry.aliases())
 
194
                            'Old format.  Slower and does not support stuff',
 
195
                            deprecated=True)
 
196
        a_registry.register_alias('deprecatedalias', 'deprecated')
 
197
        self.assertEqual({'deprecatedalias': 'deprecated'},
 
198
                         a_registry.aliases())
199
199
 
200
200
 
201
201
class SampleBranch(breezy.branch.Branch):
243
243
 
244
244
    def get_format_string(self):
245
245
        """See BzrDirFormat.get_format_string()."""
246
 
        return "Sample .bzr dir format."
 
246
        return b"Sample .bzr dir format."
247
247
 
248
248
    def initialize_on_transport(self, t):
249
249
        """Create a bzr dir."""
266
266
 
267
267
    @staticmethod
268
268
    def get_format_string():
269
 
        return "Test format 1"
 
269
        return b"Test format 1"
270
270
 
271
271
 
272
272
class BzrDirFormatTest2(bzrdir.BzrDirMetaFormat1):
273
273
 
274
274
    @staticmethod
275
275
    def get_format_string():
276
 
        return "Test format 2"
 
276
        return b"Test format 2"
277
277
 
278
278
 
279
279
class TestBzrDirFormat(TestCaseWithTransport):
283
283
        # is the right format object found for a branch?
284
284
        # create a branch with a few known format objects.
285
285
        bzr.BzrProber.formats.register(BzrDirFormatTest1.get_format_string(),
286
 
            BzrDirFormatTest1())
 
286
                                       BzrDirFormatTest1())
287
287
        self.addCleanup(bzr.BzrProber.formats.remove,
288
 
            BzrDirFormatTest1.get_format_string())
 
288
                        BzrDirFormatTest1.get_format_string())
289
289
        bzr.BzrProber.formats.register(BzrDirFormatTest2.get_format_string(),
290
 
            BzrDirFormatTest2())
 
290
                                       BzrDirFormatTest2())
291
291
        self.addCleanup(bzr.BzrProber.formats.remove,
292
 
            BzrDirFormatTest2.get_format_string())
 
292
                        BzrDirFormatTest2.get_format_string())
293
293
        t = self.get_transport()
294
294
        self.build_tree(["foo/", "bar/"], transport=t)
 
295
 
295
296
        def check_format(format, url):
296
297
            format.initialize(url)
297
298
            t = _mod_transport.get_transport_from_path(url)
308
309
    def test_find_format_unknown_format(self):
309
310
        t = self.get_transport()
310
311
        t.mkdir('.bzr')
311
 
        t.put_bytes('.bzr/branch-format', '')
 
312
        t.put_bytes('.bzr/branch-format', b'')
312
313
        self.assertRaises(UnknownFormatError,
313
314
                          bzrdir.BzrDirFormat.find_format,
314
315
                          _mod_transport.get_transport_from_path('.'))
315
316
 
 
317
    def test_find_format_line_endings(self):
 
318
        t = self.get_transport()
 
319
        t.mkdir('.bzr')
 
320
        t.put_bytes('.bzr/branch-format', b'Corrupt line endings\r\n')
 
321
        self.assertRaises(errors.LineEndingError,
 
322
                          bzrdir.BzrDirFormat.find_format,
 
323
                          _mod_transport.get_transport_from_path('.'))
 
324
 
316
325
    def test_register_unregister_format(self):
317
326
        format = SampleBzrDirFormat()
318
327
        url = self.get_url()
323
332
        # which bzrdir.Open will refuse (not supported)
324
333
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
325
334
        # which bzrdir.open_containing will refuse (not supported)
326
 
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
 
335
        self.assertRaises(UnsupportedFormatError,
 
336
                          bzrdir.BzrDir.open_containing, url)
327
337
        # but open_downlevel will work
328
338
        t = _mod_transport.get_transport_from_url(url)
329
339
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
330
340
        # unregister the format
331
341
        bzr.BzrProber.formats.remove(format.get_format_string())
332
342
        # now open_downlevel should fail too.
333
 
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
 
343
        self.assertRaises(UnknownFormatError,
 
344
                          bzrdir.BzrDir.open_unsupported, url)
334
345
 
335
346
    def test_create_branch_and_repo_uses_default(self):
336
347
        format = SampleBzrDirFormat()
379
390
                          bzrdir.BzrDir.create_standalone_workingtree,
380
391
                          self.get_readonly_url('child'), format=format)
381
392
        tree = bzrdir.BzrDir.create_standalone_workingtree('child',
382
 
            format=format)
 
393
                                                           format=format)
383
394
        tree.controldir.open_repository()
384
395
 
385
396
    def test_create_branch_convenience(self):
415
426
        format = controldir.format_registry.make_controldir('knit')
416
427
        self.make_repository('.', shared=True, format=format)
417
428
        branch = bzrdir.BzrDir.create_branch_convenience('child',
418
 
            format=format)
 
429
                                                         format=format)
419
430
        branch.controldir.open_workingtree()
420
431
        self.assertRaises(errors.NoRepositoryPresent,
421
432
                          branch.controldir.open_repository)
426
437
        format = controldir.format_registry.make_controldir('knit')
427
438
        self.make_repository('.', shared=True, format=format)
428
439
        branch = bzrdir.BzrDir.create_branch_convenience('child',
429
 
            force_new_tree=False, format=format)
 
440
                                                         force_new_tree=False, format=format)
430
441
        self.assertRaises(errors.NoWorkingTree,
431
442
                          branch.controldir.open_workingtree)
432
443
        self.assertRaises(errors.NoRepositoryPresent,
452
463
        repo = self.make_repository('.', shared=True, format=format)
453
464
        repo.set_make_working_trees(False)
454
465
        branch = bzrdir.BzrDir.create_branch_convenience('child',
455
 
            force_new_tree=True, format=format)
 
466
                                                         force_new_tree=True, format=format)
456
467
        branch.controldir.open_workingtree()
457
468
        self.assertRaises(errors.NoRepositoryPresent,
458
469
                          branch.controldir.open_repository)
463
474
        format = controldir.format_registry.make_controldir('knit')
464
475
        self.make_repository('.', shared=True, format=format)
465
476
        branch = bzrdir.BzrDir.create_branch_convenience('child',
466
 
            force_new_repo=True, format=format)
 
477
                                                         force_new_repo=True, format=format)
467
478
        branch.controldir.open_repository()
468
479
        branch.controldir.open_workingtree()
469
480
 
504
515
 
505
516
    def test_clone_on_transport_obeys_stacking_policy(self):
506
517
        child_branch, new_child_transport = self.prepare_default_stacking()
507
 
        new_child = child_branch.controldir.clone_on_transport(new_child_transport)
 
518
        new_child = child_branch.controldir.clone_on_transport(
 
519
            new_child_transport)
508
520
        self.assertEqual(child_branch.base,
509
521
                         new_child.open_branch().get_stacked_on_url())
510
522
 
532
544
        # Should end up with a 1.9 format (stackable)
533
545
        repo, control, require_stacking, repo_policy = \
534
546
            old_fmt.initialize_on_transport_ex(t,
535
 
                    repo_format_name=repo_name, stacked_on='../trunk',
536
 
                    stack_on_pwd=t.base)
 
547
                                               repo_format_name=repo_name, stacked_on='../trunk',
 
548
                                               stack_on_pwd=t.base)
537
549
        if repo is not None:
538
550
            # Repositories are open write-locked
539
551
            self.assertTrue(repo.is_write_locked())
544
556
        opened = bzrdir.BzrDir.open(t.base)
545
557
        if not isinstance(old_fmt, remote.RemoteBzrDirFormat):
546
558
            self.assertEqual(control._format.network_name(),
547
 
                old_fmt.network_name())
 
559
                             old_fmt.network_name())
548
560
            self.assertEqual(control._format.network_name(),
549
 
                opened._format.network_name())
 
561
                             opened._format.network_name())
550
562
        self.assertEqual(control.__class__, opened.__class__)
551
563
        self.assertLength(1, repo._fallback_repositories)
552
564
 
559
571
    def test_clone_ignores_policy_for_unsupported_formats(self):
560
572
        child_branch, new_child_transport = self.prepare_default_stacking(
561
573
            child_format='pack-0.92')
562
 
        new_child = child_branch.controldir.clone_on_transport(new_child_transport)
 
574
        new_child = child_branch.controldir.clone_on_transport(
 
575
            new_child_transport)
563
576
        self.assertRaises(branch.UnstackableBranchFormat,
564
577
                          new_child.open_branch().get_stacked_on_url)
565
578
 
574
587
        child_branch, new_child_transport = self.prepare_default_stacking(
575
588
            child_format='pack-0.92')
576
589
        new_child = child_branch.controldir.sprout(new_child_transport.base,
577
 
                                               stacked=True)
 
590
                                                   stacked=True)
578
591
        self.assertEqual(child_branch.controldir.root_transport.base,
579
592
                         new_child.open_branch().get_stacked_on_url())
580
593
        repo = new_child.open_repository()
585
598
        child_branch, new_child_transport = self.prepare_default_stacking(
586
599
            child_format='pack-0.92')
587
600
        new_child = child_branch.controldir.clone_on_transport(new_child_transport,
588
 
            stacked_on=child_branch.controldir.root_transport.base)
 
601
                                                               stacked_on=child_branch.controldir.root_transport.base)
589
602
        self.assertEqual(child_branch.controldir.root_transport.base,
590
603
                         new_child.open_branch().get_stacked_on_url())
591
604
        repo = new_child.open_repository()
596
609
        child_branch, new_child_transport = self.prepare_default_stacking(
597
610
            child_format='rich-root-pack')
598
611
        new_child = child_branch.controldir.sprout(new_child_transport.base,
599
 
                                               stacked=True)
 
612
                                                   stacked=True)
600
613
        repo = new_child.open_repository()
601
614
        self.assertTrue(repo._format.supports_external_lookups)
602
615
        self.assertTrue(repo.supports_rich_root())
617
630
        stack_on = self.make_branch('stack_on', format='1.6')
618
631
        stacked = self.make_branch('stack_on/stacked', format='1.6')
619
632
        policy = bzrdir.UseExistingRepository(stacked.repository,
620
 
            '.', stack_on.base)
 
633
                                              '.', stack_on.base)
621
634
        policy.configure_branch(stacked)
622
635
        self.assertEqual('..', stacked.get_stacked_on_url())
623
636
 
625
638
        stack_on = self.make_branch('stack_on', format='1.6')
626
639
        stacked = self.make_branch('stack_on/stacked', format='1.6')
627
640
        policy = bzrdir.UseExistingRepository(stacked.repository,
628
 
            '.', self.get_readonly_url('stack_on'))
 
641
                                              '.', self.get_readonly_url('stack_on'))
629
642
        policy.configure_branch(stacked)
630
643
        self.assertEqual(self.get_readonly_url('stack_on'),
631
644
                         stacked.get_stacked_on_url())
645
658
            self.transport_readonly_server = http_server.HttpServer
646
659
 
647
660
    def local_branch_path(self, branch):
648
 
         return os.path.realpath(urlutils.local_path_from_url(branch.base))
 
661
        return os.path.realpath(urlutils.local_path_from_url(branch.base))
649
662
 
650
663
    def test_open_containing(self):
651
664
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
653
666
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
654
667
                          self.get_readonly_url('g/p/q'))
655
668
        control = bzrdir.BzrDir.create(self.get_url())
656
 
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
 
669
        branch, relpath = bzrdir.BzrDir.open_containing(
 
670
            self.get_readonly_url(''))
657
671
        self.assertEqual('', relpath)
658
 
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
 
672
        branch, relpath = bzrdir.BzrDir.open_containing(
 
673
            self.get_readonly_url('g/p/q'))
659
674
        self.assertEqual('g/p/q', relpath)
660
675
 
661
676
    def test_open_containing_tree_branch_or_repository_empty(self):
662
677
        self.assertRaises(errors.NotBranchError,
663
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository,
664
 
            self.get_readonly_url(''))
 
678
                          bzrdir.BzrDir.open_containing_tree_branch_or_repository,
 
679
                          self.get_readonly_url(''))
665
680
 
666
681
    def test_open_containing_tree_branch_or_repository_all(self):
667
682
        self.make_branch_and_tree('topdir')
747
762
 
748
763
    def test_open_containing_from_transport(self):
749
764
        self.assertRaises(NotBranchError,
750
 
            bzrdir.BzrDir.open_containing_from_transport,
751
 
            _mod_transport.get_transport_from_url(self.get_readonly_url('')))
 
765
                          bzrdir.BzrDir.open_containing_from_transport,
 
766
                          _mod_transport.get_transport_from_url(self.get_readonly_url('')))
752
767
        self.assertRaises(NotBranchError,
753
 
            bzrdir.BzrDir.open_containing_from_transport,
754
 
            _mod_transport.get_transport_from_url(
755
 
                self.get_readonly_url('g/p/q')))
 
768
                          bzrdir.BzrDir.open_containing_from_transport,
 
769
                          _mod_transport.get_transport_from_url(
 
770
                              self.get_readonly_url('g/p/q')))
756
771
        control = bzrdir.BzrDir.create(self.get_url())
757
772
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
758
773
            _mod_transport.get_transport_from_url(
827
842
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
828
843
 
829
844
    def test_sprout_recursive(self):
830
 
        tree = self.make_branch_and_tree('tree1',
831
 
                                         format='development-subtree')
832
 
        sub_tree = self.make_branch_and_tree('tree1/subtree',
833
 
            format='development-subtree')
834
 
        sub_tree.set_root_id('subtree-root')
 
845
        tree = self.make_branch_and_tree('tree1')
 
846
        sub_tree = self.make_branch_and_tree('tree1/subtree')
 
847
        sub_tree.set_root_id(b'subtree-root')
835
848
        tree.add_reference(sub_tree)
 
849
        tree.set_reference_info('subtree', sub_tree.branch.user_url)
836
850
        self.build_tree(['tree1/subtree/file'])
837
851
        sub_tree.add('file')
838
852
        tree.commit('Initial commit')
840
854
        tree2.lock_read()
841
855
        self.addCleanup(tree2.unlock)
842
856
        self.assertPathExists('tree2/subtree/file')
843
 
        self.assertEqual(
844
 
                'tree-reference',
845
 
                tree2.kind('subtree', 'subtree-root'))
 
857
        self.assertEqual('tree-reference', tree2.kind('subtree'))
846
858
 
847
859
    def test_cloning_metadir(self):
848
860
        """Ensure that cloning metadir is suitable"""
851
863
        branch = self.make_branch('branch', format='knit')
852
864
        format = branch.controldir.cloning_metadir()
853
865
        self.assertIsInstance(format.workingtree_format,
854
 
            workingtree_4.WorkingTreeFormat6)
 
866
                              workingtree_4.WorkingTreeFormat6)
855
867
 
856
868
    def test_sprout_recursive_treeless(self):
857
869
        tree = self.make_branch_and_tree('tree1',
858
 
            format='development-subtree')
 
870
                                         format='development-subtree')
859
871
        sub_tree = self.make_branch_and_tree('tree1/subtree',
860
 
            format='development-subtree')
 
872
                                             format='development-subtree')
861
873
        tree.add_reference(sub_tree)
 
874
        tree.set_reference_info('subtree', sub_tree.branch.user_url)
862
875
        self.build_tree(['tree1/subtree/file'])
863
876
        sub_tree.add('file')
864
877
        tree.commit('Initial commit')
865
878
        # The following line force the orhaning to reveal bug #634470
866
 
        tree.branch.get_config_stack().set(
867
 
            'bzr.transform.orphan_policy', 'move')
 
879
        tree.branch.get_config_stack().set('transform.orphan_policy', 'move')
868
880
        tree.controldir.destroy_workingtree()
869
881
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
870
882
        # fail :-( ) -- vila 20100909
871
883
        repo = self.make_repository('repo', shared=True,
872
 
            format='development-subtree')
 
884
                                    format='development-subtree')
873
885
        repo.set_make_working_trees(False)
874
886
        # FIXME: we just deleted the workingtree and now we want to use it ????
875
887
        # At a minimum, we should use tree.branch below (but this fails too
879
891
        # by bzrdir.BzrDirMeta1.destroy_workingtree when it ignores the
880
892
        # [DeletingParent('Not deleting', u'subtree', None)] conflict). See bug
881
893
        # #634470.  -- vila 20100909
882
 
        self.assertRaises(errors.NotBranchError,
883
 
                          tree.controldir.sprout, 'repo/tree2')
884
 
#        self.assertPathExists('repo/tree2/subtree')
885
 
#        self.assertPathDoesNotExist('repo/tree2/subtree/file')
 
894
        tree.controldir.sprout('repo/tree2')
 
895
        self.assertPathExists('repo/tree2/subtree')
 
896
        self.assertPathDoesNotExist('repo/tree2/subtree/file')
886
897
 
887
898
    def make_foo_bar_baz(self):
888
899
        foo = bzrdir.BzrDir.create_branch_convenience('foo').controldir
893
904
    def test_find_controldirs(self):
894
905
        foo, bar, baz = self.make_foo_bar_baz()
895
906
        t = self.get_transport()
896
 
        self.assertEqualBzrdirs([baz, foo, bar], bzrdir.BzrDir.find_controldirs(t))
 
907
        self.assertEqualBzrdirs(
 
908
            [baz, foo, bar], bzrdir.BzrDir.find_controldirs(t))
897
909
 
898
910
    def make_fake_permission_denied_transport(self, transport, paths):
899
911
        """Create a transport that raises PermissionDenied for some paths."""
920
932
            self.make_fake_permission_denied_transport(t, ['foo'])
921
933
        # local transport
922
934
        self.assertBranchUrlsEndWith('/baz/',
923
 
            bzrdir.BzrDir.find_controldirs(path_filter_transport))
 
935
                                     bzrdir.BzrDir.find_controldirs(path_filter_transport))
924
936
        # smart server
925
937
        smart_transport = self.make_smart_server('.',
926
 
            backing_server=path_filter_server)
 
938
                                                 backing_server=path_filter_server)
927
939
        self.assertBranchUrlsEndWith('/baz/',
928
 
            bzrdir.BzrDir.find_controldirs(smart_transport))
 
940
                                     bzrdir.BzrDir.find_controldirs(smart_transport))
929
941
 
930
942
    def test_find_controldirs_list_current(self):
931
943
        def list_current(transport):
983
995
        abranch = bzrdir.BzrDir.create(abranch_url).create_branch()
984
996
        t.delete_tree('arepo/.bzr')
985
997
        self.assertRaises(errors.NoRepositoryPresent,
986
 
            branch.Branch.open, abranch_url)
 
998
                          branch.Branch.open, abranch_url)
987
999
        self.make_branch('baz')
988
1000
        for actual_bzrdir in bzrdir.BzrDir.find_branches(t):
989
1001
            self.assertEndsWith(actual_bzrdir.user_url, '/baz/')
1000
1012
        self.assertEqual(branch_base,
1001
1013
                         dir.get_branch_transport(BzrBranchFormat5()).base)
1002
1014
        repository_base = t.clone('repository').base
1003
 
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
 
1015
        self.assertEqual(
 
1016
            repository_base, dir.get_repository_transport(None).base)
1004
1017
        repository_format = repository.format_registry.get_default()
1005
1018
        self.assertEqual(repository_base,
1006
1019
                         dir.get_repository_transport(repository_format).base)
1007
1020
        checkout_base = t.clone('checkout').base
1008
 
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
 
1021
        self.assertEqual(
 
1022
            checkout_base, dir.get_workingtree_transport(None).base)
1009
1023
        self.assertEqual(checkout_base,
1010
1024
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
1011
1025
 
1027
1041
        otherdir = controldir.format_registry.make_controldir('knit')
1028
1042
        self.assertEqual(otherdir, mydir)
1029
1043
        self.assertFalse(otherdir != mydir)
1030
 
        otherdir2 = controldir.format_registry.make_controldir('development-subtree')
 
1044
        otherdir2 = controldir.format_registry.make_controldir(
 
1045
            'development-subtree')
1031
1046
        self.assertNotEqual(otherdir2, mydir)
1032
1047
        self.assertFalse(otherdir2 == mydir)
1033
1048
 
1034
1049
    def test_with_features(self):
1035
1050
        tree = self.make_branch_and_tree('tree', format='2a')
1036
 
        tree.controldir.update_feature_flags({"bar": "required"})
 
1051
        tree.controldir.update_feature_flags({b"bar": b"required"})
1037
1052
        self.assertRaises(bzrdir.MissingFeature, bzrdir.BzrDir.open, 'tree')
1038
 
        bzrdir.BzrDirMetaFormat1.register_feature('bar')
1039
 
        self.addCleanup(bzrdir.BzrDirMetaFormat1.unregister_feature, 'bar')
 
1053
        bzrdir.BzrDirMetaFormat1.register_feature(b'bar')
 
1054
        self.addCleanup(bzrdir.BzrDirMetaFormat1.unregister_feature, b'bar')
1040
1055
        dir = bzrdir.BzrDir.open('tree')
1041
 
        self.assertEqual("required", dir._format.features.get("bar"))
1042
 
        tree.controldir.update_feature_flags({"bar": None, "nonexistant": None})
 
1056
        self.assertEqual(b"required", dir._format.features.get(b"bar"))
 
1057
        tree.controldir.update_feature_flags({
 
1058
            b"bar": None,
 
1059
            b"nonexistant": None})
1043
1060
        dir = bzrdir.BzrDir.open('tree')
1044
1061
        self.assertEqual({}, dir._format.features)
1045
1062
 
1087
1104
        # outside a repo the default convenience output is a repo+branch_tree
1088
1105
        format = controldir.format_registry.make_controldir('knit')
1089
1106
        self.assertRaises(errors.NotLocalUrl,
1090
 
            bzrdir.BzrDir.create_branch_convenience,
1091
 
            self.get_url('foo'),
1092
 
            force_new_tree=True,
1093
 
            format=format)
 
1107
                          bzrdir.BzrDir.create_branch_convenience,
 
1108
                          self.get_url('foo'),
 
1109
                          force_new_tree=True,
 
1110
                          format=format)
1094
1111
        t = self.get_transport()
1095
1112
        self.assertFalse(t.has('foo'))
1096
1113
 
1116
1133
                              workingtree_4.WorkingTreeFormat4)
1117
1134
 
1118
1135
 
1119
 
class TestHTTPRedirections(object):
 
1136
class TestHTTPRedirectionsBase(object):
1120
1137
    """Test redirection between two http servers.
1121
1138
 
1122
1139
    This MUST be used by daughter classes that also inherit from
1136
1153
        return http_utils.HTTPServerRedirecting()
1137
1154
 
1138
1155
    def setUp(self):
1139
 
        super(TestHTTPRedirections, self).setUp()
 
1156
        super(TestHTTPRedirectionsBase, self).setUp()
1140
1157
        # The redirections will point to the new server
1141
1158
        self.new_server = self.get_readonly_server()
1142
1159
        # The requests to the old server will be redirected
1170
1187
        self.assertIsInstance(bdir.root_transport, type(start))
1171
1188
 
1172
1189
 
1173
 
class TestHTTPRedirections_urllib(TestHTTPRedirections,
1174
 
                                  http_utils.TestCaseWithTwoWebservers):
 
1190
class TestHTTPRedirections(TestHTTPRedirectionsBase,
 
1191
                           http_utils.TestCaseWithTwoWebservers):
1175
1192
    """Tests redirections for urllib implementation"""
1176
1193
 
1177
 
    _transport = HttpTransport_urllib
 
1194
    _transport = HttpTransport
1178
1195
 
1179
1196
    def _qualified_url(self, host, port):
1180
 
        result = 'http+urllib://%s:%s' % (host, port)
 
1197
        result = 'http://%s:%s' % (host, port)
1181
1198
        self.permit_url(result)
1182
1199
        return result
1183
1200
 
1184
1201
 
1185
 
 
1186
 
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
1187
 
                                  http_utils.TestCaseWithTwoWebservers):
 
1202
class TestHTTPRedirections_nosmart(TestHTTPRedirectionsBase,
 
1203
                                   http_utils.TestCaseWithTwoWebservers):
1188
1204
    """Tests redirections for the nosmart decorator"""
1189
1205
 
1190
1206
    _transport = NoSmartTransportDecorator
1195
1211
        return result
1196
1212
 
1197
1213
 
1198
 
class TestHTTPRedirections_readonly(TestHTTPRedirections,
 
1214
class TestHTTPRedirections_readonly(TestHTTPRedirectionsBase,
1199
1215
                                    http_utils.TestCaseWithTwoWebservers):
1200
1216
    """Tests redirections for readonly decoratror"""
1201
1217
 
1215
1231
 
1216
1232
    def get_ls(self):
1217
1233
        f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
1218
 
            stderr=subprocess.PIPE)
 
1234
                             stderr=subprocess.PIPE)
1219
1235
        out, err = f.communicate()
1220
1236
        self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
1221
1237
                         % (self.ls, err))
1222
1238
        return out.splitlines()
1223
1239
 
1224
1240
    def test_dot_bzr_hidden(self):
1225
 
        if sys.platform == 'win32' and not win32utils.has_win32file:
1226
 
            raise TestSkipped('unable to make file hidden without pywin32 library')
1227
1241
        b = bzrdir.BzrDir.create('.')
1228
1242
        self.build_tree(['a'])
1229
 
        self.assertEqual(['a'], self.get_ls())
 
1243
        self.assertEqual([b'a'], self.get_ls())
1230
1244
 
1231
1245
    def test_dot_bzr_hidden_with_url(self):
1232
 
        if sys.platform == 'win32' and not win32utils.has_win32file:
1233
 
            raise TestSkipped('unable to make file hidden without pywin32 library')
1234
1246
        b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1235
1247
        self.build_tree(['a'])
1236
 
        self.assertEqual(['a'], self.get_ls())
 
1248
        self.assertEqual([b'a'], self.get_ls())
1237
1249
 
1238
1250
 
1239
1251
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1355
1367
 
1356
1368
    def test_pre_open_actual_exceptions_raised(self):
1357
1369
        count = [0]
 
1370
 
1358
1371
        def fail_once(transport):
1359
1372
            count[0] += 1
1360
1373
            if count[0] == 1:
1369
1382
        from ..controldir import RepoInitHookParams
1370
1383
        calls = []
1371
1384
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1372
 
            calls.append, None)
 
1385
                                               calls.append, None)
1373
1386
        self.make_repository('foo')
1374
1387
        self.assertLength(1, calls)
1375
1388
        params = calls[0]
1380
1393
    def test_post_repo_init_hook_repr(self):
1381
1394
        param_reprs = []
1382
1395
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1383
 
            lambda params: param_reprs.append(repr(params)), None)
 
1396
                                               lambda params: param_reprs.append(repr(params)), None)
1384
1397
        self.make_repository('foo')
1385
1398
        self.assertLength(1, param_reprs)
1386
1399
        param_repr = param_reprs[0]
1396
1409
        super(TestGenerateBackupName, self).setUp()
1397
1410
        self._transport = self.get_transport()
1398
1411
        bzrdir.BzrDir.create(self.get_url(),
1399
 
            possible_transports=[self._transport])
 
1412
                             possible_transports=[self._transport])
1400
1413
        self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
1401
1414
 
1402
1415
    def test_new(self):
1403
1416
        self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
1404
1417
 
1405
1418
    def test_exiting(self):
1406
 
        self._transport.put_bytes("a.~1~", "some content")
 
1419
        self._transport.put_bytes("a.~1~", b"some content")
1407
1420
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1408
1421
 
1409
1422
 
1456
1469
            errors.AlreadyBranchError, tree.controldir.create_branch,
1457
1470
            name='foo')
1458
1471
 
 
1472
    def test_supports_relative_reference(self):
 
1473
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1474
        target1 = tree.controldir.create_branch(name='target1')
 
1475
        target2 = tree.controldir.create_branch(name='target2')
 
1476
        source = tree.controldir.set_branch_reference(target1, name='source')
 
1477
        self.assertEqual(
 
1478
            target1.user_url, tree.controldir.open_branch('source').user_url)
 
1479
        source.controldir.get_branch_transport(None, 'source').put_bytes(
 
1480
            'location', b'file:,branch=target2')
 
1481
        self.assertEqual(
 
1482
            target2.user_url, tree.controldir.open_branch('source').user_url)
 
1483
 
1459
1484
 
1460
1485
class SampleBzrFormat(bzrdir.BzrFormat):
1461
1486
 
1462
1487
    @classmethod
1463
1488
    def get_format_string(cls):
1464
 
        return "First line\n"
 
1489
        return b"First line\n"
1465
1490
 
1466
1491
 
1467
1492
class TestBzrFormat(TestCase):
1469
1494
 
1470
1495
    def test_as_string(self):
1471
1496
        format = SampleBzrFormat()
1472
 
        format.features = {"foo": "required"}
1473
 
        self.assertEqual(format.as_string(),
1474
 
            "First line\n"
1475
 
            "required foo\n")
1476
 
        format.features["another"] = "optional"
1477
 
        self.assertEqual(format.as_string(),
1478
 
            "First line\n"
1479
 
            "required foo\n"
1480
 
            "optional another\n")
 
1497
        format.features = {b"foo": b"required"}
 
1498
        self.assertEqual(format.as_string(),
 
1499
                         b"First line\n"
 
1500
                         b"required foo\n")
 
1501
        format.features[b"another"] = b"optional"
 
1502
        self.assertEqual(format.as_string(),
 
1503
                         b"First line\n"
 
1504
                         b"optional another\n"
 
1505
                         b"required foo\n")
1481
1506
 
1482
1507
    def test_network_name(self):
1483
1508
        # The network string should include the feature info
1484
1509
        format = SampleBzrFormat()
1485
 
        format.features = {"foo": "required"}
 
1510
        format.features = {b"foo": b"required"}
1486
1511
        self.assertEqual(
1487
 
            "First line\nrequired foo\n",
 
1512
            b"First line\nrequired foo\n",
1488
1513
            format.network_name())
1489
1514
 
1490
1515
    def test_from_string_no_features(self):
1491
1516
        # No features
1492
1517
        format = SampleBzrFormat.from_string(
1493
 
            "First line\n")
 
1518
            b"First line\n")
1494
1519
        self.assertEqual({}, format.features)
1495
1520
 
1496
1521
    def test_from_string_with_feature(self):
1497
1522
        # Proper feature
1498
1523
        format = SampleBzrFormat.from_string(
1499
 
            "First line\nrequired foo\n")
1500
 
        self.assertEqual("required", format.features.get("foo"))
 
1524
            b"First line\nrequired foo\n")
 
1525
        self.assertEqual(b"required", format.features.get(b"foo"))
1501
1526
 
1502
1527
    def test_from_string_format_string_mismatch(self):
1503
1528
        # The first line has to match the format string
1504
1529
        self.assertRaises(AssertionError, SampleBzrFormat.from_string,
1505
 
            "Second line\nrequired foo\n")
 
1530
                          b"Second line\nrequired foo\n")
1506
1531
 
1507
1532
    def test_from_string_missing_space(self):
1508
1533
        # At least one space is required in the feature lines
1509
1534
        self.assertRaises(errors.ParseFormatError, SampleBzrFormat.from_string,
1510
 
            "First line\nfoo\n")
 
1535
                          b"First line\nfoo\n")
1511
1536
 
1512
1537
    def test_from_string_with_spaces(self):
1513
1538
        # Feature with spaces (in case we add stuff like this in the future)
1514
1539
        format = SampleBzrFormat.from_string(
1515
 
            "First line\nrequired foo with spaces\n")
1516
 
        self.assertEqual("required", format.features.get("foo with spaces"))
 
1540
            b"First line\nrequired foo with spaces\n")
 
1541
        self.assertEqual(b"required", format.features.get(b"foo with spaces"))
1517
1542
 
1518
1543
    def test_eq(self):
1519
1544
        format1 = SampleBzrFormat()
1520
 
        format1.features = {"nested-trees": "optional"}
 
1545
        format1.features = {b"nested-trees": b"optional"}
1521
1546
        format2 = SampleBzrFormat()
1522
 
        format2.features = {"nested-trees": "optional"}
 
1547
        format2.features = {b"nested-trees": b"optional"}
1523
1548
        self.assertEqual(format1, format1)
1524
1549
        self.assertEqual(format1, format2)
1525
1550
        format3 = SampleBzrFormat()
1528
1553
    def test_check_support_status_optional(self):
1529
1554
        # Optional, so silently ignore
1530
1555
        format = SampleBzrFormat()
1531
 
        format.features = {"nested-trees": "optional"}
 
1556
        format.features = {b"nested-trees": b"optional"}
1532
1557
        format.check_support_status(True)
1533
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1534
 
        SampleBzrFormat.register_feature("nested-trees")
 
1558
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
 
1559
        SampleBzrFormat.register_feature(b"nested-trees")
1535
1560
        format.check_support_status(True)
1536
1561
 
1537
1562
    def test_check_support_status_required(self):
1538
1563
        # Optional, so trigger an exception
1539
1564
        format = SampleBzrFormat()
1540
 
        format.features = {"nested-trees": "required"}
 
1565
        format.features = {b"nested-trees": b"required"}
1541
1566
        self.assertRaises(bzrdir.MissingFeature, format.check_support_status,
1542
 
            True)
1543
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1544
 
        SampleBzrFormat.register_feature("nested-trees")
 
1567
                          True)
 
1568
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
 
1569
        SampleBzrFormat.register_feature(b"nested-trees")
1545
1570
        format.check_support_status(True)
1546
1571
 
1547
1572
    def test_check_support_status_unknown(self):
1548
1573
        # treat unknown necessity as required
1549
1574
        format = SampleBzrFormat()
1550
 
        format.features = {"nested-trees": "unknown"}
 
1575
        format.features = {b"nested-trees": b"unknown"}
1551
1576
        self.assertRaises(bzrdir.MissingFeature, format.check_support_status,
1552
 
            True)
1553
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1554
 
        SampleBzrFormat.register_feature("nested-trees")
 
1577
                          True)
 
1578
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
 
1579
        SampleBzrFormat.register_feature(b"nested-trees")
1555
1580
        format.check_support_status(True)
1556
1581
 
1557
1582
    def test_feature_already_registered(self):
1558
1583
        # a feature can only be registered once
1559
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1560
 
        SampleBzrFormat.register_feature("nested-trees")
 
1584
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
 
1585
        SampleBzrFormat.register_feature(b"nested-trees")
1561
1586
        self.assertRaises(bzrdir.FeatureAlreadyRegistered,
1562
 
            SampleBzrFormat.register_feature, "nested-trees")
 
1587
                          SampleBzrFormat.register_feature, b"nested-trees")
1563
1588
 
1564
1589
    def test_feature_with_space(self):
1565
1590
        # spaces are not allowed in feature names
1566
1591
        self.assertRaises(ValueError, SampleBzrFormat.register_feature,
1567
 
            "nested trees")
 
1592
                          b"nested trees")