~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Robey Pointer
  • Date: 2006-07-01 19:03:33 UTC
  • mfrom: (1829 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1830.
  • Revision ID: robey@lag.net-20060701190333-f58465aec4bd3412
merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests for the BzrDir facility and any format specific tests.
 
18
 
 
19
For interface contract tests, see tests/bzr_dir_implementations.
 
20
"""
 
21
 
 
22
from StringIO import StringIO
 
23
 
 
24
import bzrlib.branch
 
25
import bzrlib.bzrdir as bzrdir
 
26
import bzrlib.errors as errors
 
27
from bzrlib.errors import (NotBranchError,
 
28
                           UnknownFormatError,
 
29
                           UnsupportedFormatError,
 
30
                           )
 
31
import bzrlib.repository as repository
 
32
from bzrlib.tests import TestCase, TestCaseWithTransport
 
33
from bzrlib.transport import get_transport
 
34
from bzrlib.transport.http import HttpServer
 
35
from bzrlib.transport.memory import MemoryServer
 
36
import bzrlib.workingtree as workingtree
 
37
 
 
38
 
 
39
class TestDefaultFormat(TestCase):
 
40
 
 
41
    def test_get_set_default_format(self):
 
42
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
43
        # default is BzrDirFormat6
 
44
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
 
45
        bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
 
46
        # creating a bzr dir should now create an instrumented dir.
 
47
        try:
 
48
            result = bzrdir.BzrDir.create('memory:///')
 
49
            self.failUnless(isinstance(result, SampleBzrDir))
 
50
        finally:
 
51
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
52
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
 
53
 
 
54
 
 
55
class SampleBranch(bzrlib.branch.Branch):
 
56
    """A dummy branch for guess what, dummy use."""
 
57
 
 
58
    def __init__(self, dir):
 
59
        self.bzrdir = dir
 
60
 
 
61
 
 
62
class SampleBzrDir(bzrdir.BzrDir):
 
63
    """A sample BzrDir implementation to allow testing static methods."""
 
64
 
 
65
    def create_repository(self):
 
66
        """See BzrDir.create_repository."""
 
67
        return "A repository"
 
68
 
 
69
    def open_repository(self):
 
70
        """See BzrDir.open_repository."""
 
71
        return "A repository"
 
72
 
 
73
    def create_branch(self):
 
74
        """See BzrDir.create_branch."""
 
75
        return SampleBranch(self)
 
76
 
 
77
    def create_workingtree(self):
 
78
        """See BzrDir.create_workingtree."""
 
79
        return "A tree"
 
80
 
 
81
 
 
82
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
 
83
    """A sample format
 
84
 
 
85
    this format is initializable, unsupported to aid in testing the 
 
86
    open and open_downlevel routines.
 
87
    """
 
88
 
 
89
    def get_format_string(self):
 
90
        """See BzrDirFormat.get_format_string()."""
 
91
        return "Sample .bzr dir format."
 
92
 
 
93
    def initialize(self, url):
 
94
        """Create a bzr dir."""
 
95
        t = get_transport(url)
 
96
        t.mkdir('.bzr')
 
97
        t.put('.bzr/branch-format', StringIO(self.get_format_string()))
 
98
        return SampleBzrDir(t, self)
 
99
 
 
100
    def is_supported(self):
 
101
        return False
 
102
 
 
103
    def open(self, transport, _found=None):
 
104
        return "opened branch."
 
105
 
 
106
 
 
107
class TestBzrDirFormat(TestCaseWithTransport):
 
108
    """Tests for the BzrDirFormat facility."""
 
109
 
 
110
    def test_find_format(self):
 
111
        # is the right format object found for a branch?
 
112
        # create a branch with a few known format objects.
 
113
        # this is not quite the same as 
 
114
        t = get_transport(self.get_url())
 
115
        self.build_tree(["foo/", "bar/"], transport=t)
 
116
        def check_format(format, url):
 
117
            format.initialize(url)
 
118
            t = get_transport(url)
 
119
            found_format = bzrdir.BzrDirFormat.find_format(t)
 
120
            self.failUnless(isinstance(found_format, format.__class__))
 
121
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
122
        check_format(bzrdir.BzrDirFormat6(), "bar")
 
123
        
 
124
    def test_find_format_nothing_there(self):
 
125
        self.assertRaises(NotBranchError,
 
126
                          bzrdir.BzrDirFormat.find_format,
 
127
                          get_transport('.'))
 
128
 
 
129
    def test_find_format_unknown_format(self):
 
130
        t = get_transport(self.get_url())
 
131
        t.mkdir('.bzr')
 
132
        t.put('.bzr/branch-format', StringIO())
 
133
        self.assertRaises(UnknownFormatError,
 
134
                          bzrdir.BzrDirFormat.find_format,
 
135
                          get_transport('.'))
 
136
 
 
137
    def test_register_unregister_format(self):
 
138
        format = SampleBzrDirFormat()
 
139
        url = self.get_url()
 
140
        # make a bzrdir
 
141
        format.initialize(url)
 
142
        # register a format for it.
 
143
        bzrdir.BzrDirFormat.register_format(format)
 
144
        # which bzrdir.Open will refuse (not supported)
 
145
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
 
146
        # which bzrdir.open_containing will refuse (not supported)
 
147
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
 
148
        # but open_downlevel will work
 
149
        t = get_transport(url)
 
150
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
 
151
        # unregister the format
 
152
        bzrdir.BzrDirFormat.unregister_format(format)
 
153
        # now open_downlevel should fail too.
 
154
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
 
155
 
 
156
    def test_create_repository(self):
 
157
        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)
 
165
 
 
166
    def test_create_repository_under_shared(self):
 
167
        # an explicit create_repository always does so.
 
168
        # we trust the format is right from the 'create_repository test'
 
169
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
170
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
171
        try:
 
172
            self.make_repository('.', shared=True)
 
173
            repo = bzrdir.BzrDir.create_repository(self.get_url('child'))
 
174
            self.assertTrue(isinstance(repo, repository.Repository))
 
175
            self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
 
176
        finally:
 
177
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
178
 
 
179
    def test_create_branch_and_repo_uses_default(self):
 
180
        format = SampleBzrDirFormat()
 
181
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
182
        bzrdir.BzrDirFormat.set_default_format(format)
 
183
        try:
 
184
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
 
185
            self.assertTrue(isinstance(branch, SampleBranch))
 
186
        finally:
 
187
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
188
 
 
189
    def test_create_branch_and_repo_under_shared(self):
 
190
        # creating a branch and repo in a shared repo uses the
 
191
        # shared repository
 
192
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
193
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
194
        try:
 
195
            self.make_repository('.', shared=True)
 
196
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'))
 
197
            self.assertRaises(errors.NoRepositoryPresent,
 
198
                              branch.bzrdir.open_repository)
 
199
        finally:
 
200
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
201
 
 
202
    def test_create_branch_and_repo_under_shared_force_new(self):
 
203
        # creating a branch and repo in a shared repo can be forced to 
 
204
        # make a new repo
 
205
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
206
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
207
        try:
 
208
            self.make_repository('.', shared=True)
 
209
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
 
210
                                                          force_new_repo=True)
 
211
            branch.bzrdir.open_repository()
 
212
        finally:
 
213
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
214
 
 
215
    def test_create_standalone_working_tree(self):
 
216
        format = SampleBzrDirFormat()
 
217
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
218
        bzrdir.BzrDirFormat.set_default_format(format)
 
219
        try:
 
220
            # note this is deliberately readonly, as this failure should 
 
221
            # occur before any writes.
 
222
            self.assertRaises(errors.NotLocalUrl,
 
223
                              bzrdir.BzrDir.create_standalone_workingtree,
 
224
                              self.get_readonly_url())
 
225
            tree = bzrdir.BzrDir.create_standalone_workingtree('.')
 
226
            self.assertEqual('A tree', tree)
 
227
        finally:
 
228
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
229
 
 
230
    def test_create_standalone_working_tree_under_shared_repo(self):
 
231
        # create standalone working tree always makes a repo.
 
232
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
233
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
234
        try:
 
235
            self.make_repository('.', shared=True)
 
236
            # note this is deliberately readonly, as this failure should 
 
237
            # occur before any writes.
 
238
            self.assertRaises(errors.NotLocalUrl,
 
239
                              bzrdir.BzrDir.create_standalone_workingtree,
 
240
                              self.get_readonly_url('child'))
 
241
            tree = bzrdir.BzrDir.create_standalone_workingtree('child')
 
242
            tree.bzrdir.open_repository()
 
243
        finally:
 
244
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
245
 
 
246
    def test_create_branch_convenience(self):
 
247
        # outside a repo the default convenience output is a repo+branch_tree
 
248
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
249
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
250
        try:
 
251
            branch = bzrdir.BzrDir.create_branch_convenience('.')
 
252
            branch.bzrdir.open_workingtree()
 
253
            branch.bzrdir.open_repository()
 
254
        finally:
 
255
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
256
 
 
257
    def test_create_branch_convenience_root(self):
 
258
        """Creating a branch at the root of a fs should work."""
 
259
        self.transport_server = MemoryServer
 
260
        # outside a repo the default convenience output is a repo+branch_tree
 
261
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
262
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
263
        try:
 
264
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url())
 
265
            self.assertRaises(errors.NoWorkingTree,
 
266
                              branch.bzrdir.open_workingtree)
 
267
            branch.bzrdir.open_repository()
 
268
        finally:
 
269
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
270
 
 
271
    def test_create_branch_convenience_under_shared_repo(self):
 
272
        # inside a repo the default convenience output is a branch+ follow the
 
273
        # repo tree policy
 
274
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
275
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
276
        try:
 
277
            self.make_repository('.', shared=True)
 
278
            branch = bzrdir.BzrDir.create_branch_convenience('child')
 
279
            branch.bzrdir.open_workingtree()
 
280
            self.assertRaises(errors.NoRepositoryPresent,
 
281
                              branch.bzrdir.open_repository)
 
282
        finally:
 
283
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
284
            
 
285
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
 
286
        # inside a repo the default convenience output is a branch+ follow the
 
287
        # repo tree policy but we can override that
 
288
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
289
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
290
        try:
 
291
            self.make_repository('.', shared=True)
 
292
            branch = bzrdir.BzrDir.create_branch_convenience('child',
 
293
                force_new_tree=False)
 
294
            self.assertRaises(errors.NoWorkingTree,
 
295
                              branch.bzrdir.open_workingtree)
 
296
            self.assertRaises(errors.NoRepositoryPresent,
 
297
                              branch.bzrdir.open_repository)
 
298
        finally:
 
299
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
300
            
 
301
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
 
302
        # inside a repo the default convenience output is a branch+ follow the
 
303
        # repo tree policy
 
304
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
305
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
306
        try:
 
307
            repo = self.make_repository('.', shared=True)
 
308
            repo.set_make_working_trees(False)
 
309
            branch = bzrdir.BzrDir.create_branch_convenience('child')
 
310
            self.assertRaises(errors.NoWorkingTree,
 
311
                              branch.bzrdir.open_workingtree)
 
312
            self.assertRaises(errors.NoRepositoryPresent,
 
313
                              branch.bzrdir.open_repository)
 
314
        finally:
 
315
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
316
 
 
317
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
 
318
        # inside a repo the default convenience output is a branch+ follow the
 
319
        # repo tree policy but we can override that
 
320
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
321
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
322
        try:
 
323
            repo = self.make_repository('.', shared=True)
 
324
            repo.set_make_working_trees(False)
 
325
            branch = bzrdir.BzrDir.create_branch_convenience('child',
 
326
                force_new_tree=True)
 
327
            branch.bzrdir.open_workingtree()
 
328
            self.assertRaises(errors.NoRepositoryPresent,
 
329
                              branch.bzrdir.open_repository)
 
330
        finally:
 
331
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
332
 
 
333
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
 
334
        # inside a repo the default convenience output is overridable to give
 
335
        # repo+branch+tree
 
336
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
337
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
338
        try:
 
339
            self.make_repository('.', shared=True)
 
340
            branch = bzrdir.BzrDir.create_branch_convenience('child',
 
341
                force_new_repo=True)
 
342
            branch.bzrdir.open_repository()
 
343
            branch.bzrdir.open_workingtree()
 
344
        finally:
 
345
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
346
 
 
347
 
 
348
class ChrootedTests(TestCaseWithTransport):
 
349
    """A support class that provides readonly urls outside the local namespace.
 
350
 
 
351
    This is done by checking if self.transport_server is a MemoryServer. if it
 
352
    is then we are chrooted already, if it is not then an HttpServer is used
 
353
    for readonly urls.
 
354
    """
 
355
 
 
356
    def setUp(self):
 
357
        super(ChrootedTests, self).setUp()
 
358
        if not self.transport_server == MemoryServer:
 
359
            self.transport_readonly_server = HttpServer
 
360
 
 
361
    def test_open_containing(self):
 
362
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
 
363
                          self.get_readonly_url(''))
 
364
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
 
365
                          self.get_readonly_url('g/p/q'))
 
366
        control = bzrdir.BzrDir.create(self.get_url())
 
367
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
 
368
        self.assertEqual('', relpath)
 
369
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
 
370
        self.assertEqual('g/p/q', relpath)
 
371
 
 
372
    def test_open_containing_from_transport(self):
 
373
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
374
                          get_transport(self.get_readonly_url('')))
 
375
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
376
                          get_transport(self.get_readonly_url('g/p/q')))
 
377
        control = bzrdir.BzrDir.create(self.get_url())
 
378
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
 
379
            get_transport(self.get_readonly_url('')))
 
380
        self.assertEqual('', relpath)
 
381
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
 
382
            get_transport(self.get_readonly_url('g/p/q')))
 
383
        self.assertEqual('g/p/q', relpath)
 
384
 
 
385
 
 
386
class TestMeta1DirFormat(TestCaseWithTransport):
 
387
    """Tests specific to the meta1 dir format."""
 
388
 
 
389
    def test_right_base_dirs(self):
 
390
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
391
        t = dir.transport
 
392
        branch_base = t.clone('branch').base
 
393
        self.assertEqual(branch_base, dir.get_branch_transport(None).base)
 
394
        self.assertEqual(branch_base,
 
395
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
 
396
        repository_base = t.clone('repository').base
 
397
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
 
398
        self.assertEqual(repository_base,
 
399
                         dir.get_repository_transport(repository.RepositoryFormat7()).base)
 
400
        checkout_base = t.clone('checkout').base
 
401
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
 
402
        self.assertEqual(checkout_base,
 
403
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
 
404
 
 
405
    def test_meta1dir_uses_lockdir(self):
 
406
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
 
407
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
408
        t = dir.transport
 
409
        self.assertIsDirectory('branch-lock', t)
 
410
 
 
411
        
 
412
class TestFormat5(TestCaseWithTransport):
 
413
    """Tests specific to the version 5 bzrdir format."""
 
414
 
 
415
    def test_same_lockfiles_between_tree_repo_branch(self):
 
416
        # this checks that only a single lockfiles instance is created 
 
417
        # for format 5 objects
 
418
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
419
        def check_dir_components_use_same_lock(dir):
 
420
            ctrl_1 = dir.open_repository().control_files
 
421
            ctrl_2 = dir.open_branch().control_files
 
422
            ctrl_3 = dir.open_workingtree()._control_files
 
423
            self.assertTrue(ctrl_1 is ctrl_2)
 
424
            self.assertTrue(ctrl_2 is ctrl_3)
 
425
        check_dir_components_use_same_lock(dir)
 
426
        # and if we open it normally.
 
427
        dir = bzrdir.BzrDir.open(self.get_url())
 
428
        check_dir_components_use_same_lock(dir)
 
429
    
 
430
    def test_can_convert(self):
 
431
        # format 5 dirs are convertable
 
432
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
433
        self.assertTrue(dir.can_convert_format())
 
434
    
 
435
    def test_needs_conversion(self):
 
436
        # format 5 dirs need a conversion if they are not the default.
 
437
        # and they start of not the default.
 
438
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
439
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
 
440
        try:
 
441
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
442
            self.assertFalse(dir.needs_format_conversion())
 
443
        finally:
 
444
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
445
        self.assertTrue(dir.needs_format_conversion())
 
446
 
 
447
 
 
448
class TestFormat6(TestCaseWithTransport):
 
449
    """Tests specific to the version 6 bzrdir format."""
 
450
 
 
451
    def test_same_lockfiles_between_tree_repo_branch(self):
 
452
        # this checks that only a single lockfiles instance is created 
 
453
        # for format 6 objects
 
454
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
455
        def check_dir_components_use_same_lock(dir):
 
456
            ctrl_1 = dir.open_repository().control_files
 
457
            ctrl_2 = dir.open_branch().control_files
 
458
            ctrl_3 = dir.open_workingtree()._control_files
 
459
            self.assertTrue(ctrl_1 is ctrl_2)
 
460
            self.assertTrue(ctrl_2 is ctrl_3)
 
461
        check_dir_components_use_same_lock(dir)
 
462
        # and if we open it normally.
 
463
        dir = bzrdir.BzrDir.open(self.get_url())
 
464
        check_dir_components_use_same_lock(dir)
 
465
    
 
466
    def test_can_convert(self):
 
467
        # format 6 dirs are convertable
 
468
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
469
        self.assertTrue(dir.can_convert_format())
 
470
    
 
471
    def test_needs_conversion(self):
 
472
        # format 6 dirs need an conversion if they are not the default.
 
473
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
474
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
475
        try:
 
476
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
477
            self.assertTrue(dir.needs_format_conversion())
 
478
        finally:
 
479
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
480
 
 
481
 
 
482
class NotBzrDir(bzrlib.bzrdir.BzrDir):
 
483
    """A non .bzr based control directory."""
 
484
 
 
485
    def __init__(self, transport, format):
 
486
        self._format = format
 
487
        self.root_transport = transport
 
488
        self.transport = transport.clone('.not')
 
489
 
 
490
 
 
491
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
 
492
    """A test class representing any non-.bzr based disk format."""
 
493
 
 
494
    def initialize_on_transport(self, transport):
 
495
        """Initialize a new .not dir in the base directory of a Transport."""
 
496
        transport.mkdir('.not')
 
497
        return self.open(transport)
 
498
 
 
499
    def open(self, transport):
 
500
        """Open this directory."""
 
501
        return NotBzrDir(transport, self)
 
502
 
 
503
    @classmethod
 
504
    def _known_formats(self):
 
505
        return set([NotBzrDirFormat()])
 
506
 
 
507
    @classmethod
 
508
    def probe_transport(self, transport):
 
509
        """Our format is present if the transport ends in '.not/'."""
 
510
        if transport.has('.not'):
 
511
            return NotBzrDirFormat()
 
512
 
 
513
 
 
514
class TestNotBzrDir(TestCaseWithTransport):
 
515
    """Tests for using the bzrdir api with a non .bzr based disk format.
 
516
    
 
517
    If/when one of these is in the core, we can let the implementation tests
 
518
    verify this works.
 
519
    """
 
520
 
 
521
    def test_create_and_find_format(self):
 
522
        # create a .notbzr dir 
 
523
        format = NotBzrDirFormat()
 
524
        dir = format.initialize(self.get_url())
 
525
        self.assertIsInstance(dir, NotBzrDir)
 
526
        # now probe for it.
 
527
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
 
528
        try:
 
529
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
 
530
                get_transport(self.get_url()))
 
531
            self.assertIsInstance(found, NotBzrDirFormat)
 
532
        finally:
 
533
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
 
534
 
 
535
    def test_included_in_known_formats(self):
 
536
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
 
537
        try:
 
538
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
 
539
            for format in formats:
 
540
                if isinstance(format, NotBzrDirFormat):
 
541
                    return
 
542
            self.fail("No NotBzrDirFormat in %s" % formats)
 
543
        finally:
 
544
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
 
545
 
 
546
 
 
547
class NonLocalTests(TestCaseWithTransport):
 
548
    """Tests for bzrdir static behaviour on non local paths."""
 
549
 
 
550
    def setUp(self):
 
551
        super(NonLocalTests, self).setUp()
 
552
        self.transport_server = MemoryServer
 
553
    
 
554
    def test_create_branch_convenience(self):
 
555
        # outside a repo the default convenience output is a repo+branch_tree
 
556
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
557
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
558
        try:
 
559
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url('foo'))
 
560
            self.assertRaises(errors.NoWorkingTree,
 
561
                              branch.bzrdir.open_workingtree)
 
562
            branch.bzrdir.open_repository()
 
563
        finally:
 
564
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
565
 
 
566
    def test_create_branch_convenience_force_tree_not_local_fails(self):
 
567
        # outside a repo the default convenience output is a repo+branch_tree
 
568
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
569
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
570
        try:
 
571
            self.assertRaises(errors.NotLocalUrl,
 
572
                bzrdir.BzrDir.create_branch_convenience,
 
573
                self.get_url('foo'),
 
574
                force_new_tree=True)
 
575
            t = get_transport(self.get_url('.'))
 
576
            self.assertFalse(t.has('foo'))
 
577
        finally:
 
578
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
579
 
 
580
    def test_clone(self):
 
581
        # clone into a nonlocal path works
 
582
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
583
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
584
        try:
 
585
            branch = bzrdir.BzrDir.create_branch_convenience('local')
 
586
        finally:
 
587
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
588
        branch.bzrdir.open_workingtree()
 
589
        result = branch.bzrdir.clone(self.get_url('remote'))
 
590
        self.assertRaises(errors.NoWorkingTree,
 
591
                          result.open_workingtree)
 
592
        result.open_branch()
 
593
        result.open_repository()
 
594