~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: 2011-05-24 14:40:29 UTC
  • mfrom: (5609.39.7 2.3)
  • Revision ID: pqm@pqm.ubuntu.com-20110524144029-owqlr788c9s99jv5
(jam) Merge bzr-2.3 into bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 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,
27
28
    bzrdir,
 
29
    controldir,
28
30
    errors,
29
31
    help_topics,
 
32
    lock,
30
33
    repository,
 
34
    revision as _mod_revision,
31
35
    osutils,
32
36
    remote,
 
37
    symbol_versioning,
 
38
    transport as _mod_transport,
33
39
    urlutils,
34
40
    win32utils,
35
 
    workingtree,
 
41
    workingtree_3,
 
42
    workingtree_4,
36
43
    )
37
44
import bzrlib.branch
38
 
from bzrlib.errors import (NotBranchError,
39
 
                           NoColocatedBranchSupport,
40
 
                           UnknownFormatError,
41
 
                           UnsupportedFormatError,
42
 
                           )
 
45
from bzrlib.errors import (
 
46
    NotBranchError,
 
47
    NoColocatedBranchSupport,
 
48
    UnknownFormatError,
 
49
    UnsupportedFormatError,
 
50
    )
43
51
from bzrlib.tests import (
44
52
    TestCase,
45
53
    TestCaseWithMemoryTransport,
52
60
    )
53
61
from bzrlib.tests.test_http import TestWithTransport_pycurl
54
62
from bzrlib.transport import (
55
 
    get_transport,
56
63
    memory,
 
64
    pathfilter,
57
65
    )
58
66
from bzrlib.transport.http._urllib import HttpTransport_urllib
59
67
from bzrlib.transport.nosmart import NoSmartTransportDecorator
60
68
from bzrlib.transport.readonly import ReadonlyTransportDecorator
61
 
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
 
69
from bzrlib.repofmt import knitrepo, knitpack_repo
62
70
 
63
71
 
64
72
class TestDefaultFormat(TestCase):
65
73
 
66
74
    def test_get_set_default_format(self):
67
75
        old_format = bzrdir.BzrDirFormat.get_default_format()
68
 
        # default is BzrDirFormat6
69
 
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
70
 
        bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
 
76
        # default is BzrDirMetaFormat1
 
77
        self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
 
78
        controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
71
79
        # creating a bzr dir should now create an instrumented dir.
72
80
        try:
73
81
            result = bzrdir.BzrDir.create('memory:///')
74
 
            self.failUnless(isinstance(result, SampleBzrDir))
 
82
            self.assertIsInstance(result, SampleBzrDir)
75
83
        finally:
76
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
84
            controldir.ControlDirFormat._set_default_format(old_format)
77
85
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
78
86
 
79
87
 
 
88
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
 
89
    """A deprecated bzr dir format."""
 
90
 
 
91
 
80
92
class TestFormatRegistry(TestCase):
81
93
 
82
94
    def make_format_registry(self):
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',
 
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',
90
103
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
91
104
            'Format using knits',
92
105
            )
93
106
        my_format_registry.set_default('knit')
94
 
        my_format_registry.register_metadir(
 
107
        bzrdir.register_metadir(my_format_registry,
95
108
            'branch6',
96
109
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
97
110
            'Experimental successor to knit.  Use at your own risk.',
98
111
            branch_format='bzrlib.branch.BzrBranchFormat6',
99
112
            experimental=True)
100
 
        my_format_registry.register_metadir(
 
113
        bzrdir.register_metadir(my_format_registry,
101
114
            'hidden format',
102
115
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
103
116
            'Experimental successor to knit.  Use at your own risk.',
104
117
            branch_format='bzrlib.branch.BzrBranchFormat6', 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)
 
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)
111
123
        return my_format_registry
112
124
 
113
125
    def test_format_registry(self):
114
126
        my_format_registry = self.make_format_registry()
115
127
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
116
 
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
117
 
        my_bzrdir = my_format_registry.make_bzrdir('weave')
118
 
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
128
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
 
129
        my_bzrdir = my_format_registry.make_bzrdir('deprecated')
 
130
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
119
131
        my_bzrdir = my_format_registry.make_bzrdir('default')
120
132
        self.assertIsInstance(my_bzrdir.repository_format,
121
133
            knitrepo.RepositoryFormatKnit1)
134
146
                         my_format_registry.get_help('knit'))
135
147
        self.assertEqual('Format using knits',
136
148
                         my_format_registry.get_help('default'))
137
 
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
138
 
                         ' checkouts or shared repositories',
139
 
                         my_format_registry.get_help('weave'))
 
149
        self.assertEqual('Some format.  Slower and unawesome and deprecated.',
 
150
                         my_format_registry.get_help('deprecated'))
140
151
 
141
152
    def test_help_topic(self):
142
153
        topics = help_topics.HelpTopicRegistry()
166
177
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
167
178
                          bzrdir.format_registry.get('default'))
168
179
            self.assertIs(
169
 
                repository.RepositoryFormat.get_default_format().__class__,
 
180
                repository.format_registry.get_default().__class__,
170
181
                knitrepo.RepositoryFormatKnit3)
171
182
        finally:
172
183
            bzrdir.format_registry.set_default_repository(old_default)
173
184
 
174
185
    def test_aliases(self):
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())
 
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())
183
194
 
184
195
 
185
196
class SampleBranch(bzrlib.branch.Branch):
242
253
        return "opened branch."
243
254
 
244
255
 
 
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
 
245
270
class TestBzrDirFormat(TestCaseWithTransport):
246
271
    """Tests for the BzrDirFormat facility."""
247
272
 
248
273
    def test_find_format(self):
249
274
        # is the right format object found for a branch?
250
275
        # create a branch with a few known format objects.
251
 
        # this is not quite the same as
252
 
        t = get_transport(self.get_url())
 
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()
253
285
        self.build_tree(["foo/", "bar/"], transport=t)
254
286
        def check_format(format, url):
255
287
            format.initialize(url)
256
 
            t = get_transport(url)
 
288
            t = _mod_transport.get_transport(url)
257
289
            found_format = bzrdir.BzrDirFormat.find_format(t)
258
 
            self.failUnless(isinstance(found_format, format.__class__))
259
 
        check_format(bzrdir.BzrDirFormat5(), "foo")
260
 
        check_format(bzrdir.BzrDirFormat6(), "bar")
 
290
            self.assertIsInstance(found_format, format.__class__)
 
291
        check_format(BzrDirFormatTest1(), "foo")
 
292
        check_format(BzrDirFormatTest2(), "bar")
261
293
 
262
294
    def test_find_format_nothing_there(self):
263
295
        self.assertRaises(NotBranchError,
264
296
                          bzrdir.BzrDirFormat.find_format,
265
 
                          get_transport('.'))
 
297
                          _mod_transport.get_transport('.'))
266
298
 
267
299
    def test_find_format_unknown_format(self):
268
 
        t = get_transport(self.get_url())
 
300
        t = self.get_transport()
269
301
        t.mkdir('.bzr')
270
302
        t.put_bytes('.bzr/branch-format', '')
271
303
        self.assertRaises(UnknownFormatError,
272
304
                          bzrdir.BzrDirFormat.find_format,
273
 
                          get_transport('.'))
 
305
                          _mod_transport.get_transport('.'))
274
306
 
275
307
    def test_register_unregister_format(self):
276
308
        format = SampleBzrDirFormat()
278
310
        # make a bzrdir
279
311
        format.initialize(url)
280
312
        # register a format for it.
281
 
        bzrdir.BzrDirFormat.register_format(format)
 
313
        bzrdir.BzrProber.formats.register(format.get_format_string(), format)
282
314
        # which bzrdir.Open will refuse (not supported)
283
315
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
284
316
        # which bzrdir.open_containing will refuse (not supported)
285
317
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
286
318
        # but open_downlevel will work
287
 
        t = get_transport(url)
 
319
        t = _mod_transport.get_transport(url)
288
320
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
289
321
        # unregister the format
290
 
        bzrdir.BzrDirFormat.unregister_format(format)
 
322
        bzrdir.BzrProber.formats.remove(format.get_format_string())
291
323
        # now open_downlevel should fail too.
292
324
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
293
325
 
470
502
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
471
503
        # Make stackable source branch with an unstackable repo format.
472
504
        source_bzrdir = self.make_bzrdir('source')
473
 
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
505
        knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
474
506
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
475
507
            source_bzrdir)
476
508
        # Make a directory with a default stacking policy
676
708
        self.assertEqual(relpath, 'baz')
677
709
 
678
710
    def test_open_containing_from_transport(self):
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')))
 
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')))
683
717
        control = bzrdir.BzrDir.create(self.get_url())
684
718
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
685
 
            get_transport(self.get_readonly_url('')))
 
719
            _mod_transport.get_transport(self.get_readonly_url('')))
686
720
        self.assertEqual('', relpath)
687
721
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
688
 
            get_transport(self.get_readonly_url('g/p/q')))
 
722
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
689
723
        self.assertEqual('g/p/q', relpath)
690
724
 
691
725
    def test_open_containing_tree_or_branch(self):
735
769
        # transport pointing at bzrdir should give a bzrdir with root transport
736
770
        # set to the given transport
737
771
        control = bzrdir.BzrDir.create(self.get_url())
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)
 
772
        t = self.get_transport()
 
773
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(t)
 
774
        self.assertEqual(t.base, opened_bzrdir.root_transport.base)
741
775
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
742
776
 
743
777
    def test_open_from_transport_no_bzrdir(self):
744
 
        transport = get_transport(self.get_url())
745
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
746
 
                          transport)
 
778
        t = self.get_transport()
 
779
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
747
780
 
748
781
    def test_open_from_transport_bzrdir_in_parent(self):
749
782
        control = bzrdir.BzrDir.create(self.get_url())
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)
 
783
        t = self.get_transport()
 
784
        t.mkdir('subdir')
 
785
        t = t.clone('subdir')
 
786
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
755
787
 
756
788
    def test_sprout_recursive(self):
757
789
        tree = self.make_branch_and_tree('tree1',
766
798
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
767
799
        tree2.lock_read()
768
800
        self.addCleanup(tree2.unlock)
769
 
        self.failUnlessExists('tree2/subtree/file')
 
801
        self.assertPathExists('tree2/subtree/file')
770
802
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
771
803
 
772
804
    def test_cloning_metadir(self):
776
808
        branch = self.make_branch('branch', format='knit')
777
809
        format = branch.bzrdir.cloning_metadir()
778
810
        self.assertIsInstance(format.workingtree_format,
779
 
            workingtree.WorkingTreeFormat3)
 
811
            workingtree_4.WorkingTreeFormat6)
780
812
 
781
813
    def test_sprout_recursive_treeless(self):
782
814
        tree = self.make_branch_and_tree('tree1',
787
819
        self.build_tree(['tree1/subtree/file'])
788
820
        sub_tree.add('file')
789
821
        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')
790
825
        tree.bzrdir.destroy_workingtree()
 
826
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
 
827
        # fail :-( ) -- vila 20100909
791
828
        repo = self.make_repository('repo', shared=True,
792
829
            format='dirstate-with-subtree')
793
830
        repo.set_make_working_trees(False)
794
 
        tree.bzrdir.sprout('repo/tree2')
795
 
        self.failUnlessExists('repo/tree2/subtree')
796
 
        self.failIfExists('repo/tree2/subtree/file')
 
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')
797
843
 
798
844
    def make_foo_bar_baz(self):
799
845
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
803
849
 
804
850
    def test_find_bzrdirs(self):
805
851
        foo, bar, baz = self.make_foo_bar_baz()
806
 
        transport = get_transport(self.get_url())
807
 
        self.assertEqualBzrdirs([baz, foo, bar],
808
 
                                bzrdir.BzrDir.find_bzrdirs(transport))
 
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))
809
886
 
810
887
    def test_find_bzrdirs_list_current(self):
811
888
        def list_current(transport):
812
889
            return [s for s in transport.list_dir('') if s != 'baz']
813
890
 
814
891
        foo, bar, baz = self.make_foo_bar_baz()
815
 
        transport = get_transport(self.get_url())
816
 
        self.assertEqualBzrdirs([foo, bar],
817
 
                                bzrdir.BzrDir.find_bzrdirs(transport,
818
 
                                    list_current=list_current))
819
 
 
 
892
        t = self.get_transport()
 
893
        self.assertEqualBzrdirs(
 
894
            [foo, bar],
 
895
            bzrdir.BzrDir.find_bzrdirs(t, list_current=list_current))
820
896
 
821
897
    def test_find_bzrdirs_evaluate(self):
822
898
        def evaluate(bzrdir):
828
904
                return False, bzrdir.root_transport.base
829
905
 
830
906
        foo, bar, baz = self.make_foo_bar_baz()
831
 
        transport = get_transport(self.get_url())
 
907
        t = self.get_transport()
832
908
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
833
 
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
834
 
                                                         evaluate=evaluate)))
 
909
                         list(bzrdir.BzrDir.find_bzrdirs(t, evaluate=evaluate)))
835
910
 
836
911
    def assertEqualBzrdirs(self, first, second):
837
912
        first = list(first)
844
919
        root = self.make_repository('', shared=True)
845
920
        foo, bar, baz = self.make_foo_bar_baz()
846
921
        qux = self.make_bzrdir('foo/qux')
847
 
        transport = get_transport(self.get_url())
848
 
        branches = bzrdir.BzrDir.find_branches(transport)
 
922
        t = self.get_transport()
 
923
        branches = bzrdir.BzrDir.find_branches(t)
849
924
        self.assertEqual(baz.root_transport.base, branches[0].base)
850
925
        self.assertEqual(foo.root_transport.base, branches[1].base)
851
926
        self.assertEqual(bar.root_transport.base, branches[2].base)
852
927
 
853
928
        # ensure this works without a top-level repo
854
 
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
 
929
        branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
855
930
        self.assertEqual(foo.root_transport.base, branches[0].base)
856
931
        self.assertEqual(bar.root_transport.base, branches[1].base)
857
932
 
858
933
 
 
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
 
859
949
class TestMeta1DirFormat(TestCaseWithTransport):
860
950
    """Tests specific to the meta1 dir format."""
861
951
 
868
958
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
869
959
        repository_base = t.clone('repository').base
870
960
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
 
961
        repository_format = repository.format_registry.get_default()
871
962
        self.assertEqual(repository_base,
872
 
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
 
963
                         dir.get_repository_transport(repository_format).base)
873
964
        checkout_base = t.clone('checkout').base
874
965
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
875
966
        self.assertEqual(checkout_base,
876
 
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
 
967
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
877
968
 
878
969
    def test_meta1dir_uses_lockdir(self):
879
970
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
921
1012
        self.assertEqual(2, rpc_count)
922
1013
 
923
1014
 
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
 
 
1053
1015
class NonLocalTests(TestCaseWithTransport):
1054
1016
    """Tests for bzrdir static behaviour on non local paths."""
1055
1017
 
1074
1036
            self.get_url('foo'),
1075
1037
            force_new_tree=True,
1076
1038
            format=format)
1077
 
        t = get_transport(self.get_url('.'))
 
1039
        t = self.get_transport()
1078
1040
        self.assertFalse(t.has('foo'))
1079
1041
 
1080
1042
    def test_clone(self):
1096
1058
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1097
1059
        checkout_format = my_bzrdir.checkout_metadir()
1098
1060
        self.assertIsInstance(checkout_format.workingtree_format,
1099
 
                              workingtree.WorkingTreeFormat3)
 
1061
                              workingtree_4.WorkingTreeFormat4)
1100
1062
 
1101
1063
 
1102
1064
class TestHTTPRedirections(object):
1111
1073
    """
1112
1074
 
1113
1075
    def create_transport_readonly_server(self):
 
1076
        # We don't set the http protocol version, relying on the default
1114
1077
        return http_utils.HTTPServerRedirecting()
1115
1078
 
1116
1079
    def create_transport_secondary_server(self):
 
1080
        # We don't set the http protocol version, relying on the default
1117
1081
        return http_utils.HTTPServerRedirecting()
1118
1082
 
1119
1083
    def setUp(self):
1274
1238
    def copy_content_into(self, destination, revision_id=None):
1275
1239
        self.calls.append('copy_content_into')
1276
1240
 
 
1241
    def last_revision(self):
 
1242
        return _mod_revision.NULL_REVISION
 
1243
 
1277
1244
    def get_parent(self):
1278
1245
        return self._parent
1279
1246
 
1280
1247
    def set_parent(self, parent):
1281
1248
        self._parent = parent
1282
1249
 
 
1250
    def lock_read(self):
 
1251
        return lock.LogicalLockResult(self.unlock)
 
1252
 
 
1253
    def unlock(self):
 
1254
        return
 
1255
 
1283
1256
 
1284
1257
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1285
1258
 
1339
1312
        url = transport.base
1340
1313
        err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1341
1314
        self.assertEqual('fail', err._preformatted_string)
 
1315
 
 
1316
    def test_post_repo_init(self):
 
1317
        from bzrlib.bzrdir import RepoInitHookParams
 
1318
        calls = []
 
1319
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
 
1320
            calls.append, None)
 
1321
        self.make_repository('foo')
 
1322
        self.assertLength(1, calls)
 
1323
        params = calls[0]
 
1324
        self.assertIsInstance(params, RepoInitHookParams)
 
1325
        self.assertTrue(hasattr(params, 'bzrdir'))
 
1326
        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