~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Aaron Bentley
  • Date: 2007-01-11 03:46:53 UTC
  • mto: (2255.6.1 dirstate)
  • mto: This revision was merged to the branch mainline in revision 2322.
  • Revision ID: aaron.bentley@utoronto.ca-20070111034653-wa1n3uy49wbvom5m
Remove get_format_*, make FormatRegistry.register_metadir vary working tree

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
from bzrlib import (
 
25
    help_topics,
 
26
    symbol_versioning,
 
27
    )
 
28
import bzrlib.branch
 
29
import bzrlib.bzrdir as bzrdir
 
30
import bzrlib.errors as errors
 
31
from bzrlib.errors import (NotBranchError,
 
32
                           UnknownFormatError,
 
33
                           UnsupportedFormatError,
 
34
                           )
 
35
import bzrlib.repository as repository
 
36
from bzrlib.tests import TestCase, TestCaseWithTransport
 
37
from bzrlib.tests.HttpServer import HttpServer
 
38
from bzrlib.transport import get_transport
 
39
from bzrlib.transport.memory import MemoryServer
 
40
import bzrlib.workingtree as workingtree
 
41
 
 
42
 
 
43
class TestDefaultFormat(TestCase):
 
44
 
 
45
    def test_get_set_default_format(self):
 
46
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
47
        # default is BzrDirFormat6
 
48
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
 
49
        self.applyDeprecated(symbol_versioning.zero_fourteen, 
 
50
                             bzrdir.BzrDirFormat.set_default_format, 
 
51
                             SampleBzrDirFormat())
 
52
        # creating a bzr dir should now create an instrumented dir.
 
53
        try:
 
54
            result = bzrdir.BzrDir.create('memory:///')
 
55
            self.failUnless(isinstance(result, SampleBzrDir))
 
56
        finally:
 
57
            self.applyDeprecated(symbol_versioning.zero_fourteen,
 
58
                bzrdir.BzrDirFormat.set_default_format, old_format)
 
59
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
 
60
 
 
61
 
 
62
class TestFormatRegistry(TestCase):
 
63
 
 
64
    def make_format_registry(self):
 
65
        my_format_registry = bzrdir.BzrDirFormatRegistry()
 
66
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
 
67
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
68
            ' repositories', deprecated=True)
 
69
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
 
70
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
 
71
        my_format_registry.register_metadir('knit', 'RepositoryFormatKnit1',
 
72
            'Format using knits')
 
73
        my_format_registry.register_metadir('experimental-knit3', 
 
74
                                            'RepositoryFormatKnit2',
 
75
                                            'Format using knits', 
 
76
                                            tree='WorkingTreeFormat4')
 
77
        my_format_registry.set_default('knit')
 
78
        my_format_registry.register_metadir('metaweave', 'RepositoryFormat7',
 
79
            'Transitional format in 0.8.  Slower than knit.', deprecated=True)
 
80
        my_format_registry.register_metadir('experimental-knit2', 
 
81
                                            'RepositoryFormatKnit2',
 
82
            'Experimental successor to knit.  Use at your own risk.')
 
83
        return my_format_registry
 
84
 
 
85
    def test_format_registry(self):
 
86
        my_format_registry = self.make_format_registry()
 
87
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
 
88
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
89
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
90
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
91
        my_bzrdir = my_format_registry.make_bzrdir('default')
 
92
        self.assertIsInstance(my_bzrdir.repository_format, 
 
93
            repository.RepositoryFormatKnit1)
 
94
        my_bzrdir = my_format_registry.make_bzrdir('knit')
 
95
        self.assertIsInstance(my_bzrdir.repository_format, 
 
96
            repository.RepositoryFormatKnit1)
 
97
        my_bzrdir = my_format_registry.make_bzrdir('metaweave')
 
98
        self.assertIsInstance(my_bzrdir.repository_format, 
 
99
            repository.RepositoryFormat7)
 
100
        self.assertIsInstance(my_bzrdir.workingtree_format, 
 
101
            workingtree.WorkingTreeFormat3)
 
102
        my_bzrdir = my_format_registry.make_bzrdir('experimental-knit3')
 
103
        self.assertIsInstance(my_bzrdir.repository_format, 
 
104
            repository.RepositoryFormatKnit2)
 
105
        self.assertIsInstance(my_bzrdir.workingtree_format, 
 
106
            workingtree.WorkingTreeFormat4)
 
107
 
 
108
    def test_get_help(self):
 
109
        my_format_registry = self.make_format_registry()
 
110
        self.assertEqual('Format registered lazily',
 
111
                         my_format_registry.get_help('lazy'))
 
112
        self.assertEqual('Format using knits', 
 
113
                         my_format_registry.get_help('knit'))
 
114
        self.assertEqual('Format using knits', 
 
115
                         my_format_registry.get_help('default'))
 
116
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
 
117
                         ' checkouts or shared repositories', 
 
118
                         my_format_registry.get_help('weave'))
 
119
        
 
120
    def test_help_topic(self):
 
121
        topics = help_topics.HelpTopicRegistry()
 
122
        topics.register('formats', self.make_format_registry().help_topic, 
 
123
                        'Directory formats')
 
124
        topic = topics.get_detail('formats')
 
125
        new, deprecated = topic.split('Deprecated formats')
 
126
        self.assertContainsRe(new, 'Bazaar directory formats')
 
127
        self.assertContainsRe(new, 
 
128
            '  knit/default:\n    \(native\) Format using knits\n')
 
129
        self.assertContainsRe(deprecated, 
 
130
            '  lazy:\n    \(native\) Format registered lazily\n')
 
131
 
 
132
    def test_set_default_repository(self):
 
133
        default_factory = bzrdir.format_registry.get('default')
 
134
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
 
135
                       if v == default_factory and k != 'default'][0]
 
136
        bzrdir.format_registry.set_default_repository('metaweave')
 
137
        try:
 
138
            self.assertIs(bzrdir.format_registry.get('metaweave'),
 
139
                          bzrdir.format_registry.get('default'))
 
140
            self.assertIs(
 
141
                repository.RepositoryFormat.get_default_format().__class__,
 
142
                repository.RepositoryFormat7)
 
143
        finally:
 
144
            bzrdir.format_registry.set_default_repository(old_default)
 
145
 
 
146
class SampleBranch(bzrlib.branch.Branch):
 
147
    """A dummy branch for guess what, dummy use."""
 
148
 
 
149
    def __init__(self, dir):
 
150
        self.bzrdir = dir
 
151
 
 
152
 
 
153
class SampleBzrDir(bzrdir.BzrDir):
 
154
    """A sample BzrDir implementation to allow testing static methods."""
 
155
 
 
156
    def create_repository(self, shared=False):
 
157
        """See BzrDir.create_repository."""
 
158
        return "A repository"
 
159
 
 
160
    def open_repository(self):
 
161
        """See BzrDir.open_repository."""
 
162
        return "A repository"
 
163
 
 
164
    def create_branch(self):
 
165
        """See BzrDir.create_branch."""
 
166
        return SampleBranch(self)
 
167
 
 
168
    def create_workingtree(self):
 
169
        """See BzrDir.create_workingtree."""
 
170
        return "A tree"
 
171
 
 
172
 
 
173
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
 
174
    """A sample format
 
175
 
 
176
    this format is initializable, unsupported to aid in testing the 
 
177
    open and open_downlevel routines.
 
178
    """
 
179
 
 
180
    def get_format_string(self):
 
181
        """See BzrDirFormat.get_format_string()."""
 
182
        return "Sample .bzr dir format."
 
183
 
 
184
    def initialize(self, url):
 
185
        """Create a bzr dir."""
 
186
        t = get_transport(url)
 
187
        t.mkdir('.bzr')
 
188
        t.put_bytes('.bzr/branch-format', self.get_format_string())
 
189
        return SampleBzrDir(t, self)
 
190
 
 
191
    def is_supported(self):
 
192
        return False
 
193
 
 
194
    def open(self, transport, _found=None):
 
195
        return "opened branch."
 
196
 
 
197
 
 
198
class TestBzrDirFormat(TestCaseWithTransport):
 
199
    """Tests for the BzrDirFormat facility."""
 
200
 
 
201
    def test_find_format(self):
 
202
        # is the right format object found for a branch?
 
203
        # create a branch with a few known format objects.
 
204
        # this is not quite the same as 
 
205
        t = get_transport(self.get_url())
 
206
        self.build_tree(["foo/", "bar/"], transport=t)
 
207
        def check_format(format, url):
 
208
            format.initialize(url)
 
209
            t = get_transport(url)
 
210
            found_format = bzrdir.BzrDirFormat.find_format(t)
 
211
            self.failUnless(isinstance(found_format, format.__class__))
 
212
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
213
        check_format(bzrdir.BzrDirFormat6(), "bar")
 
214
        
 
215
    def test_find_format_nothing_there(self):
 
216
        self.assertRaises(NotBranchError,
 
217
                          bzrdir.BzrDirFormat.find_format,
 
218
                          get_transport('.'))
 
219
 
 
220
    def test_find_format_unknown_format(self):
 
221
        t = get_transport(self.get_url())
 
222
        t.mkdir('.bzr')
 
223
        t.put_bytes('.bzr/branch-format', '')
 
224
        self.assertRaises(UnknownFormatError,
 
225
                          bzrdir.BzrDirFormat.find_format,
 
226
                          get_transport('.'))
 
227
 
 
228
    def test_register_unregister_format(self):
 
229
        format = SampleBzrDirFormat()
 
230
        url = self.get_url()
 
231
        # make a bzrdir
 
232
        format.initialize(url)
 
233
        # register a format for it.
 
234
        bzrdir.BzrDirFormat.register_format(format)
 
235
        # which bzrdir.Open will refuse (not supported)
 
236
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
 
237
        # which bzrdir.open_containing will refuse (not supported)
 
238
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
 
239
        # but open_downlevel will work
 
240
        t = get_transport(url)
 
241
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
 
242
        # unregister the format
 
243
        bzrdir.BzrDirFormat.unregister_format(format)
 
244
        # now open_downlevel should fail too.
 
245
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
 
246
 
 
247
    def test_create_repository(self):
 
248
        format = SampleBzrDirFormat()
 
249
        repo = bzrdir.BzrDir.create_repository(self.get_url(), format=format)
 
250
        self.assertEqual('A repository', repo)
 
251
 
 
252
    def test_create_repository_shared(self):
 
253
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
254
        repo = bzrdir.BzrDir.create_repository('.', shared=True)
 
255
        self.assertTrue(repo.is_shared())
 
256
 
 
257
    def test_create_repository_nonshared(self):
 
258
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
259
        repo = bzrdir.BzrDir.create_repository('.')
 
260
        self.assertFalse(repo.is_shared())
 
261
 
 
262
    def test_create_repository_under_shared(self):
 
263
        # an explicit create_repository always does so.
 
264
        # we trust the format is right from the 'create_repository test'
 
265
        format = bzrdir.format_registry.make_bzrdir('knit')
 
266
        self.make_repository('.', shared=True, format=format)
 
267
        repo = bzrdir.BzrDir.create_repository(self.get_url('child'),
 
268
                                               format=format)
 
269
        self.assertTrue(isinstance(repo, repository.Repository))
 
270
        self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
 
271
 
 
272
    def test_create_branch_and_repo_uses_default(self):
 
273
        format = SampleBzrDirFormat()
 
274
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(), 
 
275
                                                      format=format)
 
276
        self.assertTrue(isinstance(branch, SampleBranch))
 
277
 
 
278
    def test_create_branch_and_repo_under_shared(self):
 
279
        # creating a branch and repo in a shared repo uses the
 
280
        # shared repository
 
281
        format = bzrdir.format_registry.make_bzrdir('knit')
 
282
        self.make_repository('.', shared=True, format=format)
 
283
        branch = bzrdir.BzrDir.create_branch_and_repo(
 
284
            self.get_url('child'), format=format)
 
285
        self.assertRaises(errors.NoRepositoryPresent,
 
286
                          branch.bzrdir.open_repository)
 
287
 
 
288
    def test_create_branch_and_repo_under_shared_force_new(self):
 
289
        # creating a branch and repo in a shared repo can be forced to 
 
290
        # make a new repo
 
291
        format = bzrdir.format_registry.make_bzrdir('knit')
 
292
        self.make_repository('.', shared=True, format=format)
 
293
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
 
294
                                                      force_new_repo=True,
 
295
                                                      format=format)
 
296
        branch.bzrdir.open_repository()
 
297
 
 
298
    def test_create_standalone_working_tree(self):
 
299
        format = SampleBzrDirFormat()
 
300
        # note this is deliberately readonly, as this failure should 
 
301
        # occur before any writes.
 
302
        self.assertRaises(errors.NotLocalUrl,
 
303
                          bzrdir.BzrDir.create_standalone_workingtree,
 
304
                          self.get_readonly_url(), format=format)
 
305
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
 
306
                                                           format=format)
 
307
        self.assertEqual('A tree', tree)
 
308
 
 
309
    def test_create_standalone_working_tree_under_shared_repo(self):
 
310
        # create standalone working tree always makes a repo.
 
311
        format = bzrdir.format_registry.make_bzrdir('knit')
 
312
        self.make_repository('.', shared=True, format=format)
 
313
        # note this is deliberately readonly, as this failure should 
 
314
        # occur before any writes.
 
315
        self.assertRaises(errors.NotLocalUrl,
 
316
                          bzrdir.BzrDir.create_standalone_workingtree,
 
317
                          self.get_readonly_url('child'), format=format)
 
318
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
 
319
            format=format)
 
320
        tree.bzrdir.open_repository()
 
321
 
 
322
    def test_create_branch_convenience(self):
 
323
        # outside a repo the default convenience output is a repo+branch_tree
 
324
        format = bzrdir.format_registry.make_bzrdir('knit')
 
325
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
 
326
        branch.bzrdir.open_workingtree()
 
327
        branch.bzrdir.open_repository()
 
328
 
 
329
    def test_create_branch_convenience_root(self):
 
330
        """Creating a branch at the root of a fs should work."""
 
331
        self.transport_server = MemoryServer
 
332
        # outside a repo the default convenience output is a repo+branch_tree
 
333
        format = bzrdir.format_registry.make_bzrdir('knit')
 
334
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
 
335
                                                         format=format)
 
336
        self.assertRaises(errors.NoWorkingTree,
 
337
                          branch.bzrdir.open_workingtree)
 
338
        branch.bzrdir.open_repository()
 
339
 
 
340
    def test_create_branch_convenience_under_shared_repo(self):
 
341
        # inside a repo the default convenience output is a branch+ follow the
 
342
        # repo tree policy
 
343
        format = bzrdir.format_registry.make_bzrdir('knit')
 
344
        self.make_repository('.', shared=True, format=format)
 
345
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
346
            format=format)
 
347
        branch.bzrdir.open_workingtree()
 
348
        self.assertRaises(errors.NoRepositoryPresent,
 
349
                          branch.bzrdir.open_repository)
 
350
            
 
351
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
 
352
        # inside a repo the default convenience output is a branch+ follow the
 
353
        # repo tree policy but we can override that
 
354
        format = bzrdir.format_registry.make_bzrdir('knit')
 
355
        self.make_repository('.', shared=True, format=format)
 
356
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
357
            force_new_tree=False, format=format)
 
358
        self.assertRaises(errors.NoWorkingTree,
 
359
                          branch.bzrdir.open_workingtree)
 
360
        self.assertRaises(errors.NoRepositoryPresent,
 
361
                          branch.bzrdir.open_repository)
 
362
            
 
363
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
 
364
        # inside a repo the default convenience output is a branch+ follow the
 
365
        # repo tree policy
 
366
        format = bzrdir.format_registry.make_bzrdir('knit')
 
367
        repo = self.make_repository('.', shared=True, format=format)
 
368
        repo.set_make_working_trees(False)
 
369
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
 
370
                                                         format=format)
 
371
        self.assertRaises(errors.NoWorkingTree,
 
372
                          branch.bzrdir.open_workingtree)
 
373
        self.assertRaises(errors.NoRepositoryPresent,
 
374
                          branch.bzrdir.open_repository)
 
375
 
 
376
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
 
377
        # inside a repo the default convenience output is a branch+ follow the
 
378
        # repo tree policy but we can override that
 
379
        format = bzrdir.format_registry.make_bzrdir('knit')
 
380
        repo = self.make_repository('.', shared=True, format=format)
 
381
        repo.set_make_working_trees(False)
 
382
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
383
            force_new_tree=True, format=format)
 
384
        branch.bzrdir.open_workingtree()
 
385
        self.assertRaises(errors.NoRepositoryPresent,
 
386
                          branch.bzrdir.open_repository)
 
387
 
 
388
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
 
389
        # inside a repo the default convenience output is overridable to give
 
390
        # repo+branch+tree
 
391
        format = bzrdir.format_registry.make_bzrdir('knit')
 
392
        self.make_repository('.', shared=True, format=format)
 
393
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
394
            force_new_repo=True, format=format)
 
395
        branch.bzrdir.open_repository()
 
396
        branch.bzrdir.open_workingtree()
 
397
 
 
398
 
 
399
class ChrootedTests(TestCaseWithTransport):
 
400
    """A support class that provides readonly urls outside the local namespace.
 
401
 
 
402
    This is done by checking if self.transport_server is a MemoryServer. if it
 
403
    is then we are chrooted already, if it is not then an HttpServer is used
 
404
    for readonly urls.
 
405
    """
 
406
 
 
407
    def setUp(self):
 
408
        super(ChrootedTests, self).setUp()
 
409
        if not self.transport_server == MemoryServer:
 
410
            self.transport_readonly_server = HttpServer
 
411
 
 
412
    def test_open_containing(self):
 
413
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
 
414
                          self.get_readonly_url(''))
 
415
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
 
416
                          self.get_readonly_url('g/p/q'))
 
417
        control = bzrdir.BzrDir.create(self.get_url())
 
418
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
 
419
        self.assertEqual('', relpath)
 
420
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
 
421
        self.assertEqual('g/p/q', relpath)
 
422
 
 
423
    def test_open_containing_from_transport(self):
 
424
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
425
                          get_transport(self.get_readonly_url('')))
 
426
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
427
                          get_transport(self.get_readonly_url('g/p/q')))
 
428
        control = bzrdir.BzrDir.create(self.get_url())
 
429
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
 
430
            get_transport(self.get_readonly_url('')))
 
431
        self.assertEqual('', relpath)
 
432
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
 
433
            get_transport(self.get_readonly_url('g/p/q')))
 
434
        self.assertEqual('g/p/q', relpath)
 
435
 
 
436
    def test_open_from_transport(self):
 
437
        # transport pointing at bzrdir should give a bzrdir with root transport
 
438
        # set to the given transport
 
439
        control = bzrdir.BzrDir.create(self.get_url())
 
440
        transport = get_transport(self.get_url())
 
441
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
 
442
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
 
443
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
 
444
        
 
445
    def test_open_from_transport_no_bzrdir(self):
 
446
        transport = get_transport(self.get_url())
 
447
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
448
                          transport)
 
449
 
 
450
    def test_open_from_transport_bzrdir_in_parent(self):
 
451
        control = bzrdir.BzrDir.create(self.get_url())
 
452
        transport = get_transport(self.get_url())
 
453
        transport.mkdir('subdir')
 
454
        transport = transport.clone('subdir')
 
455
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
456
                          transport)
 
457
 
 
458
 
 
459
class TestMeta1DirFormat(TestCaseWithTransport):
 
460
    """Tests specific to the meta1 dir format."""
 
461
 
 
462
    def test_right_base_dirs(self):
 
463
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
464
        t = dir.transport
 
465
        branch_base = t.clone('branch').base
 
466
        self.assertEqual(branch_base, dir.get_branch_transport(None).base)
 
467
        self.assertEqual(branch_base,
 
468
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
 
469
        repository_base = t.clone('repository').base
 
470
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
 
471
        self.assertEqual(repository_base,
 
472
                         dir.get_repository_transport(repository.RepositoryFormat7()).base)
 
473
        checkout_base = t.clone('checkout').base
 
474
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
 
475
        self.assertEqual(checkout_base,
 
476
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
 
477
 
 
478
    def test_meta1dir_uses_lockdir(self):
 
479
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
 
480
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
481
        t = dir.transport
 
482
        self.assertIsDirectory('branch-lock', t)
 
483
 
 
484
        
 
485
class TestFormat5(TestCaseWithTransport):
 
486
    """Tests specific to the version 5 bzrdir format."""
 
487
 
 
488
    def test_same_lockfiles_between_tree_repo_branch(self):
 
489
        # this checks that only a single lockfiles instance is created 
 
490
        # for format 5 objects
 
491
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
492
        def check_dir_components_use_same_lock(dir):
 
493
            ctrl_1 = dir.open_repository().control_files
 
494
            ctrl_2 = dir.open_branch().control_files
 
495
            ctrl_3 = dir.open_workingtree()._control_files
 
496
            self.assertTrue(ctrl_1 is ctrl_2)
 
497
            self.assertTrue(ctrl_2 is ctrl_3)
 
498
        check_dir_components_use_same_lock(dir)
 
499
        # and if we open it normally.
 
500
        dir = bzrdir.BzrDir.open(self.get_url())
 
501
        check_dir_components_use_same_lock(dir)
 
502
    
 
503
    def test_can_convert(self):
 
504
        # format 5 dirs are convertable
 
505
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
506
        self.assertTrue(dir.can_convert_format())
 
507
    
 
508
    def test_needs_conversion(self):
 
509
        # format 5 dirs need a conversion if they are not the default.
 
510
        # and they start of not the default.
 
511
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
512
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
513
        try:
 
514
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
515
            self.assertFalse(dir.needs_format_conversion())
 
516
        finally:
 
517
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
518
        self.assertTrue(dir.needs_format_conversion())
 
519
 
 
520
 
 
521
class TestFormat6(TestCaseWithTransport):
 
522
    """Tests specific to the version 6 bzrdir format."""
 
523
 
 
524
    def test_same_lockfiles_between_tree_repo_branch(self):
 
525
        # this checks that only a single lockfiles instance is created 
 
526
        # for format 6 objects
 
527
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
528
        def check_dir_components_use_same_lock(dir):
 
529
            ctrl_1 = dir.open_repository().control_files
 
530
            ctrl_2 = dir.open_branch().control_files
 
531
            ctrl_3 = dir.open_workingtree()._control_files
 
532
            self.assertTrue(ctrl_1 is ctrl_2)
 
533
            self.assertTrue(ctrl_2 is ctrl_3)
 
534
        check_dir_components_use_same_lock(dir)
 
535
        # and if we open it normally.
 
536
        dir = bzrdir.BzrDir.open(self.get_url())
 
537
        check_dir_components_use_same_lock(dir)
 
538
    
 
539
    def test_can_convert(self):
 
540
        # format 6 dirs are convertable
 
541
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
542
        self.assertTrue(dir.can_convert_format())
 
543
    
 
544
    def test_needs_conversion(self):
 
545
        # format 6 dirs need an conversion if they are not the default.
 
546
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
547
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
 
548
        try:
 
549
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
550
            self.assertTrue(dir.needs_format_conversion())
 
551
        finally:
 
552
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
553
 
 
554
 
 
555
class NotBzrDir(bzrlib.bzrdir.BzrDir):
 
556
    """A non .bzr based control directory."""
 
557
 
 
558
    def __init__(self, transport, format):
 
559
        self._format = format
 
560
        self.root_transport = transport
 
561
        self.transport = transport.clone('.not')
 
562
 
 
563
 
 
564
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
 
565
    """A test class representing any non-.bzr based disk format."""
 
566
 
 
567
    def initialize_on_transport(self, transport):
 
568
        """Initialize a new .not dir in the base directory of a Transport."""
 
569
        transport.mkdir('.not')
 
570
        return self.open(transport)
 
571
 
 
572
    def open(self, transport):
 
573
        """Open this directory."""
 
574
        return NotBzrDir(transport, self)
 
575
 
 
576
    @classmethod
 
577
    def _known_formats(self):
 
578
        return set([NotBzrDirFormat()])
 
579
 
 
580
    @classmethod
 
581
    def probe_transport(self, transport):
 
582
        """Our format is present if the transport ends in '.not/'."""
 
583
        if transport.has('.not'):
 
584
            return NotBzrDirFormat()
 
585
 
 
586
 
 
587
class TestNotBzrDir(TestCaseWithTransport):
 
588
    """Tests for using the bzrdir api with a non .bzr based disk format.
 
589
    
 
590
    If/when one of these is in the core, we can let the implementation tests
 
591
    verify this works.
 
592
    """
 
593
 
 
594
    def test_create_and_find_format(self):
 
595
        # create a .notbzr dir 
 
596
        format = NotBzrDirFormat()
 
597
        dir = format.initialize(self.get_url())
 
598
        self.assertIsInstance(dir, NotBzrDir)
 
599
        # now probe for it.
 
600
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
 
601
        try:
 
602
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
 
603
                get_transport(self.get_url()))
 
604
            self.assertIsInstance(found, NotBzrDirFormat)
 
605
        finally:
 
606
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
 
607
 
 
608
    def test_included_in_known_formats(self):
 
609
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
 
610
        try:
 
611
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
 
612
            for format in formats:
 
613
                if isinstance(format, NotBzrDirFormat):
 
614
                    return
 
615
            self.fail("No NotBzrDirFormat in %s" % formats)
 
616
        finally:
 
617
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
 
618
 
 
619
 
 
620
class NonLocalTests(TestCaseWithTransport):
 
621
    """Tests for bzrdir static behaviour on non local paths."""
 
622
 
 
623
    def setUp(self):
 
624
        super(NonLocalTests, self).setUp()
 
625
        self.transport_server = MemoryServer
 
626
    
 
627
    def test_create_branch_convenience(self):
 
628
        # outside a repo the default convenience output is a repo+branch_tree
 
629
        format = bzrdir.format_registry.make_bzrdir('knit')
 
630
        branch = bzrdir.BzrDir.create_branch_convenience(
 
631
            self.get_url('foo'), format=format)
 
632
        self.assertRaises(errors.NoWorkingTree,
 
633
                          branch.bzrdir.open_workingtree)
 
634
        branch.bzrdir.open_repository()
 
635
 
 
636
    def test_create_branch_convenience_force_tree_not_local_fails(self):
 
637
        # outside a repo the default convenience output is a repo+branch_tree
 
638
        format = bzrdir.format_registry.make_bzrdir('knit')
 
639
        self.assertRaises(errors.NotLocalUrl,
 
640
            bzrdir.BzrDir.create_branch_convenience,
 
641
            self.get_url('foo'),
 
642
            force_new_tree=True,
 
643
            format=format)
 
644
        t = get_transport(self.get_url('.'))
 
645
        self.assertFalse(t.has('foo'))
 
646
 
 
647
    def test_clone(self):
 
648
        # clone into a nonlocal path works
 
649
        format = bzrdir.format_registry.make_bzrdir('knit')
 
650
        branch = bzrdir.BzrDir.create_branch_convenience('local',
 
651
                                                         format=format)
 
652
        branch.bzrdir.open_workingtree()
 
653
        result = branch.bzrdir.clone(self.get_url('remote'))
 
654
        self.assertRaises(errors.NoWorkingTree,
 
655
                          result.open_workingtree)
 
656
        result.open_branch()
 
657
        result.open_repository()
 
658