~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: John Arbash Meinel
  • Date: 2006-10-11 00:23:23 UTC
  • mfrom: (2070 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20061011002323-82ba88c293d7caff
[merge] bzr.dev 2070

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
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
23
22
from StringIO import StringIO
24
23
 
25
 
from bzrlib import (
26
 
    bzrdir,
27
 
    errors,
28
 
    help_topics,
29
 
    repository,
30
 
    symbol_versioning,
31
 
    urlutils,
32
 
    workingtree,
33
 
    )
34
24
import bzrlib.branch
 
25
import bzrlib.bzrdir as bzrdir
 
26
import bzrlib.errors as errors
35
27
from bzrlib.errors import (NotBranchError,
36
28
                           UnknownFormatError,
37
29
                           UnsupportedFormatError,
38
30
                           )
39
 
from bzrlib.tests import TestCase, TestCaseWithTransport, test_sftp_transport
40
 
from bzrlib.tests.HttpServer import HttpServer
 
31
import bzrlib.repository as repository
 
32
from bzrlib.tests import TestCase, TestCaseWithTransport
41
33
from bzrlib.transport import get_transport
 
34
from bzrlib.transport.http import HttpServer
42
35
from bzrlib.transport.memory import MemoryServer
43
 
from bzrlib.repofmt import knitrepo, weaverepo
 
36
import bzrlib.workingtree as workingtree
44
37
 
45
38
 
46
39
class TestDefaultFormat(TestCase):
49
42
        old_format = bzrdir.BzrDirFormat.get_default_format()
50
43
        # default is BzrDirFormat6
51
44
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
52
 
        self.applyDeprecated(symbol_versioning.zero_fourteen, 
53
 
                             bzrdir.BzrDirFormat.set_default_format, 
54
 
                             SampleBzrDirFormat())
 
45
        bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
55
46
        # creating a bzr dir should now create an instrumented dir.
56
47
        try:
57
48
            result = bzrdir.BzrDir.create('memory:///')
58
49
            self.failUnless(isinstance(result, SampleBzrDir))
59
50
        finally:
60
 
            self.applyDeprecated(symbol_versioning.zero_fourteen,
61
 
                bzrdir.BzrDirFormat.set_default_format, old_format)
 
51
            bzrdir.BzrDirFormat.set_default_format(old_format)
62
52
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
63
53
 
64
54
 
65
 
class TestFormatRegistry(TestCase):
66
 
 
67
 
    def make_format_registry(self):
68
 
        my_format_registry = bzrdir.BzrDirFormatRegistry()
69
 
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
70
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
71
 
            ' repositories', deprecated=True)
72
 
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
73
 
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
74
 
        my_format_registry.register_metadir('knit',
75
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
76
 
            'Format using knits',
77
 
            )
78
 
        my_format_registry.set_default('knit')
79
 
        my_format_registry.register_metadir(
80
 
            'branch6',
81
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
82
 
            'Experimental successor to knit.  Use at your own risk.',
83
 
            branch_format='bzrlib.branch.BzrBranchFormat6')
84
 
        return my_format_registry
85
 
 
86
 
    def test_format_registry(self):
87
 
        my_format_registry = self.make_format_registry()
88
 
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
89
 
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
90
 
        my_bzrdir = my_format_registry.make_bzrdir('weave')
91
 
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
92
 
        my_bzrdir = my_format_registry.make_bzrdir('default')
93
 
        self.assertIsInstance(my_bzrdir.repository_format, 
94
 
            knitrepo.RepositoryFormatKnit1)
95
 
        my_bzrdir = my_format_registry.make_bzrdir('knit')
96
 
        self.assertIsInstance(my_bzrdir.repository_format, 
97
 
            knitrepo.RepositoryFormatKnit1)
98
 
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
99
 
        self.assertIsInstance(my_bzrdir.get_branch_format(),
100
 
                              bzrlib.branch.BzrBranchFormat6)
101
 
 
102
 
    def test_get_help(self):
103
 
        my_format_registry = self.make_format_registry()
104
 
        self.assertEqual('Format registered lazily',
105
 
                         my_format_registry.get_help('lazy'))
106
 
        self.assertEqual('Format using knits', 
107
 
                         my_format_registry.get_help('knit'))
108
 
        self.assertEqual('Format using knits', 
109
 
                         my_format_registry.get_help('default'))
110
 
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
111
 
                         ' checkouts or shared repositories', 
112
 
                         my_format_registry.get_help('weave'))
113
 
        
114
 
    def test_help_topic(self):
115
 
        topics = help_topics.HelpTopicRegistry()
116
 
        topics.register('formats', self.make_format_registry().help_topic, 
117
 
                        'Directory formats')
118
 
        topic = topics.get_detail('formats')
119
 
        new, deprecated = topic.split('Deprecated formats')
120
 
        self.assertContainsRe(new, 'Bazaar directory formats')
121
 
        self.assertContainsRe(new, 
122
 
            '  knit/default:\n    \(native\) Format using knits\n')
123
 
        self.assertContainsRe(deprecated, 
124
 
            '  lazy:\n    \(native\) Format registered lazily\n')
125
 
 
126
 
    def test_set_default_repository(self):
127
 
        default_factory = bzrdir.format_registry.get('default')
128
 
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
129
 
                       if v == default_factory and k != 'default'][0]
130
 
        bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
131
 
        try:
132
 
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
133
 
                          bzrdir.format_registry.get('default'))
134
 
            self.assertIs(
135
 
                repository.RepositoryFormat.get_default_format().__class__,
136
 
                knitrepo.RepositoryFormatKnit3)
137
 
        finally:
138
 
            bzrdir.format_registry.set_default_repository(old_default)
139
 
 
140
 
 
141
55
class SampleBranch(bzrlib.branch.Branch):
142
56
    """A dummy branch for guess what, dummy use."""
143
57
 
241
155
 
242
156
    def test_create_repository(self):
243
157
        format = SampleBzrDirFormat()
244
 
        repo = bzrdir.BzrDir.create_repository(self.get_url(), format=format)
245
 
        self.assertEqual('A repository', repo)
 
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)
246
165
 
247
166
    def test_create_repository_shared(self):
248
167
        old_format = bzrdir.BzrDirFormat.get_default_format()
257
176
    def test_create_repository_under_shared(self):
258
177
        # an explicit create_repository always does so.
259
178
        # we trust the format is right from the 'create_repository test'
260
 
        format = bzrdir.format_registry.make_bzrdir('knit')
261
 
        self.make_repository('.', shared=True, format=format)
262
 
        repo = bzrdir.BzrDir.create_repository(self.get_url('child'),
263
 
                                               format=format)
264
 
        self.assertTrue(isinstance(repo, repository.Repository))
265
 
        self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
 
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)
266
188
 
267
189
    def test_create_branch_and_repo_uses_default(self):
268
190
        format = SampleBzrDirFormat()
269
 
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(), 
270
 
                                                      format=format)
271
 
        self.assertTrue(isinstance(branch, SampleBranch))
 
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)
272
198
 
273
199
    def test_create_branch_and_repo_under_shared(self):
274
200
        # creating a branch and repo in a shared repo uses the
275
201
        # shared repository
276
 
        format = bzrdir.format_registry.make_bzrdir('knit')
277
 
        self.make_repository('.', shared=True, format=format)
278
 
        branch = bzrdir.BzrDir.create_branch_and_repo(
279
 
            self.get_url('child'), format=format)
280
 
        self.assertRaises(errors.NoRepositoryPresent,
281
 
                          branch.bzrdir.open_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)
282
211
 
283
212
    def test_create_branch_and_repo_under_shared_force_new(self):
284
213
        # creating a branch and repo in a shared repo can be forced to 
285
214
        # make a new repo
286
 
        format = bzrdir.format_registry.make_bzrdir('knit')
287
 
        self.make_repository('.', shared=True, format=format)
288
 
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
289
 
                                                      force_new_repo=True,
290
 
                                                      format=format)
291
 
        branch.bzrdir.open_repository()
 
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)
292
224
 
293
225
    def test_create_standalone_working_tree(self):
294
226
        format = SampleBzrDirFormat()
295
 
        # note this is deliberately readonly, as this failure should 
296
 
        # occur before any writes.
297
 
        self.assertRaises(errors.NotLocalUrl,
298
 
                          bzrdir.BzrDir.create_standalone_workingtree,
299
 
                          self.get_readonly_url(), format=format)
300
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
301
 
                                                           format=format)
302
 
        self.assertEqual('A tree', tree)
 
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)
303
239
 
304
240
    def test_create_standalone_working_tree_under_shared_repo(self):
305
241
        # create standalone working tree always makes a repo.
306
 
        format = bzrdir.format_registry.make_bzrdir('knit')
307
 
        self.make_repository('.', shared=True, format=format)
308
 
        # note this is deliberately readonly, as this failure should 
309
 
        # occur before any writes.
310
 
        self.assertRaises(errors.NotLocalUrl,
311
 
                          bzrdir.BzrDir.create_standalone_workingtree,
312
 
                          self.get_readonly_url('child'), format=format)
313
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
314
 
            format=format)
315
 
        tree.bzrdir.open_repository()
 
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)
316
255
 
317
256
    def test_create_branch_convenience(self):
318
257
        # outside a repo the default convenience output is a repo+branch_tree
319
 
        format = bzrdir.format_registry.make_bzrdir('knit')
320
 
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
321
 
        branch.bzrdir.open_workingtree()
322
 
        branch.bzrdir.open_repository()
 
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)
323
266
 
324
267
    def test_create_branch_convenience_root(self):
325
268
        """Creating a branch at the root of a fs should work."""
326
269
        self.transport_server = MemoryServer
327
270
        # outside a repo the default convenience output is a repo+branch_tree
328
 
        format = bzrdir.format_registry.make_bzrdir('knit')
329
 
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
330
 
                                                         format=format)
331
 
        self.assertRaises(errors.NoWorkingTree,
332
 
                          branch.bzrdir.open_workingtree)
333
 
        branch.bzrdir.open_repository()
 
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)
334
280
 
335
281
    def test_create_branch_convenience_under_shared_repo(self):
336
282
        # inside a repo the default convenience output is a branch+ follow the
337
283
        # repo tree policy
338
 
        format = bzrdir.format_registry.make_bzrdir('knit')
339
 
        self.make_repository('.', shared=True, format=format)
340
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
341
 
            format=format)
342
 
        branch.bzrdir.open_workingtree()
343
 
        self.assertRaises(errors.NoRepositoryPresent,
344
 
                          branch.bzrdir.open_repository)
 
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)
345
294
            
346
295
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
347
296
        # inside a repo the default convenience output is a branch+ follow the
348
297
        # repo tree policy but we can override that
349
 
        format = bzrdir.format_registry.make_bzrdir('knit')
350
 
        self.make_repository('.', shared=True, format=format)
351
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
352
 
            force_new_tree=False, format=format)
353
 
        self.assertRaises(errors.NoWorkingTree,
354
 
                          branch.bzrdir.open_workingtree)
355
 
        self.assertRaises(errors.NoRepositoryPresent,
356
 
                          branch.bzrdir.open_repository)
 
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)
357
310
            
358
311
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
359
312
        # inside a repo the default convenience output is a branch+ follow the
360
313
        # repo tree policy
361
 
        format = bzrdir.format_registry.make_bzrdir('knit')
362
 
        repo = self.make_repository('.', shared=True, format=format)
363
 
        repo.set_make_working_trees(False)
364
 
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
365
 
                                                         format=format)
366
 
        self.assertRaises(errors.NoWorkingTree,
367
 
                          branch.bzrdir.open_workingtree)
368
 
        self.assertRaises(errors.NoRepositoryPresent,
369
 
                          branch.bzrdir.open_repository)
 
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)
370
326
 
371
327
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
372
328
        # inside a repo the default convenience output is a branch+ follow the
373
329
        # repo tree policy but we can override that
374
 
        format = bzrdir.format_registry.make_bzrdir('knit')
375
 
        repo = self.make_repository('.', shared=True, format=format)
376
 
        repo.set_make_working_trees(False)
377
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
378
 
            force_new_tree=True, format=format)
379
 
        branch.bzrdir.open_workingtree()
380
 
        self.assertRaises(errors.NoRepositoryPresent,
381
 
                          branch.bzrdir.open_repository)
 
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)
382
342
 
383
343
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
384
344
        # inside a repo the default convenience output is overridable to give
385
345
        # repo+branch+tree
386
 
        format = bzrdir.format_registry.make_bzrdir('knit')
387
 
        self.make_repository('.', shared=True, format=format)
388
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
389
 
            force_new_repo=True, format=format)
390
 
        branch.bzrdir.open_repository()
391
 
        branch.bzrdir.open_workingtree()
 
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)
392
356
 
393
357
 
394
358
class ChrootedTests(TestCaseWithTransport):
428
392
            get_transport(self.get_readonly_url('g/p/q')))
429
393
        self.assertEqual('g/p/q', relpath)
430
394
 
431
 
    def test_open_containing_tree_or_branch(self):
432
 
        def local_branch_path(branch):
433
 
             return os.path.realpath(
434
 
                urlutils.local_path_from_url(branch.base))
435
 
 
436
 
        self.make_branch_and_tree('topdir')
437
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
438
 
            'topdir/foo')
439
 
        self.assertEqual(os.path.realpath('topdir'),
440
 
                         os.path.realpath(tree.basedir))
441
 
        self.assertEqual(os.path.realpath('topdir'),
442
 
                         local_branch_path(branch))
443
 
        self.assertIs(tree.bzrdir, branch.bzrdir)
444
 
        self.assertEqual('foo', relpath)
445
 
        self.make_branch('topdir/foo')
446
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
447
 
            'topdir/foo')
448
 
        self.assertIs(tree, None)
449
 
        self.assertEqual(os.path.realpath('topdir/foo'),
450
 
                         local_branch_path(branch))
451
 
        self.assertEqual('', relpath)
452
 
 
453
395
    def test_open_from_transport(self):
454
396
        # transport pointing at bzrdir should give a bzrdir with root transport
455
397
        # set to the given transport
472
414
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
473
415
                          transport)
474
416
 
475
 
    def test_sprout_recursive(self):
476
 
        tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
477
 
        sub_tree = self.make_branch_and_tree('tree1/subtree',
478
 
            format='dirstate-with-subtree')
479
 
        tree.add_reference(sub_tree)
480
 
        self.build_tree(['tree1/subtree/file'])
481
 
        sub_tree.add('file')
482
 
        tree.commit('Initial commit')
483
 
        tree.bzrdir.sprout('tree2')
484
 
        self.failUnlessExists('tree2/subtree/file')
485
 
 
486
 
    def test_cloning_metadir(self):
487
 
        """Ensure that cloning metadir is suitable"""
488
 
        bzrdir = self.make_bzrdir('bzrdir')
489
 
        bzrdir.cloning_metadir()
490
 
        branch = self.make_branch('branch', format='knit')
491
 
        format = branch.bzrdir.cloning_metadir()
492
 
        self.assertIsInstance(format.workingtree_format,
493
 
            workingtree.WorkingTreeFormat3)
494
 
 
495
 
    def test_sprout_recursive_treeless(self):
496
 
        tree = self.make_branch_and_tree('tree1',
497
 
            format='dirstate-with-subtree')
498
 
        sub_tree = self.make_branch_and_tree('tree1/subtree',
499
 
            format='dirstate-with-subtree')
500
 
        tree.add_reference(sub_tree)
501
 
        self.build_tree(['tree1/subtree/file'])
502
 
        sub_tree.add('file')
503
 
        tree.commit('Initial commit')
504
 
        tree.bzrdir.destroy_workingtree()
505
 
        repo = self.make_repository('repo', shared=True,
506
 
            format='dirstate-with-subtree')
507
 
        repo.set_make_working_trees(False)
508
 
        tree.bzrdir.sprout('repo/tree2')
509
 
        self.failUnlessExists('repo/tree2/subtree')
510
 
        self.failIfExists('repo/tree2/subtree/file')
511
 
 
512
417
 
513
418
class TestMeta1DirFormat(TestCaseWithTransport):
514
419
    """Tests specific to the meta1 dir format."""
523
428
        repository_base = t.clone('repository').base
524
429
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
525
430
        self.assertEqual(repository_base,
526
 
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
 
431
                         dir.get_repository_transport(repository.RepositoryFormat7()).base)
527
432
        checkout_base = t.clone('checkout').base
528
433
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
529
434
        self.assertEqual(checkout_base,
535
440
        t = dir.transport
536
441
        self.assertIsDirectory('branch-lock', t)
537
442
 
538
 
    def test_comparison(self):
539
 
        """Equality and inequality behave properly.
540
 
 
541
 
        Metadirs should compare equal iff they have the same repo, branch and
542
 
        tree formats.
543
 
        """
544
 
        mydir = bzrdir.format_registry.make_bzrdir('knit')
545
 
        self.assertEqual(mydir, mydir)
546
 
        self.assertFalse(mydir != mydir)
547
 
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
548
 
        self.assertEqual(otherdir, mydir)
549
 
        self.assertFalse(otherdir != mydir)
550
 
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
551
 
        self.assertNotEqual(otherdir2, mydir)
552
 
        self.assertFalse(otherdir2 == mydir)
553
 
 
554
 
    def test_needs_conversion_different_working_tree(self):
555
 
        # meta1dirs need an conversion if any element is not the default.
556
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
557
 
        # test with 
558
 
        new_default = bzrdir.format_registry.make_bzrdir('dirstate')
559
 
        bzrdir.BzrDirFormat._set_default_format(new_default)
560
 
        try:
561
 
            tree = self.make_branch_and_tree('tree', format='knit')
562
 
            self.assertTrue(tree.bzrdir.needs_format_conversion())
563
 
        finally:
564
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
565
 
 
566
 
 
 
443
        
567
444
class TestFormat5(TestCaseWithTransport):
568
445
    """Tests specific to the version 5 bzrdir format."""
569
446
 
591
468
        # format 5 dirs need a conversion if they are not the default.
592
469
        # and they start of not the default.
593
470
        old_format = bzrdir.BzrDirFormat.get_default_format()
594
 
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
471
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
595
472
        try:
596
473
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
597
474
            self.assertFalse(dir.needs_format_conversion())
598
475
        finally:
599
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
476
            bzrdir.BzrDirFormat.set_default_format(old_format)
600
477
        self.assertTrue(dir.needs_format_conversion())
601
478
 
602
479
 
626
503
    def test_needs_conversion(self):
627
504
        # format 6 dirs need an conversion if they are not the default.
628
505
        old_format = bzrdir.BzrDirFormat.get_default_format()
629
 
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
 
506
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
630
507
        try:
631
508
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
632
509
            self.assertTrue(dir.needs_format_conversion())
633
510
        finally:
634
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
511
            bzrdir.BzrDirFormat.set_default_format(old_format)
635
512
 
636
513
 
637
514
class NotBzrDir(bzrlib.bzrdir.BzrDir):
708
585
    
709
586
    def test_create_branch_convenience(self):
710
587
        # outside a repo the default convenience output is a repo+branch_tree
711
 
        format = bzrdir.format_registry.make_bzrdir('knit')
712
 
        branch = bzrdir.BzrDir.create_branch_convenience(
713
 
            self.get_url('foo'), format=format)
714
 
        self.assertRaises(errors.NoWorkingTree,
715
 
                          branch.bzrdir.open_workingtree)
716
 
        branch.bzrdir.open_repository()
 
588
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
589
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
590
        try:
 
591
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url('foo'))
 
592
            self.assertRaises(errors.NoWorkingTree,
 
593
                              branch.bzrdir.open_workingtree)
 
594
            branch.bzrdir.open_repository()
 
595
        finally:
 
596
            bzrdir.BzrDirFormat.set_default_format(old_format)
717
597
 
718
598
    def test_create_branch_convenience_force_tree_not_local_fails(self):
719
599
        # outside a repo the default convenience output is a repo+branch_tree
720
 
        format = bzrdir.format_registry.make_bzrdir('knit')
721
 
        self.assertRaises(errors.NotLocalUrl,
722
 
            bzrdir.BzrDir.create_branch_convenience,
723
 
            self.get_url('foo'),
724
 
            force_new_tree=True,
725
 
            format=format)
726
 
        t = get_transport(self.get_url('.'))
727
 
        self.assertFalse(t.has('foo'))
 
600
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
601
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
602
        try:
 
603
            self.assertRaises(errors.NotLocalUrl,
 
604
                bzrdir.BzrDir.create_branch_convenience,
 
605
                self.get_url('foo'),
 
606
                force_new_tree=True)
 
607
            t = get_transport(self.get_url('.'))
 
608
            self.assertFalse(t.has('foo'))
 
609
        finally:
 
610
            bzrdir.BzrDirFormat.set_default_format(old_format)
728
611
 
729
612
    def test_clone(self):
730
613
        # clone into a nonlocal path works
731
 
        format = bzrdir.format_registry.make_bzrdir('knit')
732
 
        branch = bzrdir.BzrDir.create_branch_convenience('local',
733
 
                                                         format=format)
 
614
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
615
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
616
        try:
 
617
            branch = bzrdir.BzrDir.create_branch_convenience('local')
 
618
        finally:
 
619
            bzrdir.BzrDirFormat.set_default_format(old_format)
734
620
        branch.bzrdir.open_workingtree()
735
621
        result = branch.bzrdir.clone(self.get_url('remote'))
736
622
        self.assertRaises(errors.NoWorkingTree,
738
624
        result.open_branch()
739
625
        result.open_repository()
740
626
 
741
 
    def test_checkout_metadir(self):
742
 
        # checkout_metadir has reasonable working tree format even when no
743
 
        # working tree is present
744
 
        self.make_branch('branch-knit2', format='dirstate-with-subtree')
745
 
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
746
 
        checkout_format = my_bzrdir.checkout_metadir()
747
 
        self.assertIsInstance(checkout_format.workingtree_format,
748
 
                              workingtree.WorkingTreeFormat3)
749
 
 
750
 
 
751
 
class TestRemoteSFTP(test_sftp_transport.TestCaseWithSFTPServer):
752
 
 
753
 
    def test_open_containing_tree_or_branch(self):
754
 
        tree = self.make_branch_and_tree('tree')
755
 
        bzrdir.BzrDir.open_containing_tree_or_branch(self.get_url('tree'))