~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Vincent Ladeuil
  • Date: 2010-04-28 10:33:44 UTC
  • mfrom: (5171.2.3 401599-strict-warnings)
  • mto: This revision was merged to the branch mainline in revision 5191.
  • Revision ID: v.ladeuil+lp@free.fr-20100428103344-e32qf3cn1avdd2cb
Don't mention --no-strict when we just issue the warning about unclean trees

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
 
    config,
30
 
    controldir,
31
28
    errors,
32
29
    help_topics,
33
 
    lock,
34
30
    repository,
35
 
    revision as _mod_revision,
36
31
    osutils,
37
32
    remote,
38
 
    symbol_versioning,
39
 
    transport as _mod_transport,
40
33
    urlutils,
41
34
    win32utils,
42
 
    workingtree_3,
43
 
    workingtree_4,
 
35
    workingtree,
44
36
    )
45
37
import bzrlib.branch
46
 
from bzrlib.errors import (
47
 
    NotBranchError,
48
 
    NoColocatedBranchSupport,
49
 
    UnknownFormatError,
50
 
    UnsupportedFormatError,
51
 
    )
 
38
from bzrlib.errors import (NotBranchError,
 
39
                           NoColocatedBranchSupport,
 
40
                           UnknownFormatError,
 
41
                           UnsupportedFormatError,
 
42
                           )
52
43
from bzrlib.tests import (
53
44
    TestCase,
54
45
    TestCaseWithMemoryTransport,
61
52
    )
62
53
from bzrlib.tests.test_http import TestWithTransport_pycurl
63
54
from bzrlib.transport import (
 
55
    get_transport,
64
56
    memory,
65
 
    pathfilter,
66
57
    )
67
58
from bzrlib.transport.http._urllib import HttpTransport_urllib
68
59
from bzrlib.transport.nosmart import NoSmartTransportDecorator
69
60
from bzrlib.transport.readonly import ReadonlyTransportDecorator
70
 
from bzrlib.repofmt import knitrepo, knitpack_repo
 
61
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
71
62
 
72
63
 
73
64
class TestDefaultFormat(TestCase):
74
65
 
75
66
    def test_get_set_default_format(self):
76
67
        old_format = bzrdir.BzrDirFormat.get_default_format()
77
 
        # default is BzrDirMetaFormat1
78
 
        self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
79
 
        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())
80
71
        # creating a bzr dir should now create an instrumented dir.
81
72
        try:
82
73
            result = bzrdir.BzrDir.create('memory:///')
83
 
            self.assertIsInstance(result, SampleBzrDir)
 
74
            self.failUnless(isinstance(result, SampleBzrDir))
84
75
        finally:
85
 
            controldir.ControlDirFormat._set_default_format(old_format)
 
76
            bzrdir.BzrDirFormat._set_default_format(old_format)
86
77
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
87
78
 
88
79
 
89
 
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
90
 
    """A deprecated bzr dir format."""
91
 
 
92
 
 
93
80
class TestFormatRegistry(TestCase):
94
81
 
95
82
    def make_format_registry(self):
96
 
        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)
103
 
        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',
104
90
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
105
91
            'Format using knits',
106
92
            )
107
93
        my_format_registry.set_default('knit')
108
 
        bzrdir.register_metadir(my_format_registry,
 
94
        my_format_registry.register_metadir(
109
95
            'branch6',
110
96
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
111
97
            'Experimental successor to knit.  Use at your own risk.',
112
98
            branch_format='bzrlib.branch.BzrBranchFormat6',
113
99
            experimental=True)
114
 
        bzrdir.register_metadir(my_format_registry,
 
100
        my_format_registry.register_metadir(
115
101
            'hidden format',
116
102
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
117
103
            'Experimental successor to knit.  Use at your own risk.',
118
104
            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)
 
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)
124
111
        return my_format_registry
125
112
 
126
113
    def test_format_registry(self):
127
114
        my_format_registry = self.make_format_registry()
128
115
        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)
 
116
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
117
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
118
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
132
119
        my_bzrdir = my_format_registry.make_bzrdir('default')
133
120
        self.assertIsInstance(my_bzrdir.repository_format,
134
121
            knitrepo.RepositoryFormatKnit1)
147
134
                         my_format_registry.get_help('knit'))
148
135
        self.assertEqual('Format using knits',
149
136
                         my_format_registry.get_help('default'))
150
 
        self.assertEqual('Some format.  Slower and unawesome and deprecated.',
151
 
                         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'))
152
140
 
153
141
    def test_help_topic(self):
154
142
        topics = help_topics.HelpTopicRegistry()
178
166
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
179
167
                          bzrdir.format_registry.get('default'))
180
168
            self.assertIs(
181
 
                repository.format_registry.get_default().__class__,
 
169
                repository.RepositoryFormat.get_default_format().__class__,
182
170
                knitrepo.RepositoryFormatKnit3)
183
171
        finally:
184
172
            bzrdir.format_registry.set_default_repository(old_default)
185
173
 
186
174
    def test_aliases(self):
187
 
        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())
 
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())
195
183
 
196
184
 
197
185
class SampleBranch(bzrlib.branch.Branch):
254
242
        return "opened branch."
255
243
 
256
244
 
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
245
class TestBzrDirFormat(TestCaseWithTransport):
272
246
    """Tests for the BzrDirFormat facility."""
273
247
 
274
248
    def test_find_format(self):
275
249
        # is the right format object found for a branch?
276
250
        # 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()
 
251
        # this is not quite the same as
 
252
        t = get_transport(self.get_url())
286
253
        self.build_tree(["foo/", "bar/"], transport=t)
287
254
        def check_format(format, url):
288
255
            format.initialize(url)
289
 
            t = _mod_transport.get_transport_from_path(url)
 
256
            t = get_transport(url)
290
257
            found_format = bzrdir.BzrDirFormat.find_format(t)
291
 
            self.assertIsInstance(found_format, format.__class__)
292
 
        check_format(BzrDirFormatTest1(), "foo")
293
 
        check_format(BzrDirFormatTest2(), "bar")
 
258
            self.failUnless(isinstance(found_format, format.__class__))
 
259
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
260
        check_format(bzrdir.BzrDirFormat6(), "bar")
294
261
 
295
262
    def test_find_format_nothing_there(self):
296
263
        self.assertRaises(NotBranchError,
297
264
                          bzrdir.BzrDirFormat.find_format,
298
 
                          _mod_transport.get_transport_from_path('.'))
 
265
                          get_transport('.'))
299
266
 
300
267
    def test_find_format_unknown_format(self):
301
 
        t = self.get_transport()
 
268
        t = get_transport(self.get_url())
302
269
        t.mkdir('.bzr')
303
270
        t.put_bytes('.bzr/branch-format', '')
304
271
        self.assertRaises(UnknownFormatError,
305
272
                          bzrdir.BzrDirFormat.find_format,
306
 
                          _mod_transport.get_transport_from_path('.'))
 
273
                          get_transport('.'))
307
274
 
308
275
    def test_register_unregister_format(self):
309
276
        format = SampleBzrDirFormat()
311
278
        # make a bzrdir
312
279
        format.initialize(url)
313
280
        # register a format for it.
314
 
        bzrdir.BzrProber.formats.register(format.get_format_string(), format)
 
281
        bzrdir.BzrDirFormat.register_format(format)
315
282
        # which bzrdir.Open will refuse (not supported)
316
283
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
317
284
        # which bzrdir.open_containing will refuse (not supported)
318
285
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
319
286
        # but open_downlevel will work
320
 
        t = _mod_transport.get_transport_from_url(url)
 
287
        t = get_transport(url)
321
288
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
322
289
        # unregister the format
323
 
        bzrdir.BzrProber.formats.remove(format.get_format_string())
 
290
        bzrdir.BzrDirFormat.unregister_format(format)
324
291
        # now open_downlevel should fail too.
325
292
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
326
293
 
503
470
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
504
471
        # Make stackable source branch with an unstackable repo format.
505
472
        source_bzrdir = self.make_bzrdir('source')
506
 
        knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
473
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
507
474
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
508
475
            source_bzrdir)
509
476
        # Make a directory with a default stacking policy
709
676
        self.assertEqual(relpath, 'baz')
710
677
 
711
678
    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')))
 
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')))
719
683
        control = bzrdir.BzrDir.create(self.get_url())
720
684
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
721
 
            _mod_transport.get_transport_from_url(
722
 
                self.get_readonly_url('')))
 
685
            get_transport(self.get_readonly_url('')))
723
686
        self.assertEqual('', relpath)
724
687
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
725
 
            _mod_transport.get_transport_from_url(
726
 
                self.get_readonly_url('g/p/q')))
 
688
            get_transport(self.get_readonly_url('g/p/q')))
727
689
        self.assertEqual('g/p/q', relpath)
728
690
 
729
691
    def test_open_containing_tree_or_branch(self):
773
735
        # transport pointing at bzrdir should give a bzrdir with root transport
774
736
        # set to the given transport
775
737
        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)
 
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)
779
741
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
780
742
 
781
743
    def test_open_from_transport_no_bzrdir(self):
782
 
        t = self.get_transport()
783
 
        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)
784
747
 
785
748
    def test_open_from_transport_bzrdir_in_parent(self):
786
749
        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)
 
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)
791
755
 
792
756
    def test_sprout_recursive(self):
793
757
        tree = self.make_branch_and_tree('tree1',
802
766
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
803
767
        tree2.lock_read()
804
768
        self.addCleanup(tree2.unlock)
805
 
        self.assertPathExists('tree2/subtree/file')
 
769
        self.failUnlessExists('tree2/subtree/file')
806
770
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
807
771
 
808
772
    def test_cloning_metadir(self):
812
776
        branch = self.make_branch('branch', format='knit')
813
777
        format = branch.bzrdir.cloning_metadir()
814
778
        self.assertIsInstance(format.workingtree_format,
815
 
            workingtree_4.WorkingTreeFormat6)
 
779
            workingtree.WorkingTreeFormat3)
816
780
 
817
781
    def test_sprout_recursive_treeless(self):
818
782
        tree = self.make_branch_and_tree('tree1',
823
787
        self.build_tree(['tree1/subtree/file'])
824
788
        sub_tree.add('file')
825
789
        tree.commit('Initial commit')
826
 
        # The following line force the orhaning to reveal bug #634470
827
 
        tree.branch.get_config().set_user_option(
828
 
            'bzr.transform.orphan_policy', 'move')
829
790
        tree.bzrdir.destroy_workingtree()
830
 
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
831
 
        # fail :-( ) -- vila 20100909
832
791
        repo = self.make_repository('repo', shared=True,
833
792
            format='dirstate-with-subtree')
834
793
        repo.set_make_working_trees(False)
835
 
        # FIXME: we just deleted the workingtree and now we want to use it ????
836
 
        # At a minimum, we should use tree.branch below (but this fails too
837
 
        # currently) or stop calling this test 'treeless'. Specifically, I've
838
 
        # turn the line below into an assertRaises when 'subtree/.bzr' is
839
 
        # orphaned and sprout tries to access the branch there (which is left
840
 
        # by bzrdir.BzrDirMeta1.destroy_workingtree when it ignores the
841
 
        # [DeletingParent('Not deleting', u'subtree', None)] conflict). See bug
842
 
        # #634470.  -- vila 20100909
843
 
        self.assertRaises(errors.NotBranchError,
844
 
                          tree.bzrdir.sprout, 'repo/tree2')
845
 
#        self.assertPathExists('repo/tree2/subtree')
846
 
#        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')
847
797
 
848
798
    def make_foo_bar_baz(self):
849
799
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
853
803
 
854
804
    def test_find_bzrdirs(self):
855
805
        foo, bar, baz = self.make_foo_bar_baz()
856
 
        t = self.get_transport()
857
 
        self.assertEqualBzrdirs([baz, foo, bar], bzrdir.BzrDir.find_bzrdirs(t))
858
 
 
859
 
    def make_fake_permission_denied_transport(self, transport, paths):
860
 
        """Create a transport that raises PermissionDenied for some paths."""
861
 
        def filter(path):
862
 
            if path in paths:
863
 
                raise errors.PermissionDenied(path)
864
 
            return path
865
 
        path_filter_server = pathfilter.PathFilteringServer(transport, filter)
866
 
        path_filter_server.start_server()
867
 
        self.addCleanup(path_filter_server.stop_server)
868
 
        path_filter_transport = pathfilter.PathFilteringTransport(
869
 
            path_filter_server, '.')
870
 
        return (path_filter_server, path_filter_transport)
871
 
 
872
 
    def assertBranchUrlsEndWith(self, expect_url_suffix, actual_bzrdirs):
873
 
        """Check that each branch url ends with the given suffix."""
874
 
        for actual_bzrdir in actual_bzrdirs:
875
 
            self.assertEndsWith(actual_bzrdir.user_url, expect_url_suffix)
876
 
 
877
 
    def test_find_bzrdirs_permission_denied(self):
878
 
        foo, bar, baz = self.make_foo_bar_baz()
879
 
        t = self.get_transport()
880
 
        path_filter_server, path_filter_transport = \
881
 
            self.make_fake_permission_denied_transport(t, ['foo'])
882
 
        # local transport
883
 
        self.assertBranchUrlsEndWith('/baz/',
884
 
            bzrdir.BzrDir.find_bzrdirs(path_filter_transport))
885
 
        # smart server
886
 
        smart_transport = self.make_smart_server('.',
887
 
            backing_server=path_filter_server)
888
 
        self.assertBranchUrlsEndWith('/baz/',
889
 
            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))
890
809
 
891
810
    def test_find_bzrdirs_list_current(self):
892
811
        def list_current(transport):
893
812
            return [s for s in transport.list_dir('') if s != 'baz']
894
813
 
895
814
        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))
 
815
        transport = get_transport(self.get_url())
 
816
        self.assertEqualBzrdirs([foo, bar],
 
817
                                bzrdir.BzrDir.find_bzrdirs(transport,
 
818
                                    list_current=list_current))
 
819
 
900
820
 
901
821
    def test_find_bzrdirs_evaluate(self):
902
822
        def evaluate(bzrdir):
903
823
            try:
904
824
                repo = bzrdir.open_repository()
905
 
            except errors.NoRepositoryPresent:
 
825
            except NoRepositoryPresent:
906
826
                return True, bzrdir.root_transport.base
907
827
            else:
908
828
                return False, bzrdir.root_transport.base
909
829
 
910
830
        foo, bar, baz = self.make_foo_bar_baz()
911
 
        t = self.get_transport()
 
831
        transport = get_transport(self.get_url())
912
832
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
913
 
                         list(bzrdir.BzrDir.find_bzrdirs(t, evaluate=evaluate)))
 
833
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
 
834
                                                         evaluate=evaluate)))
914
835
 
915
836
    def assertEqualBzrdirs(self, first, second):
916
837
        first = list(first)
923
844
        root = self.make_repository('', shared=True)
924
845
        foo, bar, baz = self.make_foo_bar_baz()
925
846
        qux = self.make_bzrdir('foo/qux')
926
 
        t = self.get_transport()
927
 
        branches = bzrdir.BzrDir.find_branches(t)
 
847
        transport = get_transport(self.get_url())
 
848
        branches = bzrdir.BzrDir.find_branches(transport)
928
849
        self.assertEqual(baz.root_transport.base, branches[0].base)
929
850
        self.assertEqual(foo.root_transport.base, branches[1].base)
930
851
        self.assertEqual(bar.root_transport.base, branches[2].base)
931
852
 
932
853
        # ensure this works without a top-level repo
933
 
        branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
 
854
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
934
855
        self.assertEqual(foo.root_transport.base, branches[0].base)
935
856
        self.assertEqual(bar.root_transport.base, branches[1].base)
936
857
 
937
858
 
938
 
class TestMissingRepoBranchesSkipped(TestCaseWithMemoryTransport):
939
 
 
940
 
    def test_find_bzrdirs_missing_repo(self):
941
 
        t = self.get_transport()
942
 
        arepo = self.make_repository('arepo', shared=True)
943
 
        abranch_url = arepo.user_url + '/abranch'
944
 
        abranch = bzrdir.BzrDir.create(abranch_url).create_branch()
945
 
        t.delete_tree('arepo/.bzr')
946
 
        self.assertRaises(errors.NoRepositoryPresent,
947
 
            branch.Branch.open, abranch_url)
948
 
        self.make_branch('baz')
949
 
        for actual_bzrdir in bzrdir.BzrDir.find_branches(t):
950
 
            self.assertEndsWith(actual_bzrdir.user_url, '/baz/')
951
 
 
952
 
 
953
859
class TestMeta1DirFormat(TestCaseWithTransport):
954
860
    """Tests specific to the meta1 dir format."""
955
861
 
962
868
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
963
869
        repository_base = t.clone('repository').base
964
870
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
965
 
        repository_format = repository.format_registry.get_default()
966
871
        self.assertEqual(repository_base,
967
 
                         dir.get_repository_transport(repository_format).base)
 
872
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
968
873
        checkout_base = t.clone('checkout').base
969
874
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
970
875
        self.assertEqual(checkout_base,
971
 
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
 
876
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
972
877
 
973
878
    def test_meta1dir_uses_lockdir(self):
974
879
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
1016
921
        self.assertEqual(2, rpc_count)
1017
922
 
1018
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
 
1019
1053
class NonLocalTests(TestCaseWithTransport):
1020
1054
    """Tests for bzrdir static behaviour on non local paths."""
1021
1055
 
1040
1074
            self.get_url('foo'),
1041
1075
            force_new_tree=True,
1042
1076
            format=format)
1043
 
        t = self.get_transport()
 
1077
        t = get_transport(self.get_url('.'))
1044
1078
        self.assertFalse(t.has('foo'))
1045
1079
 
1046
1080
    def test_clone(self):
1062
1096
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1063
1097
        checkout_format = my_bzrdir.checkout_metadir()
1064
1098
        self.assertIsInstance(checkout_format.workingtree_format,
1065
 
                              workingtree_4.WorkingTreeFormat4)
 
1099
                              workingtree.WorkingTreeFormat3)
1066
1100
 
1067
1101
 
1068
1102
class TestHTTPRedirections(object):
1077
1111
    """
1078
1112
 
1079
1113
    def create_transport_readonly_server(self):
1080
 
        # We don't set the http protocol version, relying on the default
1081
1114
        return http_utils.HTTPServerRedirecting()
1082
1115
 
1083
1116
    def create_transport_secondary_server(self):
1084
 
        # We don't set the http protocol version, relying on the default
1085
1117
        return http_utils.HTTPServerRedirecting()
1086
1118
 
1087
1119
    def setUp(self):
1212
1244
 
1213
1245
    def __init__(self, *args, **kwargs):
1214
1246
        super(_TestBzrDir, self).__init__(*args, **kwargs)
1215
 
        self.test_branch = _TestBranch(self.transport)
 
1247
        self.test_branch = _TestBranch()
1216
1248
        self.test_branch.repository = self.create_repository()
1217
1249
 
1218
1250
    def open_branch(self, unsupported=False):
1229
1261
class _TestBranch(bzrlib.branch.Branch):
1230
1262
    """Test Branch implementation for TestBzrDirSprout."""
1231
1263
 
1232
 
    def __init__(self, transport, *args, **kwargs):
 
1264
    def __init__(self, *args, **kwargs):
1233
1265
        self._format = _TestBranchFormat()
1234
 
        self._transport = transport
1235
 
        self.base = transport.base
1236
1266
        super(_TestBranch, self).__init__(*args, **kwargs)
1237
1267
        self.calls = []
1238
1268
        self._parent = None
1239
1269
 
1240
1270
    def sprout(self, *args, **kwargs):
1241
1271
        self.calls.append('sprout')
1242
 
        return _TestBranch(self._transport)
 
1272
        return _TestBranch()
1243
1273
 
1244
1274
    def copy_content_into(self, destination, revision_id=None):
1245
1275
        self.calls.append('copy_content_into')
1246
1276
 
1247
 
    def last_revision(self):
1248
 
        return _mod_revision.NULL_REVISION
1249
 
 
1250
1277
    def get_parent(self):
1251
1278
        return self._parent
1252
1279
 
1253
 
    def _get_config(self):
1254
 
        return config.TransportConfig(self._transport, 'branch.conf')
1255
 
 
1256
1280
    def set_parent(self, parent):
1257
1281
        self._parent = parent
1258
1282
 
1259
 
    def lock_read(self):
1260
 
        return lock.LogicalLockResult(self.unlock)
1261
 
 
1262
 
    def unlock(self):
1263
 
        return
1264
 
 
1265
1283
 
1266
1284
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1267
1285
 
1333
1351
        self.assertIsInstance(params, RepoInitHookParams)
1334
1352
        self.assertTrue(hasattr(params, 'bzrdir'))
1335
1353
        self.assertTrue(hasattr(params, 'repository'))
1336
 
 
1337
 
    def test_post_repo_init_hook_repr(self):
1338
 
        param_reprs = []
1339
 
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1340
 
            lambda params: param_reprs.append(repr(params)), None)
1341
 
        self.make_repository('foo')
1342
 
        self.assertLength(1, param_reprs)
1343
 
        param_repr = param_reprs[0]
1344
 
        self.assertStartsWith(param_repr, '<RepoInitHookParams for ')
1345
 
 
1346
 
 
1347
 
class TestGenerateBackupName(TestCaseWithMemoryTransport):
1348
 
    # FIXME: This may need to be unified with test_osutils.TestBackupNames or
1349
 
    # moved to per_bzrdir or per_transport for better coverage ?
1350
 
    # -- vila 20100909
1351
 
 
1352
 
    def setUp(self):
1353
 
        super(TestGenerateBackupName, self).setUp()
1354
 
        self._transport = self.get_transport()
1355
 
        bzrdir.BzrDir.create(self.get_url(),
1356
 
            possible_transports=[self._transport])
1357
 
        self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
1358
 
 
1359
 
    def test_deprecated_generate_backup_name(self):
1360
 
        res = self.applyDeprecated(
1361
 
                symbol_versioning.deprecated_in((2, 3, 0)),
1362
 
                self._bzrdir.generate_backup_name, 'whatever')
1363
 
 
1364
 
    def test_new(self):
1365
 
        self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
1366
 
 
1367
 
    def test_exiting(self):
1368
 
        self._transport.put_bytes("a.~1~", "some content")
1369
 
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1370