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

  • Committer: Jelmer Vernooij
  • Date: 2020-09-02 16:35:18 UTC
  • mto: (7490.40.109 work)
  • mto: This revision was merged to the branch mainline in revision 7526.
  • Revision ID: jelmer@jelmer.uk-20200902163518-sy9f4unbboljphgu
Handle duplicate directories entries for git.

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