~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Jelmer Vernooij
  • Date: 2009-06-09 00:59:51 UTC
  • mto: (4443.1.1 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 4444.
  • Revision ID: jelmer@samba.org-20090609005951-apv900cdk35o2ygh
Move squashing of XML-invalid characters to XMLSerializer.

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
 
23
import os.path
 
24
from StringIO import StringIO
23
25
import subprocess
24
26
import sys
25
27
 
26
28
from bzrlib import (
27
 
    branch,
28
29
    bzrdir,
29
 
    config,
30
 
    controldir,
31
30
    errors,
32
31
    help_topics,
33
 
    lock,
34
32
    repository,
35
 
    revision as _mod_revision,
36
33
    osutils,
 
34
    symbol_versioning,
37
35
    remote,
38
 
    symbol_versioning,
39
 
    transport as _mod_transport,
40
36
    urlutils,
41
37
    win32utils,
42
 
    workingtree_3,
43
 
    workingtree_4,
 
38
    workingtree,
44
39
    )
45
40
import bzrlib.branch
46
 
from bzrlib.errors import (
47
 
    NotBranchError,
48
 
    NoColocatedBranchSupport,
49
 
    UnknownFormatError,
50
 
    UnsupportedFormatError,
51
 
    )
 
41
from bzrlib.errors import (NotBranchError,
 
42
                           UnknownFormatError,
 
43
                           UnsupportedFormatError,
 
44
                           )
52
45
from bzrlib.tests import (
53
46
    TestCase,
54
47
    TestCaseWithMemoryTransport,
55
48
    TestCaseWithTransport,
56
49
    TestSkipped,
 
50
    test_sftp_transport
57
51
    )
58
52
from bzrlib.tests import(
59
53
    http_server,
60
54
    http_utils,
61
55
    )
62
56
from bzrlib.tests.test_http import TestWithTransport_pycurl
63
 
from bzrlib.transport import (
64
 
    memory,
65
 
    pathfilter,
66
 
    )
 
57
from bzrlib.transport import get_transport
67
58
from bzrlib.transport.http._urllib import HttpTransport_urllib
 
59
from bzrlib.transport.memory import MemoryServer
68
60
from bzrlib.transport.nosmart import NoSmartTransportDecorator
69
61
from bzrlib.transport.readonly import ReadonlyTransportDecorator
70
 
from bzrlib.repofmt import knitrepo, knitpack_repo
 
62
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
71
63
 
72
64
 
73
65
class TestDefaultFormat(TestCase):
74
66
 
75
67
    def test_get_set_default_format(self):
76
68
        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())
 
69
        # default is BzrDirFormat6
 
70
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
 
71
        bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
80
72
        # creating a bzr dir should now create an instrumented dir.
81
73
        try:
82
74
            result = bzrdir.BzrDir.create('memory:///')
83
 
            self.assertIsInstance(result, SampleBzrDir)
 
75
            self.failUnless(isinstance(result, SampleBzrDir))
84
76
        finally:
85
 
            controldir.ControlDirFormat._set_default_format(old_format)
 
77
            bzrdir.BzrDirFormat._set_default_format(old_format)
86
78
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
87
79
 
88
80
 
89
 
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
90
 
    """A deprecated bzr dir format."""
91
 
 
92
 
 
93
81
class TestFormatRegistry(TestCase):
94
82
 
95
83
    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',
 
84
        my_format_registry = bzrdir.BzrDirFormatRegistry()
 
85
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
 
86
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
87
            ' repositories', deprecated=True)
 
88
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
 
89
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
 
90
        my_format_registry.register_metadir('knit',
104
91
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
105
92
            'Format using knits',
106
93
            )
107
94
        my_format_registry.set_default('knit')
108
 
        bzrdir.register_metadir(my_format_registry,
 
95
        my_format_registry.register_metadir(
109
96
            'branch6',
110
97
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
111
98
            'Experimental successor to knit.  Use at your own risk.',
112
99
            branch_format='bzrlib.branch.BzrBranchFormat6',
113
100
            experimental=True)
114
 
        bzrdir.register_metadir(my_format_registry,
 
101
        my_format_registry.register_metadir(
115
102
            'hidden format',
116
103
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
117
104
            'Experimental successor to knit.  Use at your own risk.',
118
105
            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)
 
106
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
 
107
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
108
            ' repositories', hidden=True)
 
109
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
 
110
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
 
111
            hidden=True)
124
112
        return my_format_registry
125
113
 
126
114
    def test_format_registry(self):
127
115
        my_format_registry = self.make_format_registry()
128
116
        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)
 
117
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
118
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
119
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
132
120
        my_bzrdir = my_format_registry.make_bzrdir('default')
133
121
        self.assertIsInstance(my_bzrdir.repository_format,
134
122
            knitrepo.RepositoryFormatKnit1)
147
135
                         my_format_registry.get_help('knit'))
148
136
        self.assertEqual('Format using knits',
149
137
                         my_format_registry.get_help('default'))
150
 
        self.assertEqual('Some format.  Slower and unawesome and deprecated.',
151
 
                         my_format_registry.get_help('deprecated'))
 
138
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
 
139
                         ' checkouts or shared repositories',
 
140
                         my_format_registry.get_help('weave'))
152
141
 
153
142
    def test_help_topic(self):
154
143
        topics = help_topics.HelpTopicRegistry()
160
149
        new = topics.get_detail('current-formats')
161
150
        rest = topics.get_detail('other-formats')
162
151
        experimental, deprecated = rest.split('Deprecated formats')
163
 
        self.assertContainsRe(new, 'formats-help')
 
152
        self.assertContainsRe(new, 'bzr help formats')
164
153
        self.assertContainsRe(new,
165
154
                ':knit:\n    \(native\) \(default\) Format using knits\n')
166
155
        self.assertContainsRe(experimental,
178
167
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
179
168
                          bzrdir.format_registry.get('default'))
180
169
            self.assertIs(
181
 
                repository.format_registry.get_default().__class__,
 
170
                repository.RepositoryFormat.get_default_format().__class__,
182
171
                knitrepo.RepositoryFormatKnit3)
183
172
        finally:
184
173
            bzrdir.format_registry.set_default_repository(old_default)
185
174
 
186
175
    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())
 
176
        a_registry = bzrdir.BzrDirFormatRegistry()
 
177
        a_registry.register('weave', bzrdir.BzrDirFormat6,
 
178
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
179
            ' repositories', deprecated=True)
 
180
        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
 
181
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
182
            ' repositories', deprecated=True, alias=True)
 
183
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
195
184
 
196
185
 
197
186
class SampleBranch(bzrlib.branch.Branch):
219
208
        """See BzrDir.open_repository."""
220
209
        return SampleRepository(self)
221
210
 
222
 
    def create_branch(self, name=None):
 
211
    def create_branch(self):
223
212
        """See BzrDir.create_branch."""
224
 
        if name is not None:
225
 
            raise NoColocatedBranchSupport(self)
226
213
        return SampleBranch(self)
227
214
 
228
215
    def create_workingtree(self):
254
241
        return "opened branch."
255
242
 
256
243
 
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
244
class TestBzrDirFormat(TestCaseWithTransport):
272
245
    """Tests for the BzrDirFormat facility."""
273
246
 
274
247
    def test_find_format(self):
275
248
        # is the right format object found for a branch?
276
249
        # 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()
 
250
        # this is not quite the same as
 
251
        t = get_transport(self.get_url())
286
252
        self.build_tree(["foo/", "bar/"], transport=t)
287
253
        def check_format(format, url):
288
254
            format.initialize(url)
289
 
            t = _mod_transport.get_transport(url)
 
255
            t = get_transport(url)
290
256
            found_format = bzrdir.BzrDirFormat.find_format(t)
291
 
            self.assertIsInstance(found_format, format.__class__)
292
 
        check_format(BzrDirFormatTest1(), "foo")
293
 
        check_format(BzrDirFormatTest2(), "bar")
 
257
            self.failUnless(isinstance(found_format, format.__class__))
 
258
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
259
        check_format(bzrdir.BzrDirFormat6(), "bar")
294
260
 
295
261
    def test_find_format_nothing_there(self):
296
262
        self.assertRaises(NotBranchError,
297
263
                          bzrdir.BzrDirFormat.find_format,
298
 
                          _mod_transport.get_transport('.'))
 
264
                          get_transport('.'))
299
265
 
300
266
    def test_find_format_unknown_format(self):
301
 
        t = self.get_transport()
 
267
        t = get_transport(self.get_url())
302
268
        t.mkdir('.bzr')
303
269
        t.put_bytes('.bzr/branch-format', '')
304
270
        self.assertRaises(UnknownFormatError,
305
271
                          bzrdir.BzrDirFormat.find_format,
306
 
                          _mod_transport.get_transport('.'))
 
272
                          get_transport('.'))
307
273
 
308
274
    def test_register_unregister_format(self):
309
275
        format = SampleBzrDirFormat()
311
277
        # make a bzrdir
312
278
        format.initialize(url)
313
279
        # register a format for it.
314
 
        bzrdir.BzrProber.formats.register(format.get_format_string(), format)
 
280
        bzrdir.BzrDirFormat.register_format(format)
315
281
        # which bzrdir.Open will refuse (not supported)
316
282
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
317
283
        # which bzrdir.open_containing will refuse (not supported)
318
284
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
319
285
        # but open_downlevel will work
320
 
        t = _mod_transport.get_transport(url)
 
286
        t = get_transport(url)
321
287
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
322
288
        # unregister the format
323
 
        bzrdir.BzrProber.formats.remove(format.get_format_string())
 
289
        bzrdir.BzrDirFormat.unregister_format(format)
324
290
        # now open_downlevel should fail too.
325
291
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
326
292
 
392
358
 
393
359
    def test_create_branch_convenience_root(self):
394
360
        """Creating a branch at the root of a fs should work."""
395
 
        self.vfs_transport_factory = memory.MemoryServer
 
361
        self.vfs_transport_factory = MemoryServer
396
362
        # outside a repo the default convenience output is a repo+branch_tree
397
363
        format = bzrdir.format_registry.make_bzrdir('knit')
398
364
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
503
469
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
504
470
        # Make stackable source branch with an unstackable repo format.
505
471
        source_bzrdir = self.make_bzrdir('source')
506
 
        knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
507
 
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
508
 
            source_bzrdir)
 
472
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
473
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(source_bzrdir)
509
474
        # Make a directory with a default stacking policy
510
475
        parent_bzrdir = self.make_bzrdir('parent')
511
476
        stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
604
569
 
605
570
    def setUp(self):
606
571
        super(ChrootedTests, self).setUp()
607
 
        if not self.vfs_transport_factory == memory.MemoryServer:
 
572
        if not self.vfs_transport_factory == MemoryServer:
608
573
            self.transport_readonly_server = http_server.HttpServer
609
574
 
610
575
    def local_branch_path(self, branch):
709
674
        self.assertEqual(relpath, 'baz')
710
675
 
711
676
    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')))
 
677
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
678
                          get_transport(self.get_readonly_url('')))
 
679
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
680
                          get_transport(self.get_readonly_url('g/p/q')))
718
681
        control = bzrdir.BzrDir.create(self.get_url())
719
682
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
720
 
            _mod_transport.get_transport(self.get_readonly_url('')))
 
683
            get_transport(self.get_readonly_url('')))
721
684
        self.assertEqual('', relpath)
722
685
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
723
 
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
 
686
            get_transport(self.get_readonly_url('g/p/q')))
724
687
        self.assertEqual('g/p/q', relpath)
725
688
 
726
689
    def test_open_containing_tree_or_branch(self):
770
733
        # transport pointing at bzrdir should give a bzrdir with root transport
771
734
        # set to the given transport
772
735
        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)
 
736
        transport = get_transport(self.get_url())
 
737
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
 
738
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
776
739
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
777
740
 
778
741
    def test_open_from_transport_no_bzrdir(self):
779
 
        t = self.get_transport()
780
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
742
        transport = get_transport(self.get_url())
 
743
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
744
                          transport)
781
745
 
782
746
    def test_open_from_transport_bzrdir_in_parent(self):
783
747
        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)
 
748
        transport = get_transport(self.get_url())
 
749
        transport.mkdir('subdir')
 
750
        transport = transport.clone('subdir')
 
751
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
752
                          transport)
788
753
 
789
754
    def test_sprout_recursive(self):
790
755
        tree = self.make_branch_and_tree('tree1',
799
764
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
800
765
        tree2.lock_read()
801
766
        self.addCleanup(tree2.unlock)
802
 
        self.assertPathExists('tree2/subtree/file')
 
767
        self.failUnlessExists('tree2/subtree/file')
803
768
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
804
769
 
805
770
    def test_cloning_metadir(self):
809
774
        branch = self.make_branch('branch', format='knit')
810
775
        format = branch.bzrdir.cloning_metadir()
811
776
        self.assertIsInstance(format.workingtree_format,
812
 
            workingtree_4.WorkingTreeFormat6)
 
777
            workingtree.WorkingTreeFormat3)
813
778
 
814
779
    def test_sprout_recursive_treeless(self):
815
780
        tree = self.make_branch_and_tree('tree1',
820
785
        self.build_tree(['tree1/subtree/file'])
821
786
        sub_tree.add('file')
822
787
        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
788
        tree.bzrdir.destroy_workingtree()
827
 
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
828
 
        # fail :-( ) -- vila 20100909
829
789
        repo = self.make_repository('repo', shared=True,
830
790
            format='dirstate-with-subtree')
831
791
        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')
 
792
        tree.bzrdir.sprout('repo/tree2')
 
793
        self.failUnlessExists('repo/tree2/subtree')
 
794
        self.failIfExists('repo/tree2/subtree/file')
844
795
 
845
796
    def make_foo_bar_baz(self):
846
797
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
850
801
 
851
802
    def test_find_bzrdirs(self):
852
803
        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))
 
804
        transport = get_transport(self.get_url())
 
805
        self.assertEqualBzrdirs([baz, foo, bar],
 
806
                                bzrdir.BzrDir.find_bzrdirs(transport))
887
807
 
888
808
    def test_find_bzrdirs_list_current(self):
889
809
        def list_current(transport):
890
810
            return [s for s in transport.list_dir('') if s != 'baz']
891
811
 
892
812
        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))
 
813
        transport = get_transport(self.get_url())
 
814
        self.assertEqualBzrdirs([foo, bar],
 
815
                                bzrdir.BzrDir.find_bzrdirs(transport,
 
816
                                    list_current=list_current))
 
817
 
897
818
 
898
819
    def test_find_bzrdirs_evaluate(self):
899
820
        def evaluate(bzrdir):
905
826
                return False, bzrdir.root_transport.base
906
827
 
907
828
        foo, bar, baz = self.make_foo_bar_baz()
908
 
        t = self.get_transport()
 
829
        transport = get_transport(self.get_url())
909
830
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
910
 
                         list(bzrdir.BzrDir.find_bzrdirs(t, evaluate=evaluate)))
 
831
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
 
832
                                                         evaluate=evaluate)))
911
833
 
912
834
    def assertEqualBzrdirs(self, first, second):
913
835
        first = list(first)
920
842
        root = self.make_repository('', shared=True)
921
843
        foo, bar, baz = self.make_foo_bar_baz()
922
844
        qux = self.make_bzrdir('foo/qux')
923
 
        t = self.get_transport()
924
 
        branches = bzrdir.BzrDir.find_branches(t)
 
845
        transport = get_transport(self.get_url())
 
846
        branches = bzrdir.BzrDir.find_branches(transport)
925
847
        self.assertEqual(baz.root_transport.base, branches[0].base)
926
848
        self.assertEqual(foo.root_transport.base, branches[1].base)
927
849
        self.assertEqual(bar.root_transport.base, branches[2].base)
928
850
 
929
851
        # ensure this works without a top-level repo
930
 
        branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
 
852
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
931
853
        self.assertEqual(foo.root_transport.base, branches[0].base)
932
854
        self.assertEqual(bar.root_transport.base, branches[1].base)
933
855
 
934
856
 
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
857
class TestMeta1DirFormat(TestCaseWithTransport):
951
858
    """Tests specific to the meta1 dir format."""
952
859
 
959
866
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
960
867
        repository_base = t.clone('repository').base
961
868
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
962
 
        repository_format = repository.format_registry.get_default()
963
869
        self.assertEqual(repository_base,
964
 
                         dir.get_repository_transport(repository_format).base)
 
870
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
965
871
        checkout_base = t.clone('checkout').base
966
872
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
967
873
        self.assertEqual(checkout_base,
968
 
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
 
874
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
969
875
 
970
876
    def test_meta1dir_uses_lockdir(self):
971
877
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
1013
919
        self.assertEqual(2, rpc_count)
1014
920
 
1015
921
 
 
922
class TestFormat5(TestCaseWithTransport):
 
923
    """Tests specific to the version 5 bzrdir format."""
 
924
 
 
925
    def test_same_lockfiles_between_tree_repo_branch(self):
 
926
        # this checks that only a single lockfiles instance is created
 
927
        # for format 5 objects
 
928
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
929
        def check_dir_components_use_same_lock(dir):
 
930
            ctrl_1 = dir.open_repository().control_files
 
931
            ctrl_2 = dir.open_branch().control_files
 
932
            ctrl_3 = dir.open_workingtree()._control_files
 
933
            self.assertTrue(ctrl_1 is ctrl_2)
 
934
            self.assertTrue(ctrl_2 is ctrl_3)
 
935
        check_dir_components_use_same_lock(dir)
 
936
        # and if we open it normally.
 
937
        dir = bzrdir.BzrDir.open(self.get_url())
 
938
        check_dir_components_use_same_lock(dir)
 
939
 
 
940
    def test_can_convert(self):
 
941
        # format 5 dirs are convertable
 
942
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
943
        self.assertTrue(dir.can_convert_format())
 
944
 
 
945
    def test_needs_conversion(self):
 
946
        # format 5 dirs need a conversion if they are not the default,
 
947
        # and they aren't
 
948
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
949
        # don't need to convert it to itself
 
950
        self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
 
951
        # do need to convert it to the current default
 
952
        self.assertTrue(dir.needs_format_conversion(
 
953
            bzrdir.BzrDirFormat.get_default_format()))
 
954
 
 
955
 
 
956
class TestFormat6(TestCaseWithTransport):
 
957
    """Tests specific to the version 6 bzrdir format."""
 
958
 
 
959
    def test_same_lockfiles_between_tree_repo_branch(self):
 
960
        # this checks that only a single lockfiles instance is created
 
961
        # for format 6 objects
 
962
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
963
        def check_dir_components_use_same_lock(dir):
 
964
            ctrl_1 = dir.open_repository().control_files
 
965
            ctrl_2 = dir.open_branch().control_files
 
966
            ctrl_3 = dir.open_workingtree()._control_files
 
967
            self.assertTrue(ctrl_1 is ctrl_2)
 
968
            self.assertTrue(ctrl_2 is ctrl_3)
 
969
        check_dir_components_use_same_lock(dir)
 
970
        # and if we open it normally.
 
971
        dir = bzrdir.BzrDir.open(self.get_url())
 
972
        check_dir_components_use_same_lock(dir)
 
973
 
 
974
    def test_can_convert(self):
 
975
        # format 6 dirs are convertable
 
976
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
977
        self.assertTrue(dir.can_convert_format())
 
978
 
 
979
    def test_needs_conversion(self):
 
980
        # format 6 dirs need an conversion if they are not the default.
 
981
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
982
        self.assertTrue(dir.needs_format_conversion(
 
983
            bzrdir.BzrDirFormat.get_default_format()))
 
984
 
 
985
 
 
986
class NotBzrDir(bzrlib.bzrdir.BzrDir):
 
987
    """A non .bzr based control directory."""
 
988
 
 
989
    def __init__(self, transport, format):
 
990
        self._format = format
 
991
        self.root_transport = transport
 
992
        self.transport = transport.clone('.not')
 
993
 
 
994
 
 
995
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
 
996
    """A test class representing any non-.bzr based disk format."""
 
997
 
 
998
    def initialize_on_transport(self, transport):
 
999
        """Initialize a new .not dir in the base directory of a Transport."""
 
1000
        transport.mkdir('.not')
 
1001
        return self.open(transport)
 
1002
 
 
1003
    def open(self, transport):
 
1004
        """Open this directory."""
 
1005
        return NotBzrDir(transport, self)
 
1006
 
 
1007
    @classmethod
 
1008
    def _known_formats(self):
 
1009
        return set([NotBzrDirFormat()])
 
1010
 
 
1011
    @classmethod
 
1012
    def probe_transport(self, transport):
 
1013
        """Our format is present if the transport ends in '.not/'."""
 
1014
        if transport.has('.not'):
 
1015
            return NotBzrDirFormat()
 
1016
 
 
1017
 
 
1018
class TestNotBzrDir(TestCaseWithTransport):
 
1019
    """Tests for using the bzrdir api with a non .bzr based disk format.
 
1020
 
 
1021
    If/when one of these is in the core, we can let the implementation tests
 
1022
    verify this works.
 
1023
    """
 
1024
 
 
1025
    def test_create_and_find_format(self):
 
1026
        # create a .notbzr dir
 
1027
        format = NotBzrDirFormat()
 
1028
        dir = format.initialize(self.get_url())
 
1029
        self.assertIsInstance(dir, NotBzrDir)
 
1030
        # now probe for it.
 
1031
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
 
1032
        try:
 
1033
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
 
1034
                get_transport(self.get_url()))
 
1035
            self.assertIsInstance(found, NotBzrDirFormat)
 
1036
        finally:
 
1037
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
 
1038
 
 
1039
    def test_included_in_known_formats(self):
 
1040
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
 
1041
        try:
 
1042
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
 
1043
            for format in formats:
 
1044
                if isinstance(format, NotBzrDirFormat):
 
1045
                    return
 
1046
            self.fail("No NotBzrDirFormat in %s" % formats)
 
1047
        finally:
 
1048
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
 
1049
 
 
1050
 
1016
1051
class NonLocalTests(TestCaseWithTransport):
1017
1052
    """Tests for bzrdir static behaviour on non local paths."""
1018
1053
 
1019
1054
    def setUp(self):
1020
1055
        super(NonLocalTests, self).setUp()
1021
 
        self.vfs_transport_factory = memory.MemoryServer
 
1056
        self.vfs_transport_factory = MemoryServer
1022
1057
 
1023
1058
    def test_create_branch_convenience(self):
1024
1059
        # outside a repo the default convenience output is a repo+branch_tree
1037
1072
            self.get_url('foo'),
1038
1073
            force_new_tree=True,
1039
1074
            format=format)
1040
 
        t = self.get_transport()
 
1075
        t = get_transport(self.get_url('.'))
1041
1076
        self.assertFalse(t.has('foo'))
1042
1077
 
1043
1078
    def test_clone(self):
1059
1094
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1060
1095
        checkout_format = my_bzrdir.checkout_metadir()
1061
1096
        self.assertIsInstance(checkout_format.workingtree_format,
1062
 
                              workingtree_4.WorkingTreeFormat4)
 
1097
                              workingtree.WorkingTreeFormat3)
1063
1098
 
1064
1099
 
1065
1100
class TestHTTPRedirections(object):
1074
1109
    """
1075
1110
 
1076
1111
    def create_transport_readonly_server(self):
1077
 
        # We don't set the http protocol version, relying on the default
1078
1112
        return http_utils.HTTPServerRedirecting()
1079
1113
 
1080
1114
    def create_transport_secondary_server(self):
1081
 
        # We don't set the http protocol version, relying on the default
1082
1115
        return http_utils.HTTPServerRedirecting()
1083
1116
 
1084
1117
    def setUp(self):
1123
1156
    _transport = HttpTransport_urllib
1124
1157
 
1125
1158
    def _qualified_url(self, host, port):
1126
 
        result = 'http+urllib://%s:%s' % (host, port)
1127
 
        self.permit_url(result)
1128
 
        return result
 
1159
        return 'http+urllib://%s:%s' % (host, port)
1129
1160
 
1130
1161
 
1131
1162
 
1135
1166
    """Tests redirections for pycurl implementation"""
1136
1167
 
1137
1168
    def _qualified_url(self, host, port):
1138
 
        result = 'http+pycurl://%s:%s' % (host, port)
1139
 
        self.permit_url(result)
1140
 
        return result
 
1169
        return 'http+pycurl://%s:%s' % (host, port)
1141
1170
 
1142
1171
 
1143
1172
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
1147
1176
    _transport = NoSmartTransportDecorator
1148
1177
 
1149
1178
    def _qualified_url(self, host, port):
1150
 
        result = 'nosmart+http://%s:%s' % (host, port)
1151
 
        self.permit_url(result)
1152
 
        return result
 
1179
        return 'nosmart+http://%s:%s' % (host, port)
1153
1180
 
1154
1181
 
1155
1182
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1159
1186
    _transport = ReadonlyTransportDecorator
1160
1187
 
1161
1188
    def _qualified_url(self, host, port):
1162
 
        result = 'readonly+http://%s:%s' % (host, port)
1163
 
        self.permit_url(result)
1164
 
        return result
 
1189
        return 'readonly+http://%s:%s' % (host, port)
1165
1190
 
1166
1191
 
1167
1192
class TestDotBzrHidden(TestCaseWithTransport):
1209
1234
 
1210
1235
    def __init__(self, *args, **kwargs):
1211
1236
        super(_TestBzrDir, self).__init__(*args, **kwargs)
1212
 
        self.test_branch = _TestBranch(self.transport)
 
1237
        self.test_branch = _TestBranch()
1213
1238
        self.test_branch.repository = self.create_repository()
1214
1239
 
1215
1240
    def open_branch(self, unsupported=False):
1226
1251
class _TestBranch(bzrlib.branch.Branch):
1227
1252
    """Test Branch implementation for TestBzrDirSprout."""
1228
1253
 
1229
 
    def __init__(self, transport, *args, **kwargs):
 
1254
    def __init__(self, *args, **kwargs):
1230
1255
        self._format = _TestBranchFormat()
1231
 
        self._transport = transport
1232
 
        self.base = transport.base
1233
1256
        super(_TestBranch, self).__init__(*args, **kwargs)
1234
1257
        self.calls = []
1235
1258
        self._parent = None
1236
1259
 
1237
1260
    def sprout(self, *args, **kwargs):
1238
1261
        self.calls.append('sprout')
1239
 
        return _TestBranch(self._transport)
 
1262
        return _TestBranch()
1240
1263
 
1241
1264
    def copy_content_into(self, destination, revision_id=None):
1242
1265
        self.calls.append('copy_content_into')
1243
1266
 
1244
 
    def last_revision(self):
1245
 
        return _mod_revision.NULL_REVISION
1246
 
 
1247
1267
    def get_parent(self):
1248
1268
        return self._parent
1249
1269
 
1250
 
    def _get_config(self):
1251
 
        return config.TransportConfig(self._transport, 'branch.conf')
1252
 
 
1253
1270
    def set_parent(self, parent):
1254
1271
        self._parent = parent
1255
1272
 
1256
 
    def lock_read(self):
1257
 
        return lock.LogicalLockResult(self.unlock)
1258
 
 
1259
 
    def unlock(self):
1260
 
        return
1261
 
 
1262
1273
 
1263
1274
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1264
1275
 
1318
1329
        url = transport.base
1319
1330
        err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1320
1331
        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