/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2013, 2016 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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
23
23
import subprocess
24
24
import sys
25
25
 
26
 
from .. import (
 
26
from bzrlib import (
27
27
    branch,
28
 
    bzr,
29
 
    config,
30
 
    controldir,
 
28
    bzrdir,
31
29
    errors,
32
30
    help_topics,
33
 
    lock,
34
31
    repository,
35
 
    revision as _mod_revision,
36
32
    osutils,
37
 
    transport as _mod_transport,
 
33
    remote,
38
34
    urlutils,
39
35
    win32utils,
40
 
    )
41
 
from ..bzr import (
42
 
    branch as bzrbranch,
43
 
    bzrdir,
44
 
    remote,
45
 
    workingtree_3,
46
 
    workingtree_4,
47
 
    )
48
 
import breezy.branch
49
 
import breezy.bzr.branch
50
 
from ..bzr.fullhistory import BzrBranchFormat5
51
 
from ..errors import (
52
 
    NotBranchError,
53
 
    NoColocatedBranchSupport,
54
 
    UnknownFormatError,
55
 
    UnsupportedFormatError,
56
 
    )
57
 
from . import (
 
36
    workingtree,
 
37
    )
 
38
import bzrlib.branch
 
39
from bzrlib.errors import (NotBranchError,
 
40
                           NoColocatedBranchSupport,
 
41
                           UnknownFormatError,
 
42
                           UnsupportedFormatError,
 
43
                           )
 
44
from bzrlib.tests import (
58
45
    TestCase,
59
46
    TestCaseWithMemoryTransport,
60
47
    TestCaseWithTransport,
61
48
    TestSkipped,
62
49
    )
63
 
from . import (
 
50
from bzrlib.tests import(
64
51
    http_server,
65
52
    http_utils,
66
53
    )
67
 
from ..transport import (
 
54
from bzrlib.tests.test_http import TestWithTransport_pycurl
 
55
from bzrlib.transport import (
 
56
    get_transport,
68
57
    memory,
69
58
    pathfilter,
70
59
    )
71
 
from ..transport.http import HttpTransport
72
 
from ..transport.nosmart import NoSmartTransportDecorator
73
 
from ..transport.readonly import ReadonlyTransportDecorator
74
 
from ..bzr import knitrepo, knitpack_repo
 
60
from bzrlib.transport.http._urllib import HttpTransport_urllib
 
61
from bzrlib.transport.nosmart import NoSmartTransportDecorator
 
62
from bzrlib.transport.readonly import ReadonlyTransportDecorator
 
63
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
75
64
 
76
65
 
77
66
class TestDefaultFormat(TestCase):
78
67
 
79
68
    def test_get_set_default_format(self):
80
69
        old_format = bzrdir.BzrDirFormat.get_default_format()
81
 
        # default is BzrDirMetaFormat1
82
 
        self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
83
 
        controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
 
70
        # default is BzrDirFormat6
 
71
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
 
72
        bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
84
73
        # creating a bzr dir should now create an instrumented dir.
85
74
        try:
86
75
            result = bzrdir.BzrDir.create('memory:///')
87
 
            self.assertIsInstance(result, SampleBzrDir)
 
76
            self.failUnless(isinstance(result, SampleBzrDir))
88
77
        finally:
89
 
            controldir.ControlDirFormat._set_default_format(old_format)
 
78
            bzrdir.BzrDirFormat._set_default_format(old_format)
90
79
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
91
80
 
92
81
 
93
 
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
94
 
    """A deprecated bzr dir format."""
95
 
 
96
 
 
97
82
class TestFormatRegistry(TestCase):
98
83
 
99
84
    def make_format_registry(self):
100
 
        my_format_registry = controldir.ControlDirFormatRegistry()
101
 
        my_format_registry.register('deprecated', DeprecatedBzrDirFormat,
102
 
                                    'Some format.  Slower and unawesome and deprecated.',
103
 
                                    deprecated=True)
104
 
        my_format_registry.register_lazy('lazy', 'breezy.tests.test_bzrdir',
105
 
                                         'DeprecatedBzrDirFormat', 'Format registered lazily',
106
 
                                         deprecated=True)
107
 
        bzr.register_metadir(my_format_registry, 'knit',
108
 
                             'breezy.bzr.knitrepo.RepositoryFormatKnit1',
109
 
                             'Format using knits',
110
 
                             )
 
85
        my_format_registry = bzrdir.BzrDirFormatRegistry()
 
86
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
 
87
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
88
            ' repositories', deprecated=True)
 
89
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
 
90
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
 
91
        my_format_registry.register_metadir('knit',
 
92
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
 
93
            'Format using knits',
 
94
            )
111
95
        my_format_registry.set_default('knit')
112
 
        bzr.register_metadir(my_format_registry,
113
 
                             'branch6',
114
 
                             'breezy.bzr.knitrepo.RepositoryFormatKnit3',
115
 
                             'Experimental successor to knit.  Use at your own risk.',
116
 
                             branch_format='breezy.bzr.branch.BzrBranchFormat6',
117
 
                             experimental=True)
118
 
        bzr.register_metadir(my_format_registry,
119
 
                             'hidden format',
120
 
                             'breezy.bzr.knitrepo.RepositoryFormatKnit3',
121
 
                             'Experimental successor to knit.  Use at your own risk.',
122
 
                             branch_format='breezy.bzr.branch.BzrBranchFormat6', hidden=True)
123
 
        my_format_registry.register('hiddendeprecated', DeprecatedBzrDirFormat,
124
 
                                    'Old format.  Slower and does not support things. ', hidden=True)
125
 
        my_format_registry.register_lazy('hiddenlazy', 'breezy.tests.test_bzrdir',
126
 
                                         'DeprecatedBzrDirFormat', 'Format registered lazily',
127
 
                                         deprecated=True, hidden=True)
 
96
        my_format_registry.register_metadir(
 
97
            'branch6',
 
98
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
 
99
            'Experimental successor to knit.  Use at your own risk.',
 
100
            branch_format='bzrlib.branch.BzrBranchFormat6',
 
101
            experimental=True)
 
102
        my_format_registry.register_metadir(
 
103
            'hidden format',
 
104
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
 
105
            'Experimental successor to knit.  Use at your own risk.',
 
106
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
 
107
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
 
108
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
109
            ' repositories', hidden=True)
 
110
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
 
111
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
 
112
            hidden=True)
128
113
        return my_format_registry
129
114
 
130
115
    def test_format_registry(self):
131
116
        my_format_registry = self.make_format_registry()
132
 
        my_bzrdir = my_format_registry.make_controldir('lazy')
133
 
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
134
 
        my_bzrdir = my_format_registry.make_controldir('deprecated')
135
 
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
136
 
        my_bzrdir = my_format_registry.make_controldir('default')
137
 
        self.assertIsInstance(my_bzrdir.repository_format,
138
 
                              knitrepo.RepositoryFormatKnit1)
139
 
        my_bzrdir = my_format_registry.make_controldir('knit')
140
 
        self.assertIsInstance(my_bzrdir.repository_format,
141
 
                              knitrepo.RepositoryFormatKnit1)
142
 
        my_bzrdir = my_format_registry.make_controldir('branch6')
 
117
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
 
118
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
119
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
120
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
121
        my_bzrdir = my_format_registry.make_bzrdir('default')
 
122
        self.assertIsInstance(my_bzrdir.repository_format,
 
123
            knitrepo.RepositoryFormatKnit1)
 
124
        my_bzrdir = my_format_registry.make_bzrdir('knit')
 
125
        self.assertIsInstance(my_bzrdir.repository_format,
 
126
            knitrepo.RepositoryFormatKnit1)
 
127
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
143
128
        self.assertIsInstance(my_bzrdir.get_branch_format(),
144
 
                              breezy.bzr.branch.BzrBranchFormat6)
 
129
                              bzrlib.branch.BzrBranchFormat6)
145
130
 
146
131
    def test_get_help(self):
147
132
        my_format_registry = self.make_format_registry()
151
136
                         my_format_registry.get_help('knit'))
152
137
        self.assertEqual('Format using knits',
153
138
                         my_format_registry.get_help('default'))
154
 
        self.assertEqual('Some format.  Slower and unawesome and deprecated.',
155
 
                         my_format_registry.get_help('deprecated'))
 
139
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
 
140
                         ' checkouts or shared repositories',
 
141
                         my_format_registry.get_help('weave'))
156
142
 
157
143
    def test_help_topic(self):
158
144
        topics = help_topics.HelpTopicRegistry()
166
152
        experimental, deprecated = rest.split('Deprecated formats')
167
153
        self.assertContainsRe(new, 'formats-help')
168
154
        self.assertContainsRe(new,
169
 
                              ':knit:\n    \\(native\\) \\(default\\) Format using knits\n')
 
155
                ':knit:\n    \(native\) \(default\) Format using knits\n')
170
156
        self.assertContainsRe(experimental,
171
 
                              ':branch6:\n    \\(native\\) Experimental successor to knit')
 
157
                ':branch6:\n    \(native\) Experimental successor to knit')
172
158
        self.assertContainsRe(deprecated,
173
 
                              ':lazy:\n    \\(native\\) Format registered lazily\n')
 
159
                ':lazy:\n    \(native\) Format registered lazily\n')
174
160
        self.assertNotContainsRe(new, 'hidden')
175
161
 
176
162
    def test_set_default_repository(self):
177
 
        default_factory = controldir.format_registry.get('default')
178
 
        old_default = [k for k, v in controldir.format_registry.iteritems()
 
163
        default_factory = bzrdir.format_registry.get('default')
 
164
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
179
165
                       if v == default_factory and k != 'default'][0]
180
 
        controldir.format_registry.set_default_repository(
181
 
            'dirstate-with-subtree')
 
166
        bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
182
167
        try:
183
 
            self.assertIs(controldir.format_registry.get('dirstate-with-subtree'),
184
 
                          controldir.format_registry.get('default'))
 
168
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
 
169
                          bzrdir.format_registry.get('default'))
185
170
            self.assertIs(
186
 
                repository.format_registry.get_default().__class__,
 
171
                repository.RepositoryFormat.get_default_format().__class__,
187
172
                knitrepo.RepositoryFormatKnit3)
188
173
        finally:
189
 
            controldir.format_registry.set_default_repository(old_default)
 
174
            bzrdir.format_registry.set_default_repository(old_default)
190
175
 
191
176
    def test_aliases(self):
192
 
        a_registry = controldir.ControlDirFormatRegistry()
193
 
        a_registry.register('deprecated', DeprecatedBzrDirFormat,
194
 
                            'Old format.  Slower and does not support stuff',
195
 
                            deprecated=True)
196
 
        a_registry.register_alias('deprecatedalias', 'deprecated')
197
 
        self.assertEqual({'deprecatedalias': 'deprecated'},
198
 
                         a_registry.aliases())
199
 
 
200
 
 
201
 
class SampleBranch(breezy.branch.Branch):
 
177
        a_registry = bzrdir.BzrDirFormatRegistry()
 
178
        a_registry.register('weave', bzrdir.BzrDirFormat6,
 
179
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
180
            ' repositories', deprecated=True)
 
181
        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
 
182
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
183
            ' repositories', deprecated=True, alias=True)
 
184
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
 
185
 
 
186
 
 
187
class SampleBranch(bzrlib.branch.Branch):
202
188
    """A dummy branch for guess what, dummy use."""
203
189
 
204
190
    def __init__(self, dir):
205
 
        self.controldir = dir
206
 
 
207
 
 
208
 
class SampleRepository(breezy.repository.Repository):
 
191
        self.bzrdir = dir
 
192
 
 
193
 
 
194
class SampleRepository(bzrlib.repository.Repository):
209
195
    """A dummy repo."""
210
196
 
211
197
    def __init__(self, dir):
212
 
        self.controldir = dir
 
198
        self.bzrdir = dir
213
199
 
214
200
 
215
201
class SampleBzrDir(bzrdir.BzrDir):
216
202
    """A sample BzrDir implementation to allow testing static methods."""
217
203
 
218
204
    def create_repository(self, shared=False):
219
 
        """See ControlDir.create_repository."""
 
205
        """See BzrDir.create_repository."""
220
206
        return "A repository"
221
207
 
222
208
    def open_repository(self):
223
 
        """See ControlDir.open_repository."""
 
209
        """See BzrDir.open_repository."""
224
210
        return SampleRepository(self)
225
211
 
226
212
    def create_branch(self, name=None):
227
 
        """See ControlDir.create_branch."""
 
213
        """See BzrDir.create_branch."""
228
214
        if name is not None:
229
215
            raise NoColocatedBranchSupport(self)
230
216
        return SampleBranch(self)
231
217
 
232
218
    def create_workingtree(self):
233
 
        """See ControlDir.create_workingtree."""
 
219
        """See BzrDir.create_workingtree."""
234
220
        return "A tree"
235
221
 
236
222
 
243
229
 
244
230
    def get_format_string(self):
245
231
        """See BzrDirFormat.get_format_string()."""
246
 
        return b"Sample .bzr dir format."
 
232
        return "Sample .bzr dir format."
247
233
 
248
234
    def initialize_on_transport(self, t):
249
235
        """Create a bzr dir."""
257
243
    def open(self, transport, _found=None):
258
244
        return "opened branch."
259
245
 
260
 
    @classmethod
261
 
    def from_string(cls, format_string):
262
 
        return cls()
263
 
 
264
 
 
265
 
class BzrDirFormatTest1(bzrdir.BzrDirMetaFormat1):
266
 
 
267
 
    @staticmethod
268
 
    def get_format_string():
269
 
        return b"Test format 1"
270
 
 
271
 
 
272
 
class BzrDirFormatTest2(bzrdir.BzrDirMetaFormat1):
273
 
 
274
 
    @staticmethod
275
 
    def get_format_string():
276
 
        return b"Test format 2"
277
 
 
278
246
 
279
247
class TestBzrDirFormat(TestCaseWithTransport):
280
248
    """Tests for the BzrDirFormat facility."""
282
250
    def test_find_format(self):
283
251
        # is the right format object found for a branch?
284
252
        # create a branch with a few known format objects.
285
 
        bzr.BzrProber.formats.register(BzrDirFormatTest1.get_format_string(),
286
 
                                       BzrDirFormatTest1())
287
 
        self.addCleanup(bzr.BzrProber.formats.remove,
288
 
                        BzrDirFormatTest1.get_format_string())
289
 
        bzr.BzrProber.formats.register(BzrDirFormatTest2.get_format_string(),
290
 
                                       BzrDirFormatTest2())
291
 
        self.addCleanup(bzr.BzrProber.formats.remove,
292
 
                        BzrDirFormatTest2.get_format_string())
293
 
        t = self.get_transport()
 
253
        # this is not quite the same as
 
254
        t = get_transport(self.get_url())
294
255
        self.build_tree(["foo/", "bar/"], transport=t)
295
 
 
296
256
        def check_format(format, url):
297
257
            format.initialize(url)
298
 
            t = _mod_transport.get_transport_from_path(url)
 
258
            t = get_transport(url)
299
259
            found_format = bzrdir.BzrDirFormat.find_format(t)
300
 
            self.assertIsInstance(found_format, format.__class__)
301
 
        check_format(BzrDirFormatTest1(), "foo")
302
 
        check_format(BzrDirFormatTest2(), "bar")
 
260
            self.failUnless(isinstance(found_format, format.__class__))
 
261
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
262
        check_format(bzrdir.BzrDirFormat6(), "bar")
303
263
 
304
264
    def test_find_format_nothing_there(self):
305
265
        self.assertRaises(NotBranchError,
306
266
                          bzrdir.BzrDirFormat.find_format,
307
 
                          _mod_transport.get_transport_from_path('.'))
 
267
                          get_transport('.'))
308
268
 
309
269
    def test_find_format_unknown_format(self):
310
 
        t = self.get_transport()
 
270
        t = get_transport(self.get_url())
311
271
        t.mkdir('.bzr')
312
 
        t.put_bytes('.bzr/branch-format', b'')
 
272
        t.put_bytes('.bzr/branch-format', '')
313
273
        self.assertRaises(UnknownFormatError,
314
274
                          bzrdir.BzrDirFormat.find_format,
315
 
                          _mod_transport.get_transport_from_path('.'))
316
 
 
317
 
    def test_find_format_line_endings(self):
318
 
        t = self.get_transport()
319
 
        t.mkdir('.bzr')
320
 
        t.put_bytes('.bzr/branch-format', b'Corrupt line endings\r\n')
321
 
        self.assertRaises(errors.LineEndingError,
322
 
                          bzrdir.BzrDirFormat.find_format,
323
 
                          _mod_transport.get_transport_from_path('.'))
 
275
                          get_transport('.'))
324
276
 
325
277
    def test_register_unregister_format(self):
326
278
        format = SampleBzrDirFormat()
328
280
        # make a bzrdir
329
281
        format.initialize(url)
330
282
        # register a format for it.
331
 
        bzr.BzrProber.formats.register(format.get_format_string(), format)
 
283
        bzrdir.BzrDirFormat.register_format(format)
332
284
        # which bzrdir.Open will refuse (not supported)
333
285
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
334
286
        # which bzrdir.open_containing will refuse (not supported)
335
 
        self.assertRaises(UnsupportedFormatError,
336
 
                          bzrdir.BzrDir.open_containing, url)
 
287
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
337
288
        # but open_downlevel will work
338
 
        t = _mod_transport.get_transport_from_url(url)
 
289
        t = get_transport(url)
339
290
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
340
291
        # unregister the format
341
 
        bzr.BzrProber.formats.remove(format.get_format_string())
 
292
        bzrdir.BzrDirFormat.unregister_format(format)
342
293
        # now open_downlevel should fail too.
343
 
        self.assertRaises(UnknownFormatError,
344
 
                          bzrdir.BzrDir.open_unsupported, url)
 
294
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
345
295
 
346
296
    def test_create_branch_and_repo_uses_default(self):
347
297
        format = SampleBzrDirFormat()
352
302
    def test_create_branch_and_repo_under_shared(self):
353
303
        # creating a branch and repo in a shared repo uses the
354
304
        # shared repository
355
 
        format = controldir.format_registry.make_controldir('knit')
 
305
        format = bzrdir.format_registry.make_bzrdir('knit')
356
306
        self.make_repository('.', shared=True, format=format)
357
307
        branch = bzrdir.BzrDir.create_branch_and_repo(
358
308
            self.get_url('child'), format=format)
359
309
        self.assertRaises(errors.NoRepositoryPresent,
360
 
                          branch.controldir.open_repository)
 
310
                          branch.bzrdir.open_repository)
361
311
 
362
312
    def test_create_branch_and_repo_under_shared_force_new(self):
363
313
        # creating a branch and repo in a shared repo can be forced to
364
314
        # make a new repo
365
 
        format = controldir.format_registry.make_controldir('knit')
 
315
        format = bzrdir.format_registry.make_bzrdir('knit')
366
316
        self.make_repository('.', shared=True, format=format)
367
317
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
368
318
                                                      force_new_repo=True,
369
319
                                                      format=format)
370
 
        branch.controldir.open_repository()
 
320
        branch.bzrdir.open_repository()
371
321
 
372
322
    def test_create_standalone_working_tree(self):
373
323
        format = SampleBzrDirFormat()
382
332
 
383
333
    def test_create_standalone_working_tree_under_shared_repo(self):
384
334
        # create standalone working tree always makes a repo.
385
 
        format = controldir.format_registry.make_controldir('knit')
 
335
        format = bzrdir.format_registry.make_bzrdir('knit')
386
336
        self.make_repository('.', shared=True, format=format)
387
337
        # note this is deliberately readonly, as this failure should
388
338
        # occur before any writes.
390
340
                          bzrdir.BzrDir.create_standalone_workingtree,
391
341
                          self.get_readonly_url('child'), format=format)
392
342
        tree = bzrdir.BzrDir.create_standalone_workingtree('child',
393
 
                                                           format=format)
394
 
        tree.controldir.open_repository()
 
343
            format=format)
 
344
        tree.bzrdir.open_repository()
395
345
 
396
346
    def test_create_branch_convenience(self):
397
347
        # outside a repo the default convenience output is a repo+branch_tree
398
 
        format = controldir.format_registry.make_controldir('knit')
 
348
        format = bzrdir.format_registry.make_bzrdir('knit')
399
349
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
400
 
        branch.controldir.open_workingtree()
401
 
        branch.controldir.open_repository()
 
350
        branch.bzrdir.open_workingtree()
 
351
        branch.bzrdir.open_repository()
402
352
 
403
353
    def test_create_branch_convenience_possible_transports(self):
404
354
        """Check that the optional 'possible_transports' is recognized"""
405
 
        format = controldir.format_registry.make_controldir('knit')
 
355
        format = bzrdir.format_registry.make_bzrdir('knit')
406
356
        t = self.get_transport()
407
357
        branch = bzrdir.BzrDir.create_branch_convenience(
408
358
            '.', format=format, possible_transports=[t])
409
 
        branch.controldir.open_workingtree()
410
 
        branch.controldir.open_repository()
 
359
        branch.bzrdir.open_workingtree()
 
360
        branch.bzrdir.open_repository()
411
361
 
412
362
    def test_create_branch_convenience_root(self):
413
363
        """Creating a branch at the root of a fs should work."""
414
364
        self.vfs_transport_factory = memory.MemoryServer
415
365
        # outside a repo the default convenience output is a repo+branch_tree
416
 
        format = controldir.format_registry.make_controldir('knit')
 
366
        format = bzrdir.format_registry.make_bzrdir('knit')
417
367
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
418
368
                                                         format=format)
419
369
        self.assertRaises(errors.NoWorkingTree,
420
 
                          branch.controldir.open_workingtree)
421
 
        branch.controldir.open_repository()
 
370
                          branch.bzrdir.open_workingtree)
 
371
        branch.bzrdir.open_repository()
422
372
 
423
373
    def test_create_branch_convenience_under_shared_repo(self):
424
374
        # inside a repo the default convenience output is a branch+ follow the
425
375
        # repo tree policy
426
 
        format = controldir.format_registry.make_controldir('knit')
 
376
        format = bzrdir.format_registry.make_bzrdir('knit')
427
377
        self.make_repository('.', shared=True, format=format)
428
378
        branch = bzrdir.BzrDir.create_branch_convenience('child',
429
 
                                                         format=format)
430
 
        branch.controldir.open_workingtree()
 
379
            format=format)
 
380
        branch.bzrdir.open_workingtree()
431
381
        self.assertRaises(errors.NoRepositoryPresent,
432
 
                          branch.controldir.open_repository)
 
382
                          branch.bzrdir.open_repository)
433
383
 
434
384
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
435
385
        # inside a repo the default convenience output is a branch+ follow the
436
386
        # repo tree policy but we can override that
437
 
        format = controldir.format_registry.make_controldir('knit')
 
387
        format = bzrdir.format_registry.make_bzrdir('knit')
438
388
        self.make_repository('.', shared=True, format=format)
439
389
        branch = bzrdir.BzrDir.create_branch_convenience('child',
440
 
                                                         force_new_tree=False, format=format)
 
390
            force_new_tree=False, format=format)
441
391
        self.assertRaises(errors.NoWorkingTree,
442
 
                          branch.controldir.open_workingtree)
 
392
                          branch.bzrdir.open_workingtree)
443
393
        self.assertRaises(errors.NoRepositoryPresent,
444
 
                          branch.controldir.open_repository)
 
394
                          branch.bzrdir.open_repository)
445
395
 
446
396
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
447
397
        # inside a repo the default convenience output is a branch+ follow the
448
398
        # repo tree policy
449
 
        format = controldir.format_registry.make_controldir('knit')
 
399
        format = bzrdir.format_registry.make_bzrdir('knit')
450
400
        repo = self.make_repository('.', shared=True, format=format)
451
401
        repo.set_make_working_trees(False)
452
402
        branch = bzrdir.BzrDir.create_branch_convenience('child',
453
403
                                                         format=format)
454
404
        self.assertRaises(errors.NoWorkingTree,
455
 
                          branch.controldir.open_workingtree)
 
405
                          branch.bzrdir.open_workingtree)
456
406
        self.assertRaises(errors.NoRepositoryPresent,
457
 
                          branch.controldir.open_repository)
 
407
                          branch.bzrdir.open_repository)
458
408
 
459
409
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
460
410
        # inside a repo the default convenience output is a branch+ follow the
461
411
        # repo tree policy but we can override that
462
 
        format = controldir.format_registry.make_controldir('knit')
 
412
        format = bzrdir.format_registry.make_bzrdir('knit')
463
413
        repo = self.make_repository('.', shared=True, format=format)
464
414
        repo.set_make_working_trees(False)
465
415
        branch = bzrdir.BzrDir.create_branch_convenience('child',
466
 
                                                         force_new_tree=True, format=format)
467
 
        branch.controldir.open_workingtree()
 
416
            force_new_tree=True, format=format)
 
417
        branch.bzrdir.open_workingtree()
468
418
        self.assertRaises(errors.NoRepositoryPresent,
469
 
                          branch.controldir.open_repository)
 
419
                          branch.bzrdir.open_repository)
470
420
 
471
421
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
472
422
        # inside a repo the default convenience output is overridable to give
473
423
        # repo+branch+tree
474
 
        format = controldir.format_registry.make_controldir('knit')
 
424
        format = bzrdir.format_registry.make_bzrdir('knit')
475
425
        self.make_repository('.', shared=True, format=format)
476
426
        branch = bzrdir.BzrDir.create_branch_convenience('child',
477
 
                                                         force_new_repo=True, format=format)
478
 
        branch.controldir.open_repository()
479
 
        branch.controldir.open_workingtree()
 
427
            force_new_repo=True, format=format)
 
428
        branch.bzrdir.open_repository()
 
429
        branch.bzrdir.open_workingtree()
480
430
 
481
431
 
482
432
class TestRepositoryAcquisitionPolicy(TestCaseWithTransport):
483
433
 
484
434
    def test_acquire_repository_standalone(self):
485
435
        """The default acquisition policy should create a standalone branch."""
486
 
        my_bzrdir = self.make_controldir('.')
 
436
        my_bzrdir = self.make_bzrdir('.')
487
437
        repo_policy = my_bzrdir.determine_repository_policy()
488
438
        repo, is_new = repo_policy.acquire_repository()
489
 
        self.assertEqual(repo.controldir.root_transport.base,
 
439
        self.assertEqual(repo.bzrdir.root_transport.base,
490
440
                         my_bzrdir.root_transport.base)
491
441
        self.assertFalse(repo.is_shared())
492
442
 
493
443
    def test_determine_stacking_policy(self):
494
 
        parent_bzrdir = self.make_controldir('.')
495
 
        child_bzrdir = self.make_controldir('child')
 
444
        parent_bzrdir = self.make_bzrdir('.')
 
445
        child_bzrdir = self.make_bzrdir('child')
496
446
        parent_bzrdir.get_config().set_default_stack_on('http://example.org')
497
447
        repo_policy = child_bzrdir.determine_repository_policy()
498
448
        self.assertEqual('http://example.org', repo_policy._stack_on)
499
449
 
500
450
    def test_determine_stacking_policy_relative(self):
501
 
        parent_bzrdir = self.make_controldir('.')
502
 
        child_bzrdir = self.make_controldir('child')
 
451
        parent_bzrdir = self.make_bzrdir('.')
 
452
        child_bzrdir = self.make_bzrdir('child')
503
453
        parent_bzrdir.get_config().set_default_stack_on('child2')
504
454
        repo_policy = child_bzrdir.determine_repository_policy()
505
455
        self.assertEqual('child2', repo_policy._stack_on)
507
457
                         repo_policy._stack_on_pwd)
508
458
 
509
459
    def prepare_default_stacking(self, child_format='1.6'):
510
 
        parent_bzrdir = self.make_controldir('.')
 
460
        parent_bzrdir = self.make_bzrdir('.')
511
461
        child_branch = self.make_branch('child', format=child_format)
512
462
        parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
513
463
        new_child_transport = parent_bzrdir.transport.clone('child2')
515
465
 
516
466
    def test_clone_on_transport_obeys_stacking_policy(self):
517
467
        child_branch, new_child_transport = self.prepare_default_stacking()
518
 
        new_child = child_branch.controldir.clone_on_transport(
519
 
            new_child_transport)
 
468
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
520
469
        self.assertEqual(child_branch.base,
521
470
                         new_child.open_branch().get_stacked_on_url())
522
471
 
523
472
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
524
473
        # Make stackable source branch with an unstackable repo format.
525
 
        source_bzrdir = self.make_controldir('source')
526
 
        knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
527
 
        source_branch = breezy.bzr.branch.BzrBranchFormat7().initialize(
 
474
        source_bzrdir = self.make_bzrdir('source')
 
475
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
476
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
528
477
            source_bzrdir)
529
478
        # Make a directory with a default stacking policy
530
 
        parent_bzrdir = self.make_controldir('parent')
 
479
        parent_bzrdir = self.make_bzrdir('parent')
531
480
        stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
532
481
        parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
533
482
        # Clone source into directory
534
483
        target = source_bzrdir.clone(self.get_url('parent/target'))
535
484
 
536
 
    def test_format_initialize_on_transport_ex_stacked_on(self):
537
 
        # trunk is a stackable format.  Note that its in the same server area
538
 
        # which is what launchpad does, but not sufficient to exercise the
539
 
        # general case.
540
 
        trunk = self.make_branch('trunk', format='1.9')
541
 
        t = self.get_transport('stacked')
542
 
        old_fmt = controldir.format_registry.make_controldir('pack-0.92')
543
 
        repo_name = old_fmt.repository_format.network_name()
544
 
        # Should end up with a 1.9 format (stackable)
545
 
        repo, control, require_stacking, repo_policy = \
546
 
            old_fmt.initialize_on_transport_ex(t,
547
 
                                               repo_format_name=repo_name, stacked_on='../trunk',
548
 
                                               stack_on_pwd=t.base)
549
 
        if repo is not None:
550
 
            # Repositories are open write-locked
551
 
            self.assertTrue(repo.is_write_locked())
552
 
            self.addCleanup(repo.unlock)
553
 
        else:
554
 
            repo = control.open_repository()
555
 
        self.assertIsInstance(control, bzrdir.BzrDir)
556
 
        opened = bzrdir.BzrDir.open(t.base)
557
 
        if not isinstance(old_fmt, remote.RemoteBzrDirFormat):
558
 
            self.assertEqual(control._format.network_name(),
559
 
                             old_fmt.network_name())
560
 
            self.assertEqual(control._format.network_name(),
561
 
                             opened._format.network_name())
562
 
        self.assertEqual(control.__class__, opened.__class__)
563
 
        self.assertLength(1, repo._fallback_repositories)
564
 
 
565
485
    def test_sprout_obeys_stacking_policy(self):
566
486
        child_branch, new_child_transport = self.prepare_default_stacking()
567
 
        new_child = child_branch.controldir.sprout(new_child_transport.base)
 
487
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
568
488
        self.assertEqual(child_branch.base,
569
489
                         new_child.open_branch().get_stacked_on_url())
570
490
 
571
491
    def test_clone_ignores_policy_for_unsupported_formats(self):
572
492
        child_branch, new_child_transport = self.prepare_default_stacking(
573
493
            child_format='pack-0.92')
574
 
        new_child = child_branch.controldir.clone_on_transport(
575
 
            new_child_transport)
576
 
        self.assertRaises(branch.UnstackableBranchFormat,
 
494
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
 
495
        self.assertRaises(errors.UnstackableBranchFormat,
577
496
                          new_child.open_branch().get_stacked_on_url)
578
497
 
579
498
    def test_sprout_ignores_policy_for_unsupported_formats(self):
580
499
        child_branch, new_child_transport = self.prepare_default_stacking(
581
500
            child_format='pack-0.92')
582
 
        new_child = child_branch.controldir.sprout(new_child_transport.base)
583
 
        self.assertRaises(branch.UnstackableBranchFormat,
 
501
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
 
502
        self.assertRaises(errors.UnstackableBranchFormat,
584
503
                          new_child.open_branch().get_stacked_on_url)
585
504
 
586
505
    def test_sprout_upgrades_format_if_stacked_specified(self):
587
506
        child_branch, new_child_transport = self.prepare_default_stacking(
588
507
            child_format='pack-0.92')
589
 
        new_child = child_branch.controldir.sprout(new_child_transport.base,
590
 
                                                   stacked=True)
591
 
        self.assertEqual(child_branch.controldir.root_transport.base,
 
508
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
 
509
                                               stacked=True)
 
510
        self.assertEqual(child_branch.bzrdir.root_transport.base,
592
511
                         new_child.open_branch().get_stacked_on_url())
593
512
        repo = new_child.open_repository()
594
513
        self.assertTrue(repo._format.supports_external_lookups)
597
516
    def test_clone_on_transport_upgrades_format_if_stacked_on_specified(self):
598
517
        child_branch, new_child_transport = self.prepare_default_stacking(
599
518
            child_format='pack-0.92')
600
 
        new_child = child_branch.controldir.clone_on_transport(new_child_transport,
601
 
                                                               stacked_on=child_branch.controldir.root_transport.base)
602
 
        self.assertEqual(child_branch.controldir.root_transport.base,
 
519
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport,
 
520
            stacked_on=child_branch.bzrdir.root_transport.base)
 
521
        self.assertEqual(child_branch.bzrdir.root_transport.base,
603
522
                         new_child.open_branch().get_stacked_on_url())
604
523
        repo = new_child.open_repository()
605
524
        self.assertTrue(repo._format.supports_external_lookups)
608
527
    def test_sprout_upgrades_to_rich_root_format_if_needed(self):
609
528
        child_branch, new_child_transport = self.prepare_default_stacking(
610
529
            child_format='rich-root-pack')
611
 
        new_child = child_branch.controldir.sprout(new_child_transport.base,
612
 
                                                   stacked=True)
 
530
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
 
531
                                               stacked=True)
613
532
        repo = new_child.open_repository()
614
533
        self.assertTrue(repo._format.supports_external_lookups)
615
534
        self.assertTrue(repo.supports_rich_root())
630
549
        stack_on = self.make_branch('stack_on', format='1.6')
631
550
        stacked = self.make_branch('stack_on/stacked', format='1.6')
632
551
        policy = bzrdir.UseExistingRepository(stacked.repository,
633
 
                                              '.', stack_on.base)
 
552
            '.', stack_on.base)
634
553
        policy.configure_branch(stacked)
635
554
        self.assertEqual('..', stacked.get_stacked_on_url())
636
555
 
638
557
        stack_on = self.make_branch('stack_on', format='1.6')
639
558
        stacked = self.make_branch('stack_on/stacked', format='1.6')
640
559
        policy = bzrdir.UseExistingRepository(stacked.repository,
641
 
                                              '.', self.get_readonly_url('stack_on'))
 
560
            '.', self.get_readonly_url('stack_on'))
642
561
        policy.configure_branch(stacked)
643
562
        self.assertEqual(self.get_readonly_url('stack_on'),
644
563
                         stacked.get_stacked_on_url())
658
577
            self.transport_readonly_server = http_server.HttpServer
659
578
 
660
579
    def local_branch_path(self, branch):
661
 
        return os.path.realpath(urlutils.local_path_from_url(branch.base))
 
580
         return os.path.realpath(urlutils.local_path_from_url(branch.base))
662
581
 
663
582
    def test_open_containing(self):
664
583
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
666
585
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
667
586
                          self.get_readonly_url('g/p/q'))
668
587
        control = bzrdir.BzrDir.create(self.get_url())
669
 
        branch, relpath = bzrdir.BzrDir.open_containing(
 
588
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
 
589
        self.assertEqual('', relpath)
 
590
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
 
591
        self.assertEqual('g/p/q', relpath)
 
592
 
 
593
    def test_open_containing_tree_branch_or_repository_empty(self):
 
594
        self.assertRaises(errors.NotBranchError,
 
595
            bzrdir.BzrDir.open_containing_tree_branch_or_repository,
670
596
            self.get_readonly_url(''))
671
 
        self.assertEqual('', relpath)
672
 
        branch, relpath = bzrdir.BzrDir.open_containing(
673
 
            self.get_readonly_url('g/p/q'))
674
 
        self.assertEqual('g/p/q', relpath)
675
 
 
676
 
    def test_open_containing_tree_branch_or_repository_empty(self):
677
 
        self.assertRaises(errors.NotBranchError,
678
 
                          bzrdir.BzrDir.open_containing_tree_branch_or_repository,
679
 
                          self.get_readonly_url(''))
680
597
 
681
598
    def test_open_containing_tree_branch_or_repository_all(self):
682
599
        self.make_branch_and_tree('topdir')
689
606
                         self.local_branch_path(branch))
690
607
        self.assertEqual(
691
608
            osutils.realpath(os.path.join('topdir', '.bzr', 'repository')),
692
 
            repo.controldir.transport.local_abspath('repository'))
 
609
            repo.bzrdir.transport.local_abspath('repository'))
693
610
        self.assertEqual(relpath, 'foo')
694
611
 
695
612
    def test_open_containing_tree_branch_or_repository_no_tree(self):
702
619
                         self.local_branch_path(branch))
703
620
        self.assertEqual(
704
621
            osutils.realpath(os.path.join('branch', '.bzr', 'repository')),
705
 
            repo.controldir.transport.local_abspath('repository'))
 
622
            repo.bzrdir.transport.local_abspath('repository'))
706
623
        self.assertEqual(relpath, 'foo')
707
624
 
708
625
    def test_open_containing_tree_branch_or_repository_repo(self):
714
631
        self.assertEqual(branch, None)
715
632
        self.assertEqual(
716
633
            osutils.realpath(os.path.join('repo', '.bzr', 'repository')),
717
 
            repo.controldir.transport.local_abspath('repository'))
 
634
            repo.bzrdir.transport.local_abspath('repository'))
718
635
        self.assertEqual(relpath, '')
719
636
 
720
637
    def test_open_containing_tree_branch_or_repository_shared_repo(self):
729
646
                         self.local_branch_path(branch))
730
647
        self.assertEqual(
731
648
            osutils.realpath(os.path.join('shared', '.bzr', 'repository')),
732
 
            repo.controldir.transport.local_abspath('repository'))
 
649
            repo.bzrdir.transport.local_abspath('repository'))
733
650
        self.assertEqual(relpath, '')
734
651
 
735
652
    def test_open_containing_tree_branch_or_repository_branch_subdir(self):
744
661
                         self.local_branch_path(branch))
745
662
        self.assertEqual(
746
663
            osutils.realpath(os.path.join('foo', '.bzr', 'repository')),
747
 
            repo.controldir.transport.local_abspath('repository'))
 
664
            repo.bzrdir.transport.local_abspath('repository'))
748
665
        self.assertEqual(relpath, 'bar')
749
666
 
750
667
    def test_open_containing_tree_branch_or_repository_repo_subdir(self):
757
674
        self.assertEqual(branch, None)
758
675
        self.assertEqual(
759
676
            osutils.realpath(os.path.join('bar', '.bzr', 'repository')),
760
 
            repo.controldir.transport.local_abspath('repository'))
 
677
            repo.bzrdir.transport.local_abspath('repository'))
761
678
        self.assertEqual(relpath, 'baz')
762
679
 
763
680
    def test_open_containing_from_transport(self):
764
 
        self.assertRaises(NotBranchError,
765
 
                          bzrdir.BzrDir.open_containing_from_transport,
766
 
                          _mod_transport.get_transport_from_url(self.get_readonly_url('')))
767
 
        self.assertRaises(NotBranchError,
768
 
                          bzrdir.BzrDir.open_containing_from_transport,
769
 
                          _mod_transport.get_transport_from_url(
770
 
                              self.get_readonly_url('g/p/q')))
 
681
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
682
                          get_transport(self.get_readonly_url('')))
 
683
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
684
                          get_transport(self.get_readonly_url('g/p/q')))
771
685
        control = bzrdir.BzrDir.create(self.get_url())
772
686
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
773
 
            _mod_transport.get_transport_from_url(
774
 
                self.get_readonly_url('')))
 
687
            get_transport(self.get_readonly_url('')))
775
688
        self.assertEqual('', relpath)
776
689
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
777
 
            _mod_transport.get_transport_from_url(
778
 
                self.get_readonly_url('g/p/q')))
 
690
            get_transport(self.get_readonly_url('g/p/q')))
779
691
        self.assertEqual('g/p/q', relpath)
780
692
 
781
693
    def test_open_containing_tree_or_branch(self):
786
698
                         os.path.realpath(tree.basedir))
787
699
        self.assertEqual(os.path.realpath('topdir'),
788
700
                         self.local_branch_path(branch))
789
 
        self.assertIs(tree.controldir, branch.controldir)
 
701
        self.assertIs(tree.bzrdir, branch.bzrdir)
790
702
        self.assertEqual('foo', relpath)
791
703
        # opening from non-local should not return the tree
792
704
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
809
721
                         os.path.realpath(tree.basedir))
810
722
        self.assertEqual(os.path.realpath('topdir'),
811
723
                         self.local_branch_path(branch))
812
 
        self.assertIs(tree.controldir, branch.controldir)
 
724
        self.assertIs(tree.bzrdir, branch.bzrdir)
813
725
        # opening from non-local should not return the tree
814
726
        tree, branch = bzrdir.BzrDir.open_tree_or_branch(
815
727
            self.get_readonly_url('topdir'))
821
733
        self.assertEqual(os.path.realpath('topdir/foo'),
822
734
                         self.local_branch_path(branch))
823
735
 
824
 
    def test_open_tree_or_branch_named(self):
825
 
        tree = self.make_branch_and_tree('topdir')
826
 
        self.assertRaises(
827
 
            NotBranchError,
828
 
            bzrdir.BzrDir.open_tree_or_branch, 'topdir', name='missing')
829
 
        tree.branch.controldir.create_branch('named')
830
 
        tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir', name='named')
831
 
        self.assertEqual(os.path.realpath('topdir'),
832
 
                         os.path.realpath(tree.basedir))
833
 
        self.assertEqual(os.path.realpath('topdir'),
834
 
                         self.local_branch_path(branch))
835
 
        self.assertEqual(branch.name, 'named')
836
 
        self.assertIs(tree.controldir, branch.controldir)
837
 
 
838
736
    def test_open_from_transport(self):
839
737
        # transport pointing at bzrdir should give a bzrdir with root transport
840
738
        # set to the given transport
841
739
        control = bzrdir.BzrDir.create(self.get_url())
842
 
        t = self.get_transport()
843
 
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(t)
844
 
        self.assertEqual(t.base, opened_bzrdir.root_transport.base)
 
740
        transport = get_transport(self.get_url())
 
741
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
 
742
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
845
743
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
846
744
 
847
745
    def test_open_from_transport_no_bzrdir(self):
848
 
        t = self.get_transport()
849
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
746
        transport = get_transport(self.get_url())
 
747
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
748
                          transport)
850
749
 
851
750
    def test_open_from_transport_bzrdir_in_parent(self):
852
751
        control = bzrdir.BzrDir.create(self.get_url())
853
 
        t = self.get_transport()
854
 
        t.mkdir('subdir')
855
 
        t = t.clone('subdir')
856
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
752
        transport = get_transport(self.get_url())
 
753
        transport.mkdir('subdir')
 
754
        transport = transport.clone('subdir')
 
755
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
756
                          transport)
857
757
 
858
758
    def test_sprout_recursive(self):
859
 
        tree = self.make_branch_and_tree('tree1')
860
 
        sub_tree = self.make_branch_and_tree('tree1/subtree')
861
 
        sub_tree.set_root_id(b'subtree-root')
 
759
        tree = self.make_branch_and_tree('tree1',
 
760
                                         format='dirstate-with-subtree')
 
761
        sub_tree = self.make_branch_and_tree('tree1/subtree',
 
762
            format='dirstate-with-subtree')
 
763
        sub_tree.set_root_id('subtree-root')
862
764
        tree.add_reference(sub_tree)
863
 
        tree.set_reference_info('subtree', sub_tree.branch.user_url)
864
765
        self.build_tree(['tree1/subtree/file'])
865
766
        sub_tree.add('file')
866
767
        tree.commit('Initial commit')
867
 
        tree2 = tree.controldir.sprout('tree2').open_workingtree()
 
768
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
868
769
        tree2.lock_read()
869
770
        self.addCleanup(tree2.unlock)
870
 
        self.assertPathExists('tree2/subtree/file')
871
 
        self.assertEqual('tree-reference', tree2.kind('subtree'))
 
771
        self.failUnlessExists('tree2/subtree/file')
 
772
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
872
773
 
873
774
    def test_cloning_metadir(self):
874
775
        """Ensure that cloning metadir is suitable"""
875
 
        bzrdir = self.make_controldir('bzrdir')
 
776
        bzrdir = self.make_bzrdir('bzrdir')
876
777
        bzrdir.cloning_metadir()
877
778
        branch = self.make_branch('branch', format='knit')
878
 
        format = branch.controldir.cloning_metadir()
 
779
        format = branch.bzrdir.cloning_metadir()
879
780
        self.assertIsInstance(format.workingtree_format,
880
 
                              workingtree_4.WorkingTreeFormat6)
 
781
            workingtree.WorkingTreeFormat3)
881
782
 
882
783
    def test_sprout_recursive_treeless(self):
883
784
        tree = self.make_branch_and_tree('tree1',
884
 
                                         format='development-subtree')
 
785
            format='dirstate-with-subtree')
885
786
        sub_tree = self.make_branch_and_tree('tree1/subtree',
886
 
                                             format='development-subtree')
 
787
            format='dirstate-with-subtree')
887
788
        tree.add_reference(sub_tree)
888
 
        tree.set_reference_info('subtree', sub_tree.branch.user_url)
889
789
        self.build_tree(['tree1/subtree/file'])
890
790
        sub_tree.add('file')
891
791
        tree.commit('Initial commit')
892
 
        # The following line force the orhaning to reveal bug #634470
893
 
        tree.branch.get_config_stack().set('transform.orphan_policy', 'move')
894
 
        tree.controldir.destroy_workingtree()
895
 
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
896
 
        # fail :-( ) -- vila 20100909
 
792
        tree.bzrdir.destroy_workingtree()
897
793
        repo = self.make_repository('repo', shared=True,
898
 
                                    format='development-subtree')
 
794
            format='dirstate-with-subtree')
899
795
        repo.set_make_working_trees(False)
900
 
        # FIXME: we just deleted the workingtree and now we want to use it ????
901
 
        # At a minimum, we should use tree.branch below (but this fails too
902
 
        # currently) or stop calling this test 'treeless'. Specifically, I've
903
 
        # turn the line below into an assertRaises when 'subtree/.bzr' is
904
 
        # orphaned and sprout tries to access the branch there (which is left
905
 
        # by bzrdir.BzrDirMeta1.destroy_workingtree when it ignores the
906
 
        # [DeletingParent('Not deleting', u'subtree', None)] conflict). See bug
907
 
        # #634470.  -- vila 20100909
908
 
        tree.controldir.sprout('repo/tree2')
909
 
        self.assertPathExists('repo/tree2/subtree')
910
 
        self.assertPathDoesNotExist('repo/tree2/subtree/file')
 
796
        tree.bzrdir.sprout('repo/tree2')
 
797
        self.failUnlessExists('repo/tree2/subtree')
 
798
        self.failIfExists('repo/tree2/subtree/file')
911
799
 
912
800
    def make_foo_bar_baz(self):
913
 
        foo = bzrdir.BzrDir.create_branch_convenience('foo').controldir
914
 
        bar = self.make_branch('foo/bar').controldir
915
 
        baz = self.make_branch('baz').controldir
 
801
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
 
802
        bar = self.make_branch('foo/bar').bzrdir
 
803
        baz = self.make_branch('baz').bzrdir
916
804
        return foo, bar, baz
917
805
 
918
 
    def test_find_controldirs(self):
 
806
    def test_find_bzrdirs(self):
919
807
        foo, bar, baz = self.make_foo_bar_baz()
920
 
        t = self.get_transport()
921
 
        self.assertEqualBzrdirs(
922
 
            [baz, foo, bar], bzrdir.BzrDir.find_controldirs(t))
 
808
        transport = get_transport(self.get_url())
 
809
        self.assertEqualBzrdirs([baz, foo, bar],
 
810
                                bzrdir.BzrDir.find_bzrdirs(transport))
923
811
 
924
812
    def make_fake_permission_denied_transport(self, transport, paths):
925
813
        """Create a transport that raises PermissionDenied for some paths."""
939
827
        for actual_bzrdir in actual_bzrdirs:
940
828
            self.assertEndsWith(actual_bzrdir.user_url, expect_url_suffix)
941
829
 
942
 
    def test_find_controldirs_permission_denied(self):
 
830
    def test_find_bzrdirs_permission_denied(self):
943
831
        foo, bar, baz = self.make_foo_bar_baz()
944
 
        t = self.get_transport()
 
832
        transport = get_transport(self.get_url())
945
833
        path_filter_server, path_filter_transport = \
946
 
            self.make_fake_permission_denied_transport(t, ['foo'])
 
834
            self.make_fake_permission_denied_transport(transport, ['foo'])
947
835
        # local transport
948
836
        self.assertBranchUrlsEndWith('/baz/',
949
 
                                     bzrdir.BzrDir.find_controldirs(path_filter_transport))
 
837
            bzrdir.BzrDir.find_bzrdirs(path_filter_transport))
950
838
        # smart server
951
839
        smart_transport = self.make_smart_server('.',
952
 
                                                 backing_server=path_filter_server)
 
840
            backing_server=path_filter_server)
953
841
        self.assertBranchUrlsEndWith('/baz/',
954
 
                                     bzrdir.BzrDir.find_controldirs(smart_transport))
 
842
            bzrdir.BzrDir.find_bzrdirs(smart_transport))
955
843
 
956
 
    def test_find_controldirs_list_current(self):
 
844
    def test_find_bzrdirs_list_current(self):
957
845
        def list_current(transport):
958
846
            return [s for s in transport.list_dir('') if s != 'baz']
959
847
 
960
848
        foo, bar, baz = self.make_foo_bar_baz()
961
 
        t = self.get_transport()
962
 
        self.assertEqualBzrdirs(
963
 
            [foo, bar],
964
 
            bzrdir.BzrDir.find_controldirs(t, list_current=list_current))
 
849
        transport = get_transport(self.get_url())
 
850
        self.assertEqualBzrdirs([foo, bar],
 
851
                                bzrdir.BzrDir.find_bzrdirs(transport,
 
852
                                    list_current=list_current))
965
853
 
966
 
    def test_find_controldirs_evaluate(self):
 
854
    def test_find_bzrdirs_evaluate(self):
967
855
        def evaluate(bzrdir):
968
856
            try:
969
857
                repo = bzrdir.open_repository()
970
 
            except errors.NoRepositoryPresent:
 
858
            except NoRepositoryPresent:
971
859
                return True, bzrdir.root_transport.base
972
860
            else:
973
861
                return False, bzrdir.root_transport.base
974
862
 
975
863
        foo, bar, baz = self.make_foo_bar_baz()
976
 
        t = self.get_transport()
 
864
        transport = get_transport(self.get_url())
977
865
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
978
 
                         list(bzrdir.BzrDir.find_controldirs(t, evaluate=evaluate)))
 
866
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
 
867
                                                         evaluate=evaluate)))
979
868
 
980
869
    def assertEqualBzrdirs(self, first, second):
981
870
        first = list(first)
987
876
    def test_find_branches(self):
988
877
        root = self.make_repository('', shared=True)
989
878
        foo, bar, baz = self.make_foo_bar_baz()
990
 
        qux = self.make_controldir('foo/qux')
991
 
        t = self.get_transport()
992
 
        branches = bzrdir.BzrDir.find_branches(t)
 
879
        qux = self.make_bzrdir('foo/qux')
 
880
        transport = get_transport(self.get_url())
 
881
        branches = bzrdir.BzrDir.find_branches(transport)
993
882
        self.assertEqual(baz.root_transport.base, branches[0].base)
994
883
        self.assertEqual(foo.root_transport.base, branches[1].base)
995
884
        self.assertEqual(bar.root_transport.base, branches[2].base)
996
885
 
997
886
        # ensure this works without a top-level repo
998
 
        branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
 
887
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
999
888
        self.assertEqual(foo.root_transport.base, branches[0].base)
1000
889
        self.assertEqual(bar.root_transport.base, branches[1].base)
1001
890
 
1002
891
 
1003
892
class TestMissingRepoBranchesSkipped(TestCaseWithMemoryTransport):
1004
893
 
1005
 
    def test_find_controldirs_missing_repo(self):
1006
 
        t = self.get_transport()
 
894
    def test_find_bzrdirs_missing_repo(self):
 
895
        transport = get_transport(self.get_url())
1007
896
        arepo = self.make_repository('arepo', shared=True)
1008
897
        abranch_url = arepo.user_url + '/abranch'
1009
898
        abranch = bzrdir.BzrDir.create(abranch_url).create_branch()
1010
 
        t.delete_tree('arepo/.bzr')
 
899
        transport.delete_tree('arepo/.bzr')
1011
900
        self.assertRaises(errors.NoRepositoryPresent,
1012
 
                          branch.Branch.open, abranch_url)
 
901
            branch.Branch.open, abranch_url)
1013
902
        self.make_branch('baz')
1014
 
        for actual_bzrdir in bzrdir.BzrDir.find_branches(t):
 
903
        for actual_bzrdir in bzrdir.BzrDir.find_branches(transport):
1015
904
            self.assertEndsWith(actual_bzrdir.user_url, '/baz/')
1016
905
 
1017
906
 
1024
913
        branch_base = t.clone('branch').base
1025
914
        self.assertEqual(branch_base, dir.get_branch_transport(None).base)
1026
915
        self.assertEqual(branch_base,
1027
 
                         dir.get_branch_transport(BzrBranchFormat5()).base)
 
916
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
1028
917
        repository_base = t.clone('repository').base
1029
 
        self.assertEqual(
1030
 
            repository_base, dir.get_repository_transport(None).base)
1031
 
        repository_format = repository.format_registry.get_default()
 
918
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
1032
919
        self.assertEqual(repository_base,
1033
 
                         dir.get_repository_transport(repository_format).base)
 
920
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
1034
921
        checkout_base = t.clone('checkout').base
1035
 
        self.assertEqual(
1036
 
            checkout_base, dir.get_workingtree_transport(None).base)
 
922
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
1037
923
        self.assertEqual(checkout_base,
1038
 
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
 
924
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
1039
925
 
1040
926
    def test_meta1dir_uses_lockdir(self):
1041
927
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
1049
935
        Metadirs should compare equal iff they have the same repo, branch and
1050
936
        tree formats.
1051
937
        """
1052
 
        mydir = controldir.format_registry.make_controldir('knit')
 
938
        mydir = bzrdir.format_registry.make_bzrdir('knit')
1053
939
        self.assertEqual(mydir, mydir)
1054
940
        self.assertFalse(mydir != mydir)
1055
 
        otherdir = controldir.format_registry.make_controldir('knit')
 
941
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
1056
942
        self.assertEqual(otherdir, mydir)
1057
943
        self.assertFalse(otherdir != mydir)
1058
 
        otherdir2 = controldir.format_registry.make_controldir(
1059
 
            'development-subtree')
 
944
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
1060
945
        self.assertNotEqual(otherdir2, mydir)
1061
946
        self.assertFalse(otherdir2 == mydir)
1062
947
 
1063
 
    def test_with_features(self):
1064
 
        tree = self.make_branch_and_tree('tree', format='2a')
1065
 
        tree.controldir.update_feature_flags({b"bar": b"required"})
1066
 
        self.assertRaises(bzrdir.MissingFeature, bzrdir.BzrDir.open, 'tree')
1067
 
        bzrdir.BzrDirMetaFormat1.register_feature(b'bar')
1068
 
        self.addCleanup(bzrdir.BzrDirMetaFormat1.unregister_feature, b'bar')
1069
 
        dir = bzrdir.BzrDir.open('tree')
1070
 
        self.assertEqual(b"required", dir._format.features.get(b"bar"))
1071
 
        tree.controldir.update_feature_flags({
1072
 
            b"bar": None,
1073
 
            b"nonexistant": None})
1074
 
        dir = bzrdir.BzrDir.open('tree')
1075
 
        self.assertEqual({}, dir._format.features)
1076
 
 
1077
948
    def test_needs_conversion_different_working_tree(self):
1078
949
        # meta1dirs need an conversion if any element is not the default.
1079
 
        new_format = controldir.format_registry.make_controldir('dirstate')
 
950
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
1080
951
        tree = self.make_branch_and_tree('tree', format='knit')
1081
 
        self.assertTrue(tree.controldir.needs_format_conversion(
 
952
        self.assertTrue(tree.bzrdir.needs_format_conversion(
1082
953
            new_format))
1083
954
 
1084
955
    def test_initialize_on_format_uses_smart_transport(self):
1085
956
        self.setup_smart_server_with_call_log()
1086
 
        new_format = controldir.format_registry.make_controldir('dirstate')
 
957
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
1087
958
        transport = self.get_transport('target')
1088
959
        transport.ensure_base()
1089
960
        self.reset_smart_call_log()
1098
969
        self.assertEqual(2, rpc_count)
1099
970
 
1100
971
 
 
972
class TestFormat5(TestCaseWithTransport):
 
973
    """Tests specific to the version 5 bzrdir format."""
 
974
 
 
975
    def test_same_lockfiles_between_tree_repo_branch(self):
 
976
        # this checks that only a single lockfiles instance is created
 
977
        # for format 5 objects
 
978
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
979
        def check_dir_components_use_same_lock(dir):
 
980
            ctrl_1 = dir.open_repository().control_files
 
981
            ctrl_2 = dir.open_branch().control_files
 
982
            ctrl_3 = dir.open_workingtree()._control_files
 
983
            self.assertTrue(ctrl_1 is ctrl_2)
 
984
            self.assertTrue(ctrl_2 is ctrl_3)
 
985
        check_dir_components_use_same_lock(dir)
 
986
        # and if we open it normally.
 
987
        dir = bzrdir.BzrDir.open(self.get_url())
 
988
        check_dir_components_use_same_lock(dir)
 
989
 
 
990
    def test_can_convert(self):
 
991
        # format 5 dirs are convertable
 
992
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
993
        self.assertTrue(dir.can_convert_format())
 
994
 
 
995
    def test_needs_conversion(self):
 
996
        # format 5 dirs need a conversion if they are not the default,
 
997
        # and they aren't
 
998
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
999
        # don't need to convert it to itself
 
1000
        self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
 
1001
        # do need to convert it to the current default
 
1002
        self.assertTrue(dir.needs_format_conversion(
 
1003
            bzrdir.BzrDirFormat.get_default_format()))
 
1004
 
 
1005
 
 
1006
class TestFormat6(TestCaseWithTransport):
 
1007
    """Tests specific to the version 6 bzrdir format."""
 
1008
 
 
1009
    def test_same_lockfiles_between_tree_repo_branch(self):
 
1010
        # this checks that only a single lockfiles instance is created
 
1011
        # for format 6 objects
 
1012
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1013
        def check_dir_components_use_same_lock(dir):
 
1014
            ctrl_1 = dir.open_repository().control_files
 
1015
            ctrl_2 = dir.open_branch().control_files
 
1016
            ctrl_3 = dir.open_workingtree()._control_files
 
1017
            self.assertTrue(ctrl_1 is ctrl_2)
 
1018
            self.assertTrue(ctrl_2 is ctrl_3)
 
1019
        check_dir_components_use_same_lock(dir)
 
1020
        # and if we open it normally.
 
1021
        dir = bzrdir.BzrDir.open(self.get_url())
 
1022
        check_dir_components_use_same_lock(dir)
 
1023
 
 
1024
    def test_can_convert(self):
 
1025
        # format 6 dirs are convertable
 
1026
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1027
        self.assertTrue(dir.can_convert_format())
 
1028
 
 
1029
    def test_needs_conversion(self):
 
1030
        # format 6 dirs need an conversion if they are not the default.
 
1031
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1032
        self.assertTrue(dir.needs_format_conversion(
 
1033
            bzrdir.BzrDirFormat.get_default_format()))
 
1034
 
 
1035
 
 
1036
class NotBzrDir(bzrlib.bzrdir.BzrDir):
 
1037
    """A non .bzr based control directory."""
 
1038
 
 
1039
    def __init__(self, transport, format):
 
1040
        self._format = format
 
1041
        self.root_transport = transport
 
1042
        self.transport = transport.clone('.not')
 
1043
 
 
1044
 
 
1045
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
 
1046
    """A test class representing any non-.bzr based disk format."""
 
1047
 
 
1048
    def initialize_on_transport(self, transport):
 
1049
        """Initialize a new .not dir in the base directory of a Transport."""
 
1050
        transport.mkdir('.not')
 
1051
        return self.open(transport)
 
1052
 
 
1053
    def open(self, transport):
 
1054
        """Open this directory."""
 
1055
        return NotBzrDir(transport, self)
 
1056
 
 
1057
    @classmethod
 
1058
    def _known_formats(self):
 
1059
        return set([NotBzrDirFormat()])
 
1060
 
 
1061
    @classmethod
 
1062
    def probe_transport(self, transport):
 
1063
        """Our format is present if the transport ends in '.not/'."""
 
1064
        if transport.has('.not'):
 
1065
            return NotBzrDirFormat()
 
1066
 
 
1067
 
 
1068
class TestNotBzrDir(TestCaseWithTransport):
 
1069
    """Tests for using the bzrdir api with a non .bzr based disk format.
 
1070
 
 
1071
    If/when one of these is in the core, we can let the implementation tests
 
1072
    verify this works.
 
1073
    """
 
1074
 
 
1075
    def test_create_and_find_format(self):
 
1076
        # create a .notbzr dir
 
1077
        format = NotBzrDirFormat()
 
1078
        dir = format.initialize(self.get_url())
 
1079
        self.assertIsInstance(dir, NotBzrDir)
 
1080
        # now probe for it.
 
1081
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
 
1082
        try:
 
1083
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
 
1084
                get_transport(self.get_url()))
 
1085
            self.assertIsInstance(found, NotBzrDirFormat)
 
1086
        finally:
 
1087
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
 
1088
 
 
1089
    def test_included_in_known_formats(self):
 
1090
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
 
1091
        try:
 
1092
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
 
1093
            for format in formats:
 
1094
                if isinstance(format, NotBzrDirFormat):
 
1095
                    return
 
1096
            self.fail("No NotBzrDirFormat in %s" % formats)
 
1097
        finally:
 
1098
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
 
1099
 
 
1100
 
1101
1101
class NonLocalTests(TestCaseWithTransport):
1102
1102
    """Tests for bzrdir static behaviour on non local paths."""
1103
1103
 
1107
1107
 
1108
1108
    def test_create_branch_convenience(self):
1109
1109
        # outside a repo the default convenience output is a repo+branch_tree
1110
 
        format = controldir.format_registry.make_controldir('knit')
 
1110
        format = bzrdir.format_registry.make_bzrdir('knit')
1111
1111
        branch = bzrdir.BzrDir.create_branch_convenience(
1112
1112
            self.get_url('foo'), format=format)
1113
1113
        self.assertRaises(errors.NoWorkingTree,
1114
 
                          branch.controldir.open_workingtree)
1115
 
        branch.controldir.open_repository()
 
1114
                          branch.bzrdir.open_workingtree)
 
1115
        branch.bzrdir.open_repository()
1116
1116
 
1117
1117
    def test_create_branch_convenience_force_tree_not_local_fails(self):
1118
1118
        # outside a repo the default convenience output is a repo+branch_tree
1119
 
        format = controldir.format_registry.make_controldir('knit')
 
1119
        format = bzrdir.format_registry.make_bzrdir('knit')
1120
1120
        self.assertRaises(errors.NotLocalUrl,
1121
 
                          bzrdir.BzrDir.create_branch_convenience,
1122
 
                          self.get_url('foo'),
1123
 
                          force_new_tree=True,
1124
 
                          format=format)
1125
 
        t = self.get_transport()
 
1121
            bzrdir.BzrDir.create_branch_convenience,
 
1122
            self.get_url('foo'),
 
1123
            force_new_tree=True,
 
1124
            format=format)
 
1125
        t = get_transport(self.get_url('.'))
1126
1126
        self.assertFalse(t.has('foo'))
1127
1127
 
1128
1128
    def test_clone(self):
1129
1129
        # clone into a nonlocal path works
1130
 
        format = controldir.format_registry.make_controldir('knit')
 
1130
        format = bzrdir.format_registry.make_bzrdir('knit')
1131
1131
        branch = bzrdir.BzrDir.create_branch_convenience('local',
1132
1132
                                                         format=format)
1133
 
        branch.controldir.open_workingtree()
1134
 
        result = branch.controldir.clone(self.get_url('remote'))
 
1133
        branch.bzrdir.open_workingtree()
 
1134
        result = branch.bzrdir.clone(self.get_url('remote'))
1135
1135
        self.assertRaises(errors.NoWorkingTree,
1136
1136
                          result.open_workingtree)
1137
1137
        result.open_branch()
1144
1144
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1145
1145
        checkout_format = my_bzrdir.checkout_metadir()
1146
1146
        self.assertIsInstance(checkout_format.workingtree_format,
1147
 
                              workingtree_4.WorkingTreeFormat4)
1148
 
 
1149
 
 
1150
 
class TestHTTPRedirectionsBase(object):
 
1147
                              workingtree.WorkingTreeFormat3)
 
1148
 
 
1149
 
 
1150
class TestHTTPRedirections(object):
1151
1151
    """Test redirection between two http servers.
1152
1152
 
1153
1153
    This MUST be used by daughter classes that also inherit from
1167
1167
        return http_utils.HTTPServerRedirecting()
1168
1168
 
1169
1169
    def setUp(self):
1170
 
        super(TestHTTPRedirectionsBase, self).setUp()
 
1170
        super(TestHTTPRedirections, self).setUp()
1171
1171
        # The redirections will point to the new server
1172
1172
        self.new_server = self.get_readonly_server()
1173
1173
        # The requests to the old server will be redirected
1201
1201
        self.assertIsInstance(bdir.root_transport, type(start))
1202
1202
 
1203
1203
 
1204
 
class TestHTTPRedirections(TestHTTPRedirectionsBase,
1205
 
                           http_utils.TestCaseWithTwoWebservers):
 
1204
class TestHTTPRedirections_urllib(TestHTTPRedirections,
 
1205
                                  http_utils.TestCaseWithTwoWebservers):
1206
1206
    """Tests redirections for urllib implementation"""
1207
1207
 
1208
 
    _transport = HttpTransport
1209
 
 
1210
 
    def _qualified_url(self, host, port):
1211
 
        result = 'http://%s:%s' % (host, port)
1212
 
        self.permit_url(result)
1213
 
        return result
1214
 
 
1215
 
 
1216
 
class TestHTTPRedirections_nosmart(TestHTTPRedirectionsBase,
1217
 
                                   http_utils.TestCaseWithTwoWebservers):
 
1208
    _transport = HttpTransport_urllib
 
1209
 
 
1210
    def _qualified_url(self, host, port):
 
1211
        result = 'http+urllib://%s:%s' % (host, port)
 
1212
        self.permit_url(result)
 
1213
        return result
 
1214
 
 
1215
 
 
1216
 
 
1217
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
 
1218
                                  TestHTTPRedirections,
 
1219
                                  http_utils.TestCaseWithTwoWebservers):
 
1220
    """Tests redirections for pycurl implementation"""
 
1221
 
 
1222
    def _qualified_url(self, host, port):
 
1223
        result = 'http+pycurl://%s:%s' % (host, port)
 
1224
        self.permit_url(result)
 
1225
        return result
 
1226
 
 
1227
 
 
1228
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
 
1229
                                  http_utils.TestCaseWithTwoWebservers):
1218
1230
    """Tests redirections for the nosmart decorator"""
1219
1231
 
1220
1232
    _transport = NoSmartTransportDecorator
1225
1237
        return result
1226
1238
 
1227
1239
 
1228
 
class TestHTTPRedirections_readonly(TestHTTPRedirectionsBase,
 
1240
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1229
1241
                                    http_utils.TestCaseWithTwoWebservers):
1230
1242
    """Tests redirections for readonly decoratror"""
1231
1243
 
1245
1257
 
1246
1258
    def get_ls(self):
1247
1259
        f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
1248
 
                             stderr=subprocess.PIPE)
 
1260
            stderr=subprocess.PIPE)
1249
1261
        out, err = f.communicate()
1250
1262
        self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
1251
1263
                         % (self.ls, err))
1252
1264
        return out.splitlines()
1253
1265
 
1254
1266
    def test_dot_bzr_hidden(self):
 
1267
        if sys.platform == 'win32' and not win32utils.has_win32file:
 
1268
            raise TestSkipped('unable to make file hidden without pywin32 library')
1255
1269
        b = bzrdir.BzrDir.create('.')
1256
1270
        self.build_tree(['a'])
1257
 
        self.assertEqual([b'a'], self.get_ls())
 
1271
        self.assertEquals(['a'], self.get_ls())
1258
1272
 
1259
1273
    def test_dot_bzr_hidden_with_url(self):
 
1274
        if sys.platform == 'win32' and not win32utils.has_win32file:
 
1275
            raise TestSkipped('unable to make file hidden without pywin32 library')
1260
1276
        b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1261
1277
        self.build_tree(['a'])
1262
 
        self.assertEqual([b'a'], self.get_ls())
 
1278
        self.assertEquals(['a'], self.get_ls())
1263
1279
 
1264
1280
 
1265
1281
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1278
1294
 
1279
1295
    def __init__(self, *args, **kwargs):
1280
1296
        super(_TestBzrDir, self).__init__(*args, **kwargs)
1281
 
        self.test_branch = _TestBranch(self.transport)
 
1297
        self.test_branch = _TestBranch()
1282
1298
        self.test_branch.repository = self.create_repository()
1283
1299
 
1284
 
    def open_branch(self, unsupported=False, possible_transports=None):
 
1300
    def open_branch(self, unsupported=False):
1285
1301
        return self.test_branch
1286
1302
 
1287
1303
    def cloning_metadir(self, require_stacking=False):
1288
1304
        return _TestBzrDirFormat()
1289
1305
 
1290
1306
 
1291
 
class _TestBranchFormat(breezy.branch.BranchFormat):
 
1307
class _TestBranchFormat(bzrlib.branch.BranchFormat):
1292
1308
    """Test Branch format for TestBzrDirSprout."""
1293
1309
 
1294
1310
 
1295
 
class _TestBranch(breezy.branch.Branch):
 
1311
class _TestBranch(bzrlib.branch.Branch):
1296
1312
    """Test Branch implementation for TestBzrDirSprout."""
1297
1313
 
1298
 
    def __init__(self, transport, *args, **kwargs):
 
1314
    def __init__(self, *args, **kwargs):
1299
1315
        self._format = _TestBranchFormat()
1300
 
        self._transport = transport
1301
 
        self.base = transport.base
1302
1316
        super(_TestBranch, self).__init__(*args, **kwargs)
1303
1317
        self.calls = []
1304
1318
        self._parent = None
1305
1319
 
1306
1320
    def sprout(self, *args, **kwargs):
1307
1321
        self.calls.append('sprout')
1308
 
        return _TestBranch(self._transport)
 
1322
        return _TestBranch()
1309
1323
 
1310
1324
    def copy_content_into(self, destination, revision_id=None):
1311
1325
        self.calls.append('copy_content_into')
1312
1326
 
1313
 
    def last_revision(self):
1314
 
        return _mod_revision.NULL_REVISION
1315
 
 
1316
1327
    def get_parent(self):
1317
1328
        return self._parent
1318
1329
 
1319
 
    def _get_config(self):
1320
 
        return config.TransportConfig(self._transport, 'branch.conf')
1321
 
 
1322
 
    def _get_config_store(self):
1323
 
        return config.BranchStore(self)
1324
 
 
1325
1330
    def set_parent(self, parent):
1326
1331
        self._parent = parent
1327
1332
 
1328
 
    def lock_read(self):
1329
 
        return lock.LogicalLockResult(self.unlock)
1330
 
 
1331
 
    def unlock(self):
1332
 
        return
1333
 
 
1334
1333
 
1335
1334
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1336
1335
 
1364
1363
 
1365
1364
    def test_sprout_parent(self):
1366
1365
        grandparent_tree = self.make_branch('grandparent')
1367
 
        parent = grandparent_tree.controldir.sprout('parent').open_branch()
1368
 
        branch_tree = parent.controldir.sprout('branch').open_branch()
 
1366
        parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
 
1367
        branch_tree = parent.bzrdir.sprout('branch').open_branch()
1369
1368
        self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
1370
1369
 
1371
1370
 
1381
1380
 
1382
1381
    def test_pre_open_actual_exceptions_raised(self):
1383
1382
        count = [0]
1384
 
 
1385
1383
        def fail_once(transport):
1386
1384
            count[0] += 1
1387
1385
            if count[0] == 1:
1393
1391
        self.assertEqual('fail', err._preformatted_string)
1394
1392
 
1395
1393
    def test_post_repo_init(self):
1396
 
        from ..controldir import RepoInitHookParams
 
1394
        from bzrlib.bzrdir import RepoInitHookParams
1397
1395
        calls = []
1398
1396
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1399
 
                                               calls.append, None)
 
1397
            calls.append, None)
1400
1398
        self.make_repository('foo')
1401
1399
        self.assertLength(1, calls)
1402
1400
        params = calls[0]
1403
1401
        self.assertIsInstance(params, RepoInitHookParams)
1404
 
        self.assertTrue(hasattr(params, 'controldir'))
 
1402
        self.assertTrue(hasattr(params, 'bzrdir'))
1405
1403
        self.assertTrue(hasattr(params, 'repository'))
1406
1404
 
1407
 
    def test_post_repo_init_hook_repr(self):
1408
 
        param_reprs = []
1409
 
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1410
 
                                               lambda params: param_reprs.append(repr(params)), None)
1411
 
        self.make_repository('foo')
1412
 
        self.assertLength(1, param_reprs)
1413
 
        param_repr = param_reprs[0]
1414
 
        self.assertStartsWith(param_repr, '<RepoInitHookParams for ')
1415
 
 
1416
1405
 
1417
1406
class TestGenerateBackupName(TestCaseWithMemoryTransport):
1418
 
    # FIXME: This may need to be unified with test_osutils.TestBackupNames or
1419
 
    # moved to per_bzrdir or per_transport for better coverage ?
1420
 
    # -- vila 20100909
1421
1407
 
1422
1408
    def setUp(self):
1423
1409
        super(TestGenerateBackupName, self).setUp()
1424
 
        self._transport = self.get_transport()
 
1410
        self._transport = get_transport(self.get_url())
1425
1411
        bzrdir.BzrDir.create(self.get_url(),
1426
 
                             possible_transports=[self._transport])
 
1412
            possible_transports=[self._transport])
1427
1413
        self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
1428
1414
 
1429
1415
    def test_new(self):
1430
 
        self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
 
1416
        self.assertEqual("a.~1~", self._bzrdir.generate_backup_name("a"))
1431
1417
 
1432
1418
    def test_exiting(self):
1433
 
        self._transport.put_bytes("a.~1~", b"some content")
1434
 
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1435
 
 
1436
 
 
1437
 
class TestMeta1DirColoFormat(TestCaseWithTransport):
1438
 
    """Tests specific to the meta1 dir with colocated branches format."""
1439
 
 
1440
 
    def test_supports_colo(self):
1441
 
        format = bzrdir.BzrDirMetaFormat1Colo()
1442
 
        self.assertTrue(format.colocated_branches)
1443
 
 
1444
 
    def test_upgrade_from_2a(self):
1445
 
        tree = self.make_branch_and_tree('.', format='2a')
1446
 
        format = bzrdir.BzrDirMetaFormat1Colo()
1447
 
        self.assertTrue(tree.controldir.needs_format_conversion(format))
1448
 
        converter = tree.controldir._format.get_converter(format)
1449
 
        result = converter.convert(tree.controldir, None)
1450
 
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1Colo)
1451
 
        self.assertFalse(result.needs_format_conversion(format))
1452
 
 
1453
 
    def test_downgrade_to_2a(self):
1454
 
        tree = self.make_branch_and_tree('.', format='development-colo')
1455
 
        format = bzrdir.BzrDirMetaFormat1()
1456
 
        self.assertTrue(tree.controldir.needs_format_conversion(format))
1457
 
        converter = tree.controldir._format.get_converter(format)
1458
 
        result = converter.convert(tree.controldir, None)
1459
 
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
1460
 
        self.assertFalse(result.needs_format_conversion(format))
1461
 
 
1462
 
    def test_downgrade_to_2a_too_many_branches(self):
1463
 
        tree = self.make_branch_and_tree('.', format='development-colo')
1464
 
        tree.controldir.create_branch(name="another-colocated-branch")
1465
 
        converter = tree.controldir._format.get_converter(
1466
 
            bzrdir.BzrDirMetaFormat1())
1467
 
        result = converter.convert(tree.controldir, bzrdir.BzrDirMetaFormat1())
1468
 
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
1469
 
 
1470
 
    def test_nested(self):
1471
 
        tree = self.make_branch_and_tree('.', format='development-colo')
1472
 
        tree.controldir.create_branch(name='foo')
1473
 
        tree.controldir.create_branch(name='fool/bla')
1474
 
        self.assertRaises(
1475
 
            errors.ParentBranchExists, tree.controldir.create_branch,
1476
 
            name='foo/bar')
1477
 
 
1478
 
    def test_parent(self):
1479
 
        tree = self.make_branch_and_tree('.', format='development-colo')
1480
 
        tree.controldir.create_branch(name='fool/bla')
1481
 
        tree.controldir.create_branch(name='foo/bar')
1482
 
        self.assertRaises(
1483
 
            errors.AlreadyBranchError, tree.controldir.create_branch,
1484
 
            name='foo')
1485
 
 
1486
 
    def test_supports_relative_reference(self):
1487
 
        tree = self.make_branch_and_tree('.', format='development-colo')
1488
 
        target1 = tree.controldir.create_branch(name='target1')
1489
 
        target2 = tree.controldir.create_branch(name='target2')
1490
 
        source = tree.controldir.set_branch_reference(target1, name='source')
1491
 
        self.assertEqual(
1492
 
            target1.user_url, tree.controldir.open_branch('source').user_url)
1493
 
        source.controldir.get_branch_transport(None, 'source').put_bytes(
1494
 
            'location', b'file:,branch=target2')
1495
 
        self.assertEqual(
1496
 
            target2.user_url, tree.controldir.open_branch('source').user_url)
1497
 
 
1498
 
 
1499
 
class SampleBzrFormat(bzrdir.BzrFormat):
1500
 
 
1501
 
    @classmethod
1502
 
    def get_format_string(cls):
1503
 
        return b"First line\n"
1504
 
 
1505
 
 
1506
 
class TestBzrFormat(TestCase):
1507
 
    """Tests for BzrFormat."""
1508
 
 
1509
 
    def test_as_string(self):
1510
 
        format = SampleBzrFormat()
1511
 
        format.features = {b"foo": b"required"}
1512
 
        self.assertEqual(format.as_string(),
1513
 
                         b"First line\n"
1514
 
                         b"required foo\n")
1515
 
        format.features[b"another"] = b"optional"
1516
 
        self.assertEqual(format.as_string(),
1517
 
                         b"First line\n"
1518
 
                         b"optional another\n"
1519
 
                         b"required foo\n")
1520
 
 
1521
 
    def test_network_name(self):
1522
 
        # The network string should include the feature info
1523
 
        format = SampleBzrFormat()
1524
 
        format.features = {b"foo": b"required"}
1525
 
        self.assertEqual(
1526
 
            b"First line\nrequired foo\n",
1527
 
            format.network_name())
1528
 
 
1529
 
    def test_from_string_no_features(self):
1530
 
        # No features
1531
 
        format = SampleBzrFormat.from_string(
1532
 
            b"First line\n")
1533
 
        self.assertEqual({}, format.features)
1534
 
 
1535
 
    def test_from_string_with_feature(self):
1536
 
        # Proper feature
1537
 
        format = SampleBzrFormat.from_string(
1538
 
            b"First line\nrequired foo\n")
1539
 
        self.assertEqual(b"required", format.features.get(b"foo"))
1540
 
 
1541
 
    def test_from_string_format_string_mismatch(self):
1542
 
        # The first line has to match the format string
1543
 
        self.assertRaises(AssertionError, SampleBzrFormat.from_string,
1544
 
                          b"Second line\nrequired foo\n")
1545
 
 
1546
 
    def test_from_string_missing_space(self):
1547
 
        # At least one space is required in the feature lines
1548
 
        self.assertRaises(errors.ParseFormatError, SampleBzrFormat.from_string,
1549
 
                          b"First line\nfoo\n")
1550
 
 
1551
 
    def test_from_string_with_spaces(self):
1552
 
        # Feature with spaces (in case we add stuff like this in the future)
1553
 
        format = SampleBzrFormat.from_string(
1554
 
            b"First line\nrequired foo with spaces\n")
1555
 
        self.assertEqual(b"required", format.features.get(b"foo with spaces"))
1556
 
 
1557
 
    def test_eq(self):
1558
 
        format1 = SampleBzrFormat()
1559
 
        format1.features = {b"nested-trees": b"optional"}
1560
 
        format2 = SampleBzrFormat()
1561
 
        format2.features = {b"nested-trees": b"optional"}
1562
 
        self.assertEqual(format1, format1)
1563
 
        self.assertEqual(format1, format2)
1564
 
        format3 = SampleBzrFormat()
1565
 
        self.assertNotEqual(format1, format3)
1566
 
 
1567
 
    def test_check_support_status_optional(self):
1568
 
        # Optional, so silently ignore
1569
 
        format = SampleBzrFormat()
1570
 
        format.features = {b"nested-trees": b"optional"}
1571
 
        format.check_support_status(True)
1572
 
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
1573
 
        SampleBzrFormat.register_feature(b"nested-trees")
1574
 
        format.check_support_status(True)
1575
 
 
1576
 
    def test_check_support_status_required(self):
1577
 
        # Optional, so trigger an exception
1578
 
        format = SampleBzrFormat()
1579
 
        format.features = {b"nested-trees": b"required"}
1580
 
        self.assertRaises(bzrdir.MissingFeature, format.check_support_status,
1581
 
                          True)
1582
 
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
1583
 
        SampleBzrFormat.register_feature(b"nested-trees")
1584
 
        format.check_support_status(True)
1585
 
 
1586
 
    def test_check_support_status_unknown(self):
1587
 
        # treat unknown necessity as required
1588
 
        format = SampleBzrFormat()
1589
 
        format.features = {b"nested-trees": b"unknown"}
1590
 
        self.assertRaises(bzrdir.MissingFeature, format.check_support_status,
1591
 
                          True)
1592
 
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
1593
 
        SampleBzrFormat.register_feature(b"nested-trees")
1594
 
        format.check_support_status(True)
1595
 
 
1596
 
    def test_feature_already_registered(self):
1597
 
        # a feature can only be registered once
1598
 
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
1599
 
        SampleBzrFormat.register_feature(b"nested-trees")
1600
 
        self.assertRaises(bzrdir.FeatureAlreadyRegistered,
1601
 
                          SampleBzrFormat.register_feature, b"nested-trees")
1602
 
 
1603
 
    def test_feature_with_space(self):
1604
 
        # spaces are not allowed in feature names
1605
 
        self.assertRaises(ValueError, SampleBzrFormat.register_feature,
1606
 
                          b"nested trees")
 
1419
        self._transport.put_bytes("a.~1~", "some content")
 
1420
        self.assertEqual("a.~2~", self._bzrdir.generate_backup_name("a"))