42
49
old_format = bzrdir.BzrDirFormat.get_default_format()
43
50
# default is BzrDirFormat6
44
51
self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
45
bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
52
self.applyDeprecated(symbol_versioning.zero_fourteen,
53
bzrdir.BzrDirFormat.set_default_format,
46
55
# creating a bzr dir should now create an instrumented dir.
48
result = bzrdir.BzrDir.create('memory:/')
57
result = bzrdir.BzrDir.create('memory:///')
49
58
self.failUnless(isinstance(result, SampleBzrDir))
51
bzrdir.BzrDirFormat.set_default_format(old_format)
60
self.applyDeprecated(symbol_versioning.zero_fourteen,
61
bzrdir.BzrDirFormat.set_default_format, old_format)
52
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.set_default('knit')
79
my_format_registry.register_metadir(
81
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
82
'Experimental successor to knit. Use at your own risk.',
83
branch_format='bzrlib.branch.BzrBranchFormat6')
84
return my_format_registry
86
def test_format_registry(self):
87
my_format_registry = self.make_format_registry()
88
my_bzrdir = my_format_registry.make_bzrdir('lazy')
89
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
90
my_bzrdir = my_format_registry.make_bzrdir('weave')
91
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
92
my_bzrdir = my_format_registry.make_bzrdir('default')
93
self.assertIsInstance(my_bzrdir.repository_format,
94
knitrepo.RepositoryFormatKnit1)
95
my_bzrdir = my_format_registry.make_bzrdir('knit')
96
self.assertIsInstance(my_bzrdir.repository_format,
97
knitrepo.RepositoryFormatKnit1)
98
my_bzrdir = my_format_registry.make_bzrdir('branch6')
99
self.assertIsInstance(my_bzrdir.get_branch_format(),
100
bzrlib.branch.BzrBranchFormat6)
102
def test_get_help(self):
103
my_format_registry = self.make_format_registry()
104
self.assertEqual('Format registered lazily',
105
my_format_registry.get_help('lazy'))
106
self.assertEqual('Format using knits',
107
my_format_registry.get_help('knit'))
108
self.assertEqual('Format using knits',
109
my_format_registry.get_help('default'))
110
self.assertEqual('Pre-0.8 format. Slower and does not support'
111
' checkouts or shared repositories',
112
my_format_registry.get_help('weave'))
114
def test_help_topic(self):
115
topics = help_topics.HelpTopicRegistry()
116
topics.register('formats', self.make_format_registry().help_topic,
118
topic = topics.get_detail('formats')
119
new, deprecated = topic.split('Deprecated formats')
120
self.assertContainsRe(new, 'Bazaar directory formats')
121
self.assertContainsRe(new,
122
' knit/default:\n \(native\) Format using knits\n')
123
self.assertContainsRe(deprecated,
124
' lazy:\n \(native\) Format registered lazily\n')
126
def test_set_default_repository(self):
127
default_factory = bzrdir.format_registry.get('default')
128
old_default = [k for k, v in bzrdir.format_registry.iteritems()
129
if v == default_factory and k != 'default'][0]
130
bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
132
self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
133
bzrdir.format_registry.get('default'))
135
repository.RepositoryFormat.get_default_format().__class__,
136
knitrepo.RepositoryFormatKnit3)
138
bzrdir.format_registry.set_default_repository(old_default)
55
141
class SampleBranch(bzrlib.branch.Branch):
56
142
"""A dummy branch for guess what, dummy use."""
156
242
def test_create_repository(self):
157
243
format = SampleBzrDirFormat()
158
old_format = bzrdir.BzrDirFormat.get_default_format()
159
bzrdir.BzrDirFormat.set_default_format(format)
161
repo = bzrdir.BzrDir.create_repository(self.get_url())
162
self.assertEqual('A repository', repo)
164
bzrdir.BzrDirFormat.set_default_format(old_format)
244
repo = bzrdir.BzrDir.create_repository(self.get_url(), format=format)
245
self.assertEqual('A repository', repo)
247
def test_create_repository_shared(self):
248
old_format = bzrdir.BzrDirFormat.get_default_format()
249
repo = bzrdir.BzrDir.create_repository('.', shared=True)
250
self.assertTrue(repo.is_shared())
252
def test_create_repository_nonshared(self):
253
old_format = bzrdir.BzrDirFormat.get_default_format()
254
repo = bzrdir.BzrDir.create_repository('.')
255
self.assertFalse(repo.is_shared())
166
257
def test_create_repository_under_shared(self):
167
258
# an explicit create_repository always does so.
168
259
# 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())
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/'))
177
bzrdir.BzrDirFormat.set_default_format(old_format)
260
format = bzrdir.format_registry.make_bzrdir('knit')
261
self.make_repository('.', shared=True, format=format)
262
repo = bzrdir.BzrDir.create_repository(self.get_url('child'),
264
self.assertTrue(isinstance(repo, repository.Repository))
265
self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
179
267
def test_create_branch_and_repo_uses_default(self):
180
268
format = SampleBzrDirFormat()
181
old_format = bzrdir.BzrDirFormat.get_default_format()
182
bzrdir.BzrDirFormat.set_default_format(format)
184
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
185
self.assertTrue(isinstance(branch, SampleBranch))
187
bzrdir.BzrDirFormat.set_default_format(old_format)
269
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
271
self.assertTrue(isinstance(branch, SampleBranch))
189
273
def test_create_branch_and_repo_under_shared(self):
190
274
# creating a branch and repo in a shared repo uses the
191
275
# shared repository
192
old_format = bzrdir.BzrDirFormat.get_default_format()
193
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
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)
200
bzrdir.BzrDirFormat.set_default_format(old_format)
276
format = bzrdir.format_registry.make_bzrdir('knit')
277
self.make_repository('.', shared=True, format=format)
278
branch = bzrdir.BzrDir.create_branch_and_repo(
279
self.get_url('child'), format=format)
280
self.assertRaises(errors.NoRepositoryPresent,
281
branch.bzrdir.open_repository)
202
283
def test_create_branch_and_repo_under_shared_force_new(self):
203
284
# creating a branch and repo in a shared repo can be forced to
204
285
# make a new repo
205
old_format = bzrdir.BzrDirFormat.get_default_format()
206
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
208
self.make_repository('.', shared=True)
209
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
211
branch.bzrdir.open_repository()
213
bzrdir.BzrDirFormat.set_default_format(old_format)
286
format = bzrdir.format_registry.make_bzrdir('knit')
287
self.make_repository('.', shared=True, format=format)
288
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
291
branch.bzrdir.open_repository()
215
293
def test_create_standalone_working_tree(self):
216
294
format = SampleBzrDirFormat()
217
old_format = bzrdir.BzrDirFormat.get_default_format()
218
bzrdir.BzrDirFormat.set_default_format(format)
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)
228
bzrdir.BzrDirFormat.set_default_format(old_format)
295
# note this is deliberately readonly, as this failure should
296
# occur before any writes.
297
self.assertRaises(errors.NotLocalUrl,
298
bzrdir.BzrDir.create_standalone_workingtree,
299
self.get_readonly_url(), format=format)
300
tree = bzrdir.BzrDir.create_standalone_workingtree('.',
302
self.assertEqual('A tree', tree)
230
304
def test_create_standalone_working_tree_under_shared_repo(self):
231
305
# create standalone working tree always makes a repo.
232
old_format = bzrdir.BzrDirFormat.get_default_format()
233
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
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()
244
bzrdir.BzrDirFormat.set_default_format(old_format)
306
format = bzrdir.format_registry.make_bzrdir('knit')
307
self.make_repository('.', shared=True, format=format)
308
# note this is deliberately readonly, as this failure should
309
# occur before any writes.
310
self.assertRaises(errors.NotLocalUrl,
311
bzrdir.BzrDir.create_standalone_workingtree,
312
self.get_readonly_url('child'), format=format)
313
tree = bzrdir.BzrDir.create_standalone_workingtree('child',
315
tree.bzrdir.open_repository()
246
317
def test_create_branch_convenience(self):
247
318
# 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())
251
branch = bzrdir.BzrDir.create_branch_convenience('.')
252
branch.bzrdir.open_workingtree()
253
branch.bzrdir.open_repository()
255
bzrdir.BzrDirFormat.set_default_format(old_format)
319
format = bzrdir.format_registry.make_bzrdir('knit')
320
branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
321
branch.bzrdir.open_workingtree()
322
branch.bzrdir.open_repository()
324
def test_create_branch_convenience_root(self):
325
"""Creating a branch at the root of a fs should work."""
326
self.transport_server = MemoryServer
327
# outside a repo the default convenience output is a repo+branch_tree
328
format = bzrdir.format_registry.make_bzrdir('knit')
329
branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
331
self.assertRaises(errors.NoWorkingTree,
332
branch.bzrdir.open_workingtree)
333
branch.bzrdir.open_repository()
257
335
def test_create_branch_convenience_under_shared_repo(self):
258
336
# inside a repo the default convenience output is a branch+ follow the
259
337
# repo tree policy
260
old_format = bzrdir.BzrDirFormat.get_default_format()
261
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
263
self.make_repository('.', shared=True)
264
branch = bzrdir.BzrDir.create_branch_convenience('child')
265
branch.bzrdir.open_workingtree()
266
self.assertRaises(errors.NoRepositoryPresent,
267
branch.bzrdir.open_repository)
269
bzrdir.BzrDirFormat.set_default_format(old_format)
338
format = bzrdir.format_registry.make_bzrdir('knit')
339
self.make_repository('.', shared=True, format=format)
340
branch = bzrdir.BzrDir.create_branch_convenience('child',
342
branch.bzrdir.open_workingtree()
343
self.assertRaises(errors.NoRepositoryPresent,
344
branch.bzrdir.open_repository)
271
346
def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
272
347
# inside a repo the default convenience output is a branch+ follow the
273
348
# repo tree policy but we can override that
274
old_format = bzrdir.BzrDirFormat.get_default_format()
275
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
277
self.make_repository('.', shared=True)
278
branch = bzrdir.BzrDir.create_branch_convenience('child',
279
force_new_tree=False)
280
self.assertRaises(errors.NoWorkingTree,
281
branch.bzrdir.open_workingtree)
282
self.assertRaises(errors.NoRepositoryPresent,
283
branch.bzrdir.open_repository)
285
bzrdir.BzrDirFormat.set_default_format(old_format)
349
format = bzrdir.format_registry.make_bzrdir('knit')
350
self.make_repository('.', shared=True, format=format)
351
branch = bzrdir.BzrDir.create_branch_convenience('child',
352
force_new_tree=False, format=format)
353
self.assertRaises(errors.NoWorkingTree,
354
branch.bzrdir.open_workingtree)
355
self.assertRaises(errors.NoRepositoryPresent,
356
branch.bzrdir.open_repository)
287
358
def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
288
359
# inside a repo the default convenience output is a branch+ follow the
289
360
# repo tree policy
290
old_format = bzrdir.BzrDirFormat.get_default_format()
291
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
293
repo = self.make_repository('.', shared=True)
294
repo.set_make_working_trees(False)
295
branch = bzrdir.BzrDir.create_branch_convenience('child')
296
self.assertRaises(errors.NoWorkingTree,
297
branch.bzrdir.open_workingtree)
298
self.assertRaises(errors.NoRepositoryPresent,
299
branch.bzrdir.open_repository)
301
bzrdir.BzrDirFormat.set_default_format(old_format)
361
format = bzrdir.format_registry.make_bzrdir('knit')
362
repo = self.make_repository('.', shared=True, format=format)
363
repo.set_make_working_trees(False)
364
branch = bzrdir.BzrDir.create_branch_convenience('child',
366
self.assertRaises(errors.NoWorkingTree,
367
branch.bzrdir.open_workingtree)
368
self.assertRaises(errors.NoRepositoryPresent,
369
branch.bzrdir.open_repository)
303
371
def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
304
372
# inside a repo the default convenience output is a branch+ follow the
305
373
# repo tree policy but we can override that
306
old_format = bzrdir.BzrDirFormat.get_default_format()
307
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
309
repo = self.make_repository('.', shared=True)
310
repo.set_make_working_trees(False)
311
branch = bzrdir.BzrDir.create_branch_convenience('child',
313
branch.bzrdir.open_workingtree()
314
self.assertRaises(errors.NoRepositoryPresent,
315
branch.bzrdir.open_repository)
317
bzrdir.BzrDirFormat.set_default_format(old_format)
374
format = bzrdir.format_registry.make_bzrdir('knit')
375
repo = self.make_repository('.', shared=True, format=format)
376
repo.set_make_working_trees(False)
377
branch = bzrdir.BzrDir.create_branch_convenience('child',
378
force_new_tree=True, format=format)
379
branch.bzrdir.open_workingtree()
380
self.assertRaises(errors.NoRepositoryPresent,
381
branch.bzrdir.open_repository)
319
383
def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
320
384
# inside a repo the default convenience output is overridable to give
321
385
# repo+branch+tree
322
old_format = bzrdir.BzrDirFormat.get_default_format()
323
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
325
self.make_repository('.', shared=True)
326
branch = bzrdir.BzrDir.create_branch_convenience('child',
328
branch.bzrdir.open_repository()
329
branch.bzrdir.open_workingtree()
331
bzrdir.BzrDirFormat.set_default_format(old_format)
386
format = bzrdir.format_registry.make_bzrdir('knit')
387
self.make_repository('.', shared=True, format=format)
388
branch = bzrdir.BzrDir.create_branch_convenience('child',
389
force_new_repo=True, format=format)
390
branch.bzrdir.open_repository()
391
branch.bzrdir.open_workingtree()
334
394
class ChrootedTests(TestCaseWithTransport):
368
428
get_transport(self.get_readonly_url('g/p/q')))
369
429
self.assertEqual('g/p/q', relpath)
431
def test_open_containing_tree_or_branch(self):
432
def local_branch_path(branch):
433
return os.path.realpath(
434
urlutils.local_path_from_url(branch.base))
436
self.make_branch_and_tree('topdir')
437
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
439
self.assertEqual(os.path.realpath('topdir'),
440
os.path.realpath(tree.basedir))
441
self.assertEqual(os.path.realpath('topdir'),
442
local_branch_path(branch))
443
self.assertIs(tree.bzrdir, branch.bzrdir)
444
self.assertEqual('foo', relpath)
445
self.make_branch('topdir/foo')
446
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
448
self.assertIs(tree, None)
449
self.assertEqual(os.path.realpath('topdir/foo'),
450
local_branch_path(branch))
451
self.assertEqual('', relpath)
453
def test_open_from_transport(self):
454
# transport pointing at bzrdir should give a bzrdir with root transport
455
# set to the given transport
456
control = bzrdir.BzrDir.create(self.get_url())
457
transport = get_transport(self.get_url())
458
opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
459
self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
460
self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
462
def test_open_from_transport_no_bzrdir(self):
463
transport = get_transport(self.get_url())
464
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
467
def test_open_from_transport_bzrdir_in_parent(self):
468
control = bzrdir.BzrDir.create(self.get_url())
469
transport = get_transport(self.get_url())
470
transport.mkdir('subdir')
471
transport = transport.clone('subdir')
472
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
475
def test_sprout_recursive(self):
476
tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
477
sub_tree = self.make_branch_and_tree('tree1/subtree',
478
format='dirstate-with-subtree')
479
tree.add_reference(sub_tree)
480
self.build_tree(['tree1/subtree/file'])
482
tree.commit('Initial commit')
483
tree.bzrdir.sprout('tree2')
484
self.failUnlessExists('tree2/subtree/file')
486
def test_cloning_metadir(self):
487
"""Ensure that cloning metadir is suitable"""
488
bzrdir = self.make_bzrdir('bzrdir')
489
bzrdir.cloning_metadir()
490
branch = self.make_branch('branch', format='knit')
491
format = branch.bzrdir.cloning_metadir()
492
self.assertIsInstance(format.workingtree_format,
493
workingtree.WorkingTreeFormat3)
495
def test_sprout_recursive_treeless(self):
496
tree = self.make_branch_and_tree('tree1',
497
format='dirstate-with-subtree')
498
sub_tree = self.make_branch_and_tree('tree1/subtree',
499
format='dirstate-with-subtree')
500
tree.add_reference(sub_tree)
501
self.build_tree(['tree1/subtree/file'])
503
tree.commit('Initial commit')
504
tree.bzrdir.destroy_workingtree()
505
repo = self.make_repository('repo', shared=True,
506
format='dirstate-with-subtree')
507
repo.set_make_working_trees(False)
508
tree.bzrdir.sprout('repo/tree2')
509
self.failUnlessExists('repo/tree2/subtree')
510
self.failIfExists('repo/tree2/subtree/file')
372
513
class TestMeta1DirFormat(TestCaseWithTransport):
373
514
"""Tests specific to the meta1 dir format."""
457
626
def test_needs_conversion(self):
458
627
# format 6 dirs need an conversion if they are not the default.
459
628
old_format = bzrdir.BzrDirFormat.get_default_format()
460
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
629
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
462
631
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
463
632
self.assertTrue(dir.needs_format_conversion())
465
bzrdir.BzrDirFormat.set_default_format(old_format)
634
bzrdir.BzrDirFormat._set_default_format(old_format)
637
class NotBzrDir(bzrlib.bzrdir.BzrDir):
638
"""A non .bzr based control directory."""
640
def __init__(self, transport, format):
641
self._format = format
642
self.root_transport = transport
643
self.transport = transport.clone('.not')
646
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
647
"""A test class representing any non-.bzr based disk format."""
649
def initialize_on_transport(self, transport):
650
"""Initialize a new .not dir in the base directory of a Transport."""
651
transport.mkdir('.not')
652
return self.open(transport)
654
def open(self, transport):
655
"""Open this directory."""
656
return NotBzrDir(transport, self)
659
def _known_formats(self):
660
return set([NotBzrDirFormat()])
663
def probe_transport(self, transport):
664
"""Our format is present if the transport ends in '.not/'."""
665
if transport.has('.not'):
666
return NotBzrDirFormat()
669
class TestNotBzrDir(TestCaseWithTransport):
670
"""Tests for using the bzrdir api with a non .bzr based disk format.
672
If/when one of these is in the core, we can let the implementation tests
676
def test_create_and_find_format(self):
677
# create a .notbzr dir
678
format = NotBzrDirFormat()
679
dir = format.initialize(self.get_url())
680
self.assertIsInstance(dir, NotBzrDir)
682
bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
684
found = bzrlib.bzrdir.BzrDirFormat.find_format(
685
get_transport(self.get_url()))
686
self.assertIsInstance(found, NotBzrDirFormat)
688
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
690
def test_included_in_known_formats(self):
691
bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
693
formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
694
for format in formats:
695
if isinstance(format, NotBzrDirFormat):
697
self.fail("No NotBzrDirFormat in %s" % formats)
699
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
468
702
class NonLocalTests(TestCaseWithTransport):