/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-09-22 02:38:35 UTC
  • mfrom: (7058.5.4 upload-symlin)
  • Revision ID: breezy.the.bot@gmail.com-20180922023835-wb9nczxp63jpeudb
Add symlink support to 'brz upload'.

Merged from https://code.launchpad.net/~jelmer/brz/upload-symlinks/+merge/355061

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