~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-02-17 13:49:11 UTC
  • mfrom: (4988.11.1 imports)
  • Revision ID: pqm@pqm.ubuntu.com-20100217134911-s77se00ni7xc1hz8
(Jelmer) Remove some unused imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 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
                           UnknownFormatError,
 
40
                           UnsupportedFormatError,
 
41
                           )
51
42
from bzrlib.tests import (
52
43
    TestCase,
53
44
    TestCaseWithMemoryTransport,
60
51
    )
61
52
from bzrlib.tests.test_http import TestWithTransport_pycurl
62
53
from bzrlib.transport import (
 
54
    get_transport,
63
55
    memory,
64
 
    pathfilter,
65
56
    )
66
57
from bzrlib.transport.http._urllib import HttpTransport_urllib
67
58
from bzrlib.transport.nosmart import NoSmartTransportDecorator
68
59
from bzrlib.transport.readonly import ReadonlyTransportDecorator
69
 
from bzrlib.repofmt import knitrepo, knitpack_repo
 
60
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
70
61
 
71
62
 
72
63
class TestDefaultFormat(TestCase):
73
64
 
74
65
    def test_get_set_default_format(self):
75
66
        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())
 
67
        # default is BzrDirFormat6
 
68
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
 
69
        bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
79
70
        # creating a bzr dir should now create an instrumented dir.
80
71
        try:
81
72
            result = bzrdir.BzrDir.create('memory:///')
82
 
            self.assertIsInstance(result, SampleBzrDir)
 
73
            self.failUnless(isinstance(result, SampleBzrDir))
83
74
        finally:
84
 
            controldir.ControlDirFormat._set_default_format(old_format)
 
75
            bzrdir.BzrDirFormat._set_default_format(old_format)
85
76
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
86
77
 
87
78
 
88
 
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
89
 
    """A deprecated bzr dir format."""
90
 
 
91
 
 
92
79
class TestFormatRegistry(TestCase):
93
80
 
94
81
    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',
 
82
        my_format_registry = bzrdir.BzrDirFormatRegistry()
 
83
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
 
84
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
85
            ' repositories', deprecated=True)
 
86
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
 
87
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
 
88
        my_format_registry.register_metadir('knit',
103
89
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
104
90
            'Format using knits',
105
91
            )
106
92
        my_format_registry.set_default('knit')
107
 
        bzrdir.register_metadir(my_format_registry,
 
93
        my_format_registry.register_metadir(
108
94
            'branch6',
109
95
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
110
96
            'Experimental successor to knit.  Use at your own risk.',
111
97
            branch_format='bzrlib.branch.BzrBranchFormat6',
112
98
            experimental=True)
113
 
        bzrdir.register_metadir(my_format_registry,
 
99
        my_format_registry.register_metadir(
114
100
            'hidden format',
115
101
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
116
102
            'Experimental successor to knit.  Use at your own risk.',
117
103
            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)
 
104
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
 
105
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
106
            ' repositories', hidden=True)
 
107
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
 
108
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
 
109
            hidden=True)
123
110
        return my_format_registry
124
111
 
125
112
    def test_format_registry(self):
126
113
        my_format_registry = self.make_format_registry()
127
114
        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)
 
115
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
116
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
117
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
131
118
        my_bzrdir = my_format_registry.make_bzrdir('default')
132
119
        self.assertIsInstance(my_bzrdir.repository_format,
133
120
            knitrepo.RepositoryFormatKnit1)
146
133
                         my_format_registry.get_help('knit'))
147
134
        self.assertEqual('Format using knits',
148
135
                         my_format_registry.get_help('default'))
149
 
        self.assertEqual('Some format.  Slower and unawesome and deprecated.',
150
 
                         my_format_registry.get_help('deprecated'))
 
136
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
 
137
                         ' checkouts or shared repositories',
 
138
                         my_format_registry.get_help('weave'))
151
139
 
152
140
    def test_help_topic(self):
153
141
        topics = help_topics.HelpTopicRegistry()
177
165
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
178
166
                          bzrdir.format_registry.get('default'))
179
167
            self.assertIs(
180
 
                repository.format_registry.get_default().__class__,
 
168
                repository.RepositoryFormat.get_default_format().__class__,
181
169
                knitrepo.RepositoryFormatKnit3)
182
170
        finally:
183
171
            bzrdir.format_registry.set_default_repository(old_default)
184
172
 
185
173
    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())
 
174
        a_registry = bzrdir.BzrDirFormatRegistry()
 
175
        a_registry.register('weave', bzrdir.BzrDirFormat6,
 
176
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
177
            ' repositories', deprecated=True)
 
178
        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
 
179
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
180
            ' repositories', deprecated=True, alias=True)
 
181
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
194
182
 
195
183
 
196
184
class SampleBranch(bzrlib.branch.Branch):
218
206
        """See BzrDir.open_repository."""
219
207
        return SampleRepository(self)
220
208
 
221
 
    def create_branch(self, name=None):
 
209
    def create_branch(self):
222
210
        """See BzrDir.create_branch."""
223
 
        if name is not None:
224
 
            raise NoColocatedBranchSupport(self)
225
211
        return SampleBranch(self)
226
212
 
227
213
    def create_workingtree(self):
253
239
        return "opened branch."
254
240
 
255
241
 
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
242
class TestBzrDirFormat(TestCaseWithTransport):
271
243
    """Tests for the BzrDirFormat facility."""
272
244
 
273
245
    def test_find_format(self):
274
246
        # is the right format object found for a branch?
275
247
        # 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()
 
248
        # this is not quite the same as
 
249
        t = get_transport(self.get_url())
285
250
        self.build_tree(["foo/", "bar/"], transport=t)
286
251
        def check_format(format, url):
287
252
            format.initialize(url)
288
 
            t = _mod_transport.get_transport(url)
 
253
            t = get_transport(url)
289
254
            found_format = bzrdir.BzrDirFormat.find_format(t)
290
 
            self.assertIsInstance(found_format, format.__class__)
291
 
        check_format(BzrDirFormatTest1(), "foo")
292
 
        check_format(BzrDirFormatTest2(), "bar")
 
255
            self.failUnless(isinstance(found_format, format.__class__))
 
256
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
257
        check_format(bzrdir.BzrDirFormat6(), "bar")
293
258
 
294
259
    def test_find_format_nothing_there(self):
295
260
        self.assertRaises(NotBranchError,
296
261
                          bzrdir.BzrDirFormat.find_format,
297
 
                          _mod_transport.get_transport('.'))
 
262
                          get_transport('.'))
298
263
 
299
264
    def test_find_format_unknown_format(self):
300
 
        t = self.get_transport()
 
265
        t = get_transport(self.get_url())
301
266
        t.mkdir('.bzr')
302
267
        t.put_bytes('.bzr/branch-format', '')
303
268
        self.assertRaises(UnknownFormatError,
304
269
                          bzrdir.BzrDirFormat.find_format,
305
 
                          _mod_transport.get_transport('.'))
 
270
                          get_transport('.'))
306
271
 
307
272
    def test_register_unregister_format(self):
308
273
        format = SampleBzrDirFormat()
310
275
        # make a bzrdir
311
276
        format.initialize(url)
312
277
        # register a format for it.
313
 
        bzrdir.BzrProber.formats.register(format.get_format_string(), format)
 
278
        bzrdir.BzrDirFormat.register_format(format)
314
279
        # which bzrdir.Open will refuse (not supported)
315
280
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
316
281
        # which bzrdir.open_containing will refuse (not supported)
317
282
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
318
283
        # but open_downlevel will work
319
 
        t = _mod_transport.get_transport(url)
 
284
        t = get_transport(url)
320
285
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
321
286
        # unregister the format
322
 
        bzrdir.BzrProber.formats.remove(format.get_format_string())
 
287
        bzrdir.BzrDirFormat.unregister_format(format)
323
288
        # now open_downlevel should fail too.
324
289
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
325
290
 
502
467
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
503
468
        # Make stackable source branch with an unstackable repo format.
504
469
        source_bzrdir = self.make_bzrdir('source')
505
 
        knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
506
 
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
507
 
            source_bzrdir)
 
470
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
471
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(source_bzrdir)
508
472
        # Make a directory with a default stacking policy
509
473
        parent_bzrdir = self.make_bzrdir('parent')
510
474
        stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
708
672
        self.assertEqual(relpath, 'baz')
709
673
 
710
674
    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')))
 
675
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
676
                          get_transport(self.get_readonly_url('')))
 
677
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
678
                          get_transport(self.get_readonly_url('g/p/q')))
717
679
        control = bzrdir.BzrDir.create(self.get_url())
718
680
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
719
 
            _mod_transport.get_transport(self.get_readonly_url('')))
 
681
            get_transport(self.get_readonly_url('')))
720
682
        self.assertEqual('', relpath)
721
683
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
722
 
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
 
684
            get_transport(self.get_readonly_url('g/p/q')))
723
685
        self.assertEqual('g/p/q', relpath)
724
686
 
725
687
    def test_open_containing_tree_or_branch(self):
769
731
        # transport pointing at bzrdir should give a bzrdir with root transport
770
732
        # set to the given transport
771
733
        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)
 
734
        transport = get_transport(self.get_url())
 
735
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
 
736
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
775
737
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
776
738
 
777
739
    def test_open_from_transport_no_bzrdir(self):
778
 
        t = self.get_transport()
779
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
740
        transport = get_transport(self.get_url())
 
741
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
742
                          transport)
780
743
 
781
744
    def test_open_from_transport_bzrdir_in_parent(self):
782
745
        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)
 
746
        transport = get_transport(self.get_url())
 
747
        transport.mkdir('subdir')
 
748
        transport = transport.clone('subdir')
 
749
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
750
                          transport)
787
751
 
788
752
    def test_sprout_recursive(self):
789
753
        tree = self.make_branch_and_tree('tree1',
798
762
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
799
763
        tree2.lock_read()
800
764
        self.addCleanup(tree2.unlock)
801
 
        self.assertPathExists('tree2/subtree/file')
 
765
        self.failUnlessExists('tree2/subtree/file')
802
766
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
803
767
 
804
768
    def test_cloning_metadir(self):
808
772
        branch = self.make_branch('branch', format='knit')
809
773
        format = branch.bzrdir.cloning_metadir()
810
774
        self.assertIsInstance(format.workingtree_format,
811
 
            workingtree_4.WorkingTreeFormat6)
 
775
            workingtree.WorkingTreeFormat3)
812
776
 
813
777
    def test_sprout_recursive_treeless(self):
814
778
        tree = self.make_branch_and_tree('tree1',
819
783
        self.build_tree(['tree1/subtree/file'])
820
784
        sub_tree.add('file')
821
785
        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
786
        tree.bzrdir.destroy_workingtree()
826
 
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
827
 
        # fail :-( ) -- vila 20100909
828
787
        repo = self.make_repository('repo', shared=True,
829
788
            format='dirstate-with-subtree')
830
789
        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')
 
790
        tree.bzrdir.sprout('repo/tree2')
 
791
        self.failUnlessExists('repo/tree2/subtree')
 
792
        self.failIfExists('repo/tree2/subtree/file')
843
793
 
844
794
    def make_foo_bar_baz(self):
845
795
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
849
799
 
850
800
    def test_find_bzrdirs(self):
851
801
        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))
 
802
        transport = get_transport(self.get_url())
 
803
        self.assertEqualBzrdirs([baz, foo, bar],
 
804
                                bzrdir.BzrDir.find_bzrdirs(transport))
886
805
 
887
806
    def test_find_bzrdirs_list_current(self):
888
807
        def list_current(transport):
889
808
            return [s for s in transport.list_dir('') if s != 'baz']
890
809
 
891
810
        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))
 
811
        transport = get_transport(self.get_url())
 
812
        self.assertEqualBzrdirs([foo, bar],
 
813
                                bzrdir.BzrDir.find_bzrdirs(transport,
 
814
                                    list_current=list_current))
 
815
 
896
816
 
897
817
    def test_find_bzrdirs_evaluate(self):
898
818
        def evaluate(bzrdir):
904
824
                return False, bzrdir.root_transport.base
905
825
 
906
826
        foo, bar, baz = self.make_foo_bar_baz()
907
 
        t = self.get_transport()
 
827
        transport = get_transport(self.get_url())
908
828
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
909
 
                         list(bzrdir.BzrDir.find_bzrdirs(t, evaluate=evaluate)))
 
829
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
 
830
                                                         evaluate=evaluate)))
910
831
 
911
832
    def assertEqualBzrdirs(self, first, second):
912
833
        first = list(first)
919
840
        root = self.make_repository('', shared=True)
920
841
        foo, bar, baz = self.make_foo_bar_baz()
921
842
        qux = self.make_bzrdir('foo/qux')
922
 
        t = self.get_transport()
923
 
        branches = bzrdir.BzrDir.find_branches(t)
 
843
        transport = get_transport(self.get_url())
 
844
        branches = bzrdir.BzrDir.find_branches(transport)
924
845
        self.assertEqual(baz.root_transport.base, branches[0].base)
925
846
        self.assertEqual(foo.root_transport.base, branches[1].base)
926
847
        self.assertEqual(bar.root_transport.base, branches[2].base)
927
848
 
928
849
        # ensure this works without a top-level repo
929
 
        branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
 
850
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
930
851
        self.assertEqual(foo.root_transport.base, branches[0].base)
931
852
        self.assertEqual(bar.root_transport.base, branches[1].base)
932
853
 
933
854
 
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
855
class TestMeta1DirFormat(TestCaseWithTransport):
950
856
    """Tests specific to the meta1 dir format."""
951
857
 
958
864
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
959
865
        repository_base = t.clone('repository').base
960
866
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
961
 
        repository_format = repository.format_registry.get_default()
962
867
        self.assertEqual(repository_base,
963
 
                         dir.get_repository_transport(repository_format).base)
 
868
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
964
869
        checkout_base = t.clone('checkout').base
965
870
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
966
871
        self.assertEqual(checkout_base,
967
 
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
 
872
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
968
873
 
969
874
    def test_meta1dir_uses_lockdir(self):
970
875
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
1012
917
        self.assertEqual(2, rpc_count)
1013
918
 
1014
919
 
 
920
class TestFormat5(TestCaseWithTransport):
 
921
    """Tests specific to the version 5 bzrdir format."""
 
922
 
 
923
    def test_same_lockfiles_between_tree_repo_branch(self):
 
924
        # this checks that only a single lockfiles instance is created
 
925
        # for format 5 objects
 
926
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
927
        def check_dir_components_use_same_lock(dir):
 
928
            ctrl_1 = dir.open_repository().control_files
 
929
            ctrl_2 = dir.open_branch().control_files
 
930
            ctrl_3 = dir.open_workingtree()._control_files
 
931
            self.assertTrue(ctrl_1 is ctrl_2)
 
932
            self.assertTrue(ctrl_2 is ctrl_3)
 
933
        check_dir_components_use_same_lock(dir)
 
934
        # and if we open it normally.
 
935
        dir = bzrdir.BzrDir.open(self.get_url())
 
936
        check_dir_components_use_same_lock(dir)
 
937
 
 
938
    def test_can_convert(self):
 
939
        # format 5 dirs are convertable
 
940
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
941
        self.assertTrue(dir.can_convert_format())
 
942
 
 
943
    def test_needs_conversion(self):
 
944
        # format 5 dirs need a conversion if they are not the default,
 
945
        # and they aren't
 
946
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
947
        # don't need to convert it to itself
 
948
        self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
 
949
        # do need to convert it to the current default
 
950
        self.assertTrue(dir.needs_format_conversion(
 
951
            bzrdir.BzrDirFormat.get_default_format()))
 
952
 
 
953
 
 
954
class TestFormat6(TestCaseWithTransport):
 
955
    """Tests specific to the version 6 bzrdir format."""
 
956
 
 
957
    def test_same_lockfiles_between_tree_repo_branch(self):
 
958
        # this checks that only a single lockfiles instance is created
 
959
        # for format 6 objects
 
960
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
961
        def check_dir_components_use_same_lock(dir):
 
962
            ctrl_1 = dir.open_repository().control_files
 
963
            ctrl_2 = dir.open_branch().control_files
 
964
            ctrl_3 = dir.open_workingtree()._control_files
 
965
            self.assertTrue(ctrl_1 is ctrl_2)
 
966
            self.assertTrue(ctrl_2 is ctrl_3)
 
967
        check_dir_components_use_same_lock(dir)
 
968
        # and if we open it normally.
 
969
        dir = bzrdir.BzrDir.open(self.get_url())
 
970
        check_dir_components_use_same_lock(dir)
 
971
 
 
972
    def test_can_convert(self):
 
973
        # format 6 dirs are convertable
 
974
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
975
        self.assertTrue(dir.can_convert_format())
 
976
 
 
977
    def test_needs_conversion(self):
 
978
        # format 6 dirs need an conversion if they are not the default.
 
979
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
980
        self.assertTrue(dir.needs_format_conversion(
 
981
            bzrdir.BzrDirFormat.get_default_format()))
 
982
 
 
983
 
 
984
class NotBzrDir(bzrlib.bzrdir.BzrDir):
 
985
    """A non .bzr based control directory."""
 
986
 
 
987
    def __init__(self, transport, format):
 
988
        self._format = format
 
989
        self.root_transport = transport
 
990
        self.transport = transport.clone('.not')
 
991
 
 
992
 
 
993
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
 
994
    """A test class representing any non-.bzr based disk format."""
 
995
 
 
996
    def initialize_on_transport(self, transport):
 
997
        """Initialize a new .not dir in the base directory of a Transport."""
 
998
        transport.mkdir('.not')
 
999
        return self.open(transport)
 
1000
 
 
1001
    def open(self, transport):
 
1002
        """Open this directory."""
 
1003
        return NotBzrDir(transport, self)
 
1004
 
 
1005
    @classmethod
 
1006
    def _known_formats(self):
 
1007
        return set([NotBzrDirFormat()])
 
1008
 
 
1009
    @classmethod
 
1010
    def probe_transport(self, transport):
 
1011
        """Our format is present if the transport ends in '.not/'."""
 
1012
        if transport.has('.not'):
 
1013
            return NotBzrDirFormat()
 
1014
 
 
1015
 
 
1016
class TestNotBzrDir(TestCaseWithTransport):
 
1017
    """Tests for using the bzrdir api with a non .bzr based disk format.
 
1018
 
 
1019
    If/when one of these is in the core, we can let the implementation tests
 
1020
    verify this works.
 
1021
    """
 
1022
 
 
1023
    def test_create_and_find_format(self):
 
1024
        # create a .notbzr dir
 
1025
        format = NotBzrDirFormat()
 
1026
        dir = format.initialize(self.get_url())
 
1027
        self.assertIsInstance(dir, NotBzrDir)
 
1028
        # now probe for it.
 
1029
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
 
1030
        try:
 
1031
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
 
1032
                get_transport(self.get_url()))
 
1033
            self.assertIsInstance(found, NotBzrDirFormat)
 
1034
        finally:
 
1035
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
 
1036
 
 
1037
    def test_included_in_known_formats(self):
 
1038
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
 
1039
        try:
 
1040
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
 
1041
            for format in formats:
 
1042
                if isinstance(format, NotBzrDirFormat):
 
1043
                    return
 
1044
            self.fail("No NotBzrDirFormat in %s" % formats)
 
1045
        finally:
 
1046
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
 
1047
 
 
1048
 
1015
1049
class NonLocalTests(TestCaseWithTransport):
1016
1050
    """Tests for bzrdir static behaviour on non local paths."""
1017
1051
 
1036
1070
            self.get_url('foo'),
1037
1071
            force_new_tree=True,
1038
1072
            format=format)
1039
 
        t = self.get_transport()
 
1073
        t = get_transport(self.get_url('.'))
1040
1074
        self.assertFalse(t.has('foo'))
1041
1075
 
1042
1076
    def test_clone(self):
1058
1092
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1059
1093
        checkout_format = my_bzrdir.checkout_metadir()
1060
1094
        self.assertIsInstance(checkout_format.workingtree_format,
1061
 
                              workingtree_4.WorkingTreeFormat4)
 
1095
                              workingtree.WorkingTreeFormat3)
1062
1096
 
1063
1097
 
1064
1098
class TestHTTPRedirections(object):
1073
1107
    """
1074
1108
 
1075
1109
    def create_transport_readonly_server(self):
1076
 
        # We don't set the http protocol version, relying on the default
1077
1110
        return http_utils.HTTPServerRedirecting()
1078
1111
 
1079
1112
    def create_transport_secondary_server(self):
1080
 
        # We don't set the http protocol version, relying on the default
1081
1113
        return http_utils.HTTPServerRedirecting()
1082
1114
 
1083
1115
    def setUp(self):
1238
1270
    def copy_content_into(self, destination, revision_id=None):
1239
1271
        self.calls.append('copy_content_into')
1240
1272
 
1241
 
    def last_revision(self):
1242
 
        return _mod_revision.NULL_REVISION
1243
 
 
1244
1273
    def get_parent(self):
1245
1274
        return self._parent
1246
1275
 
1247
1276
    def set_parent(self, parent):
1248
1277
        self._parent = parent
1249
1278
 
1250
 
    def lock_read(self):
1251
 
        return lock.LogicalLockResult(self.unlock)
1252
 
 
1253
 
    def unlock(self):
1254
 
        return
1255
 
 
1256
1279
 
1257
1280
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1258
1281
 
1312
1335
        url = transport.base
1313
1336
        err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1314
1337
        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