1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
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.
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.
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
17
"""Tests for the BzrDir facility and any format specific tests.
19
For interface contract tests, see tests/bzr_dir_implementations.
23
from StringIO import StringIO
35
from bzrlib.errors import (NotBranchError,
37
UnsupportedFormatError,
39
from bzrlib.tests import TestCase, TestCaseWithTransport, test_sftp_transport
40
from bzrlib.tests.HttpServer import HttpServer
41
from bzrlib.transport import get_transport
42
from bzrlib.transport.memory import MemoryServer
43
from bzrlib.repofmt import knitrepo, weaverepo
46
class TestDefaultFormat(TestCase):
48
def test_get_set_default_format(self):
49
old_format = bzrdir.BzrDirFormat.get_default_format()
50
# default is BzrDirFormat6
51
self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
52
self.applyDeprecated(symbol_versioning.zero_fourteen,
53
bzrdir.BzrDirFormat.set_default_format,
55
# creating a bzr dir should now create an instrumented dir.
57
result = bzrdir.BzrDir.create('memory:///')
58
self.failUnless(isinstance(result, SampleBzrDir))
60
self.applyDeprecated(symbol_versioning.zero_fourteen,
61
bzrdir.BzrDirFormat.set_default_format, old_format)
62
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
65
class TestFormatRegistry(TestCase):
67
def make_format_registry(self):
68
my_format_registry = bzrdir.BzrDirFormatRegistry()
69
my_format_registry.register('weave', bzrdir.BzrDirFormat6,
70
'Pre-0.8 format. Slower and does not support checkouts or shared'
71
' repositories', deprecated=True)
72
my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
73
'BzrDirFormat6', 'Format registered lazily', deprecated=True)
74
my_format_registry.register_metadir('knit',
75
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
78
my_format_registry.register_metadir('experimental-knit3',
79
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
81
tree='WorkingTreeFormatAB1')
82
my_format_registry.set_default('knit')
83
my_format_registry.register_metadir(
85
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit2',
86
'Experimental successor to knit. Use at your own risk.',
88
my_format_registry.register_metadir(
90
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit2',
91
'Experimental successor to knit. Use at your own risk.',
92
branch_format='BzrBranchFormat6')
93
return my_format_registry
95
def test_format_registry(self):
96
my_format_registry = self.make_format_registry()
97
my_bzrdir = my_format_registry.make_bzrdir('lazy')
98
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
99
my_bzrdir = my_format_registry.make_bzrdir('weave')
100
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
101
my_bzrdir = my_format_registry.make_bzrdir('default')
102
self.assertIsInstance(my_bzrdir.repository_format,
103
knitrepo.RepositoryFormatKnit1)
104
my_bzrdir = my_format_registry.make_bzrdir('knit')
105
self.assertIsInstance(my_bzrdir.repository_format,
106
knitrepo.RepositoryFormatKnit1)
107
my_bzrdir = my_format_registry.make_bzrdir('experimental-knit3')
108
self.assertIsInstance(my_bzrdir.repository_format,
109
knitrepo.RepositoryFormatKnit3)
110
self.assertIsInstance(my_bzrdir.workingtree_format,
111
workingtree.WorkingTreeFormatAB1)
112
my_bzrdir = my_format_registry.make_bzrdir('branch6')
113
self.assertIsInstance(my_bzrdir.get_branch_format(),
114
bzrlib.branch.BzrBranchFormat6)
116
def test_get_help(self):
117
my_format_registry = self.make_format_registry()
118
self.assertEqual('Format registered lazily',
119
my_format_registry.get_help('lazy'))
120
self.assertEqual('Format using knits',
121
my_format_registry.get_help('knit'))
122
self.assertEqual('Format using knits',
123
my_format_registry.get_help('default'))
124
self.assertEqual('Pre-0.8 format. Slower and does not support'
125
' checkouts or shared repositories',
126
my_format_registry.get_help('weave'))
128
def test_help_topic(self):
129
topics = help_topics.HelpTopicRegistry()
130
topics.register('formats', self.make_format_registry().help_topic,
132
topic = topics.get_detail('formats')
133
new, deprecated = topic.split('Deprecated formats')
134
self.assertContainsRe(new, 'Bazaar directory formats')
135
self.assertContainsRe(new,
136
' knit/default:\n \(native\) Format using knits\n')
137
self.assertContainsRe(deprecated,
138
' lazy:\n \(native\) Format registered lazily\n')
140
def test_set_default_repository(self):
141
default_factory = bzrdir.format_registry.get('default')
142
old_default = [k for k, v in bzrdir.format_registry.iteritems()
143
if v == default_factory and k != 'default'][0]
144
bzrdir.format_registry.set_default_repository('experimental-knit2')
146
self.assertIs(bzrdir.format_registry.get('experimental-knit2'),
147
bzrdir.format_registry.get('default'))
149
repository.RepositoryFormat.get_default_format().__class__,
150
knitrepo.RepositoryFormatKnit2)
152
bzrdir.format_registry.set_default_repository(old_default)
154
class SampleBranch(bzrlib.branch.Branch):
155
"""A dummy branch for guess what, dummy use."""
157
def __init__(self, dir):
161
class SampleBzrDir(bzrdir.BzrDir):
162
"""A sample BzrDir implementation to allow testing static methods."""
164
def create_repository(self, shared=False):
165
"""See BzrDir.create_repository."""
166
return "A repository"
168
def open_repository(self):
169
"""See BzrDir.open_repository."""
170
return "A repository"
172
def create_branch(self):
173
"""See BzrDir.create_branch."""
174
return SampleBranch(self)
176
def create_workingtree(self):
177
"""See BzrDir.create_workingtree."""
181
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
184
this format is initializable, unsupported to aid in testing the
185
open and open_downlevel routines.
188
def get_format_string(self):
189
"""See BzrDirFormat.get_format_string()."""
190
return "Sample .bzr dir format."
192
def initialize(self, url):
193
"""Create a bzr dir."""
194
t = get_transport(url)
196
t.put_bytes('.bzr/branch-format', self.get_format_string())
197
return SampleBzrDir(t, self)
199
def is_supported(self):
202
def open(self, transport, _found=None):
203
return "opened branch."
206
class TestBzrDirFormat(TestCaseWithTransport):
207
"""Tests for the BzrDirFormat facility."""
209
def test_find_format(self):
210
# is the right format object found for a branch?
211
# create a branch with a few known format objects.
212
# this is not quite the same as
213
t = get_transport(self.get_url())
214
self.build_tree(["foo/", "bar/"], transport=t)
215
def check_format(format, url):
216
format.initialize(url)
217
t = get_transport(url)
218
found_format = bzrdir.BzrDirFormat.find_format(t)
219
self.failUnless(isinstance(found_format, format.__class__))
220
check_format(bzrdir.BzrDirFormat5(), "foo")
221
check_format(bzrdir.BzrDirFormat6(), "bar")
223
def test_find_format_nothing_there(self):
224
self.assertRaises(NotBranchError,
225
bzrdir.BzrDirFormat.find_format,
228
def test_find_format_unknown_format(self):
229
t = get_transport(self.get_url())
231
t.put_bytes('.bzr/branch-format', '')
232
self.assertRaises(UnknownFormatError,
233
bzrdir.BzrDirFormat.find_format,
236
def test_register_unregister_format(self):
237
format = SampleBzrDirFormat()
240
format.initialize(url)
241
# register a format for it.
242
bzrdir.BzrDirFormat.register_format(format)
243
# which bzrdir.Open will refuse (not supported)
244
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
245
# which bzrdir.open_containing will refuse (not supported)
246
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
247
# but open_downlevel will work
248
t = get_transport(url)
249
self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
250
# unregister the format
251
bzrdir.BzrDirFormat.unregister_format(format)
252
# now open_downlevel should fail too.
253
self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
255
def test_create_repository(self):
256
format = SampleBzrDirFormat()
257
repo = bzrdir.BzrDir.create_repository(self.get_url(), format=format)
258
self.assertEqual('A repository', repo)
260
def test_create_repository_shared(self):
261
old_format = bzrdir.BzrDirFormat.get_default_format()
262
repo = bzrdir.BzrDir.create_repository('.', shared=True)
263
self.assertTrue(repo.is_shared())
265
def test_create_repository_nonshared(self):
266
old_format = bzrdir.BzrDirFormat.get_default_format()
267
repo = bzrdir.BzrDir.create_repository('.')
268
self.assertFalse(repo.is_shared())
270
def test_create_repository_under_shared(self):
271
# an explicit create_repository always does so.
272
# we trust the format is right from the 'create_repository test'
273
format = bzrdir.format_registry.make_bzrdir('knit')
274
self.make_repository('.', shared=True, format=format)
275
repo = bzrdir.BzrDir.create_repository(self.get_url('child'),
277
self.assertTrue(isinstance(repo, repository.Repository))
278
self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
280
def test_create_branch_and_repo_uses_default(self):
281
format = SampleBzrDirFormat()
282
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
284
self.assertTrue(isinstance(branch, SampleBranch))
286
def test_create_branch_and_repo_under_shared(self):
287
# creating a branch and repo in a shared repo uses the
289
format = bzrdir.format_registry.make_bzrdir('knit')
290
self.make_repository('.', shared=True, format=format)
291
branch = bzrdir.BzrDir.create_branch_and_repo(
292
self.get_url('child'), format=format)
293
self.assertRaises(errors.NoRepositoryPresent,
294
branch.bzrdir.open_repository)
296
def test_create_branch_and_repo_under_shared_force_new(self):
297
# creating a branch and repo in a shared repo can be forced to
299
format = bzrdir.format_registry.make_bzrdir('knit')
300
self.make_repository('.', shared=True, format=format)
301
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
304
branch.bzrdir.open_repository()
306
def test_create_standalone_working_tree(self):
307
format = SampleBzrDirFormat()
308
# note this is deliberately readonly, as this failure should
309
# occur before any writes.
310
self.assertRaises(errors.NotLocalUrl,
311
bzrdir.BzrDir.create_standalone_workingtree,
312
self.get_readonly_url(), format=format)
313
tree = bzrdir.BzrDir.create_standalone_workingtree('.',
315
self.assertEqual('A tree', tree)
317
def test_create_standalone_working_tree_under_shared_repo(self):
318
# create standalone working tree always makes a repo.
319
format = bzrdir.format_registry.make_bzrdir('knit')
320
self.make_repository('.', shared=True, format=format)
321
# note this is deliberately readonly, as this failure should
322
# occur before any writes.
323
self.assertRaises(errors.NotLocalUrl,
324
bzrdir.BzrDir.create_standalone_workingtree,
325
self.get_readonly_url('child'), format=format)
326
tree = bzrdir.BzrDir.create_standalone_workingtree('child',
328
tree.bzrdir.open_repository()
330
def test_create_branch_convenience(self):
331
# outside a repo the default convenience output is a repo+branch_tree
332
format = bzrdir.format_registry.make_bzrdir('knit')
333
branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
334
branch.bzrdir.open_workingtree()
335
branch.bzrdir.open_repository()
337
def test_create_branch_convenience_root(self):
338
"""Creating a branch at the root of a fs should work."""
339
self.transport_server = MemoryServer
340
# 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(self.get_url(),
344
self.assertRaises(errors.NoWorkingTree,
345
branch.bzrdir.open_workingtree)
346
branch.bzrdir.open_repository()
348
def test_create_branch_convenience_under_shared_repo(self):
349
# inside a repo the default convenience output is a branch+ follow the
351
format = bzrdir.format_registry.make_bzrdir('knit')
352
self.make_repository('.', shared=True, format=format)
353
branch = bzrdir.BzrDir.create_branch_convenience('child',
355
branch.bzrdir.open_workingtree()
356
self.assertRaises(errors.NoRepositoryPresent,
357
branch.bzrdir.open_repository)
359
def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
360
# inside a repo the default convenience output is a branch+ follow the
361
# repo tree policy but we can override that
362
format = bzrdir.format_registry.make_bzrdir('knit')
363
self.make_repository('.', shared=True, format=format)
364
branch = bzrdir.BzrDir.create_branch_convenience('child',
365
force_new_tree=False, format=format)
366
self.assertRaises(errors.NoWorkingTree,
367
branch.bzrdir.open_workingtree)
368
self.assertRaises(errors.NoRepositoryPresent,
369
branch.bzrdir.open_repository)
371
def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
372
# inside a repo the default convenience output is a branch+ follow the
374
format = bzrdir.format_registry.make_bzrdir('knit')
375
repo = self.make_repository('.', shared=True, format=format)
376
repo.set_make_working_trees(False)
377
branch = bzrdir.BzrDir.create_branch_convenience('child',
379
self.assertRaises(errors.NoWorkingTree,
380
branch.bzrdir.open_workingtree)
381
self.assertRaises(errors.NoRepositoryPresent,
382
branch.bzrdir.open_repository)
384
def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
385
# inside a repo the default convenience output is a branch+ follow the
386
# repo tree policy but we can override that
387
format = bzrdir.format_registry.make_bzrdir('knit')
388
repo = self.make_repository('.', shared=True, format=format)
389
repo.set_make_working_trees(False)
390
branch = bzrdir.BzrDir.create_branch_convenience('child',
391
force_new_tree=True, format=format)
392
branch.bzrdir.open_workingtree()
393
self.assertRaises(errors.NoRepositoryPresent,
394
branch.bzrdir.open_repository)
396
def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
397
# inside a repo the default convenience output is overridable to give
399
format = bzrdir.format_registry.make_bzrdir('knit')
400
self.make_repository('.', shared=True, format=format)
401
branch = bzrdir.BzrDir.create_branch_convenience('child',
402
force_new_repo=True, format=format)
403
branch.bzrdir.open_repository()
404
branch.bzrdir.open_workingtree()
407
class ChrootedTests(TestCaseWithTransport):
408
"""A support class that provides readonly urls outside the local namespace.
410
This is done by checking if self.transport_server is a MemoryServer. if it
411
is then we are chrooted already, if it is not then an HttpServer is used
416
super(ChrootedTests, self).setUp()
417
if not self.transport_server == MemoryServer:
418
self.transport_readonly_server = HttpServer
420
def test_open_containing(self):
421
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
422
self.get_readonly_url(''))
423
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
424
self.get_readonly_url('g/p/q'))
425
control = bzrdir.BzrDir.create(self.get_url())
426
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
427
self.assertEqual('', relpath)
428
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
429
self.assertEqual('g/p/q', relpath)
431
def test_open_containing_from_transport(self):
432
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
433
get_transport(self.get_readonly_url('')))
434
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
435
get_transport(self.get_readonly_url('g/p/q')))
436
control = bzrdir.BzrDir.create(self.get_url())
437
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
438
get_transport(self.get_readonly_url('')))
439
self.assertEqual('', relpath)
440
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
441
get_transport(self.get_readonly_url('g/p/q')))
442
self.assertEqual('g/p/q', relpath)
444
def test_open_containing_tree_or_branch(self):
445
def local_branch_path(branch):
446
return os.path.realpath(
447
urlutils.local_path_from_url(branch.base))
449
self.make_branch_and_tree('topdir')
450
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
452
self.assertEqual(os.path.realpath('topdir'),
453
os.path.realpath(tree.basedir))
454
self.assertEqual(os.path.realpath('topdir'),
455
local_branch_path(branch))
456
self.assertIs(tree.bzrdir, branch.bzrdir)
457
self.assertEqual('foo', relpath)
458
self.make_branch('topdir/foo')
459
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
461
self.assertIs(tree, None)
462
self.assertEqual(os.path.realpath('topdir/foo'),
463
local_branch_path(branch))
464
self.assertEqual('', relpath)
466
def test_open_from_transport(self):
467
# transport pointing at bzrdir should give a bzrdir with root transport
468
# set to the given transport
469
control = bzrdir.BzrDir.create(self.get_url())
470
transport = get_transport(self.get_url())
471
opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
472
self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
473
self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
475
def test_open_from_transport_no_bzrdir(self):
476
transport = get_transport(self.get_url())
477
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
480
def test_open_from_transport_bzrdir_in_parent(self):
481
control = bzrdir.BzrDir.create(self.get_url())
482
transport = get_transport(self.get_url())
483
transport.mkdir('subdir')
484
transport = transport.clone('subdir')
485
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
488
def test_sprout_recursive(self):
489
tree = self.make_branch_and_tree('tree1', format='experimental-knit3')
490
sub_tree = self.make_branch_and_tree('tree1/subtree',
491
format='experimental-knit3')
492
tree.add_reference(sub_tree)
493
self.build_tree(['tree1/subtree/file'])
495
tree.commit('Initial commit')
496
tree.bzrdir.sprout('tree2')
497
self.failUnlessExists('tree2/subtree/file')
499
def test_cloning_metadir(self):
500
"""Ensure that cloning metadir is suitable"""
501
bzrdir = self.make_bzrdir('bzrdir')
502
bzrdir.cloning_metadir()
503
branch = self.make_branch('branch', format='knit')
504
format = branch.bzrdir.cloning_metadir()
505
self.assertIsInstance(format.workingtree_format,
506
workingtree.WorkingTreeFormat3)
507
branch2 = self.make_branch('branch2', format='experimental-knit3')
508
format2 = branch2.bzrdir.cloning_metadir()
509
self.assertIsInstance(format2.workingtree_format,
510
workingtree.WorkingTreeFormatAB1)
512
def test_sprout_recursive_treeless(self):
513
tree = self.make_branch_and_tree('tree1', format='experimental-knit3')
514
sub_tree = self.make_branch_and_tree('tree1/subtree',
515
format='experimental-knit3')
516
tree.add_reference(sub_tree)
517
self.build_tree(['tree1/subtree/file'])
519
tree.commit('Initial commit')
520
tree.bzrdir.destroy_workingtree()
521
repo = self.make_repository('repo', shared=True,
522
format='experimental-knit3')
523
repo.set_make_working_trees(False)
524
tree.bzrdir.sprout('repo/tree2')
525
self.failUnlessExists('repo/tree2/subtree')
526
self.failIfExists('repo/tree2/subtree/file')
529
class TestMeta1DirFormat(TestCaseWithTransport):
530
"""Tests specific to the meta1 dir format."""
532
def test_right_base_dirs(self):
533
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
535
branch_base = t.clone('branch').base
536
self.assertEqual(branch_base, dir.get_branch_transport(None).base)
537
self.assertEqual(branch_base,
538
dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
539
repository_base = t.clone('repository').base
540
self.assertEqual(repository_base, dir.get_repository_transport(None).base)
541
self.assertEqual(repository_base,
542
dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
543
checkout_base = t.clone('checkout').base
544
self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
545
self.assertEqual(checkout_base,
546
dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
548
def test_meta1dir_uses_lockdir(self):
549
"""Meta1 format uses a LockDir to guard the whole directory, not a file."""
550
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
552
self.assertIsDirectory('branch-lock', t)
554
def test_comparison(self):
555
"""Equality and inequality behave properly.
557
Metadirs should compare equal iff they have the same repo, branch and
560
mydir = bzrdir.format_registry.make_bzrdir('knit')
561
self.assertEqual(mydir, mydir)
562
self.assertFalse(mydir != mydir)
563
otherdir = bzrdir.format_registry.make_bzrdir('knit')
564
self.assertEqual(otherdir, mydir)
565
self.assertFalse(otherdir != mydir)
566
otherdir2 = bzrdir.format_registry.make_bzrdir('experimental-knit2')
567
self.assertNotEqual(otherdir2, mydir)
568
self.assertFalse(otherdir2 == mydir)
571
class TestFormat5(TestCaseWithTransport):
572
"""Tests specific to the version 5 bzrdir format."""
574
def test_same_lockfiles_between_tree_repo_branch(self):
575
# this checks that only a single lockfiles instance is created
576
# for format 5 objects
577
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
578
def check_dir_components_use_same_lock(dir):
579
ctrl_1 = dir.open_repository().control_files
580
ctrl_2 = dir.open_branch().control_files
581
ctrl_3 = dir.open_workingtree()._control_files
582
self.assertTrue(ctrl_1 is ctrl_2)
583
self.assertTrue(ctrl_2 is ctrl_3)
584
check_dir_components_use_same_lock(dir)
585
# and if we open it normally.
586
dir = bzrdir.BzrDir.open(self.get_url())
587
check_dir_components_use_same_lock(dir)
589
def test_can_convert(self):
590
# format 5 dirs are convertable
591
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
592
self.assertTrue(dir.can_convert_format())
594
def test_needs_conversion(self):
595
# format 5 dirs need a conversion if they are not the default.
596
# and they start of not the default.
597
old_format = bzrdir.BzrDirFormat.get_default_format()
598
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
600
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
601
self.assertFalse(dir.needs_format_conversion())
603
bzrdir.BzrDirFormat._set_default_format(old_format)
604
self.assertTrue(dir.needs_format_conversion())
607
class TestFormat6(TestCaseWithTransport):
608
"""Tests specific to the version 6 bzrdir format."""
610
def test_same_lockfiles_between_tree_repo_branch(self):
611
# this checks that only a single lockfiles instance is created
612
# for format 6 objects
613
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
614
def check_dir_components_use_same_lock(dir):
615
ctrl_1 = dir.open_repository().control_files
616
ctrl_2 = dir.open_branch().control_files
617
ctrl_3 = dir.open_workingtree()._control_files
618
self.assertTrue(ctrl_1 is ctrl_2)
619
self.assertTrue(ctrl_2 is ctrl_3)
620
check_dir_components_use_same_lock(dir)
621
# and if we open it normally.
622
dir = bzrdir.BzrDir.open(self.get_url())
623
check_dir_components_use_same_lock(dir)
625
def test_can_convert(self):
626
# format 6 dirs are convertable
627
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
628
self.assertTrue(dir.can_convert_format())
630
def test_needs_conversion(self):
631
# format 6 dirs need an conversion if they are not the default.
632
old_format = bzrdir.BzrDirFormat.get_default_format()
633
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
635
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
636
self.assertTrue(dir.needs_format_conversion())
638
bzrdir.BzrDirFormat._set_default_format(old_format)
641
class NotBzrDir(bzrlib.bzrdir.BzrDir):
642
"""A non .bzr based control directory."""
644
def __init__(self, transport, format):
645
self._format = format
646
self.root_transport = transport
647
self.transport = transport.clone('.not')
650
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
651
"""A test class representing any non-.bzr based disk format."""
653
def initialize_on_transport(self, transport):
654
"""Initialize a new .not dir in the base directory of a Transport."""
655
transport.mkdir('.not')
656
return self.open(transport)
658
def open(self, transport):
659
"""Open this directory."""
660
return NotBzrDir(transport, self)
663
def _known_formats(self):
664
return set([NotBzrDirFormat()])
667
def probe_transport(self, transport):
668
"""Our format is present if the transport ends in '.not/'."""
669
if transport.has('.not'):
670
return NotBzrDirFormat()
673
class TestNotBzrDir(TestCaseWithTransport):
674
"""Tests for using the bzrdir api with a non .bzr based disk format.
676
If/when one of these is in the core, we can let the implementation tests
680
def test_create_and_find_format(self):
681
# create a .notbzr dir
682
format = NotBzrDirFormat()
683
dir = format.initialize(self.get_url())
684
self.assertIsInstance(dir, NotBzrDir)
686
bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
688
found = bzrlib.bzrdir.BzrDirFormat.find_format(
689
get_transport(self.get_url()))
690
self.assertIsInstance(found, NotBzrDirFormat)
692
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
694
def test_included_in_known_formats(self):
695
bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
697
formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
698
for format in formats:
699
if isinstance(format, NotBzrDirFormat):
701
self.fail("No NotBzrDirFormat in %s" % formats)
703
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
706
class NonLocalTests(TestCaseWithTransport):
707
"""Tests for bzrdir static behaviour on non local paths."""
710
super(NonLocalTests, self).setUp()
711
self.transport_server = MemoryServer
713
def test_create_branch_convenience(self):
714
# outside a repo the default convenience output is a repo+branch_tree
715
format = bzrdir.format_registry.make_bzrdir('knit')
716
branch = bzrdir.BzrDir.create_branch_convenience(
717
self.get_url('foo'), format=format)
718
self.assertRaises(errors.NoWorkingTree,
719
branch.bzrdir.open_workingtree)
720
branch.bzrdir.open_repository()
722
def test_create_branch_convenience_force_tree_not_local_fails(self):
723
# outside a repo the default convenience output is a repo+branch_tree
724
format = bzrdir.format_registry.make_bzrdir('knit')
725
self.assertRaises(errors.NotLocalUrl,
726
bzrdir.BzrDir.create_branch_convenience,
730
t = get_transport(self.get_url('.'))
731
self.assertFalse(t.has('foo'))
733
def test_clone(self):
734
# clone into a nonlocal path works
735
format = bzrdir.format_registry.make_bzrdir('knit')
736
branch = bzrdir.BzrDir.create_branch_convenience('local',
738
branch.bzrdir.open_workingtree()
739
result = branch.bzrdir.clone(self.get_url('remote'))
740
self.assertRaises(errors.NoWorkingTree,
741
result.open_workingtree)
743
result.open_repository()
745
def test_checkout_metadir(self):
746
# checkout_metadir has reasonable working tree format even when no
747
# working tree is present
748
self.make_branch('branch-knit3', format='experimental-knit3')
749
my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit3'))
750
checkout_format = my_bzrdir.checkout_metadir()
751
self.assertIsInstance(checkout_format.workingtree_format,
752
workingtree.WorkingTreeFormatAB1)
754
self.make_branch('branch-knit2', format='experimental-knit2')
755
my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
756
checkout_format = my_bzrdir.checkout_metadir()
757
self.assertIsInstance(checkout_format.workingtree_format,
758
workingtree.WorkingTreeFormat3)
761
class TestRemoteSFTP(test_sftp_transport.TestCaseWithSFTPServer):
763
def test_open_containing_tree_or_branch(self):
764
tree = self.make_branch_and_tree('tree')
765
bzrdir.BzrDir.open_containing_tree_or_branch(self.get_url('tree'))