42
64
old_format = bzrdir.BzrDirFormat.get_default_format()
43
65
# default is BzrDirFormat6
44
66
self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
45
bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
67
bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
46
68
# creating a bzr dir should now create an instrumented dir.
48
70
result = bzrdir.BzrDir.create('memory:///')
49
71
self.failUnless(isinstance(result, SampleBzrDir))
51
bzrdir.BzrDirFormat.set_default_format(old_format)
73
bzrdir.BzrDirFormat._set_default_format(old_format)
52
74
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
77
class TestFormatRegistry(TestCase):
79
def make_format_registry(self):
80
my_format_registry = bzrdir.BzrDirFormatRegistry()
81
my_format_registry.register('weave', bzrdir.BzrDirFormat6,
82
'Pre-0.8 format. Slower and does not support checkouts or shared'
83
' repositories', deprecated=True)
84
my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
85
'BzrDirFormat6', 'Format registered lazily', deprecated=True)
86
my_format_registry.register_metadir('knit',
87
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
90
my_format_registry.set_default('knit')
91
my_format_registry.register_metadir(
93
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
94
'Experimental successor to knit. Use at your own risk.',
95
branch_format='bzrlib.branch.BzrBranchFormat6',
97
my_format_registry.register_metadir(
99
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
100
'Experimental successor to knit. Use at your own risk.',
101
branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
102
my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
103
'Pre-0.8 format. Slower and does not support checkouts or shared'
104
' repositories', hidden=True)
105
my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
106
'BzrDirFormat6', 'Format registered lazily', deprecated=True,
108
return my_format_registry
110
def test_format_registry(self):
111
my_format_registry = self.make_format_registry()
112
my_bzrdir = my_format_registry.make_bzrdir('lazy')
113
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
114
my_bzrdir = my_format_registry.make_bzrdir('weave')
115
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
116
my_bzrdir = my_format_registry.make_bzrdir('default')
117
self.assertIsInstance(my_bzrdir.repository_format,
118
knitrepo.RepositoryFormatKnit1)
119
my_bzrdir = my_format_registry.make_bzrdir('knit')
120
self.assertIsInstance(my_bzrdir.repository_format,
121
knitrepo.RepositoryFormatKnit1)
122
my_bzrdir = my_format_registry.make_bzrdir('branch6')
123
self.assertIsInstance(my_bzrdir.get_branch_format(),
124
bzrlib.branch.BzrBranchFormat6)
126
def test_get_help(self):
127
my_format_registry = self.make_format_registry()
128
self.assertEqual('Format registered lazily',
129
my_format_registry.get_help('lazy'))
130
self.assertEqual('Format using knits',
131
my_format_registry.get_help('knit'))
132
self.assertEqual('Format using knits',
133
my_format_registry.get_help('default'))
134
self.assertEqual('Pre-0.8 format. Slower and does not support'
135
' checkouts or shared repositories',
136
my_format_registry.get_help('weave'))
138
def test_help_topic(self):
139
topics = help_topics.HelpTopicRegistry()
140
registry = self.make_format_registry()
141
topics.register('current-formats', registry.help_topic,
143
topics.register('other-formats', registry.help_topic,
145
new = topics.get_detail('current-formats')
146
rest = topics.get_detail('other-formats')
147
experimental, deprecated = rest.split('Deprecated formats')
148
self.assertContainsRe(new, 'bzr help formats')
149
self.assertContainsRe(new,
150
':knit:\n \(native\) \(default\) Format using knits\n')
151
self.assertContainsRe(experimental,
152
':branch6:\n \(native\) Experimental successor to knit')
153
self.assertContainsRe(deprecated,
154
':lazy:\n \(native\) Format registered lazily\n')
155
self.assertNotContainsRe(new, 'hidden')
157
def test_set_default_repository(self):
158
default_factory = bzrdir.format_registry.get('default')
159
old_default = [k for k, v in bzrdir.format_registry.iteritems()
160
if v == default_factory and k != 'default'][0]
161
bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
163
self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
164
bzrdir.format_registry.get('default'))
166
repository.RepositoryFormat.get_default_format().__class__,
167
knitrepo.RepositoryFormatKnit3)
169
bzrdir.format_registry.set_default_repository(old_default)
171
def test_aliases(self):
172
a_registry = bzrdir.BzrDirFormatRegistry()
173
a_registry.register('weave', bzrdir.BzrDirFormat6,
174
'Pre-0.8 format. Slower and does not support checkouts or shared'
175
' repositories', deprecated=True)
176
a_registry.register('weavealias', bzrdir.BzrDirFormat6,
177
'Pre-0.8 format. Slower and does not support checkouts or shared'
178
' repositories', deprecated=True, alias=True)
179
self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
55
182
class SampleBranch(bzrlib.branch.Branch):
56
183
"""A dummy branch for guess what, dummy use."""
153
286
# now open_downlevel should fail too.
154
287
self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
156
def test_create_repository(self):
157
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)
166
def test_create_repository_shared(self):
167
old_format = bzrdir.BzrDirFormat.get_default_format()
168
repo = bzrdir.BzrDir.create_repository('.', shared=True)
169
self.assertTrue(repo.is_shared())
171
def test_create_repository_nonshared(self):
172
old_format = bzrdir.BzrDirFormat.get_default_format()
173
repo = bzrdir.BzrDir.create_repository('.')
174
self.assertFalse(repo.is_shared())
176
def test_create_repository_under_shared(self):
177
# an explicit create_repository always does so.
178
# we trust the format is right from the 'create_repository test'
179
old_format = bzrdir.BzrDirFormat.get_default_format()
180
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
182
self.make_repository('.', shared=True)
183
repo = bzrdir.BzrDir.create_repository(self.get_url('child'))
184
self.assertTrue(isinstance(repo, repository.Repository))
185
self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
187
bzrdir.BzrDirFormat.set_default_format(old_format)
189
289
def test_create_branch_and_repo_uses_default(self):
190
290
format = SampleBzrDirFormat()
191
old_format = bzrdir.BzrDirFormat.get_default_format()
192
bzrdir.BzrDirFormat.set_default_format(format)
194
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
195
self.assertTrue(isinstance(branch, SampleBranch))
197
bzrdir.BzrDirFormat.set_default_format(old_format)
291
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
293
self.assertTrue(isinstance(branch, SampleBranch))
199
295
def test_create_branch_and_repo_under_shared(self):
200
296
# creating a branch and repo in a shared repo uses the
201
297
# shared repository
202
old_format = bzrdir.BzrDirFormat.get_default_format()
203
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
205
self.make_repository('.', shared=True)
206
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'))
207
self.assertRaises(errors.NoRepositoryPresent,
208
branch.bzrdir.open_repository)
210
bzrdir.BzrDirFormat.set_default_format(old_format)
298
format = bzrdir.format_registry.make_bzrdir('knit')
299
self.make_repository('.', shared=True, format=format)
300
branch = bzrdir.BzrDir.create_branch_and_repo(
301
self.get_url('child'), format=format)
302
self.assertRaises(errors.NoRepositoryPresent,
303
branch.bzrdir.open_repository)
212
305
def test_create_branch_and_repo_under_shared_force_new(self):
213
# creating a branch and repo in a shared repo can be forced to
306
# creating a branch and repo in a shared repo can be forced to
214
307
# make a new repo
215
old_format = bzrdir.BzrDirFormat.get_default_format()
216
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
218
self.make_repository('.', shared=True)
219
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
221
branch.bzrdir.open_repository()
223
bzrdir.BzrDirFormat.set_default_format(old_format)
308
format = bzrdir.format_registry.make_bzrdir('knit')
309
self.make_repository('.', shared=True, format=format)
310
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
313
branch.bzrdir.open_repository()
225
315
def test_create_standalone_working_tree(self):
226
316
format = SampleBzrDirFormat()
227
old_format = bzrdir.BzrDirFormat.get_default_format()
228
bzrdir.BzrDirFormat.set_default_format(format)
230
# note this is deliberately readonly, as this failure should
231
# occur before any writes.
232
self.assertRaises(errors.NotLocalUrl,
233
bzrdir.BzrDir.create_standalone_workingtree,
234
self.get_readonly_url())
235
tree = bzrdir.BzrDir.create_standalone_workingtree('.')
236
self.assertEqual('A tree', tree)
238
bzrdir.BzrDirFormat.set_default_format(old_format)
317
# note this is deliberately readonly, as this failure should
318
# occur before any writes.
319
self.assertRaises(errors.NotLocalUrl,
320
bzrdir.BzrDir.create_standalone_workingtree,
321
self.get_readonly_url(), format=format)
322
tree = bzrdir.BzrDir.create_standalone_workingtree('.',
324
self.assertEqual('A tree', tree)
240
326
def test_create_standalone_working_tree_under_shared_repo(self):
241
327
# create standalone working tree always makes a repo.
242
old_format = bzrdir.BzrDirFormat.get_default_format()
243
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
245
self.make_repository('.', shared=True)
246
# note this is deliberately readonly, as this failure should
247
# occur before any writes.
248
self.assertRaises(errors.NotLocalUrl,
249
bzrdir.BzrDir.create_standalone_workingtree,
250
self.get_readonly_url('child'))
251
tree = bzrdir.BzrDir.create_standalone_workingtree('child')
252
tree.bzrdir.open_repository()
254
bzrdir.BzrDirFormat.set_default_format(old_format)
328
format = bzrdir.format_registry.make_bzrdir('knit')
329
self.make_repository('.', shared=True, format=format)
330
# note this is deliberately readonly, as this failure should
331
# occur before any writes.
332
self.assertRaises(errors.NotLocalUrl,
333
bzrdir.BzrDir.create_standalone_workingtree,
334
self.get_readonly_url('child'), format=format)
335
tree = bzrdir.BzrDir.create_standalone_workingtree('child',
337
tree.bzrdir.open_repository()
256
339
def test_create_branch_convenience(self):
257
340
# outside a repo the default convenience output is a repo+branch_tree
258
old_format = bzrdir.BzrDirFormat.get_default_format()
259
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
261
branch = bzrdir.BzrDir.create_branch_convenience('.')
262
branch.bzrdir.open_workingtree()
263
branch.bzrdir.open_repository()
265
bzrdir.BzrDirFormat.set_default_format(old_format)
341
format = bzrdir.format_registry.make_bzrdir('knit')
342
branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
343
branch.bzrdir.open_workingtree()
344
branch.bzrdir.open_repository()
346
def test_create_branch_convenience_possible_transports(self):
347
"""Check that the optional 'possible_transports' is recognized"""
348
format = bzrdir.format_registry.make_bzrdir('knit')
349
t = self.get_transport()
350
branch = bzrdir.BzrDir.create_branch_convenience(
351
'.', format=format, possible_transports=[t])
352
branch.bzrdir.open_workingtree()
353
branch.bzrdir.open_repository()
267
355
def test_create_branch_convenience_root(self):
268
356
"""Creating a branch at the root of a fs should work."""
269
self.transport_server = MemoryServer
357
self.vfs_transport_factory = MemoryServer
270
358
# outside a repo the default convenience output is a repo+branch_tree
271
old_format = bzrdir.BzrDirFormat.get_default_format()
272
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
274
branch = bzrdir.BzrDir.create_branch_convenience(self.get_url())
275
self.assertRaises(errors.NoWorkingTree,
276
branch.bzrdir.open_workingtree)
277
branch.bzrdir.open_repository()
279
bzrdir.BzrDirFormat.set_default_format(old_format)
359
format = bzrdir.format_registry.make_bzrdir('knit')
360
branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
362
self.assertRaises(errors.NoWorkingTree,
363
branch.bzrdir.open_workingtree)
364
branch.bzrdir.open_repository()
281
366
def test_create_branch_convenience_under_shared_repo(self):
282
367
# inside a repo the default convenience output is a branch+ follow the
283
368
# repo tree policy
284
old_format = bzrdir.BzrDirFormat.get_default_format()
285
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
287
self.make_repository('.', shared=True)
288
branch = bzrdir.BzrDir.create_branch_convenience('child')
289
branch.bzrdir.open_workingtree()
290
self.assertRaises(errors.NoRepositoryPresent,
291
branch.bzrdir.open_repository)
293
bzrdir.BzrDirFormat.set_default_format(old_format)
369
format = bzrdir.format_registry.make_bzrdir('knit')
370
self.make_repository('.', shared=True, format=format)
371
branch = bzrdir.BzrDir.create_branch_convenience('child',
373
branch.bzrdir.open_workingtree()
374
self.assertRaises(errors.NoRepositoryPresent,
375
branch.bzrdir.open_repository)
295
377
def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
296
378
# inside a repo the default convenience output is a branch+ follow the
297
379
# repo tree policy but we can override that
298
old_format = bzrdir.BzrDirFormat.get_default_format()
299
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
301
self.make_repository('.', shared=True)
302
branch = bzrdir.BzrDir.create_branch_convenience('child',
303
force_new_tree=False)
304
self.assertRaises(errors.NoWorkingTree,
305
branch.bzrdir.open_workingtree)
306
self.assertRaises(errors.NoRepositoryPresent,
307
branch.bzrdir.open_repository)
309
bzrdir.BzrDirFormat.set_default_format(old_format)
380
format = bzrdir.format_registry.make_bzrdir('knit')
381
self.make_repository('.', shared=True, format=format)
382
branch = bzrdir.BzrDir.create_branch_convenience('child',
383
force_new_tree=False, format=format)
384
self.assertRaises(errors.NoWorkingTree,
385
branch.bzrdir.open_workingtree)
386
self.assertRaises(errors.NoRepositoryPresent,
387
branch.bzrdir.open_repository)
311
389
def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
312
390
# inside a repo the default convenience output is a branch+ follow the
313
391
# repo tree policy
314
old_format = bzrdir.BzrDirFormat.get_default_format()
315
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
317
repo = self.make_repository('.', shared=True)
318
repo.set_make_working_trees(False)
319
branch = bzrdir.BzrDir.create_branch_convenience('child')
320
self.assertRaises(errors.NoWorkingTree,
321
branch.bzrdir.open_workingtree)
322
self.assertRaises(errors.NoRepositoryPresent,
323
branch.bzrdir.open_repository)
325
bzrdir.BzrDirFormat.set_default_format(old_format)
392
format = bzrdir.format_registry.make_bzrdir('knit')
393
repo = self.make_repository('.', shared=True, format=format)
394
repo.set_make_working_trees(False)
395
branch = bzrdir.BzrDir.create_branch_convenience('child',
397
self.assertRaises(errors.NoWorkingTree,
398
branch.bzrdir.open_workingtree)
399
self.assertRaises(errors.NoRepositoryPresent,
400
branch.bzrdir.open_repository)
327
402
def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
328
403
# inside a repo the default convenience output is a branch+ follow the
329
404
# repo tree policy but we can override that
330
old_format = bzrdir.BzrDirFormat.get_default_format()
331
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
333
repo = self.make_repository('.', shared=True)
334
repo.set_make_working_trees(False)
335
branch = bzrdir.BzrDir.create_branch_convenience('child',
337
branch.bzrdir.open_workingtree()
338
self.assertRaises(errors.NoRepositoryPresent,
339
branch.bzrdir.open_repository)
341
bzrdir.BzrDirFormat.set_default_format(old_format)
405
format = bzrdir.format_registry.make_bzrdir('knit')
406
repo = self.make_repository('.', shared=True, format=format)
407
repo.set_make_working_trees(False)
408
branch = bzrdir.BzrDir.create_branch_convenience('child',
409
force_new_tree=True, format=format)
410
branch.bzrdir.open_workingtree()
411
self.assertRaises(errors.NoRepositoryPresent,
412
branch.bzrdir.open_repository)
343
414
def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
344
415
# inside a repo the default convenience output is overridable to give
345
416
# repo+branch+tree
346
old_format = bzrdir.BzrDirFormat.get_default_format()
347
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
349
self.make_repository('.', shared=True)
350
branch = bzrdir.BzrDir.create_branch_convenience('child',
352
branch.bzrdir.open_repository()
353
branch.bzrdir.open_workingtree()
355
bzrdir.BzrDirFormat.set_default_format(old_format)
417
format = bzrdir.format_registry.make_bzrdir('knit')
418
self.make_repository('.', shared=True, format=format)
419
branch = bzrdir.BzrDir.create_branch_convenience('child',
420
force_new_repo=True, format=format)
421
branch.bzrdir.open_repository()
422
branch.bzrdir.open_workingtree()
425
class TestRepositoryAcquisitionPolicy(TestCaseWithTransport):
427
def test_acquire_repository_standalone(self):
428
"""The default acquisition policy should create a standalone branch."""
429
my_bzrdir = self.make_bzrdir('.')
430
repo_policy = my_bzrdir.determine_repository_policy()
431
repo, is_new = repo_policy.acquire_repository()
432
self.assertEqual(repo.bzrdir.root_transport.base,
433
my_bzrdir.root_transport.base)
434
self.assertFalse(repo.is_shared())
436
def test_determine_stacking_policy(self):
437
parent_bzrdir = self.make_bzrdir('.')
438
child_bzrdir = self.make_bzrdir('child')
439
parent_bzrdir.get_config().set_default_stack_on('http://example.org')
440
repo_policy = child_bzrdir.determine_repository_policy()
441
self.assertEqual('http://example.org', repo_policy._stack_on)
443
def test_determine_stacking_policy_relative(self):
444
parent_bzrdir = self.make_bzrdir('.')
445
child_bzrdir = self.make_bzrdir('child')
446
parent_bzrdir.get_config().set_default_stack_on('child2')
447
repo_policy = child_bzrdir.determine_repository_policy()
448
self.assertEqual('child2', repo_policy._stack_on)
449
self.assertEqual(parent_bzrdir.root_transport.base,
450
repo_policy._stack_on_pwd)
452
def prepare_default_stacking(self, child_format='1.6'):
453
parent_bzrdir = self.make_bzrdir('.')
454
child_branch = self.make_branch('child', format=child_format)
455
parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
456
new_child_transport = parent_bzrdir.transport.clone('child2')
457
return child_branch, new_child_transport
459
def test_clone_on_transport_obeys_stacking_policy(self):
460
child_branch, new_child_transport = self.prepare_default_stacking()
461
new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
462
self.assertEqual(child_branch.base,
463
new_child.open_branch().get_stacked_on_url())
465
def test_default_stacking_with_stackable_branch_unstackable_repo(self):
466
# Make stackable source branch with an unstackable repo format.
467
source_bzrdir = self.make_bzrdir('source')
468
pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
469
source_branch = bzrlib.branch.BzrBranchFormat7().initialize(source_bzrdir)
470
# Make a directory with a default stacking policy
471
parent_bzrdir = self.make_bzrdir('parent')
472
stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
473
parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
474
# Clone source into directory
475
target = source_bzrdir.clone(self.get_url('parent/target'))
477
def test_sprout_obeys_stacking_policy(self):
478
child_branch, new_child_transport = self.prepare_default_stacking()
479
new_child = child_branch.bzrdir.sprout(new_child_transport.base)
480
self.assertEqual(child_branch.base,
481
new_child.open_branch().get_stacked_on_url())
483
def test_clone_ignores_policy_for_unsupported_formats(self):
484
child_branch, new_child_transport = self.prepare_default_stacking(
485
child_format='pack-0.92')
486
new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
487
self.assertRaises(errors.UnstackableBranchFormat,
488
new_child.open_branch().get_stacked_on_url)
490
def test_sprout_ignores_policy_for_unsupported_formats(self):
491
child_branch, new_child_transport = self.prepare_default_stacking(
492
child_format='pack-0.92')
493
new_child = child_branch.bzrdir.sprout(new_child_transport.base)
494
self.assertRaises(errors.UnstackableBranchFormat,
495
new_child.open_branch().get_stacked_on_url)
497
def test_sprout_upgrades_format_if_stacked_specified(self):
498
child_branch, new_child_transport = self.prepare_default_stacking(
499
child_format='pack-0.92')
500
new_child = child_branch.bzrdir.sprout(new_child_transport.base,
502
self.assertEqual(child_branch.bzrdir.root_transport.base,
503
new_child.open_branch().get_stacked_on_url())
504
repo = new_child.open_repository()
505
self.assertTrue(repo._format.supports_external_lookups)
506
self.assertFalse(repo.supports_rich_root())
508
def test_clone_on_transport_upgrades_format_if_stacked_on_specified(self):
509
child_branch, new_child_transport = self.prepare_default_stacking(
510
child_format='pack-0.92')
511
new_child = child_branch.bzrdir.clone_on_transport(new_child_transport,
512
stacked_on=child_branch.bzrdir.root_transport.base)
513
self.assertEqual(child_branch.bzrdir.root_transport.base,
514
new_child.open_branch().get_stacked_on_url())
515
repo = new_child.open_repository()
516
self.assertTrue(repo._format.supports_external_lookups)
517
self.assertFalse(repo.supports_rich_root())
519
def test_sprout_upgrades_to_rich_root_format_if_needed(self):
520
child_branch, new_child_transport = self.prepare_default_stacking(
521
child_format='rich-root-pack')
522
new_child = child_branch.bzrdir.sprout(new_child_transport.base,
524
repo = new_child.open_repository()
525
self.assertTrue(repo._format.supports_external_lookups)
526
self.assertTrue(repo.supports_rich_root())
528
def test_add_fallback_repo_handles_absolute_urls(self):
529
stack_on = self.make_branch('stack_on', format='1.6')
530
repo = self.make_repository('repo', format='1.6')
531
policy = bzrdir.UseExistingRepository(repo, stack_on.base)
532
policy._add_fallback(repo)
534
def test_add_fallback_repo_handles_relative_urls(self):
535
stack_on = self.make_branch('stack_on', format='1.6')
536
repo = self.make_repository('repo', format='1.6')
537
policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
538
policy._add_fallback(repo)
540
def test_configure_relative_branch_stacking_url(self):
541
stack_on = self.make_branch('stack_on', format='1.6')
542
stacked = self.make_branch('stack_on/stacked', format='1.6')
543
policy = bzrdir.UseExistingRepository(stacked.repository,
545
policy.configure_branch(stacked)
546
self.assertEqual('..', stacked.get_stacked_on_url())
548
def test_relative_branch_stacking_to_absolute(self):
549
stack_on = self.make_branch('stack_on', format='1.6')
550
stacked = self.make_branch('stack_on/stacked', format='1.6')
551
policy = bzrdir.UseExistingRepository(stacked.repository,
552
'.', self.get_readonly_url('stack_on'))
553
policy.configure_branch(stacked)
554
self.assertEqual(self.get_readonly_url('stack_on'),
555
stacked.get_stacked_on_url())
358
558
class ChrootedTests(TestCaseWithTransport):
379
582
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
380
583
self.assertEqual('g/p/q', relpath)
585
def test_open_containing_tree_branch_or_repository_empty(self):
586
self.assertRaises(errors.NotBranchError,
587
bzrdir.BzrDir.open_containing_tree_branch_or_repository,
588
self.get_readonly_url(''))
590
def test_open_containing_tree_branch_or_repository_all(self):
591
self.make_branch_and_tree('topdir')
592
tree, branch, repo, relpath = \
593
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
595
self.assertEqual(os.path.realpath('topdir'),
596
os.path.realpath(tree.basedir))
597
self.assertEqual(os.path.realpath('topdir'),
598
self.local_branch_path(branch))
600
osutils.realpath(os.path.join('topdir', '.bzr', 'repository')),
601
repo.bzrdir.transport.local_abspath('repository'))
602
self.assertEqual(relpath, 'foo')
604
def test_open_containing_tree_branch_or_repository_no_tree(self):
605
self.make_branch('branch')
606
tree, branch, repo, relpath = \
607
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
609
self.assertEqual(tree, None)
610
self.assertEqual(os.path.realpath('branch'),
611
self.local_branch_path(branch))
613
osutils.realpath(os.path.join('branch', '.bzr', 'repository')),
614
repo.bzrdir.transport.local_abspath('repository'))
615
self.assertEqual(relpath, 'foo')
617
def test_open_containing_tree_branch_or_repository_repo(self):
618
self.make_repository('repo')
619
tree, branch, repo, relpath = \
620
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
622
self.assertEqual(tree, None)
623
self.assertEqual(branch, None)
625
osutils.realpath(os.path.join('repo', '.bzr', 'repository')),
626
repo.bzrdir.transport.local_abspath('repository'))
627
self.assertEqual(relpath, '')
629
def test_open_containing_tree_branch_or_repository_shared_repo(self):
630
self.make_repository('shared', shared=True)
631
bzrdir.BzrDir.create_branch_convenience('shared/branch',
632
force_new_tree=False)
633
tree, branch, repo, relpath = \
634
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
636
self.assertEqual(tree, None)
637
self.assertEqual(os.path.realpath('shared/branch'),
638
self.local_branch_path(branch))
640
osutils.realpath(os.path.join('shared', '.bzr', 'repository')),
641
repo.bzrdir.transport.local_abspath('repository'))
642
self.assertEqual(relpath, '')
644
def test_open_containing_tree_branch_or_repository_branch_subdir(self):
645
self.make_branch_and_tree('foo')
646
self.build_tree(['foo/bar/'])
647
tree, branch, repo, relpath = \
648
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
650
self.assertEqual(os.path.realpath('foo'),
651
os.path.realpath(tree.basedir))
652
self.assertEqual(os.path.realpath('foo'),
653
self.local_branch_path(branch))
655
osutils.realpath(os.path.join('foo', '.bzr', 'repository')),
656
repo.bzrdir.transport.local_abspath('repository'))
657
self.assertEqual(relpath, 'bar')
659
def test_open_containing_tree_branch_or_repository_repo_subdir(self):
660
self.make_repository('bar')
661
self.build_tree(['bar/baz/'])
662
tree, branch, repo, relpath = \
663
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
665
self.assertEqual(tree, None)
666
self.assertEqual(branch, None)
668
osutils.realpath(os.path.join('bar', '.bzr', 'repository')),
669
repo.bzrdir.transport.local_abspath('repository'))
670
self.assertEqual(relpath, 'baz')
382
672
def test_open_containing_from_transport(self):
383
673
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
384
674
get_transport(self.get_readonly_url('')))
392
682
get_transport(self.get_readonly_url('g/p/q')))
393
683
self.assertEqual('g/p/q', relpath)
685
def test_open_containing_tree_or_branch(self):
686
self.make_branch_and_tree('topdir')
687
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
689
self.assertEqual(os.path.realpath('topdir'),
690
os.path.realpath(tree.basedir))
691
self.assertEqual(os.path.realpath('topdir'),
692
self.local_branch_path(branch))
693
self.assertIs(tree.bzrdir, branch.bzrdir)
694
self.assertEqual('foo', relpath)
695
# opening from non-local should not return the tree
696
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
697
self.get_readonly_url('topdir/foo'))
698
self.assertEqual(None, tree)
699
self.assertEqual('foo', relpath)
701
self.make_branch('topdir/foo')
702
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
704
self.assertIs(tree, None)
705
self.assertEqual(os.path.realpath('topdir/foo'),
706
self.local_branch_path(branch))
707
self.assertEqual('', relpath)
709
def test_open_tree_or_branch(self):
710
self.make_branch_and_tree('topdir')
711
tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir')
712
self.assertEqual(os.path.realpath('topdir'),
713
os.path.realpath(tree.basedir))
714
self.assertEqual(os.path.realpath('topdir'),
715
self.local_branch_path(branch))
716
self.assertIs(tree.bzrdir, branch.bzrdir)
717
# opening from non-local should not return the tree
718
tree, branch = bzrdir.BzrDir.open_tree_or_branch(
719
self.get_readonly_url('topdir'))
720
self.assertEqual(None, tree)
722
self.make_branch('topdir/foo')
723
tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir/foo')
724
self.assertIs(tree, None)
725
self.assertEqual(os.path.realpath('topdir/foo'),
726
self.local_branch_path(branch))
728
def test_open_from_transport(self):
729
# transport pointing at bzrdir should give a bzrdir with root transport
730
# set to the given transport
731
control = bzrdir.BzrDir.create(self.get_url())
732
transport = get_transport(self.get_url())
733
opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
734
self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
735
self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
737
def test_open_from_transport_no_bzrdir(self):
738
transport = get_transport(self.get_url())
739
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
742
def test_open_from_transport_bzrdir_in_parent(self):
743
control = bzrdir.BzrDir.create(self.get_url())
744
transport = get_transport(self.get_url())
745
transport.mkdir('subdir')
746
transport = transport.clone('subdir')
747
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
750
def test_sprout_recursive(self):
751
tree = self.make_branch_and_tree('tree1',
752
format='dirstate-with-subtree')
753
sub_tree = self.make_branch_and_tree('tree1/subtree',
754
format='dirstate-with-subtree')
755
sub_tree.set_root_id('subtree-root')
756
tree.add_reference(sub_tree)
757
self.build_tree(['tree1/subtree/file'])
759
tree.commit('Initial commit')
760
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
762
self.addCleanup(tree2.unlock)
763
self.failUnlessExists('tree2/subtree/file')
764
self.assertEqual('tree-reference', tree2.kind('subtree-root'))
766
def test_cloning_metadir(self):
767
"""Ensure that cloning metadir is suitable"""
768
bzrdir = self.make_bzrdir('bzrdir')
769
bzrdir.cloning_metadir()
770
branch = self.make_branch('branch', format='knit')
771
format = branch.bzrdir.cloning_metadir()
772
self.assertIsInstance(format.workingtree_format,
773
workingtree.WorkingTreeFormat3)
775
def test_sprout_recursive_treeless(self):
776
tree = self.make_branch_and_tree('tree1',
777
format='dirstate-with-subtree')
778
sub_tree = self.make_branch_and_tree('tree1/subtree',
779
format='dirstate-with-subtree')
780
tree.add_reference(sub_tree)
781
self.build_tree(['tree1/subtree/file'])
783
tree.commit('Initial commit')
784
tree.bzrdir.destroy_workingtree()
785
repo = self.make_repository('repo', shared=True,
786
format='dirstate-with-subtree')
787
repo.set_make_working_trees(False)
788
tree.bzrdir.sprout('repo/tree2')
789
self.failUnlessExists('repo/tree2/subtree')
790
self.failIfExists('repo/tree2/subtree/file')
792
def make_foo_bar_baz(self):
793
foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
794
bar = self.make_branch('foo/bar').bzrdir
795
baz = self.make_branch('baz').bzrdir
798
def test_find_bzrdirs(self):
799
foo, bar, baz = self.make_foo_bar_baz()
800
transport = get_transport(self.get_url())
801
self.assertEqualBzrdirs([baz, foo, bar],
802
bzrdir.BzrDir.find_bzrdirs(transport))
804
def test_find_bzrdirs_list_current(self):
805
def list_current(transport):
806
return [s for s in transport.list_dir('') if s != 'baz']
808
foo, bar, baz = self.make_foo_bar_baz()
809
transport = get_transport(self.get_url())
810
self.assertEqualBzrdirs([foo, bar],
811
bzrdir.BzrDir.find_bzrdirs(transport,
812
list_current=list_current))
815
def test_find_bzrdirs_evaluate(self):
816
def evaluate(bzrdir):
818
repo = bzrdir.open_repository()
819
except NoRepositoryPresent:
820
return True, bzrdir.root_transport.base
822
return False, bzrdir.root_transport.base
824
foo, bar, baz = self.make_foo_bar_baz()
825
transport = get_transport(self.get_url())
826
self.assertEqual([baz.root_transport.base, foo.root_transport.base],
827
list(bzrdir.BzrDir.find_bzrdirs(transport,
830
def assertEqualBzrdirs(self, first, second):
832
second = list(second)
833
self.assertEqual(len(first), len(second))
834
for x, y in zip(first, second):
835
self.assertEqual(x.root_transport.base, y.root_transport.base)
837
def test_find_branches(self):
838
root = self.make_repository('', shared=True)
839
foo, bar, baz = self.make_foo_bar_baz()
840
qux = self.make_bzrdir('foo/qux')
841
transport = get_transport(self.get_url())
842
branches = bzrdir.BzrDir.find_branches(transport)
843
self.assertEqual(baz.root_transport.base, branches[0].base)
844
self.assertEqual(foo.root_transport.base, branches[1].base)
845
self.assertEqual(bar.root_transport.base, branches[2].base)
847
# ensure this works without a top-level repo
848
branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
849
self.assertEqual(foo.root_transport.base, branches[0].base)
850
self.assertEqual(bar.root_transport.base, branches[1].base)
396
853
class TestMeta1DirFormat(TestCaseWithTransport):
397
854
"""Tests specific to the meta1 dir format."""
602
1083
result.open_branch()
603
1084
result.open_repository()
1086
def test_checkout_metadir(self):
1087
# checkout_metadir has reasonable working tree format even when no
1088
# working tree is present
1089
self.make_branch('branch-knit2', format='dirstate-with-subtree')
1090
my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1091
checkout_format = my_bzrdir.checkout_metadir()
1092
self.assertIsInstance(checkout_format.workingtree_format,
1093
workingtree.WorkingTreeFormat3)
1096
class TestHTTPRedirections(object):
1097
"""Test redirection between two http servers.
1099
This MUST be used by daughter classes that also inherit from
1100
TestCaseWithTwoWebservers.
1102
We can't inherit directly from TestCaseWithTwoWebservers or the
1103
test framework will try to create an instance which cannot
1104
run, its implementation being incomplete.
1107
def create_transport_readonly_server(self):
1108
return http_utils.HTTPServerRedirecting()
1110
def create_transport_secondary_server(self):
1111
return http_utils.HTTPServerRedirecting()
1114
super(TestHTTPRedirections, self).setUp()
1115
# The redirections will point to the new server
1116
self.new_server = self.get_readonly_server()
1117
# The requests to the old server will be redirected
1118
self.old_server = self.get_secondary_server()
1119
# Configure the redirections
1120
self.old_server.redirect_to(self.new_server.host, self.new_server.port)
1122
def test_loop(self):
1123
# Both servers redirect to each other creating a loop
1124
self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1125
# Starting from either server should loop
1126
old_url = self._qualified_url(self.old_server.host,
1127
self.old_server.port)
1128
oldt = self._transport(old_url)
1129
self.assertRaises(errors.NotBranchError,
1130
bzrdir.BzrDir.open_from_transport, oldt)
1131
new_url = self._qualified_url(self.new_server.host,
1132
self.new_server.port)
1133
newt = self._transport(new_url)
1134
self.assertRaises(errors.NotBranchError,
1135
bzrdir.BzrDir.open_from_transport, newt)
1137
def test_qualifier_preserved(self):
1138
wt = self.make_branch_and_tree('branch')
1139
old_url = self._qualified_url(self.old_server.host,
1140
self.old_server.port)
1141
start = self._transport(old_url).clone('branch')
1142
bdir = bzrdir.BzrDir.open_from_transport(start)
1143
# Redirection should preserve the qualifier, hence the transport class
1145
self.assertIsInstance(bdir.root_transport, type(start))
1148
class TestHTTPRedirections_urllib(TestHTTPRedirections,
1149
http_utils.TestCaseWithTwoWebservers):
1150
"""Tests redirections for urllib implementation"""
1152
_transport = HttpTransport_urllib
1154
def _qualified_url(self, host, port):
1155
return 'http+urllib://%s:%s' % (host, port)
1159
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1160
TestHTTPRedirections,
1161
http_utils.TestCaseWithTwoWebservers):
1162
"""Tests redirections for pycurl implementation"""
1164
def _qualified_url(self, host, port):
1165
return 'http+pycurl://%s:%s' % (host, port)
1168
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
1169
http_utils.TestCaseWithTwoWebservers):
1170
"""Tests redirections for the nosmart decorator"""
1172
_transport = NoSmartTransportDecorator
1174
def _qualified_url(self, host, port):
1175
return 'nosmart+http://%s:%s' % (host, port)
1178
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1179
http_utils.TestCaseWithTwoWebservers):
1180
"""Tests redirections for readonly decoratror"""
1182
_transport = ReadonlyTransportDecorator
1184
def _qualified_url(self, host, port):
1185
return 'readonly+http://%s:%s' % (host, port)
1188
class TestDotBzrHidden(TestCaseWithTransport):
1191
if sys.platform == 'win32':
1192
ls = [os.environ['COMSPEC'], '/C', 'dir', '/B']
1195
f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
1196
stderr=subprocess.PIPE)
1197
out, err = f.communicate()
1198
self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
1200
return out.splitlines()
1202
def test_dot_bzr_hidden(self):
1203
if sys.platform == 'win32' and not win32utils.has_win32file:
1204
raise TestSkipped('unable to make file hidden without pywin32 library')
1205
b = bzrdir.BzrDir.create('.')
1206
self.build_tree(['a'])
1207
self.assertEquals(['a'], self.get_ls())
1209
def test_dot_bzr_hidden_with_url(self):
1210
if sys.platform == 'win32' and not win32utils.has_win32file:
1211
raise TestSkipped('unable to make file hidden without pywin32 library')
1212
b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1213
self.build_tree(['a'])
1214
self.assertEquals(['a'], self.get_ls())
1217
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1218
"""Test BzrDirFormat implementation for TestBzrDirSprout."""
1220
def _open(self, transport):
1221
return _TestBzrDir(transport, self)
1224
class _TestBzrDir(bzrdir.BzrDirMeta1):
1225
"""Test BzrDir implementation for TestBzrDirSprout.
1227
When created a _TestBzrDir already has repository and a branch. The branch
1228
is a test double as well.
1231
def __init__(self, *args, **kwargs):
1232
super(_TestBzrDir, self).__init__(*args, **kwargs)
1233
self.test_branch = _TestBranch()
1234
self.test_branch.repository = self.create_repository()
1236
def open_branch(self, unsupported=False):
1237
return self.test_branch
1239
def cloning_metadir(self, require_stacking=False):
1240
return _TestBzrDirFormat()
1243
class _TestBranchFormat(bzrlib.branch.BranchFormat):
1244
"""Test Branch format for TestBzrDirSprout."""
1247
class _TestBranch(bzrlib.branch.Branch):
1248
"""Test Branch implementation for TestBzrDirSprout."""
1250
def __init__(self, *args, **kwargs):
1251
self._format = _TestBranchFormat()
1252
super(_TestBranch, self).__init__(*args, **kwargs)
1256
def sprout(self, *args, **kwargs):
1257
self.calls.append('sprout')
1258
return _TestBranch()
1260
def copy_content_into(self, destination, revision_id=None):
1261
self.calls.append('copy_content_into')
1263
def get_parent(self):
1266
def set_parent(self, parent):
1267
self._parent = parent
1270
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1272
def test_sprout_uses_branch_sprout(self):
1273
"""BzrDir.sprout calls Branch.sprout.
1275
Usually, BzrDir.sprout should delegate to the branch's sprout method
1276
for part of the work. This allows the source branch to control the
1277
choice of format for the new branch.
1279
There are exceptions, but this tests avoids them:
1280
- if there's no branch in the source bzrdir,
1281
- or if the stacking has been requested and the format needs to be
1282
overridden to satisfy that.
1284
# Make an instrumented bzrdir.
1285
t = self.get_transport('source')
1287
source_bzrdir = _TestBzrDirFormat().initialize_on_transport(t)
1288
# The instrumented bzrdir has a test_branch attribute that logs calls
1289
# made to the branch contained in that bzrdir. Initially the test
1290
# branch exists but no calls have been made to it.
1291
self.assertEqual([], source_bzrdir.test_branch.calls)
1294
target_url = self.get_url('target')
1295
result = source_bzrdir.sprout(target_url, recurse='no')
1297
# The bzrdir called the branch's sprout method.
1298
self.assertSubset(['sprout'], source_bzrdir.test_branch.calls)
1300
def test_sprout_parent(self):
1301
grandparent_tree = self.make_branch('grandparent')
1302
parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1303
branch_tree = parent.bzrdir.sprout('branch').open_branch()
1304
self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
1307
class TestBzrDirHooks(TestCaseWithMemoryTransport):
1309
def test_pre_open_called(self):
1311
bzrdir.BzrDir.hooks.install_named_hook('pre_open', calls.append, None)
1312
transport = self.get_transport('foo')
1313
url = transport.base
1314
self.assertRaises(errors.NotBranchError, bzrdir.BzrDir.open, url)
1315
self.assertEqual([transport.base], [t.base for t in calls])
1317
def test_pre_open_actual_exceptions_raised(self):
1319
def fail_once(transport):
1322
raise errors.BzrError("fail")
1323
bzrdir.BzrDir.hooks.install_named_hook('pre_open', fail_once, None)
1324
transport = self.get_transport('foo')
1325
url = transport.base
1326
err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1327
self.assertEqual('fail', err._preformatted_string)