~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-06 07:13:51 UTC
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20061006071351-e3fdd47eed1c3e7e
lazy import revisionspec and errors for bzrlib.options

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 (
40
 
    TestCase,
41
 
    TestCaseWithTransport,
42
 
    test_sftp_transport
43
 
    )
44
 
from bzrlib.tests.HttpServer import HttpServer
45
 
from bzrlib.tests.HTTPTestUtil import (
46
 
    TestCaseWithTwoWebservers,
47
 
    HTTPServerRedirecting,
48
 
    )
49
 
from bzrlib.tests.test_http import TestWithTransport_pycurl
 
31
import bzrlib.repository as repository
 
32
from bzrlib.tests import TestCase, TestCaseWithTransport
50
33
from bzrlib.transport import get_transport
51
 
from bzrlib.transport.http._urllib import HttpTransport_urllib
 
34
from bzrlib.transport.http import HttpServer
52
35
from bzrlib.transport.memory import MemoryServer
53
 
from bzrlib.repofmt import knitrepo, weaverepo
 
36
import bzrlib.workingtree as workingtree
54
37
 
55
38
 
56
39
class TestDefaultFormat(TestCase):
59
42
        old_format = bzrdir.BzrDirFormat.get_default_format()
60
43
        # default is BzrDirFormat6
61
44
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
62
 
        self.applyDeprecated(symbol_versioning.zero_fourteen, 
63
 
                             bzrdir.BzrDirFormat.set_default_format, 
64
 
                             SampleBzrDirFormat())
 
45
        bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
65
46
        # creating a bzr dir should now create an instrumented dir.
66
47
        try:
67
48
            result = bzrdir.BzrDir.create('memory:///')
68
49
            self.failUnless(isinstance(result, SampleBzrDir))
69
50
        finally:
70
 
            self.applyDeprecated(symbol_versioning.zero_fourteen,
71
 
                bzrdir.BzrDirFormat.set_default_format, old_format)
 
51
            bzrdir.BzrDirFormat.set_default_format(old_format)
72
52
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
73
53
 
74
54
 
75
 
class TestFormatRegistry(TestCase):
76
 
 
77
 
    def make_format_registry(self):
78
 
        my_format_registry = bzrdir.BzrDirFormatRegistry()
79
 
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
80
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
81
 
            ' repositories', deprecated=True)
82
 
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
83
 
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
84
 
        my_format_registry.register_metadir('knit',
85
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
86
 
            'Format using knits',
87
 
            )
88
 
        my_format_registry.set_default('knit')
89
 
        my_format_registry.register_metadir(
90
 
            'branch6',
91
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
92
 
            'Experimental successor to knit.  Use at your own risk.',
93
 
            branch_format='bzrlib.branch.BzrBranchFormat6')
94
 
        my_format_registry.register_metadir(
95
 
            'hidden format',
96
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
97
 
            'Experimental successor to knit.  Use at your own risk.',
98
 
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
99
 
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
100
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
101
 
            ' repositories', hidden=True)
102
 
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
103
 
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
104
 
            hidden=True)
105
 
        return my_format_registry
106
 
 
107
 
    def test_format_registry(self):
108
 
        my_format_registry = self.make_format_registry()
109
 
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
110
 
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
111
 
        my_bzrdir = my_format_registry.make_bzrdir('weave')
112
 
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
113
 
        my_bzrdir = my_format_registry.make_bzrdir('default')
114
 
        self.assertIsInstance(my_bzrdir.repository_format, 
115
 
            knitrepo.RepositoryFormatKnit1)
116
 
        my_bzrdir = my_format_registry.make_bzrdir('knit')
117
 
        self.assertIsInstance(my_bzrdir.repository_format, 
118
 
            knitrepo.RepositoryFormatKnit1)
119
 
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
120
 
        self.assertIsInstance(my_bzrdir.get_branch_format(),
121
 
                              bzrlib.branch.BzrBranchFormat6)
122
 
 
123
 
    def test_get_help(self):
124
 
        my_format_registry = self.make_format_registry()
125
 
        self.assertEqual('Format registered lazily',
126
 
                         my_format_registry.get_help('lazy'))
127
 
        self.assertEqual('Format using knits', 
128
 
                         my_format_registry.get_help('knit'))
129
 
        self.assertEqual('Format using knits', 
130
 
                         my_format_registry.get_help('default'))
131
 
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
132
 
                         ' checkouts or shared repositories', 
133
 
                         my_format_registry.get_help('weave'))
134
 
        
135
 
    def test_help_topic(self):
136
 
        topics = help_topics.HelpTopicRegistry()
137
 
        topics.register('formats', self.make_format_registry().help_topic, 
138
 
                        'Directory formats')
139
 
        topic = topics.get_detail('formats')
140
 
        new, deprecated = topic.split('Deprecated formats')
141
 
        self.assertContainsRe(new, 'Bazaar directory formats')
142
 
        self.assertContainsRe(new, 
143
 
            '  knit/default:\n    \(native\) Format using knits\n')
144
 
        self.assertContainsRe(deprecated, 
145
 
            '  lazy:\n    \(native\) Format registered lazily\n')
146
 
        self.assertNotContainsRe(new, 'hidden')
147
 
 
148
 
    def test_set_default_repository(self):
149
 
        default_factory = bzrdir.format_registry.get('default')
150
 
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
151
 
                       if v == default_factory and k != 'default'][0]
152
 
        bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
153
 
        try:
154
 
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
155
 
                          bzrdir.format_registry.get('default'))
156
 
            self.assertIs(
157
 
                repository.RepositoryFormat.get_default_format().__class__,
158
 
                knitrepo.RepositoryFormatKnit3)
159
 
        finally:
160
 
            bzrdir.format_registry.set_default_repository(old_default)
161
 
 
162
 
 
163
55
class SampleBranch(bzrlib.branch.Branch):
164
56
    """A dummy branch for guess what, dummy use."""
165
57
 
263
155
 
264
156
    def test_create_repository(self):
265
157
        format = SampleBzrDirFormat()
266
 
        repo = bzrdir.BzrDir.create_repository(self.get_url(), format=format)
267
 
        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)
268
165
 
269
166
    def test_create_repository_shared(self):
270
167
        old_format = bzrdir.BzrDirFormat.get_default_format()
279
176
    def test_create_repository_under_shared(self):
280
177
        # an explicit create_repository always does so.
281
178
        # we trust the format is right from the 'create_repository test'
282
 
        format = bzrdir.format_registry.make_bzrdir('knit')
283
 
        self.make_repository('.', shared=True, format=format)
284
 
        repo = bzrdir.BzrDir.create_repository(self.get_url('child'),
285
 
                                               format=format)
286
 
        self.assertTrue(isinstance(repo, repository.Repository))
287
 
        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)
288
188
 
289
189
    def test_create_branch_and_repo_uses_default(self):
290
190
        format = SampleBzrDirFormat()
291
 
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(), 
292
 
                                                      format=format)
293
 
        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)
294
198
 
295
199
    def test_create_branch_and_repo_under_shared(self):
296
200
        # creating a branch and repo in a shared repo uses the
297
201
        # shared repository
298
 
        format = bzrdir.format_registry.make_bzrdir('knit')
299
 
        self.make_repository('.', shared=True, format=format)
300
 
        branch = bzrdir.BzrDir.create_branch_and_repo(
301
 
            self.get_url('child'), format=format)
302
 
        self.assertRaises(errors.NoRepositoryPresent,
303
 
                          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)
304
211
 
305
212
    def test_create_branch_and_repo_under_shared_force_new(self):
306
213
        # creating a branch and repo in a shared repo can be forced to 
307
214
        # make a new repo
308
 
        format = bzrdir.format_registry.make_bzrdir('knit')
309
 
        self.make_repository('.', shared=True, format=format)
310
 
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
311
 
                                                      force_new_repo=True,
312
 
                                                      format=format)
313
 
        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)
314
224
 
315
225
    def test_create_standalone_working_tree(self):
316
226
        format = SampleBzrDirFormat()
317
 
        # note this is deliberately readonly, as this failure should 
318
 
        # occur before any writes.
319
 
        self.assertRaises(errors.NotLocalUrl,
320
 
                          bzrdir.BzrDir.create_standalone_workingtree,
321
 
                          self.get_readonly_url(), format=format)
322
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
323
 
                                                           format=format)
324
 
        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)
325
239
 
326
240
    def test_create_standalone_working_tree_under_shared_repo(self):
327
241
        # create standalone working tree always makes a repo.
328
 
        format = bzrdir.format_registry.make_bzrdir('knit')
329
 
        self.make_repository('.', shared=True, format=format)
330
 
        # note this is deliberately readonly, as this failure should 
331
 
        # occur before any writes.
332
 
        self.assertRaises(errors.NotLocalUrl,
333
 
                          bzrdir.BzrDir.create_standalone_workingtree,
334
 
                          self.get_readonly_url('child'), format=format)
335
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
336
 
            format=format)
337
 
        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)
338
255
 
339
256
    def test_create_branch_convenience(self):
340
257
        # outside a repo the default convenience output is a repo+branch_tree
341
 
        format = bzrdir.format_registry.make_bzrdir('knit')
342
 
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
343
 
        branch.bzrdir.open_workingtree()
344
 
        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)
345
266
 
346
267
    def test_create_branch_convenience_root(self):
347
268
        """Creating a branch at the root of a fs should work."""
348
 
        self.vfs_transport_factory = MemoryServer
 
269
        self.transport_server = MemoryServer
349
270
        # outside a repo the default convenience output is a repo+branch_tree
350
 
        format = bzrdir.format_registry.make_bzrdir('knit')
351
 
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
352
 
                                                         format=format)
353
 
        self.assertRaises(errors.NoWorkingTree,
354
 
                          branch.bzrdir.open_workingtree)
355
 
        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)
356
280
 
357
281
    def test_create_branch_convenience_under_shared_repo(self):
358
282
        # inside a repo the default convenience output is a branch+ follow the
359
283
        # repo tree policy
360
 
        format = bzrdir.format_registry.make_bzrdir('knit')
361
 
        self.make_repository('.', shared=True, format=format)
362
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
363
 
            format=format)
364
 
        branch.bzrdir.open_workingtree()
365
 
        self.assertRaises(errors.NoRepositoryPresent,
366
 
                          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)
367
294
            
368
295
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
369
296
        # inside a repo the default convenience output is a branch+ follow the
370
297
        # repo tree policy but we can override that
371
 
        format = bzrdir.format_registry.make_bzrdir('knit')
372
 
        self.make_repository('.', shared=True, format=format)
373
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
374
 
            force_new_tree=False, format=format)
375
 
        self.assertRaises(errors.NoWorkingTree,
376
 
                          branch.bzrdir.open_workingtree)
377
 
        self.assertRaises(errors.NoRepositoryPresent,
378
 
                          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)
379
310
            
380
311
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
381
312
        # inside a repo the default convenience output is a branch+ follow the
382
313
        # repo tree policy
383
 
        format = bzrdir.format_registry.make_bzrdir('knit')
384
 
        repo = self.make_repository('.', shared=True, format=format)
385
 
        repo.set_make_working_trees(False)
386
 
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
387
 
                                                         format=format)
388
 
        self.assertRaises(errors.NoWorkingTree,
389
 
                          branch.bzrdir.open_workingtree)
390
 
        self.assertRaises(errors.NoRepositoryPresent,
391
 
                          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)
392
326
 
393
327
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
394
328
        # inside a repo the default convenience output is a branch+ follow the
395
329
        # repo tree policy but we can override that
396
 
        format = bzrdir.format_registry.make_bzrdir('knit')
397
 
        repo = self.make_repository('.', shared=True, format=format)
398
 
        repo.set_make_working_trees(False)
399
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
400
 
            force_new_tree=True, format=format)
401
 
        branch.bzrdir.open_workingtree()
402
 
        self.assertRaises(errors.NoRepositoryPresent,
403
 
                          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)
404
342
 
405
343
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
406
344
        # inside a repo the default convenience output is overridable to give
407
345
        # repo+branch+tree
408
 
        format = bzrdir.format_registry.make_bzrdir('knit')
409
 
        self.make_repository('.', shared=True, format=format)
410
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
411
 
            force_new_repo=True, format=format)
412
 
        branch.bzrdir.open_repository()
413
 
        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)
414
356
 
415
357
 
416
358
class ChrootedTests(TestCaseWithTransport):
423
365
 
424
366
    def setUp(self):
425
367
        super(ChrootedTests, self).setUp()
426
 
        if not self.vfs_transport_factory == MemoryServer:
 
368
        if not self.transport_server == MemoryServer:
427
369
            self.transport_readonly_server = HttpServer
428
370
 
429
371
    def test_open_containing(self):
450
392
            get_transport(self.get_readonly_url('g/p/q')))
451
393
        self.assertEqual('g/p/q', relpath)
452
394
 
453
 
    def test_open_containing_tree_or_branch(self):
454
 
        def local_branch_path(branch):
455
 
             return os.path.realpath(
456
 
                urlutils.local_path_from_url(branch.base))
457
 
 
458
 
        self.make_branch_and_tree('topdir')
459
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
460
 
            'topdir/foo')
461
 
        self.assertEqual(os.path.realpath('topdir'),
462
 
                         os.path.realpath(tree.basedir))
463
 
        self.assertEqual(os.path.realpath('topdir'),
464
 
                         local_branch_path(branch))
465
 
        self.assertIs(tree.bzrdir, branch.bzrdir)
466
 
        self.assertEqual('foo', relpath)
467
 
        # opening from non-local should not return the tree
468
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
469
 
            self.get_readonly_url('topdir/foo'))
470
 
        self.assertEqual(None, tree)
471
 
        self.assertEqual('foo', relpath)
472
 
        # without a tree:
473
 
        self.make_branch('topdir/foo')
474
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
475
 
            'topdir/foo')
476
 
        self.assertIs(tree, None)
477
 
        self.assertEqual(os.path.realpath('topdir/foo'),
478
 
                         local_branch_path(branch))
479
 
        self.assertEqual('', relpath)
480
 
 
481
395
    def test_open_from_transport(self):
482
396
        # transport pointing at bzrdir should give a bzrdir with root transport
483
397
        # set to the given transport
500
414
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
501
415
                          transport)
502
416
 
503
 
    def test_sprout_recursive(self):
504
 
        tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
505
 
        sub_tree = self.make_branch_and_tree('tree1/subtree',
506
 
            format='dirstate-with-subtree')
507
 
        tree.add_reference(sub_tree)
508
 
        self.build_tree(['tree1/subtree/file'])
509
 
        sub_tree.add('file')
510
 
        tree.commit('Initial commit')
511
 
        tree.bzrdir.sprout('tree2')
512
 
        self.failUnlessExists('tree2/subtree/file')
513
 
 
514
 
    def test_cloning_metadir(self):
515
 
        """Ensure that cloning metadir is suitable"""
516
 
        bzrdir = self.make_bzrdir('bzrdir')
517
 
        bzrdir.cloning_metadir()
518
 
        branch = self.make_branch('branch', format='knit')
519
 
        format = branch.bzrdir.cloning_metadir()
520
 
        self.assertIsInstance(format.workingtree_format,
521
 
            workingtree.WorkingTreeFormat3)
522
 
 
523
 
    def test_sprout_recursive_treeless(self):
524
 
        tree = self.make_branch_and_tree('tree1',
525
 
            format='dirstate-with-subtree')
526
 
        sub_tree = self.make_branch_and_tree('tree1/subtree',
527
 
            format='dirstate-with-subtree')
528
 
        tree.add_reference(sub_tree)
529
 
        self.build_tree(['tree1/subtree/file'])
530
 
        sub_tree.add('file')
531
 
        tree.commit('Initial commit')
532
 
        tree.bzrdir.destroy_workingtree()
533
 
        repo = self.make_repository('repo', shared=True,
534
 
            format='dirstate-with-subtree')
535
 
        repo.set_make_working_trees(False)
536
 
        tree.bzrdir.sprout('repo/tree2')
537
 
        self.failUnlessExists('repo/tree2/subtree')
538
 
        self.failIfExists('repo/tree2/subtree/file')
539
 
 
540
417
 
541
418
class TestMeta1DirFormat(TestCaseWithTransport):
542
419
    """Tests specific to the meta1 dir format."""
551
428
        repository_base = t.clone('repository').base
552
429
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
553
430
        self.assertEqual(repository_base,
554
 
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
 
431
                         dir.get_repository_transport(repository.RepositoryFormat7()).base)
555
432
        checkout_base = t.clone('checkout').base
556
433
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
557
434
        self.assertEqual(checkout_base,
563
440
        t = dir.transport
564
441
        self.assertIsDirectory('branch-lock', t)
565
442
 
566
 
    def test_comparison(self):
567
 
        """Equality and inequality behave properly.
568
 
 
569
 
        Metadirs should compare equal iff they have the same repo, branch and
570
 
        tree formats.
571
 
        """
572
 
        mydir = bzrdir.format_registry.make_bzrdir('knit')
573
 
        self.assertEqual(mydir, mydir)
574
 
        self.assertFalse(mydir != mydir)
575
 
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
576
 
        self.assertEqual(otherdir, mydir)
577
 
        self.assertFalse(otherdir != mydir)
578
 
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
579
 
        self.assertNotEqual(otherdir2, mydir)
580
 
        self.assertFalse(otherdir2 == mydir)
581
 
 
582
 
    def test_needs_conversion_different_working_tree(self):
583
 
        # meta1dirs need an conversion if any element is not the default.
584
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
585
 
        # test with 
586
 
        new_default = bzrdir.format_registry.make_bzrdir('dirstate')
587
 
        bzrdir.BzrDirFormat._set_default_format(new_default)
588
 
        try:
589
 
            tree = self.make_branch_and_tree('tree', format='knit')
590
 
            self.assertTrue(tree.bzrdir.needs_format_conversion())
591
 
        finally:
592
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
593
 
 
594
 
 
 
443
        
595
444
class TestFormat5(TestCaseWithTransport):
596
445
    """Tests specific to the version 5 bzrdir format."""
597
446
 
619
468
        # format 5 dirs need a conversion if they are not the default.
620
469
        # and they start of not the default.
621
470
        old_format = bzrdir.BzrDirFormat.get_default_format()
622
 
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
471
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
623
472
        try:
624
473
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
625
474
            self.assertFalse(dir.needs_format_conversion())
626
475
        finally:
627
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
476
            bzrdir.BzrDirFormat.set_default_format(old_format)
628
477
        self.assertTrue(dir.needs_format_conversion())
629
478
 
630
479
 
654
503
    def test_needs_conversion(self):
655
504
        # format 6 dirs need an conversion if they are not the default.
656
505
        old_format = bzrdir.BzrDirFormat.get_default_format()
657
 
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
 
506
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
658
507
        try:
659
508
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
660
509
            self.assertTrue(dir.needs_format_conversion())
661
510
        finally:
662
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
511
            bzrdir.BzrDirFormat.set_default_format(old_format)
663
512
 
664
513
 
665
514
class NotBzrDir(bzrlib.bzrdir.BzrDir):
732
581
 
733
582
    def setUp(self):
734
583
        super(NonLocalTests, self).setUp()
735
 
        self.vfs_transport_factory = MemoryServer
 
584
        self.transport_server = MemoryServer
736
585
    
737
586
    def test_create_branch_convenience(self):
738
587
        # outside a repo the default convenience output is a repo+branch_tree
739
 
        format = bzrdir.format_registry.make_bzrdir('knit')
740
 
        branch = bzrdir.BzrDir.create_branch_convenience(
741
 
            self.get_url('foo'), format=format)
742
 
        self.assertRaises(errors.NoWorkingTree,
743
 
                          branch.bzrdir.open_workingtree)
744
 
        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)
745
597
 
746
598
    def test_create_branch_convenience_force_tree_not_local_fails(self):
747
599
        # outside a repo the default convenience output is a repo+branch_tree
748
 
        format = bzrdir.format_registry.make_bzrdir('knit')
749
 
        self.assertRaises(errors.NotLocalUrl,
750
 
            bzrdir.BzrDir.create_branch_convenience,
751
 
            self.get_url('foo'),
752
 
            force_new_tree=True,
753
 
            format=format)
754
 
        t = get_transport(self.get_url('.'))
755
 
        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)
756
611
 
757
612
    def test_clone(self):
758
613
        # clone into a nonlocal path works
759
 
        format = bzrdir.format_registry.make_bzrdir('knit')
760
 
        branch = bzrdir.BzrDir.create_branch_convenience('local',
761
 
                                                         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)
762
620
        branch.bzrdir.open_workingtree()
763
621
        result = branch.bzrdir.clone(self.get_url('remote'))
764
622
        self.assertRaises(errors.NoWorkingTree,
766
624
        result.open_branch()
767
625
        result.open_repository()
768
626
 
769
 
    def test_checkout_metadir(self):
770
 
        # checkout_metadir has reasonable working tree format even when no
771
 
        # working tree is present
772
 
        self.make_branch('branch-knit2', format='dirstate-with-subtree')
773
 
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
774
 
        checkout_format = my_bzrdir.checkout_metadir()
775
 
        self.assertIsInstance(checkout_format.workingtree_format,
776
 
                              workingtree.WorkingTreeFormat3)
777
 
 
778
 
 
779
 
class TestHTTPRedirectionLoop(object):
780
 
    """Test redirection loop between two http servers.
781
 
 
782
 
    This MUST be used by daughter classes that also inherit from
783
 
    TestCaseWithTwoWebservers.
784
 
 
785
 
    We can't inherit directly from TestCaseWithTwoWebservers or the
786
 
    test framework will try to create an instance which cannot
787
 
    run, its implementation being incomplete. 
788
 
    """
789
 
 
790
 
    # Should be defined by daughter classes to ensure redirection
791
 
    # still use the same transport implementation (not currently
792
 
    # enforced as it's a bit tricky to get right (see the FIXME
793
 
    # in BzrDir.open_from_transport for the unique use case so
794
 
    # far)
795
 
    _qualifier = None
796
 
 
797
 
    def create_transport_readonly_server(self):
798
 
        return HTTPServerRedirecting()
799
 
 
800
 
    def create_transport_secondary_server(self):
801
 
        return HTTPServerRedirecting()
802
 
 
803
 
    def setUp(self):
804
 
        # Both servers redirect to each server creating a loop
805
 
        super(TestHTTPRedirectionLoop, self).setUp()
806
 
        # The redirections will point to the new server
807
 
        self.new_server = self.get_readonly_server()
808
 
        # The requests to the old server will be redirected
809
 
        self.old_server = self.get_secondary_server()
810
 
        # Configure the redirections
811
 
        self.old_server.redirect_to(self.new_server.host, self.new_server.port)
812
 
        self.new_server.redirect_to(self.old_server.host, self.old_server.port)
813
 
 
814
 
    def _qualified_url(self, host, port):
815
 
        return 'http+%s://%s:%s' % (self._qualifier, host, port)
816
 
 
817
 
    def test_loop(self):
818
 
        # Starting from either server should loop
819
 
        old_url = self._qualified_url(self.old_server.host, 
820
 
                                      self.old_server.port)
821
 
        oldt = self._transport(old_url)
822
 
        self.assertRaises(errors.NotBranchError,
823
 
                          bzrdir.BzrDir.open_from_transport, oldt)
824
 
        new_url = self._qualified_url(self.new_server.host, 
825
 
                                      self.new_server.port)
826
 
        newt = self._transport(new_url)
827
 
        self.assertRaises(errors.NotBranchError,
828
 
                          bzrdir.BzrDir.open_from_transport, newt)
829
 
 
830
 
 
831
 
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
832
 
                                  TestCaseWithTwoWebservers):
833
 
    """Tests redirections for urllib implementation"""
834
 
 
835
 
    _qualifier = 'urllib'
836
 
    _transport = HttpTransport_urllib
837
 
 
838
 
 
839
 
 
840
 
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
841
 
                                  TestHTTPRedirectionLoop,
842
 
                                  TestCaseWithTwoWebservers):
843
 
    """Tests redirections for pycurl implementation"""
844
 
 
845
 
    _qualifier = 'pycurl'