~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Aaron Bentley
  • Date: 2009-06-19 21:16:31 UTC
  • mto: This revision was merged to the branch mainline in revision 4481.
  • Revision ID: aaron@aaronbentley.com-20090619211631-4fnkv2uui98xj7ux
Provide control over switch and shelver messaging.

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
16
16
 
17
17
"""Tests for the BzrDir facility and any format specific tests.
18
18
 
19
 
For interface contract tests, see tests/per_bzr_dir.
 
19
For interface contract tests, see tests/bzr_dir_implementations.
20
20
"""
21
21
 
22
22
import os
24
24
import sys
25
25
 
26
26
from bzrlib import (
27
 
    branch,
28
27
    bzrdir,
29
 
    config,
30
 
    controldir,
31
28
    errors,
32
29
    help_topics,
33
 
    lock,
34
30
    repository,
35
 
    revision as _mod_revision,
36
31
    osutils,
37
32
    remote,
38
 
    symbol_versioning,
39
 
    transport as _mod_transport,
40
33
    urlutils,
41
34
    win32utils,
42
 
    workingtree_3,
43
 
    workingtree_4,
 
35
    workingtree,
44
36
    )
45
37
import bzrlib.branch
46
 
from bzrlib.errors import (
47
 
    NotBranchError,
48
 
    NoColocatedBranchSupport,
49
 
    UnknownFormatError,
50
 
    UnsupportedFormatError,
51
 
    )
 
38
from bzrlib.errors import (NotBranchError,
 
39
                           UnknownFormatError,
 
40
                           UnsupportedFormatError,
 
41
                           )
52
42
from bzrlib.tests import (
53
43
    TestCase,
54
44
    TestCaseWithMemoryTransport,
60
50
    http_utils,
61
51
    )
62
52
from bzrlib.tests.test_http import TestWithTransport_pycurl
63
 
from bzrlib.transport import (
64
 
    memory,
65
 
    pathfilter,
66
 
    )
 
53
from bzrlib.transport import get_transport
67
54
from bzrlib.transport.http._urllib import HttpTransport_urllib
 
55
from bzrlib.transport.memory import MemoryServer
68
56
from bzrlib.transport.nosmart import NoSmartTransportDecorator
69
57
from bzrlib.transport.readonly import ReadonlyTransportDecorator
70
 
from bzrlib.repofmt import knitrepo, knitpack_repo
 
58
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
71
59
 
72
60
 
73
61
class TestDefaultFormat(TestCase):
74
62
 
75
63
    def test_get_set_default_format(self):
76
64
        old_format = bzrdir.BzrDirFormat.get_default_format()
77
 
        # default is BzrDirMetaFormat1
78
 
        self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
79
 
        controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
 
65
        # default is BzrDirFormat6
 
66
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
 
67
        bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
80
68
        # creating a bzr dir should now create an instrumented dir.
81
69
        try:
82
70
            result = bzrdir.BzrDir.create('memory:///')
83
 
            self.assertIsInstance(result, SampleBzrDir)
 
71
            self.failUnless(isinstance(result, SampleBzrDir))
84
72
        finally:
85
 
            controldir.ControlDirFormat._set_default_format(old_format)
 
73
            bzrdir.BzrDirFormat._set_default_format(old_format)
86
74
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
87
75
 
88
76
 
89
 
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
90
 
    """A deprecated bzr dir format."""
91
 
 
92
 
 
93
77
class TestFormatRegistry(TestCase):
94
78
 
95
79
    def make_format_registry(self):
96
 
        my_format_registry = controldir.ControlDirFormatRegistry()
97
 
        my_format_registry.register('deprecated', DeprecatedBzrDirFormat,
98
 
            'Some format.  Slower and unawesome and deprecated.',
99
 
            deprecated=True)
100
 
        my_format_registry.register_lazy('lazy', 'bzrlib.tests.test_bzrdir',
101
 
            'DeprecatedBzrDirFormat', 'Format registered lazily',
102
 
            deprecated=True)
103
 
        bzrdir.register_metadir(my_format_registry, 'knit',
 
80
        my_format_registry = bzrdir.BzrDirFormatRegistry()
 
81
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
 
82
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
83
            ' repositories', deprecated=True)
 
84
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
 
85
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
 
86
        my_format_registry.register_metadir('knit',
104
87
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
105
88
            'Format using knits',
106
89
            )
107
90
        my_format_registry.set_default('knit')
108
 
        bzrdir.register_metadir(my_format_registry,
 
91
        my_format_registry.register_metadir(
109
92
            'branch6',
110
93
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
111
94
            'Experimental successor to knit.  Use at your own risk.',
112
95
            branch_format='bzrlib.branch.BzrBranchFormat6',
113
96
            experimental=True)
114
 
        bzrdir.register_metadir(my_format_registry,
 
97
        my_format_registry.register_metadir(
115
98
            'hidden format',
116
99
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
117
100
            'Experimental successor to knit.  Use at your own risk.',
118
101
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
119
 
        my_format_registry.register('hiddendeprecated', DeprecatedBzrDirFormat,
120
 
            'Old format.  Slower and does not support things. ', hidden=True)
121
 
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.tests.test_bzrdir',
122
 
            'DeprecatedBzrDirFormat', 'Format registered lazily',
123
 
            deprecated=True, hidden=True)
 
102
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
 
103
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
104
            ' repositories', hidden=True)
 
105
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
 
106
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
 
107
            hidden=True)
124
108
        return my_format_registry
125
109
 
126
110
    def test_format_registry(self):
127
111
        my_format_registry = self.make_format_registry()
128
112
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
129
 
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
130
 
        my_bzrdir = my_format_registry.make_bzrdir('deprecated')
131
 
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
 
113
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
114
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
115
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
132
116
        my_bzrdir = my_format_registry.make_bzrdir('default')
133
117
        self.assertIsInstance(my_bzrdir.repository_format,
134
118
            knitrepo.RepositoryFormatKnit1)
147
131
                         my_format_registry.get_help('knit'))
148
132
        self.assertEqual('Format using knits',
149
133
                         my_format_registry.get_help('default'))
150
 
        self.assertEqual('Some format.  Slower and unawesome and deprecated.',
151
 
                         my_format_registry.get_help('deprecated'))
 
134
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
 
135
                         ' checkouts or shared repositories',
 
136
                         my_format_registry.get_help('weave'))
152
137
 
153
138
    def test_help_topic(self):
154
139
        topics = help_topics.HelpTopicRegistry()
160
145
        new = topics.get_detail('current-formats')
161
146
        rest = topics.get_detail('other-formats')
162
147
        experimental, deprecated = rest.split('Deprecated formats')
163
 
        self.assertContainsRe(new, 'formats-help')
 
148
        self.assertContainsRe(new, 'bzr help formats')
164
149
        self.assertContainsRe(new,
165
150
                ':knit:\n    \(native\) \(default\) Format using knits\n')
166
151
        self.assertContainsRe(experimental,
178
163
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
179
164
                          bzrdir.format_registry.get('default'))
180
165
            self.assertIs(
181
 
                repository.format_registry.get_default().__class__,
 
166
                repository.RepositoryFormat.get_default_format().__class__,
182
167
                knitrepo.RepositoryFormatKnit3)
183
168
        finally:
184
169
            bzrdir.format_registry.set_default_repository(old_default)
185
170
 
186
171
    def test_aliases(self):
187
 
        a_registry = controldir.ControlDirFormatRegistry()
188
 
        a_registry.register('deprecated', DeprecatedBzrDirFormat,
189
 
            'Old format.  Slower and does not support stuff',
190
 
            deprecated=True)
191
 
        a_registry.register('deprecatedalias', DeprecatedBzrDirFormat,
192
 
            'Old format.  Slower and does not support stuff',
193
 
            deprecated=True, alias=True)
194
 
        self.assertEqual(frozenset(['deprecatedalias']), a_registry.aliases())
 
172
        a_registry = bzrdir.BzrDirFormatRegistry()
 
173
        a_registry.register('weave', bzrdir.BzrDirFormat6,
 
174
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
175
            ' repositories', deprecated=True)
 
176
        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
 
177
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
178
            ' repositories', deprecated=True, alias=True)
 
179
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
195
180
 
196
181
 
197
182
class SampleBranch(bzrlib.branch.Branch):
219
204
        """See BzrDir.open_repository."""
220
205
        return SampleRepository(self)
221
206
 
222
 
    def create_branch(self, name=None):
 
207
    def create_branch(self):
223
208
        """See BzrDir.create_branch."""
224
 
        if name is not None:
225
 
            raise NoColocatedBranchSupport(self)
226
209
        return SampleBranch(self)
227
210
 
228
211
    def create_workingtree(self):
254
237
        return "opened branch."
255
238
 
256
239
 
257
 
class BzrDirFormatTest1(bzrdir.BzrDirMetaFormat1):
258
 
 
259
 
    @staticmethod
260
 
    def get_format_string():
261
 
        return "Test format 1"
262
 
 
263
 
 
264
 
class BzrDirFormatTest2(bzrdir.BzrDirMetaFormat1):
265
 
 
266
 
    @staticmethod
267
 
    def get_format_string():
268
 
        return "Test format 2"
269
 
 
270
 
 
271
240
class TestBzrDirFormat(TestCaseWithTransport):
272
241
    """Tests for the BzrDirFormat facility."""
273
242
 
274
243
    def test_find_format(self):
275
244
        # is the right format object found for a branch?
276
245
        # create a branch with a few known format objects.
277
 
        bzrdir.BzrProber.formats.register(BzrDirFormatTest1.get_format_string(),
278
 
            BzrDirFormatTest1())
279
 
        self.addCleanup(bzrdir.BzrProber.formats.remove,
280
 
            BzrDirFormatTest1.get_format_string())
281
 
        bzrdir.BzrProber.formats.register(BzrDirFormatTest2.get_format_string(),
282
 
            BzrDirFormatTest2())
283
 
        self.addCleanup(bzrdir.BzrProber.formats.remove,
284
 
            BzrDirFormatTest2.get_format_string())
285
 
        t = self.get_transport()
 
246
        # this is not quite the same as
 
247
        t = get_transport(self.get_url())
286
248
        self.build_tree(["foo/", "bar/"], transport=t)
287
249
        def check_format(format, url):
288
250
            format.initialize(url)
289
 
            t = _mod_transport.get_transport(url)
 
251
            t = get_transport(url)
290
252
            found_format = bzrdir.BzrDirFormat.find_format(t)
291
 
            self.assertIsInstance(found_format, format.__class__)
292
 
        check_format(BzrDirFormatTest1(), "foo")
293
 
        check_format(BzrDirFormatTest2(), "bar")
 
253
            self.failUnless(isinstance(found_format, format.__class__))
 
254
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
255
        check_format(bzrdir.BzrDirFormat6(), "bar")
294
256
 
295
257
    def test_find_format_nothing_there(self):
296
258
        self.assertRaises(NotBranchError,
297
259
                          bzrdir.BzrDirFormat.find_format,
298
 
                          _mod_transport.get_transport('.'))
 
260
                          get_transport('.'))
299
261
 
300
262
    def test_find_format_unknown_format(self):
301
 
        t = self.get_transport()
 
263
        t = get_transport(self.get_url())
302
264
        t.mkdir('.bzr')
303
265
        t.put_bytes('.bzr/branch-format', '')
304
266
        self.assertRaises(UnknownFormatError,
305
267
                          bzrdir.BzrDirFormat.find_format,
306
 
                          _mod_transport.get_transport('.'))
 
268
                          get_transport('.'))
307
269
 
308
270
    def test_register_unregister_format(self):
309
271
        format = SampleBzrDirFormat()
311
273
        # make a bzrdir
312
274
        format.initialize(url)
313
275
        # register a format for it.
314
 
        bzrdir.BzrProber.formats.register(format.get_format_string(), format)
 
276
        bzrdir.BzrDirFormat.register_format(format)
315
277
        # which bzrdir.Open will refuse (not supported)
316
278
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
317
279
        # which bzrdir.open_containing will refuse (not supported)
318
280
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
319
281
        # but open_downlevel will work
320
 
        t = _mod_transport.get_transport(url)
 
282
        t = get_transport(url)
321
283
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
322
284
        # unregister the format
323
 
        bzrdir.BzrProber.formats.remove(format.get_format_string())
 
285
        bzrdir.BzrDirFormat.unregister_format(format)
324
286
        # now open_downlevel should fail too.
325
287
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
326
288
 
392
354
 
393
355
    def test_create_branch_convenience_root(self):
394
356
        """Creating a branch at the root of a fs should work."""
395
 
        self.vfs_transport_factory = memory.MemoryServer
 
357
        self.vfs_transport_factory = MemoryServer
396
358
        # outside a repo the default convenience output is a repo+branch_tree
397
359
        format = bzrdir.format_registry.make_bzrdir('knit')
398
360
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
503
465
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
504
466
        # Make stackable source branch with an unstackable repo format.
505
467
        source_bzrdir = self.make_bzrdir('source')
506
 
        knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
507
 
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
508
 
            source_bzrdir)
 
468
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
469
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(source_bzrdir)
509
470
        # Make a directory with a default stacking policy
510
471
        parent_bzrdir = self.make_bzrdir('parent')
511
472
        stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
604
565
 
605
566
    def setUp(self):
606
567
        super(ChrootedTests, self).setUp()
607
 
        if not self.vfs_transport_factory == memory.MemoryServer:
 
568
        if not self.vfs_transport_factory == MemoryServer:
608
569
            self.transport_readonly_server = http_server.HttpServer
609
570
 
610
571
    def local_branch_path(self, branch):
709
670
        self.assertEqual(relpath, 'baz')
710
671
 
711
672
    def test_open_containing_from_transport(self):
712
 
        self.assertRaises(NotBranchError,
713
 
            bzrdir.BzrDir.open_containing_from_transport,
714
 
            _mod_transport.get_transport(self.get_readonly_url('')))
715
 
        self.assertRaises(NotBranchError,
716
 
            bzrdir.BzrDir.open_containing_from_transport,
717
 
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
 
673
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
674
                          get_transport(self.get_readonly_url('')))
 
675
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
676
                          get_transport(self.get_readonly_url('g/p/q')))
718
677
        control = bzrdir.BzrDir.create(self.get_url())
719
678
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
720
 
            _mod_transport.get_transport(self.get_readonly_url('')))
 
679
            get_transport(self.get_readonly_url('')))
721
680
        self.assertEqual('', relpath)
722
681
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
723
 
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
 
682
            get_transport(self.get_readonly_url('g/p/q')))
724
683
        self.assertEqual('g/p/q', relpath)
725
684
 
726
685
    def test_open_containing_tree_or_branch(self):
770
729
        # transport pointing at bzrdir should give a bzrdir with root transport
771
730
        # set to the given transport
772
731
        control = bzrdir.BzrDir.create(self.get_url())
773
 
        t = self.get_transport()
774
 
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(t)
775
 
        self.assertEqual(t.base, opened_bzrdir.root_transport.base)
 
732
        transport = get_transport(self.get_url())
 
733
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
 
734
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
776
735
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
777
736
 
778
737
    def test_open_from_transport_no_bzrdir(self):
779
 
        t = self.get_transport()
780
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
738
        transport = get_transport(self.get_url())
 
739
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
740
                          transport)
781
741
 
782
742
    def test_open_from_transport_bzrdir_in_parent(self):
783
743
        control = bzrdir.BzrDir.create(self.get_url())
784
 
        t = self.get_transport()
785
 
        t.mkdir('subdir')
786
 
        t = t.clone('subdir')
787
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
744
        transport = get_transport(self.get_url())
 
745
        transport.mkdir('subdir')
 
746
        transport = transport.clone('subdir')
 
747
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
748
                          transport)
788
749
 
789
750
    def test_sprout_recursive(self):
790
751
        tree = self.make_branch_and_tree('tree1',
799
760
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
800
761
        tree2.lock_read()
801
762
        self.addCleanup(tree2.unlock)
802
 
        self.assertPathExists('tree2/subtree/file')
 
763
        self.failUnlessExists('tree2/subtree/file')
803
764
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
804
765
 
805
766
    def test_cloning_metadir(self):
809
770
        branch = self.make_branch('branch', format='knit')
810
771
        format = branch.bzrdir.cloning_metadir()
811
772
        self.assertIsInstance(format.workingtree_format,
812
 
            workingtree_4.WorkingTreeFormat6)
 
773
            workingtree.WorkingTreeFormat3)
813
774
 
814
775
    def test_sprout_recursive_treeless(self):
815
776
        tree = self.make_branch_and_tree('tree1',
820
781
        self.build_tree(['tree1/subtree/file'])
821
782
        sub_tree.add('file')
822
783
        tree.commit('Initial commit')
823
 
        # The following line force the orhaning to reveal bug #634470
824
 
        tree.branch.get_config().set_user_option(
825
 
            'bzr.transform.orphan_policy', 'move')
826
784
        tree.bzrdir.destroy_workingtree()
827
 
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
828
 
        # fail :-( ) -- vila 20100909
829
785
        repo = self.make_repository('repo', shared=True,
830
786
            format='dirstate-with-subtree')
831
787
        repo.set_make_working_trees(False)
832
 
        # FIXME: we just deleted the workingtree and now we want to use it ????
833
 
        # At a minimum, we should use tree.branch below (but this fails too
834
 
        # currently) or stop calling this test 'treeless'. Specifically, I've
835
 
        # turn the line below into an assertRaises when 'subtree/.bzr' is
836
 
        # orphaned and sprout tries to access the branch there (which is left
837
 
        # by bzrdir.BzrDirMeta1.destroy_workingtree when it ignores the
838
 
        # [DeletingParent('Not deleting', u'subtree', None)] conflict). See bug
839
 
        # #634470.  -- vila 20100909
840
 
        self.assertRaises(errors.NotBranchError,
841
 
                          tree.bzrdir.sprout, 'repo/tree2')
842
 
#        self.assertPathExists('repo/tree2/subtree')
843
 
#        self.assertPathDoesNotExist('repo/tree2/subtree/file')
 
788
        tree.bzrdir.sprout('repo/tree2')
 
789
        self.failUnlessExists('repo/tree2/subtree')
 
790
        self.failIfExists('repo/tree2/subtree/file')
844
791
 
845
792
    def make_foo_bar_baz(self):
846
793
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
850
797
 
851
798
    def test_find_bzrdirs(self):
852
799
        foo, bar, baz = self.make_foo_bar_baz()
853
 
        t = self.get_transport()
854
 
        self.assertEqualBzrdirs([baz, foo, bar], bzrdir.BzrDir.find_bzrdirs(t))
855
 
 
856
 
    def make_fake_permission_denied_transport(self, transport, paths):
857
 
        """Create a transport that raises PermissionDenied for some paths."""
858
 
        def filter(path):
859
 
            if path in paths:
860
 
                raise errors.PermissionDenied(path)
861
 
            return path
862
 
        path_filter_server = pathfilter.PathFilteringServer(transport, filter)
863
 
        path_filter_server.start_server()
864
 
        self.addCleanup(path_filter_server.stop_server)
865
 
        path_filter_transport = pathfilter.PathFilteringTransport(
866
 
            path_filter_server, '.')
867
 
        return (path_filter_server, path_filter_transport)
868
 
 
869
 
    def assertBranchUrlsEndWith(self, expect_url_suffix, actual_bzrdirs):
870
 
        """Check that each branch url ends with the given suffix."""
871
 
        for actual_bzrdir in actual_bzrdirs:
872
 
            self.assertEndsWith(actual_bzrdir.user_url, expect_url_suffix)
873
 
 
874
 
    def test_find_bzrdirs_permission_denied(self):
875
 
        foo, bar, baz = self.make_foo_bar_baz()
876
 
        t = self.get_transport()
877
 
        path_filter_server, path_filter_transport = \
878
 
            self.make_fake_permission_denied_transport(t, ['foo'])
879
 
        # local transport
880
 
        self.assertBranchUrlsEndWith('/baz/',
881
 
            bzrdir.BzrDir.find_bzrdirs(path_filter_transport))
882
 
        # smart server
883
 
        smart_transport = self.make_smart_server('.',
884
 
            backing_server=path_filter_server)
885
 
        self.assertBranchUrlsEndWith('/baz/',
886
 
            bzrdir.BzrDir.find_bzrdirs(smart_transport))
 
800
        transport = get_transport(self.get_url())
 
801
        self.assertEqualBzrdirs([baz, foo, bar],
 
802
                                bzrdir.BzrDir.find_bzrdirs(transport))
887
803
 
888
804
    def test_find_bzrdirs_list_current(self):
889
805
        def list_current(transport):
890
806
            return [s for s in transport.list_dir('') if s != 'baz']
891
807
 
892
808
        foo, bar, baz = self.make_foo_bar_baz()
893
 
        t = self.get_transport()
894
 
        self.assertEqualBzrdirs(
895
 
            [foo, bar],
896
 
            bzrdir.BzrDir.find_bzrdirs(t, list_current=list_current))
 
809
        transport = get_transport(self.get_url())
 
810
        self.assertEqualBzrdirs([foo, bar],
 
811
                                bzrdir.BzrDir.find_bzrdirs(transport,
 
812
                                    list_current=list_current))
 
813
 
897
814
 
898
815
    def test_find_bzrdirs_evaluate(self):
899
816
        def evaluate(bzrdir):
905
822
                return False, bzrdir.root_transport.base
906
823
 
907
824
        foo, bar, baz = self.make_foo_bar_baz()
908
 
        t = self.get_transport()
 
825
        transport = get_transport(self.get_url())
909
826
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
910
 
                         list(bzrdir.BzrDir.find_bzrdirs(t, evaluate=evaluate)))
 
827
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
 
828
                                                         evaluate=evaluate)))
911
829
 
912
830
    def assertEqualBzrdirs(self, first, second):
913
831
        first = list(first)
920
838
        root = self.make_repository('', shared=True)
921
839
        foo, bar, baz = self.make_foo_bar_baz()
922
840
        qux = self.make_bzrdir('foo/qux')
923
 
        t = self.get_transport()
924
 
        branches = bzrdir.BzrDir.find_branches(t)
 
841
        transport = get_transport(self.get_url())
 
842
        branches = bzrdir.BzrDir.find_branches(transport)
925
843
        self.assertEqual(baz.root_transport.base, branches[0].base)
926
844
        self.assertEqual(foo.root_transport.base, branches[1].base)
927
845
        self.assertEqual(bar.root_transport.base, branches[2].base)
928
846
 
929
847
        # ensure this works without a top-level repo
930
 
        branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
 
848
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
931
849
        self.assertEqual(foo.root_transport.base, branches[0].base)
932
850
        self.assertEqual(bar.root_transport.base, branches[1].base)
933
851
 
934
852
 
935
 
class TestMissingRepoBranchesSkipped(TestCaseWithMemoryTransport):
936
 
 
937
 
    def test_find_bzrdirs_missing_repo(self):
938
 
        t = self.get_transport()
939
 
        arepo = self.make_repository('arepo', shared=True)
940
 
        abranch_url = arepo.user_url + '/abranch'
941
 
        abranch = bzrdir.BzrDir.create(abranch_url).create_branch()
942
 
        t.delete_tree('arepo/.bzr')
943
 
        self.assertRaises(errors.NoRepositoryPresent,
944
 
            branch.Branch.open, abranch_url)
945
 
        self.make_branch('baz')
946
 
        for actual_bzrdir in bzrdir.BzrDir.find_branches(t):
947
 
            self.assertEndsWith(actual_bzrdir.user_url, '/baz/')
948
 
 
949
 
 
950
853
class TestMeta1DirFormat(TestCaseWithTransport):
951
854
    """Tests specific to the meta1 dir format."""
952
855
 
959
862
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
960
863
        repository_base = t.clone('repository').base
961
864
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
962
 
        repository_format = repository.format_registry.get_default()
963
865
        self.assertEqual(repository_base,
964
 
                         dir.get_repository_transport(repository_format).base)
 
866
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
965
867
        checkout_base = t.clone('checkout').base
966
868
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
967
869
        self.assertEqual(checkout_base,
968
 
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
 
870
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
969
871
 
970
872
    def test_meta1dir_uses_lockdir(self):
971
873
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
1013
915
        self.assertEqual(2, rpc_count)
1014
916
 
1015
917
 
 
918
class TestFormat5(TestCaseWithTransport):
 
919
    """Tests specific to the version 5 bzrdir format."""
 
920
 
 
921
    def test_same_lockfiles_between_tree_repo_branch(self):
 
922
        # this checks that only a single lockfiles instance is created
 
923
        # for format 5 objects
 
924
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
925
        def check_dir_components_use_same_lock(dir):
 
926
            ctrl_1 = dir.open_repository().control_files
 
927
            ctrl_2 = dir.open_branch().control_files
 
928
            ctrl_3 = dir.open_workingtree()._control_files
 
929
            self.assertTrue(ctrl_1 is ctrl_2)
 
930
            self.assertTrue(ctrl_2 is ctrl_3)
 
931
        check_dir_components_use_same_lock(dir)
 
932
        # and if we open it normally.
 
933
        dir = bzrdir.BzrDir.open(self.get_url())
 
934
        check_dir_components_use_same_lock(dir)
 
935
 
 
936
    def test_can_convert(self):
 
937
        # format 5 dirs are convertable
 
938
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
939
        self.assertTrue(dir.can_convert_format())
 
940
 
 
941
    def test_needs_conversion(self):
 
942
        # format 5 dirs need a conversion if they are not the default,
 
943
        # and they aren't
 
944
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
945
        # don't need to convert it to itself
 
946
        self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
 
947
        # do need to convert it to the current default
 
948
        self.assertTrue(dir.needs_format_conversion(
 
949
            bzrdir.BzrDirFormat.get_default_format()))
 
950
 
 
951
 
 
952
class TestFormat6(TestCaseWithTransport):
 
953
    """Tests specific to the version 6 bzrdir format."""
 
954
 
 
955
    def test_same_lockfiles_between_tree_repo_branch(self):
 
956
        # this checks that only a single lockfiles instance is created
 
957
        # for format 6 objects
 
958
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
959
        def check_dir_components_use_same_lock(dir):
 
960
            ctrl_1 = dir.open_repository().control_files
 
961
            ctrl_2 = dir.open_branch().control_files
 
962
            ctrl_3 = dir.open_workingtree()._control_files
 
963
            self.assertTrue(ctrl_1 is ctrl_2)
 
964
            self.assertTrue(ctrl_2 is ctrl_3)
 
965
        check_dir_components_use_same_lock(dir)
 
966
        # and if we open it normally.
 
967
        dir = bzrdir.BzrDir.open(self.get_url())
 
968
        check_dir_components_use_same_lock(dir)
 
969
 
 
970
    def test_can_convert(self):
 
971
        # format 6 dirs are convertable
 
972
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
973
        self.assertTrue(dir.can_convert_format())
 
974
 
 
975
    def test_needs_conversion(self):
 
976
        # format 6 dirs need an conversion if they are not the default.
 
977
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
978
        self.assertTrue(dir.needs_format_conversion(
 
979
            bzrdir.BzrDirFormat.get_default_format()))
 
980
 
 
981
 
 
982
class NotBzrDir(bzrlib.bzrdir.BzrDir):
 
983
    """A non .bzr based control directory."""
 
984
 
 
985
    def __init__(self, transport, format):
 
986
        self._format = format
 
987
        self.root_transport = transport
 
988
        self.transport = transport.clone('.not')
 
989
 
 
990
 
 
991
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
 
992
    """A test class representing any non-.bzr based disk format."""
 
993
 
 
994
    def initialize_on_transport(self, transport):
 
995
        """Initialize a new .not dir in the base directory of a Transport."""
 
996
        transport.mkdir('.not')
 
997
        return self.open(transport)
 
998
 
 
999
    def open(self, transport):
 
1000
        """Open this directory."""
 
1001
        return NotBzrDir(transport, self)
 
1002
 
 
1003
    @classmethod
 
1004
    def _known_formats(self):
 
1005
        return set([NotBzrDirFormat()])
 
1006
 
 
1007
    @classmethod
 
1008
    def probe_transport(self, transport):
 
1009
        """Our format is present if the transport ends in '.not/'."""
 
1010
        if transport.has('.not'):
 
1011
            return NotBzrDirFormat()
 
1012
 
 
1013
 
 
1014
class TestNotBzrDir(TestCaseWithTransport):
 
1015
    """Tests for using the bzrdir api with a non .bzr based disk format.
 
1016
 
 
1017
    If/when one of these is in the core, we can let the implementation tests
 
1018
    verify this works.
 
1019
    """
 
1020
 
 
1021
    def test_create_and_find_format(self):
 
1022
        # create a .notbzr dir
 
1023
        format = NotBzrDirFormat()
 
1024
        dir = format.initialize(self.get_url())
 
1025
        self.assertIsInstance(dir, NotBzrDir)
 
1026
        # now probe for it.
 
1027
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
 
1028
        try:
 
1029
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
 
1030
                get_transport(self.get_url()))
 
1031
            self.assertIsInstance(found, NotBzrDirFormat)
 
1032
        finally:
 
1033
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
 
1034
 
 
1035
    def test_included_in_known_formats(self):
 
1036
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
 
1037
        try:
 
1038
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
 
1039
            for format in formats:
 
1040
                if isinstance(format, NotBzrDirFormat):
 
1041
                    return
 
1042
            self.fail("No NotBzrDirFormat in %s" % formats)
 
1043
        finally:
 
1044
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
 
1045
 
 
1046
 
1016
1047
class NonLocalTests(TestCaseWithTransport):
1017
1048
    """Tests for bzrdir static behaviour on non local paths."""
1018
1049
 
1019
1050
    def setUp(self):
1020
1051
        super(NonLocalTests, self).setUp()
1021
 
        self.vfs_transport_factory = memory.MemoryServer
 
1052
        self.vfs_transport_factory = MemoryServer
1022
1053
 
1023
1054
    def test_create_branch_convenience(self):
1024
1055
        # outside a repo the default convenience output is a repo+branch_tree
1037
1068
            self.get_url('foo'),
1038
1069
            force_new_tree=True,
1039
1070
            format=format)
1040
 
        t = self.get_transport()
 
1071
        t = get_transport(self.get_url('.'))
1041
1072
        self.assertFalse(t.has('foo'))
1042
1073
 
1043
1074
    def test_clone(self):
1059
1090
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1060
1091
        checkout_format = my_bzrdir.checkout_metadir()
1061
1092
        self.assertIsInstance(checkout_format.workingtree_format,
1062
 
                              workingtree_4.WorkingTreeFormat4)
 
1093
                              workingtree.WorkingTreeFormat3)
1063
1094
 
1064
1095
 
1065
1096
class TestHTTPRedirections(object):
1074
1105
    """
1075
1106
 
1076
1107
    def create_transport_readonly_server(self):
1077
 
        # We don't set the http protocol version, relying on the default
1078
1108
        return http_utils.HTTPServerRedirecting()
1079
1109
 
1080
1110
    def create_transport_secondary_server(self):
1081
 
        # We don't set the http protocol version, relying on the default
1082
1111
        return http_utils.HTTPServerRedirecting()
1083
1112
 
1084
1113
    def setUp(self):
1123
1152
    _transport = HttpTransport_urllib
1124
1153
 
1125
1154
    def _qualified_url(self, host, port):
1126
 
        result = 'http+urllib://%s:%s' % (host, port)
1127
 
        self.permit_url(result)
1128
 
        return result
 
1155
        return 'http+urllib://%s:%s' % (host, port)
1129
1156
 
1130
1157
 
1131
1158
 
1135
1162
    """Tests redirections for pycurl implementation"""
1136
1163
 
1137
1164
    def _qualified_url(self, host, port):
1138
 
        result = 'http+pycurl://%s:%s' % (host, port)
1139
 
        self.permit_url(result)
1140
 
        return result
 
1165
        return 'http+pycurl://%s:%s' % (host, port)
1141
1166
 
1142
1167
 
1143
1168
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
1147
1172
    _transport = NoSmartTransportDecorator
1148
1173
 
1149
1174
    def _qualified_url(self, host, port):
1150
 
        result = 'nosmart+http://%s:%s' % (host, port)
1151
 
        self.permit_url(result)
1152
 
        return result
 
1175
        return 'nosmart+http://%s:%s' % (host, port)
1153
1176
 
1154
1177
 
1155
1178
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1159
1182
    _transport = ReadonlyTransportDecorator
1160
1183
 
1161
1184
    def _qualified_url(self, host, port):
1162
 
        result = 'readonly+http://%s:%s' % (host, port)
1163
 
        self.permit_url(result)
1164
 
        return result
 
1185
        return 'readonly+http://%s:%s' % (host, port)
1165
1186
 
1166
1187
 
1167
1188
class TestDotBzrHidden(TestCaseWithTransport):
1209
1230
 
1210
1231
    def __init__(self, *args, **kwargs):
1211
1232
        super(_TestBzrDir, self).__init__(*args, **kwargs)
1212
 
        self.test_branch = _TestBranch(self.transport)
 
1233
        self.test_branch = _TestBranch()
1213
1234
        self.test_branch.repository = self.create_repository()
1214
1235
 
1215
1236
    def open_branch(self, unsupported=False):
1226
1247
class _TestBranch(bzrlib.branch.Branch):
1227
1248
    """Test Branch implementation for TestBzrDirSprout."""
1228
1249
 
1229
 
    def __init__(self, transport, *args, **kwargs):
 
1250
    def __init__(self, *args, **kwargs):
1230
1251
        self._format = _TestBranchFormat()
1231
 
        self._transport = transport
1232
 
        self.base = transport.base
1233
1252
        super(_TestBranch, self).__init__(*args, **kwargs)
1234
1253
        self.calls = []
1235
1254
        self._parent = None
1236
1255
 
1237
1256
    def sprout(self, *args, **kwargs):
1238
1257
        self.calls.append('sprout')
1239
 
        return _TestBranch(self._transport)
 
1258
        return _TestBranch()
1240
1259
 
1241
1260
    def copy_content_into(self, destination, revision_id=None):
1242
1261
        self.calls.append('copy_content_into')
1243
1262
 
1244
 
    def last_revision(self):
1245
 
        return _mod_revision.NULL_REVISION
1246
 
 
1247
1263
    def get_parent(self):
1248
1264
        return self._parent
1249
1265
 
1250
 
    def _get_config(self):
1251
 
        return config.TransportConfig(self._transport, 'branch.conf')
1252
 
 
1253
1266
    def set_parent(self, parent):
1254
1267
        self._parent = parent
1255
1268
 
1256
 
    def lock_read(self):
1257
 
        return lock.LogicalLockResult(self.unlock)
1258
 
 
1259
 
    def unlock(self):
1260
 
        return
1261
 
 
1262
1269
 
1263
1270
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1264
1271
 
1318
1325
        url = transport.base
1319
1326
        err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1320
1327
        self.assertEqual('fail', err._preformatted_string)
1321
 
 
1322
 
    def test_post_repo_init(self):
1323
 
        from bzrlib.bzrdir import RepoInitHookParams
1324
 
        calls = []
1325
 
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1326
 
            calls.append, None)
1327
 
        self.make_repository('foo')
1328
 
        self.assertLength(1, calls)
1329
 
        params = calls[0]
1330
 
        self.assertIsInstance(params, RepoInitHookParams)
1331
 
        self.assertTrue(hasattr(params, 'bzrdir'))
1332
 
        self.assertTrue(hasattr(params, 'repository'))
1333
 
 
1334
 
    def test_post_repo_init_hook_repr(self):
1335
 
        param_reprs = []
1336
 
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1337
 
            lambda params: param_reprs.append(repr(params)), None)
1338
 
        self.make_repository('foo')
1339
 
        self.assertLength(1, param_reprs)
1340
 
        param_repr = param_reprs[0]
1341
 
        self.assertStartsWith(param_repr, '<RepoInitHookParams for ')
1342
 
 
1343
 
 
1344
 
class TestGenerateBackupName(TestCaseWithMemoryTransport):
1345
 
    # FIXME: This may need to be unified with test_osutils.TestBackupNames or
1346
 
    # moved to per_bzrdir or per_transport for better coverage ?
1347
 
    # -- vila 20100909
1348
 
 
1349
 
    def setUp(self):
1350
 
        super(TestGenerateBackupName, self).setUp()
1351
 
        self._transport = self.get_transport()
1352
 
        bzrdir.BzrDir.create(self.get_url(),
1353
 
            possible_transports=[self._transport])
1354
 
        self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
1355
 
 
1356
 
    def test_deprecated_generate_backup_name(self):
1357
 
        res = self.applyDeprecated(
1358
 
                symbol_versioning.deprecated_in((2, 3, 0)),
1359
 
                self._bzrdir.generate_backup_name, 'whatever')
1360
 
 
1361
 
    def test_new(self):
1362
 
        self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
1363
 
 
1364
 
    def test_exiting(self):
1365
 
        self._transport.put_bytes("a.~1~", "some content")
1366
 
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1367