/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to breezy/tests/test_bzrdir.py

  • Committer: Jelmer Vernooij
  • Date: 2020-02-05 01:40:59 UTC
  • mto: This revision was merged to the branch mainline in revision 7480.
  • Revision ID: jelmer@jelmer.uk-20200205014059-1jrhjaphw5vh9i7s
Fix Python 2.7 build.

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 ..bzr import (
 
42
    branch as bzrbranch,
 
43
    bzrdir,
 
44
    remote,
 
45
    workingtree_3,
 
46
    workingtree_4,
 
47
    )
 
48
import breezy.branch
 
49
import breezy.bzr.branch
 
50
from ..bzr.fullhistory import BzrBranchFormat5
 
51
from ..errors import (
 
52
    NotBranchError,
 
53
    NoColocatedBranchSupport,
 
54
    UnknownFormatError,
 
55
    UnsupportedFormatError,
 
56
    )
 
57
from . import (
44
58
    TestCase,
45
59
    TestCaseWithMemoryTransport,
46
60
    TestCaseWithTransport,
47
61
    TestSkipped,
48
62
    )
49
 
from bzrlib.tests import(
 
63
from . 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 ..bzr 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', 'breezy.tests.test_bzrdir',
 
105
                                         'DeprecatedBzrDirFormat', 'Format registered lazily',
 
106
                                         deprecated=True)
 
107
        bzr.register_metadir(my_format_registry, 'knit',
 
108
                             'breezy.bzr.knitrepo.RepositoryFormatKnit1',
 
109
                             'Format using knits',
 
110
                             )
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', 'breezy.tests.test_bzrdir',
 
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(errors.LineEndingError,
 
322
                          bzrdir.BzrDirFormat.find_format,
 
323
                          _mod_transport.get_transport_from_path('.'))
274
324
 
275
325
    def test_register_unregister_format(self):
276
326
        format = SampleBzrDirFormat()
278
328
        # make a bzrdir
279
329
        format.initialize(url)
280
330
        # register a format for it.
281
 
        bzrdir.BzrDirFormat.register_format(format)
 
331
        bzr.BzrProber.formats.register(format.get_format_string(), format)
282
332
        # which bzrdir.Open will refuse (not supported)
283
333
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
284
334
        # which bzrdir.open_containing will refuse (not supported)
285
 
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
 
335
        self.assertRaises(UnsupportedFormatError,
 
336
                          bzrdir.BzrDir.open_containing, url)
286
337
        # but open_downlevel will work
287
 
        t = get_transport(url)
 
338
        t = _mod_transport.get_transport_from_url(url)
288
339
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
289
340
        # unregister the format
290
 
        bzrdir.BzrDirFormat.unregister_format(format)
 
341
        bzr.BzrProber.formats.remove(format.get_format_string())
291
342
        # now open_downlevel should fail too.
292
 
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
 
343
        self.assertRaises(UnknownFormatError,
 
344
                          bzrdir.BzrDir.open_unsupported, url)
293
345
 
294
346
    def test_create_branch_and_repo_uses_default(self):
295
347
        format = SampleBzrDirFormat()
300
352
    def test_create_branch_and_repo_under_shared(self):
301
353
        # creating a branch and repo in a shared repo uses the
302
354
        # shared repository
303
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
355
        format = controldir.format_registry.make_controldir('knit')
304
356
        self.make_repository('.', shared=True, format=format)
305
357
        branch = bzrdir.BzrDir.create_branch_and_repo(
306
358
            self.get_url('child'), format=format)
307
359
        self.assertRaises(errors.NoRepositoryPresent,
308
 
                          branch.bzrdir.open_repository)
 
360
                          branch.controldir.open_repository)
309
361
 
310
362
    def test_create_branch_and_repo_under_shared_force_new(self):
311
363
        # creating a branch and repo in a shared repo can be forced to
312
364
        # make a new repo
313
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
365
        format = controldir.format_registry.make_controldir('knit')
314
366
        self.make_repository('.', shared=True, format=format)
315
367
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
316
368
                                                      force_new_repo=True,
317
369
                                                      format=format)
318
 
        branch.bzrdir.open_repository()
 
370
        branch.controldir.open_repository()
319
371
 
320
372
    def test_create_standalone_working_tree(self):
321
373
        format = SampleBzrDirFormat()
330
382
 
331
383
    def test_create_standalone_working_tree_under_shared_repo(self):
332
384
        # create standalone working tree always makes a repo.
333
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
385
        format = controldir.format_registry.make_controldir('knit')
334
386
        self.make_repository('.', shared=True, format=format)
335
387
        # note this is deliberately readonly, as this failure should
336
388
        # occur before any writes.
338
390
                          bzrdir.BzrDir.create_standalone_workingtree,
339
391
                          self.get_readonly_url('child'), format=format)
340
392
        tree = bzrdir.BzrDir.create_standalone_workingtree('child',
341
 
            format=format)
342
 
        tree.bzrdir.open_repository()
 
393
                                                           format=format)
 
394
        tree.controldir.open_repository()
343
395
 
344
396
    def test_create_branch_convenience(self):
345
397
        # outside a repo the default convenience output is a repo+branch_tree
346
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
398
        format = controldir.format_registry.make_controldir('knit')
347
399
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
348
 
        branch.bzrdir.open_workingtree()
349
 
        branch.bzrdir.open_repository()
 
400
        branch.controldir.open_workingtree()
 
401
        branch.controldir.open_repository()
350
402
 
351
403
    def test_create_branch_convenience_possible_transports(self):
352
404
        """Check that the optional 'possible_transports' is recognized"""
353
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
405
        format = controldir.format_registry.make_controldir('knit')
354
406
        t = self.get_transport()
355
407
        branch = bzrdir.BzrDir.create_branch_convenience(
356
408
            '.', format=format, possible_transports=[t])
357
 
        branch.bzrdir.open_workingtree()
358
 
        branch.bzrdir.open_repository()
 
409
        branch.controldir.open_workingtree()
 
410
        branch.controldir.open_repository()
359
411
 
360
412
    def test_create_branch_convenience_root(self):
361
413
        """Creating a branch at the root of a fs should work."""
362
414
        self.vfs_transport_factory = memory.MemoryServer
363
415
        # outside a repo the default convenience output is a repo+branch_tree
364
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
416
        format = controldir.format_registry.make_controldir('knit')
365
417
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
366
418
                                                         format=format)
367
419
        self.assertRaises(errors.NoWorkingTree,
368
 
                          branch.bzrdir.open_workingtree)
369
 
        branch.bzrdir.open_repository()
 
420
                          branch.controldir.open_workingtree)
 
421
        branch.controldir.open_repository()
370
422
 
371
423
    def test_create_branch_convenience_under_shared_repo(self):
372
424
        # inside a repo the default convenience output is a branch+ follow the
373
425
        # repo tree policy
374
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
426
        format = controldir.format_registry.make_controldir('knit')
375
427
        self.make_repository('.', shared=True, format=format)
376
428
        branch = bzrdir.BzrDir.create_branch_convenience('child',
377
 
            format=format)
378
 
        branch.bzrdir.open_workingtree()
 
429
                                                         format=format)
 
430
        branch.controldir.open_workingtree()
379
431
        self.assertRaises(errors.NoRepositoryPresent,
380
 
                          branch.bzrdir.open_repository)
 
432
                          branch.controldir.open_repository)
381
433
 
382
434
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
383
435
        # inside a repo the default convenience output is a branch+ follow the
384
436
        # repo tree policy but we can override that
385
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
437
        format = controldir.format_registry.make_controldir('knit')
386
438
        self.make_repository('.', shared=True, format=format)
387
439
        branch = bzrdir.BzrDir.create_branch_convenience('child',
388
 
            force_new_tree=False, format=format)
 
440
                                                         force_new_tree=False, format=format)
389
441
        self.assertRaises(errors.NoWorkingTree,
390
 
                          branch.bzrdir.open_workingtree)
 
442
                          branch.controldir.open_workingtree)
391
443
        self.assertRaises(errors.NoRepositoryPresent,
392
 
                          branch.bzrdir.open_repository)
 
444
                          branch.controldir.open_repository)
393
445
 
394
446
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
395
447
        # inside a repo the default convenience output is a branch+ follow the
396
448
        # repo tree policy
397
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
449
        format = controldir.format_registry.make_controldir('knit')
398
450
        repo = self.make_repository('.', shared=True, format=format)
399
451
        repo.set_make_working_trees(False)
400
452
        branch = bzrdir.BzrDir.create_branch_convenience('child',
401
453
                                                         format=format)
402
454
        self.assertRaises(errors.NoWorkingTree,
403
 
                          branch.bzrdir.open_workingtree)
 
455
                          branch.controldir.open_workingtree)
404
456
        self.assertRaises(errors.NoRepositoryPresent,
405
 
                          branch.bzrdir.open_repository)
 
457
                          branch.controldir.open_repository)
406
458
 
407
459
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
408
460
        # inside a repo the default convenience output is a branch+ follow the
409
461
        # repo tree policy but we can override that
410
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
462
        format = controldir.format_registry.make_controldir('knit')
411
463
        repo = self.make_repository('.', shared=True, format=format)
412
464
        repo.set_make_working_trees(False)
413
465
        branch = bzrdir.BzrDir.create_branch_convenience('child',
414
 
            force_new_tree=True, format=format)
415
 
        branch.bzrdir.open_workingtree()
 
466
                                                         force_new_tree=True, format=format)
 
467
        branch.controldir.open_workingtree()
416
468
        self.assertRaises(errors.NoRepositoryPresent,
417
 
                          branch.bzrdir.open_repository)
 
469
                          branch.controldir.open_repository)
418
470
 
419
471
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
420
472
        # inside a repo the default convenience output is overridable to give
421
473
        # repo+branch+tree
422
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
474
        format = controldir.format_registry.make_controldir('knit')
423
475
        self.make_repository('.', shared=True, format=format)
424
476
        branch = bzrdir.BzrDir.create_branch_convenience('child',
425
 
            force_new_repo=True, format=format)
426
 
        branch.bzrdir.open_repository()
427
 
        branch.bzrdir.open_workingtree()
 
477
                                                         force_new_repo=True, format=format)
 
478
        branch.controldir.open_repository()
 
479
        branch.controldir.open_workingtree()
428
480
 
429
481
 
430
482
class TestRepositoryAcquisitionPolicy(TestCaseWithTransport):
431
483
 
432
484
    def test_acquire_repository_standalone(self):
433
485
        """The default acquisition policy should create a standalone branch."""
434
 
        my_bzrdir = self.make_bzrdir('.')
 
486
        my_bzrdir = self.make_controldir('.')
435
487
        repo_policy = my_bzrdir.determine_repository_policy()
436
488
        repo, is_new = repo_policy.acquire_repository()
437
 
        self.assertEqual(repo.bzrdir.root_transport.base,
 
489
        self.assertEqual(repo.controldir.root_transport.base,
438
490
                         my_bzrdir.root_transport.base)
439
491
        self.assertFalse(repo.is_shared())
440
492
 
441
493
    def test_determine_stacking_policy(self):
442
 
        parent_bzrdir = self.make_bzrdir('.')
443
 
        child_bzrdir = self.make_bzrdir('child')
 
494
        parent_bzrdir = self.make_controldir('.')
 
495
        child_bzrdir = self.make_controldir('child')
444
496
        parent_bzrdir.get_config().set_default_stack_on('http://example.org')
445
497
        repo_policy = child_bzrdir.determine_repository_policy()
446
498
        self.assertEqual('http://example.org', repo_policy._stack_on)
447
499
 
448
500
    def test_determine_stacking_policy_relative(self):
449
 
        parent_bzrdir = self.make_bzrdir('.')
450
 
        child_bzrdir = self.make_bzrdir('child')
 
501
        parent_bzrdir = self.make_controldir('.')
 
502
        child_bzrdir = self.make_controldir('child')
451
503
        parent_bzrdir.get_config().set_default_stack_on('child2')
452
504
        repo_policy = child_bzrdir.determine_repository_policy()
453
505
        self.assertEqual('child2', repo_policy._stack_on)
455
507
                         repo_policy._stack_on_pwd)
456
508
 
457
509
    def prepare_default_stacking(self, child_format='1.6'):
458
 
        parent_bzrdir = self.make_bzrdir('.')
 
510
        parent_bzrdir = self.make_controldir('.')
459
511
        child_branch = self.make_branch('child', format=child_format)
460
512
        parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
461
513
        new_child_transport = parent_bzrdir.transport.clone('child2')
463
515
 
464
516
    def test_clone_on_transport_obeys_stacking_policy(self):
465
517
        child_branch, new_child_transport = self.prepare_default_stacking()
466
 
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
 
518
        new_child = child_branch.controldir.clone_on_transport(
 
519
            new_child_transport)
467
520
        self.assertEqual(child_branch.base,
468
521
                         new_child.open_branch().get_stacked_on_url())
469
522
 
470
523
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
471
524
        # 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(
 
525
        source_bzrdir = self.make_controldir('source')
 
526
        knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
527
        source_branch = breezy.bzr.branch.BzrBranchFormat7().initialize(
475
528
            source_bzrdir)
476
529
        # Make a directory with a default stacking policy
477
 
        parent_bzrdir = self.make_bzrdir('parent')
 
530
        parent_bzrdir = self.make_controldir('parent')
478
531
        stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
479
532
        parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
480
533
        # Clone source into directory
481
534
        target = source_bzrdir.clone(self.get_url('parent/target'))
482
535
 
 
536
    def test_format_initialize_on_transport_ex_stacked_on(self):
 
537
        # trunk is a stackable format.  Note that its in the same server area
 
538
        # which is what launchpad does, but not sufficient to exercise the
 
539
        # general case.
 
540
        trunk = self.make_branch('trunk', format='1.9')
 
541
        t = self.get_transport('stacked')
 
542
        old_fmt = controldir.format_registry.make_controldir('pack-0.92')
 
543
        repo_name = old_fmt.repository_format.network_name()
 
544
        # Should end up with a 1.9 format (stackable)
 
545
        repo, control, require_stacking, repo_policy = \
 
546
            old_fmt.initialize_on_transport_ex(t,
 
547
                                               repo_format_name=repo_name, stacked_on='../trunk',
 
548
                                               stack_on_pwd=t.base)
 
549
        if repo is not None:
 
550
            # Repositories are open write-locked
 
551
            self.assertTrue(repo.is_write_locked())
 
552
            self.addCleanup(repo.unlock)
 
553
        else:
 
554
            repo = control.open_repository()
 
555
        self.assertIsInstance(control, bzrdir.BzrDir)
 
556
        opened = bzrdir.BzrDir.open(t.base)
 
557
        if not isinstance(old_fmt, remote.RemoteBzrDirFormat):
 
558
            self.assertEqual(control._format.network_name(),
 
559
                             old_fmt.network_name())
 
560
            self.assertEqual(control._format.network_name(),
 
561
                             opened._format.network_name())
 
562
        self.assertEqual(control.__class__, opened.__class__)
 
563
        self.assertLength(1, repo._fallback_repositories)
 
564
 
483
565
    def test_sprout_obeys_stacking_policy(self):
484
566
        child_branch, new_child_transport = self.prepare_default_stacking()
485
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
 
567
        new_child = child_branch.controldir.sprout(new_child_transport.base)
486
568
        self.assertEqual(child_branch.base,
487
569
                         new_child.open_branch().get_stacked_on_url())
488
570
 
489
571
    def test_clone_ignores_policy_for_unsupported_formats(self):
490
572
        child_branch, new_child_transport = self.prepare_default_stacking(
491
573
            child_format='pack-0.92')
492
 
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
493
 
        self.assertRaises(errors.UnstackableBranchFormat,
 
574
        new_child = child_branch.controldir.clone_on_transport(
 
575
            new_child_transport)
 
576
        self.assertRaises(branch.UnstackableBranchFormat,
494
577
                          new_child.open_branch().get_stacked_on_url)
495
578
 
496
579
    def test_sprout_ignores_policy_for_unsupported_formats(self):
497
580
        child_branch, new_child_transport = self.prepare_default_stacking(
498
581
            child_format='pack-0.92')
499
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
500
 
        self.assertRaises(errors.UnstackableBranchFormat,
 
582
        new_child = child_branch.controldir.sprout(new_child_transport.base)
 
583
        self.assertRaises(branch.UnstackableBranchFormat,
501
584
                          new_child.open_branch().get_stacked_on_url)
502
585
 
503
586
    def test_sprout_upgrades_format_if_stacked_specified(self):
504
587
        child_branch, new_child_transport = self.prepare_default_stacking(
505
588
            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,
 
589
        new_child = child_branch.controldir.sprout(new_child_transport.base,
 
590
                                                   stacked=True)
 
591
        self.assertEqual(child_branch.controldir.root_transport.base,
509
592
                         new_child.open_branch().get_stacked_on_url())
510
593
        repo = new_child.open_repository()
511
594
        self.assertTrue(repo._format.supports_external_lookups)
514
597
    def test_clone_on_transport_upgrades_format_if_stacked_on_specified(self):
515
598
        child_branch, new_child_transport = self.prepare_default_stacking(
516
599
            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,
 
600
        new_child = child_branch.controldir.clone_on_transport(new_child_transport,
 
601
                                                               stacked_on=child_branch.controldir.root_transport.base)
 
602
        self.assertEqual(child_branch.controldir.root_transport.base,
520
603
                         new_child.open_branch().get_stacked_on_url())
521
604
        repo = new_child.open_repository()
522
605
        self.assertTrue(repo._format.supports_external_lookups)
525
608
    def test_sprout_upgrades_to_rich_root_format_if_needed(self):
526
609
        child_branch, new_child_transport = self.prepare_default_stacking(
527
610
            child_format='rich-root-pack')
528
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
529
 
                                               stacked=True)
 
611
        new_child = child_branch.controldir.sprout(new_child_transport.base,
 
612
                                                   stacked=True)
530
613
        repo = new_child.open_repository()
531
614
        self.assertTrue(repo._format.supports_external_lookups)
532
615
        self.assertTrue(repo.supports_rich_root())
547
630
        stack_on = self.make_branch('stack_on', format='1.6')
548
631
        stacked = self.make_branch('stack_on/stacked', format='1.6')
549
632
        policy = bzrdir.UseExistingRepository(stacked.repository,
550
 
            '.', stack_on.base)
 
633
                                              '.', stack_on.base)
551
634
        policy.configure_branch(stacked)
552
635
        self.assertEqual('..', stacked.get_stacked_on_url())
553
636
 
555
638
        stack_on = self.make_branch('stack_on', format='1.6')
556
639
        stacked = self.make_branch('stack_on/stacked', format='1.6')
557
640
        policy = bzrdir.UseExistingRepository(stacked.repository,
558
 
            '.', self.get_readonly_url('stack_on'))
 
641
                                              '.', self.get_readonly_url('stack_on'))
559
642
        policy.configure_branch(stacked)
560
643
        self.assertEqual(self.get_readonly_url('stack_on'),
561
644
                         stacked.get_stacked_on_url())
575
658
            self.transport_readonly_server = http_server.HttpServer
576
659
 
577
660
    def local_branch_path(self, branch):
578
 
         return os.path.realpath(urlutils.local_path_from_url(branch.base))
 
661
        return os.path.realpath(urlutils.local_path_from_url(branch.base))
579
662
 
580
663
    def test_open_containing(self):
581
664
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
583
666
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
584
667
                          self.get_readonly_url('g/p/q'))
585
668
        control = bzrdir.BzrDir.create(self.get_url())
586
 
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
 
669
        branch, relpath = bzrdir.BzrDir.open_containing(
 
670
            self.get_readonly_url(''))
587
671
        self.assertEqual('', relpath)
588
 
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
 
672
        branch, relpath = bzrdir.BzrDir.open_containing(
 
673
            self.get_readonly_url('g/p/q'))
589
674
        self.assertEqual('g/p/q', relpath)
590
675
 
591
676
    def test_open_containing_tree_branch_or_repository_empty(self):
592
677
        self.assertRaises(errors.NotBranchError,
593
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository,
594
 
            self.get_readonly_url(''))
 
678
                          bzrdir.BzrDir.open_containing_tree_branch_or_repository,
 
679
                          self.get_readonly_url(''))
595
680
 
596
681
    def test_open_containing_tree_branch_or_repository_all(self):
597
682
        self.make_branch_and_tree('topdir')
604
689
                         self.local_branch_path(branch))
605
690
        self.assertEqual(
606
691
            osutils.realpath(os.path.join('topdir', '.bzr', 'repository')),
607
 
            repo.bzrdir.transport.local_abspath('repository'))
 
692
            repo.controldir.transport.local_abspath('repository'))
608
693
        self.assertEqual(relpath, 'foo')
609
694
 
610
695
    def test_open_containing_tree_branch_or_repository_no_tree(self):
617
702
                         self.local_branch_path(branch))
618
703
        self.assertEqual(
619
704
            osutils.realpath(os.path.join('branch', '.bzr', 'repository')),
620
 
            repo.bzrdir.transport.local_abspath('repository'))
 
705
            repo.controldir.transport.local_abspath('repository'))
621
706
        self.assertEqual(relpath, 'foo')
622
707
 
623
708
    def test_open_containing_tree_branch_or_repository_repo(self):
629
714
        self.assertEqual(branch, None)
630
715
        self.assertEqual(
631
716
            osutils.realpath(os.path.join('repo', '.bzr', 'repository')),
632
 
            repo.bzrdir.transport.local_abspath('repository'))
 
717
            repo.controldir.transport.local_abspath('repository'))
633
718
        self.assertEqual(relpath, '')
634
719
 
635
720
    def test_open_containing_tree_branch_or_repository_shared_repo(self):
644
729
                         self.local_branch_path(branch))
645
730
        self.assertEqual(
646
731
            osutils.realpath(os.path.join('shared', '.bzr', 'repository')),
647
 
            repo.bzrdir.transport.local_abspath('repository'))
 
732
            repo.controldir.transport.local_abspath('repository'))
648
733
        self.assertEqual(relpath, '')
649
734
 
650
735
    def test_open_containing_tree_branch_or_repository_branch_subdir(self):
659
744
                         self.local_branch_path(branch))
660
745
        self.assertEqual(
661
746
            osutils.realpath(os.path.join('foo', '.bzr', 'repository')),
662
 
            repo.bzrdir.transport.local_abspath('repository'))
 
747
            repo.controldir.transport.local_abspath('repository'))
663
748
        self.assertEqual(relpath, 'bar')
664
749
 
665
750
    def test_open_containing_tree_branch_or_repository_repo_subdir(self):
672
757
        self.assertEqual(branch, None)
673
758
        self.assertEqual(
674
759
            osutils.realpath(os.path.join('bar', '.bzr', 'repository')),
675
 
            repo.bzrdir.transport.local_abspath('repository'))
 
760
            repo.controldir.transport.local_abspath('repository'))
676
761
        self.assertEqual(relpath, 'baz')
677
762
 
678
763
    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')))
 
764
        self.assertRaises(NotBranchError,
 
765
                          bzrdir.BzrDir.open_containing_from_transport,
 
766
                          _mod_transport.get_transport_from_url(self.get_readonly_url('')))
 
767
        self.assertRaises(NotBranchError,
 
768
                          bzrdir.BzrDir.open_containing_from_transport,
 
769
                          _mod_transport.get_transport_from_url(
 
770
                              self.get_readonly_url('g/p/q')))
683
771
        control = bzrdir.BzrDir.create(self.get_url())
684
772
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
685
 
            get_transport(self.get_readonly_url('')))
 
773
            _mod_transport.get_transport_from_url(
 
774
                self.get_readonly_url('')))
686
775
        self.assertEqual('', relpath)
687
776
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
688
 
            get_transport(self.get_readonly_url('g/p/q')))
 
777
            _mod_transport.get_transport_from_url(
 
778
                self.get_readonly_url('g/p/q')))
689
779
        self.assertEqual('g/p/q', relpath)
690
780
 
691
781
    def test_open_containing_tree_or_branch(self):
696
786
                         os.path.realpath(tree.basedir))
697
787
        self.assertEqual(os.path.realpath('topdir'),
698
788
                         self.local_branch_path(branch))
699
 
        self.assertIs(tree.bzrdir, branch.bzrdir)
 
789
        self.assertIs(tree.controldir, branch.controldir)
700
790
        self.assertEqual('foo', relpath)
701
791
        # opening from non-local should not return the tree
702
792
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
719
809
                         os.path.realpath(tree.basedir))
720
810
        self.assertEqual(os.path.realpath('topdir'),
721
811
                         self.local_branch_path(branch))
722
 
        self.assertIs(tree.bzrdir, branch.bzrdir)
 
812
        self.assertIs(tree.controldir, branch.controldir)
723
813
        # opening from non-local should not return the tree
724
814
        tree, branch = bzrdir.BzrDir.open_tree_or_branch(
725
815
            self.get_readonly_url('topdir'))
735
825
        # transport pointing at bzrdir should give a bzrdir with root transport
736
826
        # set to the given transport
737
827
        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)
 
828
        t = self.get_transport()
 
829
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(t)
 
830
        self.assertEqual(t.base, opened_bzrdir.root_transport.base)
741
831
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
742
832
 
743
833
    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)
 
834
        t = self.get_transport()
 
835
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
747
836
 
748
837
    def test_open_from_transport_bzrdir_in_parent(self):
749
838
        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)
 
839
        t = self.get_transport()
 
840
        t.mkdir('subdir')
 
841
        t = t.clone('subdir')
 
842
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
755
843
 
756
844
    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')
 
845
        tree = self.make_branch_and_tree('tree1')
 
846
        sub_tree = self.make_branch_and_tree('tree1/subtree')
 
847
        sub_tree.set_root_id(b'subtree-root')
762
848
        tree.add_reference(sub_tree)
 
849
        tree.set_reference_info('subtree', sub_tree.branch.user_url)
763
850
        self.build_tree(['tree1/subtree/file'])
764
851
        sub_tree.add('file')
765
852
        tree.commit('Initial commit')
766
 
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
 
853
        tree2 = tree.controldir.sprout('tree2').open_workingtree()
767
854
        tree2.lock_read()
768
855
        self.addCleanup(tree2.unlock)
769
 
        self.failUnlessExists('tree2/subtree/file')
770
 
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
 
856
        self.assertPathExists('tree2/subtree/file')
 
857
        self.assertEqual('tree-reference', tree2.kind('subtree'))
771
858
 
772
859
    def test_cloning_metadir(self):
773
860
        """Ensure that cloning metadir is suitable"""
774
 
        bzrdir = self.make_bzrdir('bzrdir')
 
861
        bzrdir = self.make_controldir('bzrdir')
775
862
        bzrdir.cloning_metadir()
776
863
        branch = self.make_branch('branch', format='knit')
777
 
        format = branch.bzrdir.cloning_metadir()
 
864
        format = branch.controldir.cloning_metadir()
778
865
        self.assertIsInstance(format.workingtree_format,
779
 
            workingtree.WorkingTreeFormat3)
 
866
                              workingtree_4.WorkingTreeFormat6)
780
867
 
781
868
    def test_sprout_recursive_treeless(self):
782
869
        tree = self.make_branch_and_tree('tree1',
783
 
            format='dirstate-with-subtree')
 
870
                                         format='development-subtree')
784
871
        sub_tree = self.make_branch_and_tree('tree1/subtree',
785
 
            format='dirstate-with-subtree')
 
872
                                             format='development-subtree')
786
873
        tree.add_reference(sub_tree)
 
874
        tree.set_reference_info('subtree', sub_tree.branch.user_url)
787
875
        self.build_tree(['tree1/subtree/file'])
788
876
        sub_tree.add('file')
789
877
        tree.commit('Initial commit')
790
 
        tree.bzrdir.destroy_workingtree()
 
878
        # The following line force the orhaning to reveal bug #634470
 
879
        tree.branch.get_config_stack().set('transform.orphan_policy', 'move')
 
880
        tree.controldir.destroy_workingtree()
 
881
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
 
882
        # fail :-( ) -- vila 20100909
791
883
        repo = self.make_repository('repo', shared=True,
792
 
            format='dirstate-with-subtree')
 
884
                                    format='development-subtree')
793
885
        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')
 
886
        # FIXME: we just deleted the workingtree and now we want to use it ????
 
887
        # At a minimum, we should use tree.branch below (but this fails too
 
888
        # currently) or stop calling this test 'treeless'. Specifically, I've
 
889
        # turn the line below into an assertRaises when 'subtree/.bzr' is
 
890
        # orphaned and sprout tries to access the branch there (which is left
 
891
        # by bzrdir.BzrDirMeta1.destroy_workingtree when it ignores the
 
892
        # [DeletingParent('Not deleting', u'subtree', None)] conflict). See bug
 
893
        # #634470.  -- vila 20100909
 
894
        tree.controldir.sprout('repo/tree2')
 
895
        self.assertPathExists('repo/tree2/subtree')
 
896
        self.assertPathDoesNotExist('repo/tree2/subtree/file')
797
897
 
798
898
    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
 
899
        foo = bzrdir.BzrDir.create_branch_convenience('foo').controldir
 
900
        bar = self.make_branch('foo/bar').controldir
 
901
        baz = self.make_branch('baz').controldir
802
902
        return foo, bar, baz
803
903
 
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):
 
904
    def test_find_controldirs(self):
 
905
        foo, bar, baz = self.make_foo_bar_baz()
 
906
        t = self.get_transport()
 
907
        self.assertEqualBzrdirs(
 
908
            [baz, foo, bar], bzrdir.BzrDir.find_controldirs(t))
 
909
 
 
910
    def make_fake_permission_denied_transport(self, transport, paths):
 
911
        """Create a transport that raises PermissionDenied for some paths."""
 
912
        def filter(path):
 
913
            if path in paths:
 
914
                raise errors.PermissionDenied(path)
 
915
            return path
 
916
        path_filter_server = pathfilter.PathFilteringServer(transport, filter)
 
917
        path_filter_server.start_server()
 
918
        self.addCleanup(path_filter_server.stop_server)
 
919
        path_filter_transport = pathfilter.PathFilteringTransport(
 
920
            path_filter_server, '.')
 
921
        return (path_filter_server, path_filter_transport)
 
922
 
 
923
    def assertBranchUrlsEndWith(self, expect_url_suffix, actual_bzrdirs):
 
924
        """Check that each branch url ends with the given suffix."""
 
925
        for actual_bzrdir in actual_bzrdirs:
 
926
            self.assertEndsWith(actual_bzrdir.user_url, expect_url_suffix)
 
927
 
 
928
    def test_find_controldirs_permission_denied(self):
 
929
        foo, bar, baz = self.make_foo_bar_baz()
 
930
        t = self.get_transport()
 
931
        path_filter_server, path_filter_transport = \
 
932
            self.make_fake_permission_denied_transport(t, ['foo'])
 
933
        # local transport
 
934
        self.assertBranchUrlsEndWith('/baz/',
 
935
                                     bzrdir.BzrDir.find_controldirs(path_filter_transport))
 
936
        # smart server
 
937
        smart_transport = self.make_smart_server('.',
 
938
                                                 backing_server=path_filter_server)
 
939
        self.assertBranchUrlsEndWith('/baz/',
 
940
                                     bzrdir.BzrDir.find_controldirs(smart_transport))
 
941
 
 
942
    def test_find_controldirs_list_current(self):
811
943
        def list_current(transport):
812
944
            return [s for s in transport.list_dir('') if s != 'baz']
813
945
 
814
946
        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):
 
947
        t = self.get_transport()
 
948
        self.assertEqualBzrdirs(
 
949
            [foo, bar],
 
950
            bzrdir.BzrDir.find_controldirs(t, list_current=list_current))
 
951
 
 
952
    def test_find_controldirs_evaluate(self):
822
953
        def evaluate(bzrdir):
823
954
            try:
824
955
                repo = bzrdir.open_repository()
825
 
            except NoRepositoryPresent:
 
956
            except errors.NoRepositoryPresent:
826
957
                return True, bzrdir.root_transport.base
827
958
            else:
828
959
                return False, bzrdir.root_transport.base
829
960
 
830
961
        foo, bar, baz = self.make_foo_bar_baz()
831
 
        transport = get_transport(self.get_url())
 
962
        t = self.get_transport()
832
963
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
833
 
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
834
 
                                                         evaluate=evaluate)))
 
964
                         list(bzrdir.BzrDir.find_controldirs(t, evaluate=evaluate)))
835
965
 
836
966
    def assertEqualBzrdirs(self, first, second):
837
967
        first = list(first)
843
973
    def test_find_branches(self):
844
974
        root = self.make_repository('', shared=True)
845
975
        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)
 
976
        qux = self.make_controldir('foo/qux')
 
977
        t = self.get_transport()
 
978
        branches = bzrdir.BzrDir.find_branches(t)
849
979
        self.assertEqual(baz.root_transport.base, branches[0].base)
850
980
        self.assertEqual(foo.root_transport.base, branches[1].base)
851
981
        self.assertEqual(bar.root_transport.base, branches[2].base)
852
982
 
853
983
        # ensure this works without a top-level repo
854
 
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
 
984
        branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
855
985
        self.assertEqual(foo.root_transport.base, branches[0].base)
856
986
        self.assertEqual(bar.root_transport.base, branches[1].base)
857
987
 
858
988
 
 
989
class TestMissingRepoBranchesSkipped(TestCaseWithMemoryTransport):
 
990
 
 
991
    def test_find_controldirs_missing_repo(self):
 
992
        t = self.get_transport()
 
993
        arepo = self.make_repository('arepo', shared=True)
 
994
        abranch_url = arepo.user_url + '/abranch'
 
995
        abranch = bzrdir.BzrDir.create(abranch_url).create_branch()
 
996
        t.delete_tree('arepo/.bzr')
 
997
        self.assertRaises(errors.NoRepositoryPresent,
 
998
                          branch.Branch.open, abranch_url)
 
999
        self.make_branch('baz')
 
1000
        for actual_bzrdir in bzrdir.BzrDir.find_branches(t):
 
1001
            self.assertEndsWith(actual_bzrdir.user_url, '/baz/')
 
1002
 
 
1003
 
859
1004
class TestMeta1DirFormat(TestCaseWithTransport):
860
1005
    """Tests specific to the meta1 dir format."""
861
1006
 
865
1010
        branch_base = t.clone('branch').base
866
1011
        self.assertEqual(branch_base, dir.get_branch_transport(None).base)
867
1012
        self.assertEqual(branch_base,
868
 
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
 
1013
                         dir.get_branch_transport(BzrBranchFormat5()).base)
869
1014
        repository_base = t.clone('repository').base
870
 
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
 
1015
        self.assertEqual(
 
1016
            repository_base, dir.get_repository_transport(None).base)
 
1017
        repository_format = repository.format_registry.get_default()
871
1018
        self.assertEqual(repository_base,
872
 
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
 
1019
                         dir.get_repository_transport(repository_format).base)
873
1020
        checkout_base = t.clone('checkout').base
874
 
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
 
1021
        self.assertEqual(
 
1022
            checkout_base, dir.get_workingtree_transport(None).base)
875
1023
        self.assertEqual(checkout_base,
876
 
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
 
1024
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
877
1025
 
878
1026
    def test_meta1dir_uses_lockdir(self):
879
1027
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
887
1035
        Metadirs should compare equal iff they have the same repo, branch and
888
1036
        tree formats.
889
1037
        """
890
 
        mydir = bzrdir.format_registry.make_bzrdir('knit')
 
1038
        mydir = controldir.format_registry.make_controldir('knit')
891
1039
        self.assertEqual(mydir, mydir)
892
1040
        self.assertFalse(mydir != mydir)
893
 
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
 
1041
        otherdir = controldir.format_registry.make_controldir('knit')
894
1042
        self.assertEqual(otherdir, mydir)
895
1043
        self.assertFalse(otherdir != mydir)
896
 
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
 
1044
        otherdir2 = controldir.format_registry.make_controldir(
 
1045
            'development-subtree')
897
1046
        self.assertNotEqual(otherdir2, mydir)
898
1047
        self.assertFalse(otherdir2 == mydir)
899
1048
 
 
1049
    def test_with_features(self):
 
1050
        tree = self.make_branch_and_tree('tree', format='2a')
 
1051
        tree.controldir.update_feature_flags({b"bar": b"required"})
 
1052
        self.assertRaises(bzrdir.MissingFeature, bzrdir.BzrDir.open, 'tree')
 
1053
        bzrdir.BzrDirMetaFormat1.register_feature(b'bar')
 
1054
        self.addCleanup(bzrdir.BzrDirMetaFormat1.unregister_feature, b'bar')
 
1055
        dir = bzrdir.BzrDir.open('tree')
 
1056
        self.assertEqual(b"required", dir._format.features.get(b"bar"))
 
1057
        tree.controldir.update_feature_flags({
 
1058
            b"bar": None,
 
1059
            b"nonexistant": None})
 
1060
        dir = bzrdir.BzrDir.open('tree')
 
1061
        self.assertEqual({}, dir._format.features)
 
1062
 
900
1063
    def test_needs_conversion_different_working_tree(self):
901
1064
        # meta1dirs need an conversion if any element is not the default.
902
 
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
 
1065
        new_format = controldir.format_registry.make_controldir('dirstate')
903
1066
        tree = self.make_branch_and_tree('tree', format='knit')
904
 
        self.assertTrue(tree.bzrdir.needs_format_conversion(
 
1067
        self.assertTrue(tree.controldir.needs_format_conversion(
905
1068
            new_format))
906
1069
 
907
1070
    def test_initialize_on_format_uses_smart_transport(self):
908
1071
        self.setup_smart_server_with_call_log()
909
 
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
 
1072
        new_format = controldir.format_registry.make_controldir('dirstate')
910
1073
        transport = self.get_transport('target')
911
1074
        transport.ensure_base()
912
1075
        self.reset_smart_call_log()
921
1084
        self.assertEqual(2, rpc_count)
922
1085
 
923
1086
 
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
1087
class NonLocalTests(TestCaseWithTransport):
1054
1088
    """Tests for bzrdir static behaviour on non local paths."""
1055
1089
 
1059
1093
 
1060
1094
    def test_create_branch_convenience(self):
1061
1095
        # outside a repo the default convenience output is a repo+branch_tree
1062
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
1096
        format = controldir.format_registry.make_controldir('knit')
1063
1097
        branch = bzrdir.BzrDir.create_branch_convenience(
1064
1098
            self.get_url('foo'), format=format)
1065
1099
        self.assertRaises(errors.NoWorkingTree,
1066
 
                          branch.bzrdir.open_workingtree)
1067
 
        branch.bzrdir.open_repository()
 
1100
                          branch.controldir.open_workingtree)
 
1101
        branch.controldir.open_repository()
1068
1102
 
1069
1103
    def test_create_branch_convenience_force_tree_not_local_fails(self):
1070
1104
        # outside a repo the default convenience output is a repo+branch_tree
1071
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
1105
        format = controldir.format_registry.make_controldir('knit')
1072
1106
        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('.'))
 
1107
                          bzrdir.BzrDir.create_branch_convenience,
 
1108
                          self.get_url('foo'),
 
1109
                          force_new_tree=True,
 
1110
                          format=format)
 
1111
        t = self.get_transport()
1078
1112
        self.assertFalse(t.has('foo'))
1079
1113
 
1080
1114
    def test_clone(self):
1081
1115
        # clone into a nonlocal path works
1082
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
1116
        format = controldir.format_registry.make_controldir('knit')
1083
1117
        branch = bzrdir.BzrDir.create_branch_convenience('local',
1084
1118
                                                         format=format)
1085
 
        branch.bzrdir.open_workingtree()
1086
 
        result = branch.bzrdir.clone(self.get_url('remote'))
 
1119
        branch.controldir.open_workingtree()
 
1120
        result = branch.controldir.clone(self.get_url('remote'))
1087
1121
        self.assertRaises(errors.NoWorkingTree,
1088
1122
                          result.open_workingtree)
1089
1123
        result.open_branch()
1096
1130
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1097
1131
        checkout_format = my_bzrdir.checkout_metadir()
1098
1132
        self.assertIsInstance(checkout_format.workingtree_format,
1099
 
                              workingtree.WorkingTreeFormat3)
1100
 
 
1101
 
 
1102
 
class TestHTTPRedirections(object):
 
1133
                              workingtree_4.WorkingTreeFormat4)
 
1134
 
 
1135
 
 
1136
class TestHTTPRedirectionsBase(object):
1103
1137
    """Test redirection between two http servers.
1104
1138
 
1105
1139
    This MUST be used by daughter classes that also inherit from
1111
1145
    """
1112
1146
 
1113
1147
    def create_transport_readonly_server(self):
 
1148
        # We don't set the http protocol version, relying on the default
1114
1149
        return http_utils.HTTPServerRedirecting()
1115
1150
 
1116
1151
    def create_transport_secondary_server(self):
 
1152
        # We don't set the http protocol version, relying on the default
1117
1153
        return http_utils.HTTPServerRedirecting()
1118
1154
 
1119
1155
    def setUp(self):
1120
 
        super(TestHTTPRedirections, self).setUp()
 
1156
        super(TestHTTPRedirectionsBase, self).setUp()
1121
1157
        # The redirections will point to the new server
1122
1158
        self.new_server = self.get_readonly_server()
1123
1159
        # The requests to the old server will be redirected
1151
1187
        self.assertIsInstance(bdir.root_transport, type(start))
1152
1188
 
1153
1189
 
1154
 
class TestHTTPRedirections_urllib(TestHTTPRedirections,
1155
 
                                  http_utils.TestCaseWithTwoWebservers):
 
1190
class TestHTTPRedirections(TestHTTPRedirectionsBase,
 
1191
                           http_utils.TestCaseWithTwoWebservers):
1156
1192
    """Tests redirections for urllib implementation"""
1157
1193
 
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):
 
1194
    _transport = HttpTransport
 
1195
 
 
1196
    def _qualified_url(self, host, port):
 
1197
        result = 'http://%s:%s' % (host, port)
 
1198
        self.permit_url(result)
 
1199
        return result
 
1200
 
 
1201
 
 
1202
class TestHTTPRedirections_nosmart(TestHTTPRedirectionsBase,
 
1203
                                   http_utils.TestCaseWithTwoWebservers):
1180
1204
    """Tests redirections for the nosmart decorator"""
1181
1205
 
1182
1206
    _transport = NoSmartTransportDecorator
1187
1211
        return result
1188
1212
 
1189
1213
 
1190
 
class TestHTTPRedirections_readonly(TestHTTPRedirections,
 
1214
class TestHTTPRedirections_readonly(TestHTTPRedirectionsBase,
1191
1215
                                    http_utils.TestCaseWithTwoWebservers):
1192
1216
    """Tests redirections for readonly decoratror"""
1193
1217
 
1207
1231
 
1208
1232
    def get_ls(self):
1209
1233
        f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
1210
 
            stderr=subprocess.PIPE)
 
1234
                             stderr=subprocess.PIPE)
1211
1235
        out, err = f.communicate()
1212
1236
        self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
1213
1237
                         % (self.ls, err))
1214
1238
        return out.splitlines()
1215
1239
 
1216
1240
    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
1241
        b = bzrdir.BzrDir.create('.')
1220
1242
        self.build_tree(['a'])
1221
 
        self.assertEquals(['a'], self.get_ls())
 
1243
        self.assertEqual([b'a'], self.get_ls())
1222
1244
 
1223
1245
    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
1246
        b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1227
1247
        self.build_tree(['a'])
1228
 
        self.assertEquals(['a'], self.get_ls())
 
1248
        self.assertEqual([b'a'], self.get_ls())
1229
1249
 
1230
1250
 
1231
1251
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1244
1264
 
1245
1265
    def __init__(self, *args, **kwargs):
1246
1266
        super(_TestBzrDir, self).__init__(*args, **kwargs)
1247
 
        self.test_branch = _TestBranch()
 
1267
        self.test_branch = _TestBranch(self.transport)
1248
1268
        self.test_branch.repository = self.create_repository()
1249
1269
 
1250
 
    def open_branch(self, unsupported=False):
 
1270
    def open_branch(self, unsupported=False, possible_transports=None):
1251
1271
        return self.test_branch
1252
1272
 
1253
1273
    def cloning_metadir(self, require_stacking=False):
1254
1274
        return _TestBzrDirFormat()
1255
1275
 
1256
1276
 
1257
 
class _TestBranchFormat(bzrlib.branch.BranchFormat):
 
1277
class _TestBranchFormat(breezy.branch.BranchFormat):
1258
1278
    """Test Branch format for TestBzrDirSprout."""
1259
1279
 
1260
1280
 
1261
 
class _TestBranch(bzrlib.branch.Branch):
 
1281
class _TestBranch(breezy.branch.Branch):
1262
1282
    """Test Branch implementation for TestBzrDirSprout."""
1263
1283
 
1264
 
    def __init__(self, *args, **kwargs):
 
1284
    def __init__(self, transport, *args, **kwargs):
1265
1285
        self._format = _TestBranchFormat()
 
1286
        self._transport = transport
 
1287
        self.base = transport.base
1266
1288
        super(_TestBranch, self).__init__(*args, **kwargs)
1267
1289
        self.calls = []
1268
1290
        self._parent = None
1269
1291
 
1270
1292
    def sprout(self, *args, **kwargs):
1271
1293
        self.calls.append('sprout')
1272
 
        return _TestBranch()
 
1294
        return _TestBranch(self._transport)
1273
1295
 
1274
1296
    def copy_content_into(self, destination, revision_id=None):
1275
1297
        self.calls.append('copy_content_into')
1276
1298
 
 
1299
    def last_revision(self):
 
1300
        return _mod_revision.NULL_REVISION
 
1301
 
1277
1302
    def get_parent(self):
1278
1303
        return self._parent
1279
1304
 
 
1305
    def _get_config(self):
 
1306
        return config.TransportConfig(self._transport, 'branch.conf')
 
1307
 
 
1308
    def _get_config_store(self):
 
1309
        return config.BranchStore(self)
 
1310
 
1280
1311
    def set_parent(self, parent):
1281
1312
        self._parent = parent
1282
1313
 
 
1314
    def lock_read(self):
 
1315
        return lock.LogicalLockResult(self.unlock)
 
1316
 
 
1317
    def unlock(self):
 
1318
        return
 
1319
 
1283
1320
 
1284
1321
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1285
1322
 
1313
1350
 
1314
1351
    def test_sprout_parent(self):
1315
1352
        grandparent_tree = self.make_branch('grandparent')
1316
 
        parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1317
 
        branch_tree = parent.bzrdir.sprout('branch').open_branch()
 
1353
        parent = grandparent_tree.controldir.sprout('parent').open_branch()
 
1354
        branch_tree = parent.controldir.sprout('branch').open_branch()
1318
1355
        self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
1319
1356
 
1320
1357
 
1330
1367
 
1331
1368
    def test_pre_open_actual_exceptions_raised(self):
1332
1369
        count = [0]
 
1370
 
1333
1371
        def fail_once(transport):
1334
1372
            count[0] += 1
1335
1373
            if count[0] == 1:
1341
1379
        self.assertEqual('fail', err._preformatted_string)
1342
1380
 
1343
1381
    def test_post_repo_init(self):
1344
 
        from bzrlib.bzrdir import RepoInitHookParams
 
1382
        from ..controldir import RepoInitHookParams
1345
1383
        calls = []
1346
1384
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1347
 
            calls.append, None)
 
1385
                                               calls.append, None)
1348
1386
        self.make_repository('foo')
1349
1387
        self.assertLength(1, calls)
1350
1388
        params = calls[0]
1351
1389
        self.assertIsInstance(params, RepoInitHookParams)
1352
 
        self.assertTrue(hasattr(params, 'bzrdir'))
 
1390
        self.assertTrue(hasattr(params, 'controldir'))
1353
1391
        self.assertTrue(hasattr(params, 'repository'))
 
1392
 
 
1393
    def test_post_repo_init_hook_repr(self):
 
1394
        param_reprs = []
 
1395
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
 
1396
                                               lambda params: param_reprs.append(repr(params)), None)
 
1397
        self.make_repository('foo')
 
1398
        self.assertLength(1, param_reprs)
 
1399
        param_repr = param_reprs[0]
 
1400
        self.assertStartsWith(param_repr, '<RepoInitHookParams for ')
 
1401
 
 
1402
 
 
1403
class TestGenerateBackupName(TestCaseWithMemoryTransport):
 
1404
    # FIXME: This may need to be unified with test_osutils.TestBackupNames or
 
1405
    # moved to per_bzrdir or per_transport for better coverage ?
 
1406
    # -- vila 20100909
 
1407
 
 
1408
    def setUp(self):
 
1409
        super(TestGenerateBackupName, self).setUp()
 
1410
        self._transport = self.get_transport()
 
1411
        bzrdir.BzrDir.create(self.get_url(),
 
1412
                             possible_transports=[self._transport])
 
1413
        self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
 
1414
 
 
1415
    def test_new(self):
 
1416
        self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
 
1417
 
 
1418
    def test_exiting(self):
 
1419
        self._transport.put_bytes("a.~1~", b"some content")
 
1420
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
 
1421
 
 
1422
 
 
1423
class TestMeta1DirColoFormat(TestCaseWithTransport):
 
1424
    """Tests specific to the meta1 dir with colocated branches format."""
 
1425
 
 
1426
    def test_supports_colo(self):
 
1427
        format = bzrdir.BzrDirMetaFormat1Colo()
 
1428
        self.assertTrue(format.colocated_branches)
 
1429
 
 
1430
    def test_upgrade_from_2a(self):
 
1431
        tree = self.make_branch_and_tree('.', format='2a')
 
1432
        format = bzrdir.BzrDirMetaFormat1Colo()
 
1433
        self.assertTrue(tree.controldir.needs_format_conversion(format))
 
1434
        converter = tree.controldir._format.get_converter(format)
 
1435
        result = converter.convert(tree.controldir, None)
 
1436
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1Colo)
 
1437
        self.assertFalse(result.needs_format_conversion(format))
 
1438
 
 
1439
    def test_downgrade_to_2a(self):
 
1440
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1441
        format = bzrdir.BzrDirMetaFormat1()
 
1442
        self.assertTrue(tree.controldir.needs_format_conversion(format))
 
1443
        converter = tree.controldir._format.get_converter(format)
 
1444
        result = converter.convert(tree.controldir, None)
 
1445
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
 
1446
        self.assertFalse(result.needs_format_conversion(format))
 
1447
 
 
1448
    def test_downgrade_to_2a_too_many_branches(self):
 
1449
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1450
        tree.controldir.create_branch(name="another-colocated-branch")
 
1451
        converter = tree.controldir._format.get_converter(
 
1452
            bzrdir.BzrDirMetaFormat1())
 
1453
        result = converter.convert(tree.controldir, bzrdir.BzrDirMetaFormat1())
 
1454
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
 
1455
 
 
1456
    def test_nested(self):
 
1457
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1458
        tree.controldir.create_branch(name='foo')
 
1459
        tree.controldir.create_branch(name='fool/bla')
 
1460
        self.assertRaises(
 
1461
            errors.ParentBranchExists, tree.controldir.create_branch,
 
1462
            name='foo/bar')
 
1463
 
 
1464
    def test_parent(self):
 
1465
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1466
        tree.controldir.create_branch(name='fool/bla')
 
1467
        tree.controldir.create_branch(name='foo/bar')
 
1468
        self.assertRaises(
 
1469
            errors.AlreadyBranchError, tree.controldir.create_branch,
 
1470
            name='foo')
 
1471
 
 
1472
    def test_supports_relative_reference(self):
 
1473
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1474
        target1 = tree.controldir.create_branch(name='target1')
 
1475
        target2 = tree.controldir.create_branch(name='target2')
 
1476
        source = tree.controldir.set_branch_reference(target1, name='source')
 
1477
        self.assertEqual(
 
1478
            target1.user_url, tree.controldir.open_branch('source').user_url)
 
1479
        source.controldir.get_branch_transport(None, 'source').put_bytes(
 
1480
            'location', b'file:,branch=target2')
 
1481
        self.assertEqual(
 
1482
            target2.user_url, tree.controldir.open_branch('source').user_url)
 
1483
 
 
1484
 
 
1485
class SampleBzrFormat(bzrdir.BzrFormat):
 
1486
 
 
1487
    @classmethod
 
1488
    def get_format_string(cls):
 
1489
        return b"First line\n"
 
1490
 
 
1491
 
 
1492
class TestBzrFormat(TestCase):
 
1493
    """Tests for BzrFormat."""
 
1494
 
 
1495
    def test_as_string(self):
 
1496
        format = SampleBzrFormat()
 
1497
        format.features = {b"foo": b"required"}
 
1498
        self.assertEqual(format.as_string(),
 
1499
                         b"First line\n"
 
1500
                         b"required foo\n")
 
1501
        format.features[b"another"] = b"optional"
 
1502
        self.assertEqual(format.as_string(),
 
1503
                         b"First line\n"
 
1504
                         b"optional another\n"
 
1505
                         b"required foo\n")
 
1506
 
 
1507
    def test_network_name(self):
 
1508
        # The network string should include the feature info
 
1509
        format = SampleBzrFormat()
 
1510
        format.features = {b"foo": b"required"}
 
1511
        self.assertEqual(
 
1512
            b"First line\nrequired foo\n",
 
1513
            format.network_name())
 
1514
 
 
1515
    def test_from_string_no_features(self):
 
1516
        # No features
 
1517
        format = SampleBzrFormat.from_string(
 
1518
            b"First line\n")
 
1519
        self.assertEqual({}, format.features)
 
1520
 
 
1521
    def test_from_string_with_feature(self):
 
1522
        # Proper feature
 
1523
        format = SampleBzrFormat.from_string(
 
1524
            b"First line\nrequired foo\n")
 
1525
        self.assertEqual(b"required", format.features.get(b"foo"))
 
1526
 
 
1527
    def test_from_string_format_string_mismatch(self):
 
1528
        # The first line has to match the format string
 
1529
        self.assertRaises(AssertionError, SampleBzrFormat.from_string,
 
1530
                          b"Second line\nrequired foo\n")
 
1531
 
 
1532
    def test_from_string_missing_space(self):
 
1533
        # At least one space is required in the feature lines
 
1534
        self.assertRaises(errors.ParseFormatError, SampleBzrFormat.from_string,
 
1535
                          b"First line\nfoo\n")
 
1536
 
 
1537
    def test_from_string_with_spaces(self):
 
1538
        # Feature with spaces (in case we add stuff like this in the future)
 
1539
        format = SampleBzrFormat.from_string(
 
1540
            b"First line\nrequired foo with spaces\n")
 
1541
        self.assertEqual(b"required", format.features.get(b"foo with spaces"))
 
1542
 
 
1543
    def test_eq(self):
 
1544
        format1 = SampleBzrFormat()
 
1545
        format1.features = {b"nested-trees": b"optional"}
 
1546
        format2 = SampleBzrFormat()
 
1547
        format2.features = {b"nested-trees": b"optional"}
 
1548
        self.assertEqual(format1, format1)
 
1549
        self.assertEqual(format1, format2)
 
1550
        format3 = SampleBzrFormat()
 
1551
        self.assertNotEqual(format1, format3)
 
1552
 
 
1553
    def test_check_support_status_optional(self):
 
1554
        # Optional, so silently ignore
 
1555
        format = SampleBzrFormat()
 
1556
        format.features = {b"nested-trees": b"optional"}
 
1557
        format.check_support_status(True)
 
1558
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
 
1559
        SampleBzrFormat.register_feature(b"nested-trees")
 
1560
        format.check_support_status(True)
 
1561
 
 
1562
    def test_check_support_status_required(self):
 
1563
        # Optional, so trigger an exception
 
1564
        format = SampleBzrFormat()
 
1565
        format.features = {b"nested-trees": b"required"}
 
1566
        self.assertRaises(bzrdir.MissingFeature, format.check_support_status,
 
1567
                          True)
 
1568
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
 
1569
        SampleBzrFormat.register_feature(b"nested-trees")
 
1570
        format.check_support_status(True)
 
1571
 
 
1572
    def test_check_support_status_unknown(self):
 
1573
        # treat unknown necessity as required
 
1574
        format = SampleBzrFormat()
 
1575
        format.features = {b"nested-trees": b"unknown"}
 
1576
        self.assertRaises(bzrdir.MissingFeature, format.check_support_status,
 
1577
                          True)
 
1578
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
 
1579
        SampleBzrFormat.register_feature(b"nested-trees")
 
1580
        format.check_support_status(True)
 
1581
 
 
1582
    def test_feature_already_registered(self):
 
1583
        # a feature can only be registered once
 
1584
        self.addCleanup(SampleBzrFormat.unregister_feature, b"nested-trees")
 
1585
        SampleBzrFormat.register_feature(b"nested-trees")
 
1586
        self.assertRaises(bzrdir.FeatureAlreadyRegistered,
 
1587
                          SampleBzrFormat.register_feature, b"nested-trees")
 
1588
 
 
1589
    def test_feature_with_space(self):
 
1590
        # spaces are not allowed in feature names
 
1591
        self.assertRaises(ValueError, SampleBzrFormat.register_feature,
 
1592
                          b"nested trees")