~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-02-01 23:48:08 UTC
  • mfrom: (2225.1.6 revert)
  • Revision ID: pqm@pqm.ubuntu.com-20070201234808-3b1302d73474bd8c
Display changes made by revert

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
For interface contract tests, see tests/bzr_dir_implementations.
20
20
"""
21
21
 
 
22
import os.path
22
23
from StringIO import StringIO
23
24
 
 
25
from bzrlib import (
 
26
    help_topics,
 
27
    symbol_versioning,
 
28
    urlutils,
 
29
    )
24
30
import bzrlib.branch
25
31
import bzrlib.bzrdir as bzrdir
26
32
import bzrlib.errors as errors
29
35
                           UnsupportedFormatError,
30
36
                           )
31
37
import bzrlib.repository as repository
32
 
from bzrlib.tests import TestCase, TestCaseWithTransport
 
38
from bzrlib.tests import TestCase, TestCaseWithTransport, test_sftp_transport
 
39
from bzrlib.tests.HttpServer import HttpServer
33
40
from bzrlib.transport import get_transport
34
 
from bzrlib.transport.http import HttpServer
35
41
from bzrlib.transport.memory import MemoryServer
36
42
import bzrlib.workingtree as workingtree
37
43
 
42
48
        old_format = bzrdir.BzrDirFormat.get_default_format()
43
49
        # default is BzrDirFormat6
44
50
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
45
 
        bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
 
51
        self.applyDeprecated(symbol_versioning.zero_fourteen, 
 
52
                             bzrdir.BzrDirFormat.set_default_format, 
 
53
                             SampleBzrDirFormat())
46
54
        # creating a bzr dir should now create an instrumented dir.
47
55
        try:
48
56
            result = bzrdir.BzrDir.create('memory:///')
49
57
            self.failUnless(isinstance(result, SampleBzrDir))
50
58
        finally:
51
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
59
            self.applyDeprecated(symbol_versioning.zero_fourteen,
 
60
                bzrdir.BzrDirFormat.set_default_format, old_format)
52
61
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
53
62
 
54
63
 
 
64
class TestFormatRegistry(TestCase):
 
65
 
 
66
    def make_format_registry(self):
 
67
        my_format_registry = bzrdir.BzrDirFormatRegistry()
 
68
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
 
69
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
70
            ' repositories', deprecated=True)
 
71
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
 
72
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
 
73
        my_format_registry.register_metadir('knit', 'RepositoryFormatKnit1',
 
74
            'Format using knits')
 
75
        my_format_registry.set_default('knit')
 
76
        my_format_registry.register_metadir('metaweave', 'RepositoryFormat7',
 
77
            'Transitional format in 0.8.  Slower than knit.', deprecated=True)
 
78
        my_format_registry.register_metadir('experimental-knit2', 
 
79
                                            'RepositoryFormatKnit2',
 
80
            'Experimental successor to knit.  Use at your own risk.')
 
81
        return my_format_registry
 
82
 
 
83
    def test_format_registry(self):
 
84
        my_format_registry = self.make_format_registry()
 
85
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
 
86
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
87
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
88
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
89
        my_bzrdir = my_format_registry.make_bzrdir('default')
 
90
        self.assertIsInstance(my_bzrdir.repository_format, 
 
91
            repository.RepositoryFormatKnit1)
 
92
        my_bzrdir = my_format_registry.make_bzrdir('knit')
 
93
        self.assertIsInstance(my_bzrdir.repository_format, 
 
94
            repository.RepositoryFormatKnit1)
 
95
        my_bzrdir = my_format_registry.make_bzrdir('metaweave')
 
96
        self.assertIsInstance(my_bzrdir.repository_format, 
 
97
            repository.RepositoryFormat7)
 
98
 
 
99
    def test_get_help(self):
 
100
        my_format_registry = self.make_format_registry()
 
101
        self.assertEqual('Format registered lazily',
 
102
                         my_format_registry.get_help('lazy'))
 
103
        self.assertEqual('Format using knits', 
 
104
                         my_format_registry.get_help('knit'))
 
105
        self.assertEqual('Format using knits', 
 
106
                         my_format_registry.get_help('default'))
 
107
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
 
108
                         ' checkouts or shared repositories', 
 
109
                         my_format_registry.get_help('weave'))
 
110
        
 
111
    def test_help_topic(self):
 
112
        topics = help_topics.HelpTopicRegistry()
 
113
        topics.register('formats', self.make_format_registry().help_topic, 
 
114
                        'Directory formats')
 
115
        topic = topics.get_detail('formats')
 
116
        new, deprecated = topic.split('Deprecated formats')
 
117
        self.assertContainsRe(new, 'Bazaar directory formats')
 
118
        self.assertContainsRe(new, 
 
119
            '  knit/default:\n    \(native\) Format using knits\n')
 
120
        self.assertContainsRe(deprecated, 
 
121
            '  lazy:\n    \(native\) Format registered lazily\n')
 
122
 
 
123
    def test_set_default_repository(self):
 
124
        default_factory = bzrdir.format_registry.get('default')
 
125
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
 
126
                       if v == default_factory and k != 'default'][0]
 
127
        bzrdir.format_registry.set_default_repository('metaweave')
 
128
        try:
 
129
            self.assertIs(bzrdir.format_registry.get('metaweave'),
 
130
                          bzrdir.format_registry.get('default'))
 
131
            self.assertIs(
 
132
                repository.RepositoryFormat.get_default_format().__class__,
 
133
                repository.RepositoryFormat7)
 
134
        finally:
 
135
            bzrdir.format_registry.set_default_repository(old_default)
 
136
 
55
137
class SampleBranch(bzrlib.branch.Branch):
56
138
    """A dummy branch for guess what, dummy use."""
57
139
 
94
176
        """Create a bzr dir."""
95
177
        t = get_transport(url)
96
178
        t.mkdir('.bzr')
97
 
        t.put('.bzr/branch-format', StringIO(self.get_format_string()))
 
179
        t.put_bytes('.bzr/branch-format', self.get_format_string())
98
180
        return SampleBzrDir(t, self)
99
181
 
100
182
    def is_supported(self):
129
211
    def test_find_format_unknown_format(self):
130
212
        t = get_transport(self.get_url())
131
213
        t.mkdir('.bzr')
132
 
        t.put('.bzr/branch-format', StringIO())
 
214
        t.put_bytes('.bzr/branch-format', '')
133
215
        self.assertRaises(UnknownFormatError,
134
216
                          bzrdir.BzrDirFormat.find_format,
135
217
                          get_transport('.'))
155
237
 
156
238
    def test_create_repository(self):
157
239
        format = SampleBzrDirFormat()
158
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
159
 
        bzrdir.BzrDirFormat.set_default_format(format)
160
 
        try:
161
 
            repo = bzrdir.BzrDir.create_repository(self.get_url())
162
 
            self.assertEqual('A repository', repo)
163
 
        finally:
164
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
240
        repo = bzrdir.BzrDir.create_repository(self.get_url(), format=format)
 
241
        self.assertEqual('A repository', repo)
165
242
 
166
243
    def test_create_repository_shared(self):
167
244
        old_format = bzrdir.BzrDirFormat.get_default_format()
176
253
    def test_create_repository_under_shared(self):
177
254
        # an explicit create_repository always does so.
178
255
        # we trust the format is right from the 'create_repository test'
179
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
180
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
181
 
        try:
182
 
            self.make_repository('.', shared=True)
183
 
            repo = bzrdir.BzrDir.create_repository(self.get_url('child'))
184
 
            self.assertTrue(isinstance(repo, repository.Repository))
185
 
            self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
186
 
        finally:
187
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
256
        format = bzrdir.format_registry.make_bzrdir('knit')
 
257
        self.make_repository('.', shared=True, format=format)
 
258
        repo = bzrdir.BzrDir.create_repository(self.get_url('child'),
 
259
                                               format=format)
 
260
        self.assertTrue(isinstance(repo, repository.Repository))
 
261
        self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
188
262
 
189
263
    def test_create_branch_and_repo_uses_default(self):
190
264
        format = SampleBzrDirFormat()
191
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
192
 
        bzrdir.BzrDirFormat.set_default_format(format)
193
 
        try:
194
 
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
195
 
            self.assertTrue(isinstance(branch, SampleBranch))
196
 
        finally:
197
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
265
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(), 
 
266
                                                      format=format)
 
267
        self.assertTrue(isinstance(branch, SampleBranch))
198
268
 
199
269
    def test_create_branch_and_repo_under_shared(self):
200
270
        # creating a branch and repo in a shared repo uses the
201
271
        # shared repository
202
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
203
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
204
 
        try:
205
 
            self.make_repository('.', shared=True)
206
 
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'))
207
 
            self.assertRaises(errors.NoRepositoryPresent,
208
 
                              branch.bzrdir.open_repository)
209
 
        finally:
210
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
272
        format = bzrdir.format_registry.make_bzrdir('knit')
 
273
        self.make_repository('.', shared=True, format=format)
 
274
        branch = bzrdir.BzrDir.create_branch_and_repo(
 
275
            self.get_url('child'), format=format)
 
276
        self.assertRaises(errors.NoRepositoryPresent,
 
277
                          branch.bzrdir.open_repository)
211
278
 
212
279
    def test_create_branch_and_repo_under_shared_force_new(self):
213
280
        # creating a branch and repo in a shared repo can be forced to 
214
281
        # make a new repo
215
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
216
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
217
 
        try:
218
 
            self.make_repository('.', shared=True)
219
 
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
220
 
                                                          force_new_repo=True)
221
 
            branch.bzrdir.open_repository()
222
 
        finally:
223
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
282
        format = bzrdir.format_registry.make_bzrdir('knit')
 
283
        self.make_repository('.', shared=True, format=format)
 
284
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
 
285
                                                      force_new_repo=True,
 
286
                                                      format=format)
 
287
        branch.bzrdir.open_repository()
224
288
 
225
289
    def test_create_standalone_working_tree(self):
226
290
        format = SampleBzrDirFormat()
227
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
228
 
        bzrdir.BzrDirFormat.set_default_format(format)
229
 
        try:
230
 
            # note this is deliberately readonly, as this failure should 
231
 
            # occur before any writes.
232
 
            self.assertRaises(errors.NotLocalUrl,
233
 
                              bzrdir.BzrDir.create_standalone_workingtree,
234
 
                              self.get_readonly_url())
235
 
            tree = bzrdir.BzrDir.create_standalone_workingtree('.')
236
 
            self.assertEqual('A tree', tree)
237
 
        finally:
238
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
291
        # note this is deliberately readonly, as this failure should 
 
292
        # occur before any writes.
 
293
        self.assertRaises(errors.NotLocalUrl,
 
294
                          bzrdir.BzrDir.create_standalone_workingtree,
 
295
                          self.get_readonly_url(), format=format)
 
296
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
 
297
                                                           format=format)
 
298
        self.assertEqual('A tree', tree)
239
299
 
240
300
    def test_create_standalone_working_tree_under_shared_repo(self):
241
301
        # create standalone working tree always makes a repo.
242
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
243
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
244
 
        try:
245
 
            self.make_repository('.', shared=True)
246
 
            # note this is deliberately readonly, as this failure should 
247
 
            # occur before any writes.
248
 
            self.assertRaises(errors.NotLocalUrl,
249
 
                              bzrdir.BzrDir.create_standalone_workingtree,
250
 
                              self.get_readonly_url('child'))
251
 
            tree = bzrdir.BzrDir.create_standalone_workingtree('child')
252
 
            tree.bzrdir.open_repository()
253
 
        finally:
254
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
302
        format = bzrdir.format_registry.make_bzrdir('knit')
 
303
        self.make_repository('.', shared=True, format=format)
 
304
        # note this is deliberately readonly, as this failure should 
 
305
        # occur before any writes.
 
306
        self.assertRaises(errors.NotLocalUrl,
 
307
                          bzrdir.BzrDir.create_standalone_workingtree,
 
308
                          self.get_readonly_url('child'), format=format)
 
309
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
 
310
            format=format)
 
311
        tree.bzrdir.open_repository()
255
312
 
256
313
    def test_create_branch_convenience(self):
257
314
        # outside a repo the default convenience output is a repo+branch_tree
258
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
259
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
260
 
        try:
261
 
            branch = bzrdir.BzrDir.create_branch_convenience('.')
262
 
            branch.bzrdir.open_workingtree()
263
 
            branch.bzrdir.open_repository()
264
 
        finally:
265
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
315
        format = bzrdir.format_registry.make_bzrdir('knit')
 
316
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
 
317
        branch.bzrdir.open_workingtree()
 
318
        branch.bzrdir.open_repository()
266
319
 
267
320
    def test_create_branch_convenience_root(self):
268
321
        """Creating a branch at the root of a fs should work."""
269
322
        self.transport_server = MemoryServer
270
323
        # outside a repo the default convenience output is a repo+branch_tree
271
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
272
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
273
 
        try:
274
 
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url())
275
 
            self.assertRaises(errors.NoWorkingTree,
276
 
                              branch.bzrdir.open_workingtree)
277
 
            branch.bzrdir.open_repository()
278
 
        finally:
279
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
324
        format = bzrdir.format_registry.make_bzrdir('knit')
 
325
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
 
326
                                                         format=format)
 
327
        self.assertRaises(errors.NoWorkingTree,
 
328
                          branch.bzrdir.open_workingtree)
 
329
        branch.bzrdir.open_repository()
280
330
 
281
331
    def test_create_branch_convenience_under_shared_repo(self):
282
332
        # inside a repo the default convenience output is a branch+ follow the
283
333
        # repo tree policy
284
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
285
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
286
 
        try:
287
 
            self.make_repository('.', shared=True)
288
 
            branch = bzrdir.BzrDir.create_branch_convenience('child')
289
 
            branch.bzrdir.open_workingtree()
290
 
            self.assertRaises(errors.NoRepositoryPresent,
291
 
                              branch.bzrdir.open_repository)
292
 
        finally:
293
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
334
        format = bzrdir.format_registry.make_bzrdir('knit')
 
335
        self.make_repository('.', shared=True, format=format)
 
336
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
337
            format=format)
 
338
        branch.bzrdir.open_workingtree()
 
339
        self.assertRaises(errors.NoRepositoryPresent,
 
340
                          branch.bzrdir.open_repository)
294
341
            
295
342
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
296
343
        # inside a repo the default convenience output is a branch+ follow the
297
344
        # repo tree policy but we can override that
298
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
299
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
300
 
        try:
301
 
            self.make_repository('.', shared=True)
302
 
            branch = bzrdir.BzrDir.create_branch_convenience('child',
303
 
                force_new_tree=False)
304
 
            self.assertRaises(errors.NoWorkingTree,
305
 
                              branch.bzrdir.open_workingtree)
306
 
            self.assertRaises(errors.NoRepositoryPresent,
307
 
                              branch.bzrdir.open_repository)
308
 
        finally:
309
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
345
        format = bzrdir.format_registry.make_bzrdir('knit')
 
346
        self.make_repository('.', shared=True, format=format)
 
347
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
348
            force_new_tree=False, format=format)
 
349
        self.assertRaises(errors.NoWorkingTree,
 
350
                          branch.bzrdir.open_workingtree)
 
351
        self.assertRaises(errors.NoRepositoryPresent,
 
352
                          branch.bzrdir.open_repository)
310
353
            
311
354
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
312
355
        # inside a repo the default convenience output is a branch+ follow the
313
356
        # repo tree policy
314
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
315
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
316
 
        try:
317
 
            repo = self.make_repository('.', shared=True)
318
 
            repo.set_make_working_trees(False)
319
 
            branch = bzrdir.BzrDir.create_branch_convenience('child')
320
 
            self.assertRaises(errors.NoWorkingTree,
321
 
                              branch.bzrdir.open_workingtree)
322
 
            self.assertRaises(errors.NoRepositoryPresent,
323
 
                              branch.bzrdir.open_repository)
324
 
        finally:
325
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
357
        format = bzrdir.format_registry.make_bzrdir('knit')
 
358
        repo = self.make_repository('.', shared=True, format=format)
 
359
        repo.set_make_working_trees(False)
 
360
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
 
361
                                                         format=format)
 
362
        self.assertRaises(errors.NoWorkingTree,
 
363
                          branch.bzrdir.open_workingtree)
 
364
        self.assertRaises(errors.NoRepositoryPresent,
 
365
                          branch.bzrdir.open_repository)
326
366
 
327
367
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
328
368
        # inside a repo the default convenience output is a branch+ follow the
329
369
        # repo tree policy but we can override that
330
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
331
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
332
 
        try:
333
 
            repo = self.make_repository('.', shared=True)
334
 
            repo.set_make_working_trees(False)
335
 
            branch = bzrdir.BzrDir.create_branch_convenience('child',
336
 
                force_new_tree=True)
337
 
            branch.bzrdir.open_workingtree()
338
 
            self.assertRaises(errors.NoRepositoryPresent,
339
 
                              branch.bzrdir.open_repository)
340
 
        finally:
341
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
370
        format = bzrdir.format_registry.make_bzrdir('knit')
 
371
        repo = self.make_repository('.', shared=True, format=format)
 
372
        repo.set_make_working_trees(False)
 
373
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
374
            force_new_tree=True, format=format)
 
375
        branch.bzrdir.open_workingtree()
 
376
        self.assertRaises(errors.NoRepositoryPresent,
 
377
                          branch.bzrdir.open_repository)
342
378
 
343
379
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
344
380
        # inside a repo the default convenience output is overridable to give
345
381
        # repo+branch+tree
346
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
347
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
348
 
        try:
349
 
            self.make_repository('.', shared=True)
350
 
            branch = bzrdir.BzrDir.create_branch_convenience('child',
351
 
                force_new_repo=True)
352
 
            branch.bzrdir.open_repository()
353
 
            branch.bzrdir.open_workingtree()
354
 
        finally:
355
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
382
        format = bzrdir.format_registry.make_bzrdir('knit')
 
383
        self.make_repository('.', shared=True, format=format)
 
384
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
385
            force_new_repo=True, format=format)
 
386
        branch.bzrdir.open_repository()
 
387
        branch.bzrdir.open_workingtree()
356
388
 
357
389
 
358
390
class ChrootedTests(TestCaseWithTransport):
392
424
            get_transport(self.get_readonly_url('g/p/q')))
393
425
        self.assertEqual('g/p/q', relpath)
394
426
 
 
427
    def test_open_containing_tree_or_branch(self):
 
428
        def local_branch_path(branch):
 
429
             return os.path.realpath(
 
430
                urlutils.local_path_from_url(branch.base))
 
431
 
 
432
        self.make_branch_and_tree('topdir')
 
433
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
 
434
            'topdir/foo')
 
435
        self.assertEqual(os.path.realpath('topdir'),
 
436
                         os.path.realpath(tree.basedir))
 
437
        self.assertEqual(os.path.realpath('topdir'),
 
438
                         local_branch_path(branch))
 
439
        self.assertIs(tree.bzrdir, branch.bzrdir)
 
440
        self.assertEqual('foo', relpath)
 
441
        self.make_branch('topdir/foo')
 
442
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
 
443
            'topdir/foo')
 
444
        self.assertIs(tree, None)
 
445
        self.assertEqual(os.path.realpath('topdir/foo'),
 
446
                         local_branch_path(branch))
 
447
        self.assertEqual('', relpath)
 
448
 
 
449
    def test_open_from_transport(self):
 
450
        # transport pointing at bzrdir should give a bzrdir with root transport
 
451
        # set to the given transport
 
452
        control = bzrdir.BzrDir.create(self.get_url())
 
453
        transport = get_transport(self.get_url())
 
454
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
 
455
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
 
456
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
 
457
        
 
458
    def test_open_from_transport_no_bzrdir(self):
 
459
        transport = get_transport(self.get_url())
 
460
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
461
                          transport)
 
462
 
 
463
    def test_open_from_transport_bzrdir_in_parent(self):
 
464
        control = bzrdir.BzrDir.create(self.get_url())
 
465
        transport = get_transport(self.get_url())
 
466
        transport.mkdir('subdir')
 
467
        transport = transport.clone('subdir')
 
468
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
469
                          transport)
 
470
 
395
471
 
396
472
class TestMeta1DirFormat(TestCaseWithTransport):
397
473
    """Tests specific to the meta1 dir format."""
446
522
        # format 5 dirs need a conversion if they are not the default.
447
523
        # and they start of not the default.
448
524
        old_format = bzrdir.BzrDirFormat.get_default_format()
449
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
 
525
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
450
526
        try:
451
527
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
452
528
            self.assertFalse(dir.needs_format_conversion())
453
529
        finally:
454
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
530
            bzrdir.BzrDirFormat._set_default_format(old_format)
455
531
        self.assertTrue(dir.needs_format_conversion())
456
532
 
457
533
 
481
557
    def test_needs_conversion(self):
482
558
        # format 6 dirs need an conversion if they are not the default.
483
559
        old_format = bzrdir.BzrDirFormat.get_default_format()
484
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
560
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
485
561
        try:
486
562
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
487
563
            self.assertTrue(dir.needs_format_conversion())
488
564
        finally:
489
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
565
            bzrdir.BzrDirFormat._set_default_format(old_format)
490
566
 
491
567
 
492
568
class NotBzrDir(bzrlib.bzrdir.BzrDir):
563
639
    
564
640
    def test_create_branch_convenience(self):
565
641
        # outside a repo the default convenience output is a repo+branch_tree
566
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
567
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
568
 
        try:
569
 
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url('foo'))
570
 
            self.assertRaises(errors.NoWorkingTree,
571
 
                              branch.bzrdir.open_workingtree)
572
 
            branch.bzrdir.open_repository()
573
 
        finally:
574
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
642
        format = bzrdir.format_registry.make_bzrdir('knit')
 
643
        branch = bzrdir.BzrDir.create_branch_convenience(
 
644
            self.get_url('foo'), format=format)
 
645
        self.assertRaises(errors.NoWorkingTree,
 
646
                          branch.bzrdir.open_workingtree)
 
647
        branch.bzrdir.open_repository()
575
648
 
576
649
    def test_create_branch_convenience_force_tree_not_local_fails(self):
577
650
        # outside a repo the default convenience output is a repo+branch_tree
578
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
579
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
580
 
        try:
581
 
            self.assertRaises(errors.NotLocalUrl,
582
 
                bzrdir.BzrDir.create_branch_convenience,
583
 
                self.get_url('foo'),
584
 
                force_new_tree=True)
585
 
            t = get_transport(self.get_url('.'))
586
 
            self.assertFalse(t.has('foo'))
587
 
        finally:
588
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
651
        format = bzrdir.format_registry.make_bzrdir('knit')
 
652
        self.assertRaises(errors.NotLocalUrl,
 
653
            bzrdir.BzrDir.create_branch_convenience,
 
654
            self.get_url('foo'),
 
655
            force_new_tree=True,
 
656
            format=format)
 
657
        t = get_transport(self.get_url('.'))
 
658
        self.assertFalse(t.has('foo'))
589
659
 
590
660
    def test_clone(self):
591
661
        # clone into a nonlocal path works
592
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
593
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
594
 
        try:
595
 
            branch = bzrdir.BzrDir.create_branch_convenience('local')
596
 
        finally:
597
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
662
        format = bzrdir.format_registry.make_bzrdir('knit')
 
663
        branch = bzrdir.BzrDir.create_branch_convenience('local',
 
664
                                                         format=format)
598
665
        branch.bzrdir.open_workingtree()
599
666
        result = branch.bzrdir.clone(self.get_url('remote'))
600
667
        self.assertRaises(errors.NoWorkingTree,
602
669
        result.open_branch()
603
670
        result.open_repository()
604
671
 
 
672
 
 
673
class TestRemoteSFTP(test_sftp_transport.TestCaseWithSFTPServer):
 
674
 
 
675
    def test_open_containing_tree_or_branch(self):
 
676
        tree = self.make_branch_and_tree('tree')
 
677
        bzrdir.BzrDir.open_containing_tree_or_branch(self.get_url('tree'))