~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-05-28 00:25:32 UTC
  • mfrom: (5264.1.2 command-help-bug-177500)
  • Revision ID: pqm@pqm.ubuntu.com-20100528002532-9bzj1fajyxckd1rg
(lifeless) Stop raising at runtime when a command has no help,
 instead have a test in the test suite that checks all known command objects.
 (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
24
24
import sys
25
25
 
26
26
from bzrlib import (
27
 
    branch,
28
27
    bzrdir,
29
 
    controldir,
30
28
    errors,
31
29
    help_topics,
32
 
    lock,
33
30
    repository,
34
 
    revision as _mod_revision,
35
31
    osutils,
36
32
    remote,
37
 
    symbol_versioning,
38
 
    transport as _mod_transport,
39
33
    urlutils,
40
34
    win32utils,
41
 
    workingtree_3,
42
 
    workingtree_4,
 
35
    workingtree,
43
36
    )
44
37
import bzrlib.branch
45
 
from bzrlib.errors import (
46
 
    NotBranchError,
47
 
    NoColocatedBranchSupport,
48
 
    UnknownFormatError,
49
 
    UnsupportedFormatError,
50
 
    )
 
38
from bzrlib.errors import (NotBranchError,
 
39
                           NoColocatedBranchSupport,
 
40
                           UnknownFormatError,
 
41
                           UnsupportedFormatError,
 
42
                           )
51
43
from bzrlib.tests import (
52
44
    TestCase,
53
45
    TestCaseWithMemoryTransport,
60
52
    )
61
53
from bzrlib.tests.test_http import TestWithTransport_pycurl
62
54
from bzrlib.transport import (
 
55
    get_transport,
63
56
    memory,
64
 
    pathfilter,
65
57
    )
66
58
from bzrlib.transport.http._urllib import HttpTransport_urllib
67
59
from bzrlib.transport.nosmart import NoSmartTransportDecorator
68
60
from bzrlib.transport.readonly import ReadonlyTransportDecorator
69
 
from bzrlib.repofmt import knitrepo, knitpack_repo
 
61
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
70
62
 
71
63
 
72
64
class TestDefaultFormat(TestCase):
73
65
 
74
66
    def test_get_set_default_format(self):
75
67
        old_format = bzrdir.BzrDirFormat.get_default_format()
76
 
        # default is BzrDirMetaFormat1
77
 
        self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
78
 
        controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
 
68
        # default is BzrDirFormat6
 
69
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
 
70
        bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
79
71
        # creating a bzr dir should now create an instrumented dir.
80
72
        try:
81
73
            result = bzrdir.BzrDir.create('memory:///')
82
 
            self.assertIsInstance(result, SampleBzrDir)
 
74
            self.failUnless(isinstance(result, SampleBzrDir))
83
75
        finally:
84
 
            controldir.ControlDirFormat._set_default_format(old_format)
 
76
            bzrdir.BzrDirFormat._set_default_format(old_format)
85
77
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
86
78
 
87
79
 
88
 
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
89
 
    """A deprecated bzr dir format."""
90
 
 
91
 
 
92
80
class TestFormatRegistry(TestCase):
93
81
 
94
82
    def make_format_registry(self):
95
 
        my_format_registry = controldir.ControlDirFormatRegistry()
96
 
        my_format_registry.register('deprecated', DeprecatedBzrDirFormat,
97
 
            'Some format.  Slower and unawesome and deprecated.',
98
 
            deprecated=True)
99
 
        my_format_registry.register_lazy('lazy', 'bzrlib.tests.test_bzrdir',
100
 
            'DeprecatedBzrDirFormat', 'Format registered lazily',
101
 
            deprecated=True)
102
 
        bzrdir.register_metadir(my_format_registry, 'knit',
 
83
        my_format_registry = bzrdir.BzrDirFormatRegistry()
 
84
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
 
85
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
86
            ' repositories', deprecated=True)
 
87
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
 
88
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
 
89
        my_format_registry.register_metadir('knit',
103
90
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
104
91
            'Format using knits',
105
92
            )
106
93
        my_format_registry.set_default('knit')
107
 
        bzrdir.register_metadir(my_format_registry,
 
94
        my_format_registry.register_metadir(
108
95
            'branch6',
109
96
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
110
97
            'Experimental successor to knit.  Use at your own risk.',
111
98
            branch_format='bzrlib.branch.BzrBranchFormat6',
112
99
            experimental=True)
113
 
        bzrdir.register_metadir(my_format_registry,
 
100
        my_format_registry.register_metadir(
114
101
            'hidden format',
115
102
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
116
103
            'Experimental successor to knit.  Use at your own risk.',
117
104
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
118
 
        my_format_registry.register('hiddendeprecated', DeprecatedBzrDirFormat,
119
 
            'Old format.  Slower and does not support things. ', hidden=True)
120
 
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.tests.test_bzrdir',
121
 
            'DeprecatedBzrDirFormat', 'Format registered lazily',
122
 
            deprecated=True, hidden=True)
 
105
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
 
106
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
107
            ' repositories', hidden=True)
 
108
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
 
109
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
 
110
            hidden=True)
123
111
        return my_format_registry
124
112
 
125
113
    def test_format_registry(self):
126
114
        my_format_registry = self.make_format_registry()
127
115
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
128
 
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
129
 
        my_bzrdir = my_format_registry.make_bzrdir('deprecated')
130
 
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
 
116
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
117
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
118
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
131
119
        my_bzrdir = my_format_registry.make_bzrdir('default')
132
120
        self.assertIsInstance(my_bzrdir.repository_format,
133
121
            knitrepo.RepositoryFormatKnit1)
146
134
                         my_format_registry.get_help('knit'))
147
135
        self.assertEqual('Format using knits',
148
136
                         my_format_registry.get_help('default'))
149
 
        self.assertEqual('Some format.  Slower and unawesome and deprecated.',
150
 
                         my_format_registry.get_help('deprecated'))
 
137
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
 
138
                         ' checkouts or shared repositories',
 
139
                         my_format_registry.get_help('weave'))
151
140
 
152
141
    def test_help_topic(self):
153
142
        topics = help_topics.HelpTopicRegistry()
177
166
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
178
167
                          bzrdir.format_registry.get('default'))
179
168
            self.assertIs(
180
 
                repository.format_registry.get_default().__class__,
 
169
                repository.RepositoryFormat.get_default_format().__class__,
181
170
                knitrepo.RepositoryFormatKnit3)
182
171
        finally:
183
172
            bzrdir.format_registry.set_default_repository(old_default)
184
173
 
185
174
    def test_aliases(self):
186
 
        a_registry = controldir.ControlDirFormatRegistry()
187
 
        a_registry.register('deprecated', DeprecatedBzrDirFormat,
188
 
            'Old format.  Slower and does not support stuff',
189
 
            deprecated=True)
190
 
        a_registry.register('deprecatedalias', DeprecatedBzrDirFormat,
191
 
            'Old format.  Slower and does not support stuff',
192
 
            deprecated=True, alias=True)
193
 
        self.assertEqual(frozenset(['deprecatedalias']), a_registry.aliases())
 
175
        a_registry = bzrdir.BzrDirFormatRegistry()
 
176
        a_registry.register('weave', bzrdir.BzrDirFormat6,
 
177
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
178
            ' repositories', deprecated=True)
 
179
        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
 
180
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
181
            ' repositories', deprecated=True, alias=True)
 
182
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
194
183
 
195
184
 
196
185
class SampleBranch(bzrlib.branch.Branch):
253
242
        return "opened branch."
254
243
 
255
244
 
256
 
class BzrDirFormatTest1(bzrdir.BzrDirMetaFormat1):
257
 
 
258
 
    @staticmethod
259
 
    def get_format_string():
260
 
        return "Test format 1"
261
 
 
262
 
 
263
 
class BzrDirFormatTest2(bzrdir.BzrDirMetaFormat1):
264
 
 
265
 
    @staticmethod
266
 
    def get_format_string():
267
 
        return "Test format 2"
268
 
 
269
 
 
270
245
class TestBzrDirFormat(TestCaseWithTransport):
271
246
    """Tests for the BzrDirFormat facility."""
272
247
 
273
248
    def test_find_format(self):
274
249
        # is the right format object found for a branch?
275
250
        # create a branch with a few known format objects.
276
 
        bzrdir.BzrProber.formats.register(BzrDirFormatTest1.get_format_string(),
277
 
            BzrDirFormatTest1())
278
 
        self.addCleanup(bzrdir.BzrProber.formats.remove,
279
 
            BzrDirFormatTest1.get_format_string())
280
 
        bzrdir.BzrProber.formats.register(BzrDirFormatTest2.get_format_string(),
281
 
            BzrDirFormatTest2())
282
 
        self.addCleanup(bzrdir.BzrProber.formats.remove,
283
 
            BzrDirFormatTest2.get_format_string())
284
 
        t = self.get_transport()
 
251
        # this is not quite the same as
 
252
        t = get_transport(self.get_url())
285
253
        self.build_tree(["foo/", "bar/"], transport=t)
286
254
        def check_format(format, url):
287
255
            format.initialize(url)
288
 
            t = _mod_transport.get_transport(url)
 
256
            t = get_transport(url)
289
257
            found_format = bzrdir.BzrDirFormat.find_format(t)
290
 
            self.assertIsInstance(found_format, format.__class__)
291
 
        check_format(BzrDirFormatTest1(), "foo")
292
 
        check_format(BzrDirFormatTest2(), "bar")
 
258
            self.failUnless(isinstance(found_format, format.__class__))
 
259
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
260
        check_format(bzrdir.BzrDirFormat6(), "bar")
293
261
 
294
262
    def test_find_format_nothing_there(self):
295
263
        self.assertRaises(NotBranchError,
296
264
                          bzrdir.BzrDirFormat.find_format,
297
 
                          _mod_transport.get_transport('.'))
 
265
                          get_transport('.'))
298
266
 
299
267
    def test_find_format_unknown_format(self):
300
 
        t = self.get_transport()
 
268
        t = get_transport(self.get_url())
301
269
        t.mkdir('.bzr')
302
270
        t.put_bytes('.bzr/branch-format', '')
303
271
        self.assertRaises(UnknownFormatError,
304
272
                          bzrdir.BzrDirFormat.find_format,
305
 
                          _mod_transport.get_transport('.'))
 
273
                          get_transport('.'))
306
274
 
307
275
    def test_register_unregister_format(self):
308
276
        format = SampleBzrDirFormat()
310
278
        # make a bzrdir
311
279
        format.initialize(url)
312
280
        # register a format for it.
313
 
        bzrdir.BzrProber.formats.register(format.get_format_string(), format)
 
281
        bzrdir.BzrDirFormat.register_format(format)
314
282
        # which bzrdir.Open will refuse (not supported)
315
283
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
316
284
        # which bzrdir.open_containing will refuse (not supported)
317
285
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
318
286
        # but open_downlevel will work
319
 
        t = _mod_transport.get_transport(url)
 
287
        t = get_transport(url)
320
288
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
321
289
        # unregister the format
322
 
        bzrdir.BzrProber.formats.remove(format.get_format_string())
 
290
        bzrdir.BzrDirFormat.unregister_format(format)
323
291
        # now open_downlevel should fail too.
324
292
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
325
293
 
502
470
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
503
471
        # Make stackable source branch with an unstackable repo format.
504
472
        source_bzrdir = self.make_bzrdir('source')
505
 
        knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
473
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
506
474
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
507
475
            source_bzrdir)
508
476
        # Make a directory with a default stacking policy
708
676
        self.assertEqual(relpath, 'baz')
709
677
 
710
678
    def test_open_containing_from_transport(self):
711
 
        self.assertRaises(NotBranchError,
712
 
            bzrdir.BzrDir.open_containing_from_transport,
713
 
            _mod_transport.get_transport(self.get_readonly_url('')))
714
 
        self.assertRaises(NotBranchError,
715
 
            bzrdir.BzrDir.open_containing_from_transport,
716
 
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
 
679
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
680
                          get_transport(self.get_readonly_url('')))
 
681
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
682
                          get_transport(self.get_readonly_url('g/p/q')))
717
683
        control = bzrdir.BzrDir.create(self.get_url())
718
684
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
719
 
            _mod_transport.get_transport(self.get_readonly_url('')))
 
685
            get_transport(self.get_readonly_url('')))
720
686
        self.assertEqual('', relpath)
721
687
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
722
 
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
 
688
            get_transport(self.get_readonly_url('g/p/q')))
723
689
        self.assertEqual('g/p/q', relpath)
724
690
 
725
691
    def test_open_containing_tree_or_branch(self):
769
735
        # transport pointing at bzrdir should give a bzrdir with root transport
770
736
        # set to the given transport
771
737
        control = bzrdir.BzrDir.create(self.get_url())
772
 
        t = self.get_transport()
773
 
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(t)
774
 
        self.assertEqual(t.base, opened_bzrdir.root_transport.base)
 
738
        transport = get_transport(self.get_url())
 
739
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
 
740
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
775
741
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
776
742
 
777
743
    def test_open_from_transport_no_bzrdir(self):
778
 
        t = self.get_transport()
779
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
744
        transport = get_transport(self.get_url())
 
745
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
746
                          transport)
780
747
 
781
748
    def test_open_from_transport_bzrdir_in_parent(self):
782
749
        control = bzrdir.BzrDir.create(self.get_url())
783
 
        t = self.get_transport()
784
 
        t.mkdir('subdir')
785
 
        t = t.clone('subdir')
786
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
750
        transport = get_transport(self.get_url())
 
751
        transport.mkdir('subdir')
 
752
        transport = transport.clone('subdir')
 
753
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
754
                          transport)
787
755
 
788
756
    def test_sprout_recursive(self):
789
757
        tree = self.make_branch_and_tree('tree1',
798
766
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
799
767
        tree2.lock_read()
800
768
        self.addCleanup(tree2.unlock)
801
 
        self.assertPathExists('tree2/subtree/file')
 
769
        self.failUnlessExists('tree2/subtree/file')
802
770
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
803
771
 
804
772
    def test_cloning_metadir(self):
808
776
        branch = self.make_branch('branch', format='knit')
809
777
        format = branch.bzrdir.cloning_metadir()
810
778
        self.assertIsInstance(format.workingtree_format,
811
 
            workingtree_4.WorkingTreeFormat6)
 
779
            workingtree.WorkingTreeFormat3)
812
780
 
813
781
    def test_sprout_recursive_treeless(self):
814
782
        tree = self.make_branch_and_tree('tree1',
819
787
        self.build_tree(['tree1/subtree/file'])
820
788
        sub_tree.add('file')
821
789
        tree.commit('Initial commit')
822
 
        # The following line force the orhaning to reveal bug #634470
823
 
        tree.branch.get_config().set_user_option(
824
 
            'bzr.transform.orphan_policy', 'move')
825
790
        tree.bzrdir.destroy_workingtree()
826
 
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
827
 
        # fail :-( ) -- vila 20100909
828
791
        repo = self.make_repository('repo', shared=True,
829
792
            format='dirstate-with-subtree')
830
793
        repo.set_make_working_trees(False)
831
 
        # FIXME: we just deleted the workingtree and now we want to use it ????
832
 
        # At a minimum, we should use tree.branch below (but this fails too
833
 
        # currently) or stop calling this test 'treeless'. Specifically, I've
834
 
        # turn the line below into an assertRaises when 'subtree/.bzr' is
835
 
        # orphaned and sprout tries to access the branch there (which is left
836
 
        # by bzrdir.BzrDirMeta1.destroy_workingtree when it ignores the
837
 
        # [DeletingParent('Not deleting', u'subtree', None)] conflict). See bug
838
 
        # #634470.  -- vila 20100909
839
 
        self.assertRaises(errors.NotBranchError,
840
 
                          tree.bzrdir.sprout, 'repo/tree2')
841
 
#        self.assertPathExists('repo/tree2/subtree')
842
 
#        self.assertPathDoesNotExist('repo/tree2/subtree/file')
 
794
        tree.bzrdir.sprout('repo/tree2')
 
795
        self.failUnlessExists('repo/tree2/subtree')
 
796
        self.failIfExists('repo/tree2/subtree/file')
843
797
 
844
798
    def make_foo_bar_baz(self):
845
799
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
849
803
 
850
804
    def test_find_bzrdirs(self):
851
805
        foo, bar, baz = self.make_foo_bar_baz()
852
 
        t = self.get_transport()
853
 
        self.assertEqualBzrdirs([baz, foo, bar], bzrdir.BzrDir.find_bzrdirs(t))
854
 
 
855
 
    def make_fake_permission_denied_transport(self, transport, paths):
856
 
        """Create a transport that raises PermissionDenied for some paths."""
857
 
        def filter(path):
858
 
            if path in paths:
859
 
                raise errors.PermissionDenied(path)
860
 
            return path
861
 
        path_filter_server = pathfilter.PathFilteringServer(transport, filter)
862
 
        path_filter_server.start_server()
863
 
        self.addCleanup(path_filter_server.stop_server)
864
 
        path_filter_transport = pathfilter.PathFilteringTransport(
865
 
            path_filter_server, '.')
866
 
        return (path_filter_server, path_filter_transport)
867
 
 
868
 
    def assertBranchUrlsEndWith(self, expect_url_suffix, actual_bzrdirs):
869
 
        """Check that each branch url ends with the given suffix."""
870
 
        for actual_bzrdir in actual_bzrdirs:
871
 
            self.assertEndsWith(actual_bzrdir.user_url, expect_url_suffix)
872
 
 
873
 
    def test_find_bzrdirs_permission_denied(self):
874
 
        foo, bar, baz = self.make_foo_bar_baz()
875
 
        t = self.get_transport()
876
 
        path_filter_server, path_filter_transport = \
877
 
            self.make_fake_permission_denied_transport(t, ['foo'])
878
 
        # local transport
879
 
        self.assertBranchUrlsEndWith('/baz/',
880
 
            bzrdir.BzrDir.find_bzrdirs(path_filter_transport))
881
 
        # smart server
882
 
        smart_transport = self.make_smart_server('.',
883
 
            backing_server=path_filter_server)
884
 
        self.assertBranchUrlsEndWith('/baz/',
885
 
            bzrdir.BzrDir.find_bzrdirs(smart_transport))
 
806
        transport = get_transport(self.get_url())
 
807
        self.assertEqualBzrdirs([baz, foo, bar],
 
808
                                bzrdir.BzrDir.find_bzrdirs(transport))
886
809
 
887
810
    def test_find_bzrdirs_list_current(self):
888
811
        def list_current(transport):
889
812
            return [s for s in transport.list_dir('') if s != 'baz']
890
813
 
891
814
        foo, bar, baz = self.make_foo_bar_baz()
892
 
        t = self.get_transport()
893
 
        self.assertEqualBzrdirs(
894
 
            [foo, bar],
895
 
            bzrdir.BzrDir.find_bzrdirs(t, list_current=list_current))
 
815
        transport = get_transport(self.get_url())
 
816
        self.assertEqualBzrdirs([foo, bar],
 
817
                                bzrdir.BzrDir.find_bzrdirs(transport,
 
818
                                    list_current=list_current))
 
819
 
896
820
 
897
821
    def test_find_bzrdirs_evaluate(self):
898
822
        def evaluate(bzrdir):
904
828
                return False, bzrdir.root_transport.base
905
829
 
906
830
        foo, bar, baz = self.make_foo_bar_baz()
907
 
        t = self.get_transport()
 
831
        transport = get_transport(self.get_url())
908
832
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
909
 
                         list(bzrdir.BzrDir.find_bzrdirs(t, evaluate=evaluate)))
 
833
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
 
834
                                                         evaluate=evaluate)))
910
835
 
911
836
    def assertEqualBzrdirs(self, first, second):
912
837
        first = list(first)
919
844
        root = self.make_repository('', shared=True)
920
845
        foo, bar, baz = self.make_foo_bar_baz()
921
846
        qux = self.make_bzrdir('foo/qux')
922
 
        t = self.get_transport()
923
 
        branches = bzrdir.BzrDir.find_branches(t)
 
847
        transport = get_transport(self.get_url())
 
848
        branches = bzrdir.BzrDir.find_branches(transport)
924
849
        self.assertEqual(baz.root_transport.base, branches[0].base)
925
850
        self.assertEqual(foo.root_transport.base, branches[1].base)
926
851
        self.assertEqual(bar.root_transport.base, branches[2].base)
927
852
 
928
853
        # ensure this works without a top-level repo
929
 
        branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
 
854
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
930
855
        self.assertEqual(foo.root_transport.base, branches[0].base)
931
856
        self.assertEqual(bar.root_transport.base, branches[1].base)
932
857
 
933
858
 
934
 
class TestMissingRepoBranchesSkipped(TestCaseWithMemoryTransport):
935
 
 
936
 
    def test_find_bzrdirs_missing_repo(self):
937
 
        t = self.get_transport()
938
 
        arepo = self.make_repository('arepo', shared=True)
939
 
        abranch_url = arepo.user_url + '/abranch'
940
 
        abranch = bzrdir.BzrDir.create(abranch_url).create_branch()
941
 
        t.delete_tree('arepo/.bzr')
942
 
        self.assertRaises(errors.NoRepositoryPresent,
943
 
            branch.Branch.open, abranch_url)
944
 
        self.make_branch('baz')
945
 
        for actual_bzrdir in bzrdir.BzrDir.find_branches(t):
946
 
            self.assertEndsWith(actual_bzrdir.user_url, '/baz/')
947
 
 
948
 
 
949
859
class TestMeta1DirFormat(TestCaseWithTransport):
950
860
    """Tests specific to the meta1 dir format."""
951
861
 
958
868
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
959
869
        repository_base = t.clone('repository').base
960
870
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
961
 
        repository_format = repository.format_registry.get_default()
962
871
        self.assertEqual(repository_base,
963
 
                         dir.get_repository_transport(repository_format).base)
 
872
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
964
873
        checkout_base = t.clone('checkout').base
965
874
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
966
875
        self.assertEqual(checkout_base,
967
 
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
 
876
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
968
877
 
969
878
    def test_meta1dir_uses_lockdir(self):
970
879
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
1012
921
        self.assertEqual(2, rpc_count)
1013
922
 
1014
923
 
 
924
class TestFormat5(TestCaseWithTransport):
 
925
    """Tests specific to the version 5 bzrdir format."""
 
926
 
 
927
    def test_same_lockfiles_between_tree_repo_branch(self):
 
928
        # this checks that only a single lockfiles instance is created
 
929
        # for format 5 objects
 
930
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
931
        def check_dir_components_use_same_lock(dir):
 
932
            ctrl_1 = dir.open_repository().control_files
 
933
            ctrl_2 = dir.open_branch().control_files
 
934
            ctrl_3 = dir.open_workingtree()._control_files
 
935
            self.assertTrue(ctrl_1 is ctrl_2)
 
936
            self.assertTrue(ctrl_2 is ctrl_3)
 
937
        check_dir_components_use_same_lock(dir)
 
938
        # and if we open it normally.
 
939
        dir = bzrdir.BzrDir.open(self.get_url())
 
940
        check_dir_components_use_same_lock(dir)
 
941
 
 
942
    def test_can_convert(self):
 
943
        # format 5 dirs are convertable
 
944
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
945
        self.assertTrue(dir.can_convert_format())
 
946
 
 
947
    def test_needs_conversion(self):
 
948
        # format 5 dirs need a conversion if they are not the default,
 
949
        # and they aren't
 
950
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
951
        # don't need to convert it to itself
 
952
        self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
 
953
        # do need to convert it to the current default
 
954
        self.assertTrue(dir.needs_format_conversion(
 
955
            bzrdir.BzrDirFormat.get_default_format()))
 
956
 
 
957
 
 
958
class TestFormat6(TestCaseWithTransport):
 
959
    """Tests specific to the version 6 bzrdir format."""
 
960
 
 
961
    def test_same_lockfiles_between_tree_repo_branch(self):
 
962
        # this checks that only a single lockfiles instance is created
 
963
        # for format 6 objects
 
964
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
965
        def check_dir_components_use_same_lock(dir):
 
966
            ctrl_1 = dir.open_repository().control_files
 
967
            ctrl_2 = dir.open_branch().control_files
 
968
            ctrl_3 = dir.open_workingtree()._control_files
 
969
            self.assertTrue(ctrl_1 is ctrl_2)
 
970
            self.assertTrue(ctrl_2 is ctrl_3)
 
971
        check_dir_components_use_same_lock(dir)
 
972
        # and if we open it normally.
 
973
        dir = bzrdir.BzrDir.open(self.get_url())
 
974
        check_dir_components_use_same_lock(dir)
 
975
 
 
976
    def test_can_convert(self):
 
977
        # format 6 dirs are convertable
 
978
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
979
        self.assertTrue(dir.can_convert_format())
 
980
 
 
981
    def test_needs_conversion(self):
 
982
        # format 6 dirs need an conversion if they are not the default.
 
983
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
984
        self.assertTrue(dir.needs_format_conversion(
 
985
            bzrdir.BzrDirFormat.get_default_format()))
 
986
 
 
987
 
 
988
class NotBzrDir(bzrlib.bzrdir.BzrDir):
 
989
    """A non .bzr based control directory."""
 
990
 
 
991
    def __init__(self, transport, format):
 
992
        self._format = format
 
993
        self.root_transport = transport
 
994
        self.transport = transport.clone('.not')
 
995
 
 
996
 
 
997
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
 
998
    """A test class representing any non-.bzr based disk format."""
 
999
 
 
1000
    def initialize_on_transport(self, transport):
 
1001
        """Initialize a new .not dir in the base directory of a Transport."""
 
1002
        transport.mkdir('.not')
 
1003
        return self.open(transport)
 
1004
 
 
1005
    def open(self, transport):
 
1006
        """Open this directory."""
 
1007
        return NotBzrDir(transport, self)
 
1008
 
 
1009
    @classmethod
 
1010
    def _known_formats(self):
 
1011
        return set([NotBzrDirFormat()])
 
1012
 
 
1013
    @classmethod
 
1014
    def probe_transport(self, transport):
 
1015
        """Our format is present if the transport ends in '.not/'."""
 
1016
        if transport.has('.not'):
 
1017
            return NotBzrDirFormat()
 
1018
 
 
1019
 
 
1020
class TestNotBzrDir(TestCaseWithTransport):
 
1021
    """Tests for using the bzrdir api with a non .bzr based disk format.
 
1022
 
 
1023
    If/when one of these is in the core, we can let the implementation tests
 
1024
    verify this works.
 
1025
    """
 
1026
 
 
1027
    def test_create_and_find_format(self):
 
1028
        # create a .notbzr dir
 
1029
        format = NotBzrDirFormat()
 
1030
        dir = format.initialize(self.get_url())
 
1031
        self.assertIsInstance(dir, NotBzrDir)
 
1032
        # now probe for it.
 
1033
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
 
1034
        try:
 
1035
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
 
1036
                get_transport(self.get_url()))
 
1037
            self.assertIsInstance(found, NotBzrDirFormat)
 
1038
        finally:
 
1039
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
 
1040
 
 
1041
    def test_included_in_known_formats(self):
 
1042
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
 
1043
        try:
 
1044
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
 
1045
            for format in formats:
 
1046
                if isinstance(format, NotBzrDirFormat):
 
1047
                    return
 
1048
            self.fail("No NotBzrDirFormat in %s" % formats)
 
1049
        finally:
 
1050
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
 
1051
 
 
1052
 
1015
1053
class NonLocalTests(TestCaseWithTransport):
1016
1054
    """Tests for bzrdir static behaviour on non local paths."""
1017
1055
 
1036
1074
            self.get_url('foo'),
1037
1075
            force_new_tree=True,
1038
1076
            format=format)
1039
 
        t = self.get_transport()
 
1077
        t = get_transport(self.get_url('.'))
1040
1078
        self.assertFalse(t.has('foo'))
1041
1079
 
1042
1080
    def test_clone(self):
1058
1096
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1059
1097
        checkout_format = my_bzrdir.checkout_metadir()
1060
1098
        self.assertIsInstance(checkout_format.workingtree_format,
1061
 
                              workingtree_4.WorkingTreeFormat4)
 
1099
                              workingtree.WorkingTreeFormat3)
1062
1100
 
1063
1101
 
1064
1102
class TestHTTPRedirections(object):
1073
1111
    """
1074
1112
 
1075
1113
    def create_transport_readonly_server(self):
1076
 
        # We don't set the http protocol version, relying on the default
1077
1114
        return http_utils.HTTPServerRedirecting()
1078
1115
 
1079
1116
    def create_transport_secondary_server(self):
1080
 
        # We don't set the http protocol version, relying on the default
1081
1117
        return http_utils.HTTPServerRedirecting()
1082
1118
 
1083
1119
    def setUp(self):
1238
1274
    def copy_content_into(self, destination, revision_id=None):
1239
1275
        self.calls.append('copy_content_into')
1240
1276
 
1241
 
    def last_revision(self):
1242
 
        return _mod_revision.NULL_REVISION
1243
 
 
1244
1277
    def get_parent(self):
1245
1278
        return self._parent
1246
1279
 
1247
1280
    def set_parent(self, parent):
1248
1281
        self._parent = parent
1249
1282
 
1250
 
    def lock_read(self):
1251
 
        return lock.LogicalLockResult(self.unlock)
1252
 
 
1253
 
    def unlock(self):
1254
 
        return
1255
 
 
1256
1283
 
1257
1284
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1258
1285
 
1324
1351
        self.assertIsInstance(params, RepoInitHookParams)
1325
1352
        self.assertTrue(hasattr(params, 'bzrdir'))
1326
1353
        self.assertTrue(hasattr(params, 'repository'))
1327
 
 
1328
 
    def test_post_repo_init_hook_repr(self):
1329
 
        param_reprs = []
1330
 
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1331
 
            lambda params: param_reprs.append(repr(params)), None)
1332
 
        self.make_repository('foo')
1333
 
        self.assertLength(1, param_reprs)
1334
 
        param_repr = param_reprs[0]
1335
 
        self.assertStartsWith(param_repr, '<RepoInitHookParams for ')
1336
 
 
1337
 
 
1338
 
class TestGenerateBackupName(TestCaseWithMemoryTransport):
1339
 
    # FIXME: This may need to be unified with test_osutils.TestBackupNames or
1340
 
    # moved to per_bzrdir or per_transport for better coverage ?
1341
 
    # -- vila 20100909
1342
 
 
1343
 
    def setUp(self):
1344
 
        super(TestGenerateBackupName, self).setUp()
1345
 
        self._transport = self.get_transport()
1346
 
        bzrdir.BzrDir.create(self.get_url(),
1347
 
            possible_transports=[self._transport])
1348
 
        self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
1349
 
 
1350
 
    def test_deprecated_generate_backup_name(self):
1351
 
        res = self.applyDeprecated(
1352
 
                symbol_versioning.deprecated_in((2, 3, 0)),
1353
 
                self._bzrdir.generate_backup_name, 'whatever')
1354
 
 
1355
 
    def test_new(self):
1356
 
        self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
1357
 
 
1358
 
    def test_exiting(self):
1359
 
        self._transport.put_bytes("a.~1~", "some content")
1360
 
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1361