~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Jelmer Vernooij
  • Date: 2011-04-19 10:42:59 UTC
  • mto: This revision was merged to the branch mainline in revision 5806.
  • Revision ID: jelmer@samba.org-20110419104259-g9exlcp1f5jdu3ci
Move Inventory._get_mutable_inventory -> mutable_inventory_from_tree.

Show diffs side-by-side

added added

removed removed

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