/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 01:35:53 UTC
  • mto: (6670.4.8 move-bzr)
  • mto: This revision was merged to the branch mainline in revision 6681.
  • Revision ID: jelmer@jelmer.uk-20170610013553-560y7mn3su4pp763
Fix remaining tests.

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