/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 breezy/tests/test_bzrdir.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-10 16:40:42 UTC
  • mfrom: (6653.6.7 rename-controldir)
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170610164042-zrxqgy2htyduvke2
MergeĀ rename-controldirĀ branch.

Show diffs side-by-side

added added

removed removed

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