~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Martin Pool
  • Date: 2011-01-20 23:07:25 UTC
  • mfrom: (5626 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5630.
  • Revision ID: mbp@canonical.com-20110120230725-12l7ltnko5x3fgnz
merge news

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
from bzrlib import (
27
27
    branch,
28
28
    bzrdir,
29
 
    config,
30
29
    controldir,
31
30
    errors,
32
31
    help_topics,
33
32
    lock,
34
33
    repository,
35
 
    revision as _mod_revision,
36
34
    osutils,
37
35
    remote,
38
36
    symbol_versioning,
39
 
    transport as _mod_transport,
40
37
    urlutils,
41
38
    win32utils,
42
 
    workingtree_3,
43
 
    workingtree_4,
 
39
    workingtree,
44
40
    )
45
41
import bzrlib.branch
46
 
from bzrlib.errors import (
47
 
    NotBranchError,
48
 
    NoColocatedBranchSupport,
49
 
    UnknownFormatError,
50
 
    UnsupportedFormatError,
51
 
    )
 
42
from bzrlib.errors import (NotBranchError,
 
43
                           NoColocatedBranchSupport,
 
44
                           UnknownFormatError,
 
45
                           UnsupportedFormatError,
 
46
                           )
52
47
from bzrlib.tests import (
53
48
    TestCase,
54
49
    TestCaseWithMemoryTransport,
61
56
    )
62
57
from bzrlib.tests.test_http import TestWithTransport_pycurl
63
58
from bzrlib.transport import (
 
59
    get_transport,
64
60
    memory,
65
61
    pathfilter,
66
62
    )
67
63
from bzrlib.transport.http._urllib import HttpTransport_urllib
68
64
from bzrlib.transport.nosmart import NoSmartTransportDecorator
69
65
from bzrlib.transport.readonly import ReadonlyTransportDecorator
70
 
from bzrlib.repofmt import knitrepo, knitpack_repo
 
66
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
71
67
 
72
68
 
73
69
class TestDefaultFormat(TestCase):
74
70
 
75
71
    def test_get_set_default_format(self):
76
72
        old_format = bzrdir.BzrDirFormat.get_default_format()
77
 
        # default is BzrDirMetaFormat1
78
 
        self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
 
73
        # default is BzrDirFormat6
 
74
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
79
75
        controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
80
76
        # creating a bzr dir should now create an instrumented dir.
81
77
        try:
82
78
            result = bzrdir.BzrDir.create('memory:///')
83
 
            self.assertIsInstance(result, SampleBzrDir)
 
79
            self.failUnless(isinstance(result, SampleBzrDir))
84
80
        finally:
85
81
            controldir.ControlDirFormat._set_default_format(old_format)
86
82
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
87
83
 
88
84
 
89
 
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
90
 
    """A deprecated bzr dir format."""
91
 
 
92
 
 
93
85
class TestFormatRegistry(TestCase):
94
86
 
95
87
    def make_format_registry(self):
96
88
        my_format_registry = controldir.ControlDirFormatRegistry()
97
 
        my_format_registry.register('deprecated', DeprecatedBzrDirFormat,
98
 
            'Some format.  Slower and unawesome and deprecated.',
99
 
            deprecated=True)
100
 
        my_format_registry.register_lazy('lazy', 'bzrlib.tests.test_bzrdir',
101
 
            'DeprecatedBzrDirFormat', 'Format registered lazily',
102
 
            deprecated=True)
 
89
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
 
90
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
91
            ' repositories', deprecated=True)
 
92
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
 
93
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
103
94
        bzrdir.register_metadir(my_format_registry, 'knit',
104
95
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
105
96
            'Format using knits',
116
107
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
117
108
            'Experimental successor to knit.  Use at your own risk.',
118
109
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
119
 
        my_format_registry.register('hiddendeprecated', DeprecatedBzrDirFormat,
120
 
            'Old format.  Slower and does not support things. ', hidden=True)
121
 
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.tests.test_bzrdir',
122
 
            'DeprecatedBzrDirFormat', 'Format registered lazily',
123
 
            deprecated=True, hidden=True)
 
110
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
 
111
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
112
            ' repositories', hidden=True)
 
113
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
 
114
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
 
115
            hidden=True)
124
116
        return my_format_registry
125
117
 
126
118
    def test_format_registry(self):
127
119
        my_format_registry = self.make_format_registry()
128
120
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
129
 
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
130
 
        my_bzrdir = my_format_registry.make_bzrdir('deprecated')
131
 
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
 
121
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
122
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
123
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
132
124
        my_bzrdir = my_format_registry.make_bzrdir('default')
133
125
        self.assertIsInstance(my_bzrdir.repository_format,
134
126
            knitrepo.RepositoryFormatKnit1)
147
139
                         my_format_registry.get_help('knit'))
148
140
        self.assertEqual('Format using knits',
149
141
                         my_format_registry.get_help('default'))
150
 
        self.assertEqual('Some format.  Slower and unawesome and deprecated.',
151
 
                         my_format_registry.get_help('deprecated'))
 
142
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
 
143
                         ' checkouts or shared repositories',
 
144
                         my_format_registry.get_help('weave'))
152
145
 
153
146
    def test_help_topic(self):
154
147
        topics = help_topics.HelpTopicRegistry()
178
171
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
179
172
                          bzrdir.format_registry.get('default'))
180
173
            self.assertIs(
181
 
                repository.format_registry.get_default().__class__,
 
174
                repository.RepositoryFormat.get_default_format().__class__,
182
175
                knitrepo.RepositoryFormatKnit3)
183
176
        finally:
184
177
            bzrdir.format_registry.set_default_repository(old_default)
185
178
 
186
179
    def test_aliases(self):
187
180
        a_registry = controldir.ControlDirFormatRegistry()
188
 
        a_registry.register('deprecated', DeprecatedBzrDirFormat,
189
 
            'Old format.  Slower and does not support stuff',
190
 
            deprecated=True)
191
 
        a_registry.register('deprecatedalias', DeprecatedBzrDirFormat,
192
 
            'Old format.  Slower and does not support stuff',
193
 
            deprecated=True, alias=True)
194
 
        self.assertEqual(frozenset(['deprecatedalias']), a_registry.aliases())
 
181
        a_registry.register('weave', bzrdir.BzrDirFormat6,
 
182
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
183
            ' repositories', deprecated=True)
 
184
        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
 
185
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
186
            ' repositories', deprecated=True, alias=True)
 
187
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
195
188
 
196
189
 
197
190
class SampleBranch(bzrlib.branch.Branch):
254
247
        return "opened branch."
255
248
 
256
249
 
257
 
class BzrDirFormatTest1(bzrdir.BzrDirMetaFormat1):
258
 
 
259
 
    @staticmethod
260
 
    def get_format_string():
261
 
        return "Test format 1"
262
 
 
263
 
 
264
 
class BzrDirFormatTest2(bzrdir.BzrDirMetaFormat1):
265
 
 
266
 
    @staticmethod
267
 
    def get_format_string():
268
 
        return "Test format 2"
269
 
 
270
 
 
271
250
class TestBzrDirFormat(TestCaseWithTransport):
272
251
    """Tests for the BzrDirFormat facility."""
273
252
 
274
253
    def test_find_format(self):
275
254
        # is the right format object found for a branch?
276
255
        # create a branch with a few known format objects.
277
 
        bzrdir.BzrProber.formats.register(BzrDirFormatTest1.get_format_string(),
278
 
            BzrDirFormatTest1())
279
 
        self.addCleanup(bzrdir.BzrProber.formats.remove,
280
 
            BzrDirFormatTest1.get_format_string())
281
 
        bzrdir.BzrProber.formats.register(BzrDirFormatTest2.get_format_string(),
282
 
            BzrDirFormatTest2())
283
 
        self.addCleanup(bzrdir.BzrProber.formats.remove,
284
 
            BzrDirFormatTest2.get_format_string())
285
 
        t = self.get_transport()
 
256
        # this is not quite the same as
 
257
        t = get_transport(self.get_url())
286
258
        self.build_tree(["foo/", "bar/"], transport=t)
287
259
        def check_format(format, url):
288
260
            format.initialize(url)
289
 
            t = _mod_transport.get_transport_from_path(url)
 
261
            t = get_transport(url)
290
262
            found_format = bzrdir.BzrDirFormat.find_format(t)
291
 
            self.assertIsInstance(found_format, format.__class__)
292
 
        check_format(BzrDirFormatTest1(), "foo")
293
 
        check_format(BzrDirFormatTest2(), "bar")
 
263
            self.failUnless(isinstance(found_format, format.__class__))
 
264
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
265
        check_format(bzrdir.BzrDirFormat6(), "bar")
294
266
 
295
267
    def test_find_format_nothing_there(self):
296
268
        self.assertRaises(NotBranchError,
297
269
                          bzrdir.BzrDirFormat.find_format,
298
 
                          _mod_transport.get_transport_from_path('.'))
 
270
                          get_transport('.'))
299
271
 
300
272
    def test_find_format_unknown_format(self):
301
 
        t = self.get_transport()
 
273
        t = get_transport(self.get_url())
302
274
        t.mkdir('.bzr')
303
275
        t.put_bytes('.bzr/branch-format', '')
304
276
        self.assertRaises(UnknownFormatError,
305
277
                          bzrdir.BzrDirFormat.find_format,
306
 
                          _mod_transport.get_transport_from_path('.'))
 
278
                          get_transport('.'))
307
279
 
308
280
    def test_register_unregister_format(self):
309
281
        format = SampleBzrDirFormat()
311
283
        # make a bzrdir
312
284
        format.initialize(url)
313
285
        # register a format for it.
314
 
        bzrdir.BzrProber.formats.register(format.get_format_string(), format)
 
286
        bzrdir.BzrDirFormat.register_format(format)
315
287
        # which bzrdir.Open will refuse (not supported)
316
288
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
317
289
        # which bzrdir.open_containing will refuse (not supported)
318
290
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
319
291
        # but open_downlevel will work
320
 
        t = _mod_transport.get_transport_from_url(url)
 
292
        t = get_transport(url)
321
293
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
322
294
        # unregister the format
323
 
        bzrdir.BzrProber.formats.remove(format.get_format_string())
 
295
        bzrdir.BzrDirFormat.unregister_format(format)
324
296
        # now open_downlevel should fail too.
325
297
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
326
298
 
503
475
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
504
476
        # Make stackable source branch with an unstackable repo format.
505
477
        source_bzrdir = self.make_bzrdir('source')
506
 
        knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
478
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
507
479
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
508
480
            source_bzrdir)
509
481
        # Make a directory with a default stacking policy
709
681
        self.assertEqual(relpath, 'baz')
710
682
 
711
683
    def test_open_containing_from_transport(self):
712
 
        self.assertRaises(NotBranchError,
713
 
            bzrdir.BzrDir.open_containing_from_transport,
714
 
            _mod_transport.get_transport_from_url(self.get_readonly_url('')))
715
 
        self.assertRaises(NotBranchError,
716
 
            bzrdir.BzrDir.open_containing_from_transport,
717
 
            _mod_transport.get_transport_from_url(
718
 
                self.get_readonly_url('g/p/q')))
 
684
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
685
                          get_transport(self.get_readonly_url('')))
 
686
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
687
                          get_transport(self.get_readonly_url('g/p/q')))
719
688
        control = bzrdir.BzrDir.create(self.get_url())
720
689
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
721
 
            _mod_transport.get_transport_from_url(
722
 
                self.get_readonly_url('')))
 
690
            get_transport(self.get_readonly_url('')))
723
691
        self.assertEqual('', relpath)
724
692
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
725
 
            _mod_transport.get_transport_from_url(
726
 
                self.get_readonly_url('g/p/q')))
 
693
            get_transport(self.get_readonly_url('g/p/q')))
727
694
        self.assertEqual('g/p/q', relpath)
728
695
 
729
696
    def test_open_containing_tree_or_branch(self):
773
740
        # transport pointing at bzrdir should give a bzrdir with root transport
774
741
        # set to the given transport
775
742
        control = bzrdir.BzrDir.create(self.get_url())
776
 
        t = self.get_transport()
777
 
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(t)
778
 
        self.assertEqual(t.base, opened_bzrdir.root_transport.base)
 
743
        transport = get_transport(self.get_url())
 
744
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
 
745
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
779
746
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
780
747
 
781
748
    def test_open_from_transport_no_bzrdir(self):
782
 
        t = self.get_transport()
783
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
749
        transport = get_transport(self.get_url())
 
750
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
751
                          transport)
784
752
 
785
753
    def test_open_from_transport_bzrdir_in_parent(self):
786
754
        control = bzrdir.BzrDir.create(self.get_url())
787
 
        t = self.get_transport()
788
 
        t.mkdir('subdir')
789
 
        t = t.clone('subdir')
790
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
755
        transport = get_transport(self.get_url())
 
756
        transport.mkdir('subdir')
 
757
        transport = transport.clone('subdir')
 
758
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
759
                          transport)
791
760
 
792
761
    def test_sprout_recursive(self):
793
762
        tree = self.make_branch_and_tree('tree1',
802
771
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
803
772
        tree2.lock_read()
804
773
        self.addCleanup(tree2.unlock)
805
 
        self.assertPathExists('tree2/subtree/file')
 
774
        self.failUnlessExists('tree2/subtree/file')
806
775
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
807
776
 
808
777
    def test_cloning_metadir(self):
812
781
        branch = self.make_branch('branch', format='knit')
813
782
        format = branch.bzrdir.cloning_metadir()
814
783
        self.assertIsInstance(format.workingtree_format,
815
 
            workingtree_4.WorkingTreeFormat6)
 
784
            workingtree.WorkingTreeFormat3)
816
785
 
817
786
    def test_sprout_recursive_treeless(self):
818
787
        tree = self.make_branch_and_tree('tree1',
842
811
        # #634470.  -- vila 20100909
843
812
        self.assertRaises(errors.NotBranchError,
844
813
                          tree.bzrdir.sprout, 'repo/tree2')
845
 
#        self.assertPathExists('repo/tree2/subtree')
846
 
#        self.assertPathDoesNotExist('repo/tree2/subtree/file')
 
814
#        self.failUnlessExists('repo/tree2/subtree')
 
815
#        self.failIfExists('repo/tree2/subtree/file')
847
816
 
848
817
    def make_foo_bar_baz(self):
849
818
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
853
822
 
854
823
    def test_find_bzrdirs(self):
855
824
        foo, bar, baz = self.make_foo_bar_baz()
856
 
        t = self.get_transport()
857
 
        self.assertEqualBzrdirs([baz, foo, bar], bzrdir.BzrDir.find_bzrdirs(t))
 
825
        transport = get_transport(self.get_url())
 
826
        self.assertEqualBzrdirs([baz, foo, bar],
 
827
                                bzrdir.BzrDir.find_bzrdirs(transport))
858
828
 
859
829
    def make_fake_permission_denied_transport(self, transport, paths):
860
830
        """Create a transport that raises PermissionDenied for some paths."""
876
846
 
877
847
    def test_find_bzrdirs_permission_denied(self):
878
848
        foo, bar, baz = self.make_foo_bar_baz()
879
 
        t = self.get_transport()
 
849
        transport = get_transport(self.get_url())
880
850
        path_filter_server, path_filter_transport = \
881
 
            self.make_fake_permission_denied_transport(t, ['foo'])
 
851
            self.make_fake_permission_denied_transport(transport, ['foo'])
882
852
        # local transport
883
853
        self.assertBranchUrlsEndWith('/baz/',
884
854
            bzrdir.BzrDir.find_bzrdirs(path_filter_transport))
893
863
            return [s for s in transport.list_dir('') if s != 'baz']
894
864
 
895
865
        foo, bar, baz = self.make_foo_bar_baz()
896
 
        t = self.get_transport()
897
 
        self.assertEqualBzrdirs(
898
 
            [foo, bar],
899
 
            bzrdir.BzrDir.find_bzrdirs(t, list_current=list_current))
 
866
        transport = get_transport(self.get_url())
 
867
        self.assertEqualBzrdirs([foo, bar],
 
868
                                bzrdir.BzrDir.find_bzrdirs(transport,
 
869
                                    list_current=list_current))
900
870
 
901
871
    def test_find_bzrdirs_evaluate(self):
902
872
        def evaluate(bzrdir):
903
873
            try:
904
874
                repo = bzrdir.open_repository()
905
 
            except errors.NoRepositoryPresent:
 
875
            except NoRepositoryPresent:
906
876
                return True, bzrdir.root_transport.base
907
877
            else:
908
878
                return False, bzrdir.root_transport.base
909
879
 
910
880
        foo, bar, baz = self.make_foo_bar_baz()
911
 
        t = self.get_transport()
 
881
        transport = get_transport(self.get_url())
912
882
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
913
 
                         list(bzrdir.BzrDir.find_bzrdirs(t, evaluate=evaluate)))
 
883
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
 
884
                                                         evaluate=evaluate)))
914
885
 
915
886
    def assertEqualBzrdirs(self, first, second):
916
887
        first = list(first)
923
894
        root = self.make_repository('', shared=True)
924
895
        foo, bar, baz = self.make_foo_bar_baz()
925
896
        qux = self.make_bzrdir('foo/qux')
926
 
        t = self.get_transport()
927
 
        branches = bzrdir.BzrDir.find_branches(t)
 
897
        transport = get_transport(self.get_url())
 
898
        branches = bzrdir.BzrDir.find_branches(transport)
928
899
        self.assertEqual(baz.root_transport.base, branches[0].base)
929
900
        self.assertEqual(foo.root_transport.base, branches[1].base)
930
901
        self.assertEqual(bar.root_transport.base, branches[2].base)
931
902
 
932
903
        # ensure this works without a top-level repo
933
 
        branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
 
904
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
934
905
        self.assertEqual(foo.root_transport.base, branches[0].base)
935
906
        self.assertEqual(bar.root_transport.base, branches[1].base)
936
907
 
938
909
class TestMissingRepoBranchesSkipped(TestCaseWithMemoryTransport):
939
910
 
940
911
    def test_find_bzrdirs_missing_repo(self):
941
 
        t = self.get_transport()
 
912
        transport = get_transport(self.get_url())
942
913
        arepo = self.make_repository('arepo', shared=True)
943
914
        abranch_url = arepo.user_url + '/abranch'
944
915
        abranch = bzrdir.BzrDir.create(abranch_url).create_branch()
945
 
        t.delete_tree('arepo/.bzr')
 
916
        transport.delete_tree('arepo/.bzr')
946
917
        self.assertRaises(errors.NoRepositoryPresent,
947
918
            branch.Branch.open, abranch_url)
948
919
        self.make_branch('baz')
949
 
        for actual_bzrdir in bzrdir.BzrDir.find_branches(t):
 
920
        for actual_bzrdir in bzrdir.BzrDir.find_branches(transport):
950
921
            self.assertEndsWith(actual_bzrdir.user_url, '/baz/')
951
922
 
952
923
 
962
933
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
963
934
        repository_base = t.clone('repository').base
964
935
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
965
 
        repository_format = repository.format_registry.get_default()
966
936
        self.assertEqual(repository_base,
967
 
                         dir.get_repository_transport(repository_format).base)
 
937
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
968
938
        checkout_base = t.clone('checkout').base
969
939
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
970
940
        self.assertEqual(checkout_base,
971
 
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
 
941
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
972
942
 
973
943
    def test_meta1dir_uses_lockdir(self):
974
944
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
1016
986
        self.assertEqual(2, rpc_count)
1017
987
 
1018
988
 
 
989
class TestFormat5(TestCaseWithTransport):
 
990
    """Tests specific to the version 5 bzrdir format."""
 
991
 
 
992
    def test_same_lockfiles_between_tree_repo_branch(self):
 
993
        # this checks that only a single lockfiles instance is created
 
994
        # for format 5 objects
 
995
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
996
        def check_dir_components_use_same_lock(dir):
 
997
            ctrl_1 = dir.open_repository().control_files
 
998
            ctrl_2 = dir.open_branch().control_files
 
999
            ctrl_3 = dir.open_workingtree()._control_files
 
1000
            self.assertTrue(ctrl_1 is ctrl_2)
 
1001
            self.assertTrue(ctrl_2 is ctrl_3)
 
1002
        check_dir_components_use_same_lock(dir)
 
1003
        # and if we open it normally.
 
1004
        dir = bzrdir.BzrDir.open(self.get_url())
 
1005
        check_dir_components_use_same_lock(dir)
 
1006
 
 
1007
    def test_can_convert(self):
 
1008
        # format 5 dirs are convertable
 
1009
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
1010
        self.assertTrue(dir.can_convert_format())
 
1011
 
 
1012
    def test_needs_conversion(self):
 
1013
        # format 5 dirs need a conversion if they are not the default,
 
1014
        # and they aren't
 
1015
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
1016
        # don't need to convert it to itself
 
1017
        self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
 
1018
        # do need to convert it to the current default
 
1019
        self.assertTrue(dir.needs_format_conversion(
 
1020
            bzrdir.BzrDirFormat.get_default_format()))
 
1021
 
 
1022
 
 
1023
class TestFormat6(TestCaseWithTransport):
 
1024
    """Tests specific to the version 6 bzrdir format."""
 
1025
 
 
1026
    def test_same_lockfiles_between_tree_repo_branch(self):
 
1027
        # this checks that only a single lockfiles instance is created
 
1028
        # for format 6 objects
 
1029
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1030
        def check_dir_components_use_same_lock(dir):
 
1031
            ctrl_1 = dir.open_repository().control_files
 
1032
            ctrl_2 = dir.open_branch().control_files
 
1033
            ctrl_3 = dir.open_workingtree()._control_files
 
1034
            self.assertTrue(ctrl_1 is ctrl_2)
 
1035
            self.assertTrue(ctrl_2 is ctrl_3)
 
1036
        check_dir_components_use_same_lock(dir)
 
1037
        # and if we open it normally.
 
1038
        dir = bzrdir.BzrDir.open(self.get_url())
 
1039
        check_dir_components_use_same_lock(dir)
 
1040
 
 
1041
    def test_can_convert(self):
 
1042
        # format 6 dirs are convertable
 
1043
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1044
        self.assertTrue(dir.can_convert_format())
 
1045
 
 
1046
    def test_needs_conversion(self):
 
1047
        # format 6 dirs need an conversion if they are not the default.
 
1048
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1049
        self.assertTrue(dir.needs_format_conversion(
 
1050
            bzrdir.BzrDirFormat.get_default_format()))
 
1051
 
 
1052
 
 
1053
class NotBzrDir(bzrlib.bzrdir.BzrDir):
 
1054
    """A non .bzr based control directory."""
 
1055
 
 
1056
    def __init__(self, transport, format):
 
1057
        self._format = format
 
1058
        self.root_transport = transport
 
1059
        self.transport = transport.clone('.not')
 
1060
 
 
1061
 
 
1062
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
 
1063
    """A test class representing any non-.bzr based disk format."""
 
1064
 
 
1065
    def initialize_on_transport(self, transport):
 
1066
        """Initialize a new .not dir in the base directory of a Transport."""
 
1067
        transport.mkdir('.not')
 
1068
        return self.open(transport)
 
1069
 
 
1070
    def open(self, transport):
 
1071
        """Open this directory."""
 
1072
        return NotBzrDir(transport, self)
 
1073
 
 
1074
    @classmethod
 
1075
    def _known_formats(self):
 
1076
        return set([NotBzrDirFormat()])
 
1077
 
 
1078
 
 
1079
class NotBzrDirProber(controldir.Prober):
 
1080
 
 
1081
    def probe_transport(self, transport):
 
1082
        """Our format is present if the transport ends in '.not/'."""
 
1083
        if transport.has('.not'):
 
1084
            return NotBzrDirFormat()
 
1085
 
 
1086
 
 
1087
class TestNotBzrDir(TestCaseWithTransport):
 
1088
    """Tests for using the bzrdir api with a non .bzr based disk format.
 
1089
 
 
1090
    If/when one of these is in the core, we can let the implementation tests
 
1091
    verify this works.
 
1092
    """
 
1093
 
 
1094
    def test_create_and_find_format(self):
 
1095
        # create a .notbzr dir
 
1096
        format = NotBzrDirFormat()
 
1097
        dir = format.initialize(self.get_url())
 
1098
        self.assertIsInstance(dir, NotBzrDir)
 
1099
        # now probe for it.
 
1100
        controldir.ControlDirFormat.register_prober(NotBzrDirProber)
 
1101
        try:
 
1102
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
 
1103
                get_transport(self.get_url()))
 
1104
            self.assertIsInstance(found, NotBzrDirFormat)
 
1105
        finally:
 
1106
            controldir.ControlDirFormat.unregister_prober(NotBzrDirProber)
 
1107
 
 
1108
    def test_included_in_known_formats(self):
 
1109
        not_format = NotBzrDirFormat()
 
1110
        bzrlib.controldir.ControlDirFormat.register_format(not_format)
 
1111
        try:
 
1112
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
 
1113
            for format in formats:
 
1114
                if isinstance(format, NotBzrDirFormat):
 
1115
                    return
 
1116
            self.fail("No NotBzrDirFormat in %s" % formats)
 
1117
        finally:
 
1118
            bzrlib.controldir.ControlDirFormat.unregister_format(not_format)
 
1119
 
 
1120
 
1019
1121
class NonLocalTests(TestCaseWithTransport):
1020
1122
    """Tests for bzrdir static behaviour on non local paths."""
1021
1123
 
1040
1142
            self.get_url('foo'),
1041
1143
            force_new_tree=True,
1042
1144
            format=format)
1043
 
        t = self.get_transport()
 
1145
        t = get_transport(self.get_url('.'))
1044
1146
        self.assertFalse(t.has('foo'))
1045
1147
 
1046
1148
    def test_clone(self):
1062
1164
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1063
1165
        checkout_format = my_bzrdir.checkout_metadir()
1064
1166
        self.assertIsInstance(checkout_format.workingtree_format,
1065
 
                              workingtree_4.WorkingTreeFormat4)
 
1167
                              workingtree.WorkingTreeFormat3)
1066
1168
 
1067
1169
 
1068
1170
class TestHTTPRedirections(object):
1212
1314
 
1213
1315
    def __init__(self, *args, **kwargs):
1214
1316
        super(_TestBzrDir, self).__init__(*args, **kwargs)
1215
 
        self.test_branch = _TestBranch(self.transport)
 
1317
        self.test_branch = _TestBranch()
1216
1318
        self.test_branch.repository = self.create_repository()
1217
1319
 
1218
1320
    def open_branch(self, unsupported=False):
1229
1331
class _TestBranch(bzrlib.branch.Branch):
1230
1332
    """Test Branch implementation for TestBzrDirSprout."""
1231
1333
 
1232
 
    def __init__(self, transport, *args, **kwargs):
 
1334
    def __init__(self, *args, **kwargs):
1233
1335
        self._format = _TestBranchFormat()
1234
 
        self._transport = transport
1235
 
        self.base = transport.base
1236
1336
        super(_TestBranch, self).__init__(*args, **kwargs)
1237
1337
        self.calls = []
1238
1338
        self._parent = None
1239
1339
 
1240
1340
    def sprout(self, *args, **kwargs):
1241
1341
        self.calls.append('sprout')
1242
 
        return _TestBranch(self._transport)
 
1342
        return _TestBranch()
1243
1343
 
1244
1344
    def copy_content_into(self, destination, revision_id=None):
1245
1345
        self.calls.append('copy_content_into')
1246
1346
 
1247
 
    def last_revision(self):
1248
 
        return _mod_revision.NULL_REVISION
1249
 
 
1250
1347
    def get_parent(self):
1251
1348
        return self._parent
1252
1349
 
1253
 
    def _get_config(self):
1254
 
        return config.TransportConfig(self._transport, 'branch.conf')
1255
 
 
1256
1350
    def set_parent(self, parent):
1257
1351
        self._parent = parent
1258
1352
 
1351
1445
 
1352
1446
    def setUp(self):
1353
1447
        super(TestGenerateBackupName, self).setUp()
1354
 
        self._transport = self.get_transport()
 
1448
        self._transport = get_transport(self.get_url())
1355
1449
        bzrdir.BzrDir.create(self.get_url(),
1356
1450
            possible_transports=[self._transport])
1357
1451
        self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
1367
1461
    def test_exiting(self):
1368
1462
        self._transport.put_bytes("a.~1~", "some content")
1369
1463
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1370