~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Patch Queue Manager
  • Date: 2011-09-22 14:12:18 UTC
  • mfrom: (6155.3.1 jam)
  • Revision ID: pqm@pqm.ubuntu.com-20110922141218-86s4uu6nqvourw4f
(jameinel) Cleanup comments bzrlib/smart/__init__.py (John A Meinel)

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