42
67
old_format = bzrdir.BzrDirFormat.get_default_format()
43
68
# default is BzrDirFormat6
44
69
self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
45
bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
70
bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
46
71
# creating a bzr dir should now create an instrumented dir.
48
result = bzrdir.BzrDir.create('memory:/')
73
result = bzrdir.BzrDir.create('memory:///')
49
74
self.failUnless(isinstance(result, SampleBzrDir))
51
bzrdir.BzrDirFormat.set_default_format(old_format)
76
bzrdir.BzrDirFormat._set_default_format(old_format)
52
77
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
80
class TestFormatRegistry(TestCase):
82
def make_format_registry(self):
83
my_format_registry = bzrdir.BzrDirFormatRegistry()
84
my_format_registry.register('weave', bzrdir.BzrDirFormat6,
85
'Pre-0.8 format. Slower and does not support checkouts or shared'
86
' repositories', deprecated=True)
87
my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
88
'BzrDirFormat6', 'Format registered lazily', deprecated=True)
89
my_format_registry.register_metadir('knit',
90
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
93
my_format_registry.set_default('knit')
94
my_format_registry.register_metadir(
96
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
97
'Experimental successor to knit. Use at your own risk.',
98
branch_format='bzrlib.branch.BzrBranchFormat6',
100
my_format_registry.register_metadir(
102
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
103
'Experimental successor to knit. Use at your own risk.',
104
branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
105
my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
106
'Pre-0.8 format. Slower and does not support checkouts or shared'
107
' repositories', hidden=True)
108
my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
109
'BzrDirFormat6', 'Format registered lazily', deprecated=True,
111
return my_format_registry
113
def test_format_registry(self):
114
my_format_registry = self.make_format_registry()
115
my_bzrdir = my_format_registry.make_bzrdir('lazy')
116
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
117
my_bzrdir = my_format_registry.make_bzrdir('weave')
118
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
119
my_bzrdir = my_format_registry.make_bzrdir('default')
120
self.assertIsInstance(my_bzrdir.repository_format,
121
knitrepo.RepositoryFormatKnit1)
122
my_bzrdir = my_format_registry.make_bzrdir('knit')
123
self.assertIsInstance(my_bzrdir.repository_format,
124
knitrepo.RepositoryFormatKnit1)
125
my_bzrdir = my_format_registry.make_bzrdir('branch6')
126
self.assertIsInstance(my_bzrdir.get_branch_format(),
127
bzrlib.branch.BzrBranchFormat6)
129
def test_get_help(self):
130
my_format_registry = self.make_format_registry()
131
self.assertEqual('Format registered lazily',
132
my_format_registry.get_help('lazy'))
133
self.assertEqual('Format using knits',
134
my_format_registry.get_help('knit'))
135
self.assertEqual('Format using knits',
136
my_format_registry.get_help('default'))
137
self.assertEqual('Pre-0.8 format. Slower and does not support'
138
' checkouts or shared repositories',
139
my_format_registry.get_help('weave'))
141
def test_help_topic(self):
142
topics = help_topics.HelpTopicRegistry()
143
registry = self.make_format_registry()
144
topics.register('current-formats', registry.help_topic,
146
topics.register('other-formats', registry.help_topic,
148
new = topics.get_detail('current-formats')
149
rest = topics.get_detail('other-formats')
150
experimental, deprecated = rest.split('Deprecated formats')
151
self.assertContainsRe(new, 'formats-help')
152
self.assertContainsRe(new,
153
':knit:\n \(native\) \(default\) Format using knits\n')
154
self.assertContainsRe(experimental,
155
':branch6:\n \(native\) Experimental successor to knit')
156
self.assertContainsRe(deprecated,
157
':lazy:\n \(native\) Format registered lazily\n')
158
self.assertNotContainsRe(new, 'hidden')
160
def test_set_default_repository(self):
161
default_factory = bzrdir.format_registry.get('default')
162
old_default = [k for k, v in bzrdir.format_registry.iteritems()
163
if v == default_factory and k != 'default'][0]
164
bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
166
self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
167
bzrdir.format_registry.get('default'))
169
repository.RepositoryFormat.get_default_format().__class__,
170
knitrepo.RepositoryFormatKnit3)
172
bzrdir.format_registry.set_default_repository(old_default)
174
def test_aliases(self):
175
a_registry = bzrdir.BzrDirFormatRegistry()
176
a_registry.register('weave', bzrdir.BzrDirFormat6,
177
'Pre-0.8 format. Slower and does not support checkouts or shared'
178
' repositories', deprecated=True)
179
a_registry.register('weavealias', bzrdir.BzrDirFormat6,
180
'Pre-0.8 format. Slower and does not support checkouts or shared'
181
' repositories', deprecated=True, alias=True)
182
self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
55
185
class SampleBranch(bzrlib.branch.Branch):
56
186
"""A dummy branch for guess what, dummy use."""
153
291
# now open_downlevel should fail too.
154
292
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_under_shared(self):
167
# an explicit create_repository always does so.
168
# we trust the format is right from the 'create_repository test'
169
old_format = bzrdir.BzrDirFormat.get_default_format()
170
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
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)
179
294
def test_create_branch_and_repo_uses_default(self):
180
295
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)
296
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
298
self.assertTrue(isinstance(branch, SampleBranch))
189
300
def test_create_branch_and_repo_under_shared(self):
190
301
# creating a branch and repo in a shared repo uses the
191
302
# 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)
303
format = bzrdir.format_registry.make_bzrdir('knit')
304
self.make_repository('.', shared=True, format=format)
305
branch = bzrdir.BzrDir.create_branch_and_repo(
306
self.get_url('child'), format=format)
307
self.assertRaises(errors.NoRepositoryPresent,
308
branch.bzrdir.open_repository)
202
310
def test_create_branch_and_repo_under_shared_force_new(self):
203
# creating a branch and repo in a shared repo can be forced to
311
# creating a branch and repo in a shared repo can be forced to
204
312
# 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)
313
format = bzrdir.format_registry.make_bzrdir('knit')
314
self.make_repository('.', shared=True, format=format)
315
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
318
branch.bzrdir.open_repository()
215
320
def test_create_standalone_working_tree(self):
216
321
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)
322
# note this is deliberately readonly, as this failure should
323
# occur before any writes.
324
self.assertRaises(errors.NotLocalUrl,
325
bzrdir.BzrDir.create_standalone_workingtree,
326
self.get_readonly_url(), format=format)
327
tree = bzrdir.BzrDir.create_standalone_workingtree('.',
329
self.assertEqual('A tree', tree)
230
331
def test_create_standalone_working_tree_under_shared_repo(self):
231
332
# 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)
333
format = bzrdir.format_registry.make_bzrdir('knit')
334
self.make_repository('.', shared=True, format=format)
335
# note this is deliberately readonly, as this failure should
336
# occur before any writes.
337
self.assertRaises(errors.NotLocalUrl,
338
bzrdir.BzrDir.create_standalone_workingtree,
339
self.get_readonly_url('child'), format=format)
340
tree = bzrdir.BzrDir.create_standalone_workingtree('child',
342
tree.bzrdir.open_repository()
246
344
def test_create_branch_convenience(self):
247
345
# 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)
346
format = bzrdir.format_registry.make_bzrdir('knit')
347
branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
348
branch.bzrdir.open_workingtree()
349
branch.bzrdir.open_repository()
351
def test_create_branch_convenience_possible_transports(self):
352
"""Check that the optional 'possible_transports' is recognized"""
353
format = bzrdir.format_registry.make_bzrdir('knit')
354
t = self.get_transport()
355
branch = bzrdir.BzrDir.create_branch_convenience(
356
'.', format=format, possible_transports=[t])
357
branch.bzrdir.open_workingtree()
358
branch.bzrdir.open_repository()
360
def test_create_branch_convenience_root(self):
361
"""Creating a branch at the root of a fs should work."""
362
self.vfs_transport_factory = memory.MemoryServer
363
# outside a repo the default convenience output is a repo+branch_tree
364
format = bzrdir.format_registry.make_bzrdir('knit')
365
branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
367
self.assertRaises(errors.NoWorkingTree,
368
branch.bzrdir.open_workingtree)
369
branch.bzrdir.open_repository()
257
371
def test_create_branch_convenience_under_shared_repo(self):
258
372
# inside a repo the default convenience output is a branch+ follow the
259
373
# 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)
374
format = bzrdir.format_registry.make_bzrdir('knit')
375
self.make_repository('.', shared=True, format=format)
376
branch = bzrdir.BzrDir.create_branch_convenience('child',
378
branch.bzrdir.open_workingtree()
379
self.assertRaises(errors.NoRepositoryPresent,
380
branch.bzrdir.open_repository)
271
382
def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
272
383
# inside a repo the default convenience output is a branch+ follow the
273
384
# 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)
385
format = bzrdir.format_registry.make_bzrdir('knit')
386
self.make_repository('.', shared=True, format=format)
387
branch = bzrdir.BzrDir.create_branch_convenience('child',
388
force_new_tree=False, format=format)
389
self.assertRaises(errors.NoWorkingTree,
390
branch.bzrdir.open_workingtree)
391
self.assertRaises(errors.NoRepositoryPresent,
392
branch.bzrdir.open_repository)
287
394
def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
288
395
# inside a repo the default convenience output is a branch+ follow the
289
396
# 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)
397
format = bzrdir.format_registry.make_bzrdir('knit')
398
repo = self.make_repository('.', shared=True, format=format)
399
repo.set_make_working_trees(False)
400
branch = bzrdir.BzrDir.create_branch_convenience('child',
402
self.assertRaises(errors.NoWorkingTree,
403
branch.bzrdir.open_workingtree)
404
self.assertRaises(errors.NoRepositoryPresent,
405
branch.bzrdir.open_repository)
303
407
def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
304
408
# inside a repo the default convenience output is a branch+ follow the
305
409
# 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)
410
format = bzrdir.format_registry.make_bzrdir('knit')
411
repo = self.make_repository('.', shared=True, format=format)
412
repo.set_make_working_trees(False)
413
branch = bzrdir.BzrDir.create_branch_convenience('child',
414
force_new_tree=True, format=format)
415
branch.bzrdir.open_workingtree()
416
self.assertRaises(errors.NoRepositoryPresent,
417
branch.bzrdir.open_repository)
319
419
def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
320
420
# inside a repo the default convenience output is overridable to give
321
421
# 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)
422
format = bzrdir.format_registry.make_bzrdir('knit')
423
self.make_repository('.', shared=True, format=format)
424
branch = bzrdir.BzrDir.create_branch_convenience('child',
425
force_new_repo=True, format=format)
426
branch.bzrdir.open_repository()
427
branch.bzrdir.open_workingtree()
430
class TestRepositoryAcquisitionPolicy(TestCaseWithTransport):
432
def test_acquire_repository_standalone(self):
433
"""The default acquisition policy should create a standalone branch."""
434
my_bzrdir = self.make_bzrdir('.')
435
repo_policy = my_bzrdir.determine_repository_policy()
436
repo, is_new = repo_policy.acquire_repository()
437
self.assertEqual(repo.bzrdir.root_transport.base,
438
my_bzrdir.root_transport.base)
439
self.assertFalse(repo.is_shared())
441
def test_determine_stacking_policy(self):
442
parent_bzrdir = self.make_bzrdir('.')
443
child_bzrdir = self.make_bzrdir('child')
444
parent_bzrdir.get_config().set_default_stack_on('http://example.org')
445
repo_policy = child_bzrdir.determine_repository_policy()
446
self.assertEqual('http://example.org', repo_policy._stack_on)
448
def test_determine_stacking_policy_relative(self):
449
parent_bzrdir = self.make_bzrdir('.')
450
child_bzrdir = self.make_bzrdir('child')
451
parent_bzrdir.get_config().set_default_stack_on('child2')
452
repo_policy = child_bzrdir.determine_repository_policy()
453
self.assertEqual('child2', repo_policy._stack_on)
454
self.assertEqual(parent_bzrdir.root_transport.base,
455
repo_policy._stack_on_pwd)
457
def prepare_default_stacking(self, child_format='1.6'):
458
parent_bzrdir = self.make_bzrdir('.')
459
child_branch = self.make_branch('child', format=child_format)
460
parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
461
new_child_transport = parent_bzrdir.transport.clone('child2')
462
return child_branch, new_child_transport
464
def test_clone_on_transport_obeys_stacking_policy(self):
465
child_branch, new_child_transport = self.prepare_default_stacking()
466
new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
467
self.assertEqual(child_branch.base,
468
new_child.open_branch().get_stacked_on_url())
470
def test_default_stacking_with_stackable_branch_unstackable_repo(self):
471
# Make stackable source branch with an unstackable repo format.
472
source_bzrdir = self.make_bzrdir('source')
473
pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
474
source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
476
# Make a directory with a default stacking policy
477
parent_bzrdir = self.make_bzrdir('parent')
478
stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
479
parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
480
# Clone source into directory
481
target = source_bzrdir.clone(self.get_url('parent/target'))
483
def test_sprout_obeys_stacking_policy(self):
484
child_branch, new_child_transport = self.prepare_default_stacking()
485
new_child = child_branch.bzrdir.sprout(new_child_transport.base)
486
self.assertEqual(child_branch.base,
487
new_child.open_branch().get_stacked_on_url())
489
def test_clone_ignores_policy_for_unsupported_formats(self):
490
child_branch, new_child_transport = self.prepare_default_stacking(
491
child_format='pack-0.92')
492
new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
493
self.assertRaises(errors.UnstackableBranchFormat,
494
new_child.open_branch().get_stacked_on_url)
496
def test_sprout_ignores_policy_for_unsupported_formats(self):
497
child_branch, new_child_transport = self.prepare_default_stacking(
498
child_format='pack-0.92')
499
new_child = child_branch.bzrdir.sprout(new_child_transport.base)
500
self.assertRaises(errors.UnstackableBranchFormat,
501
new_child.open_branch().get_stacked_on_url)
503
def test_sprout_upgrades_format_if_stacked_specified(self):
504
child_branch, new_child_transport = self.prepare_default_stacking(
505
child_format='pack-0.92')
506
new_child = child_branch.bzrdir.sprout(new_child_transport.base,
508
self.assertEqual(child_branch.bzrdir.root_transport.base,
509
new_child.open_branch().get_stacked_on_url())
510
repo = new_child.open_repository()
511
self.assertTrue(repo._format.supports_external_lookups)
512
self.assertFalse(repo.supports_rich_root())
514
def test_clone_on_transport_upgrades_format_if_stacked_on_specified(self):
515
child_branch, new_child_transport = self.prepare_default_stacking(
516
child_format='pack-0.92')
517
new_child = child_branch.bzrdir.clone_on_transport(new_child_transport,
518
stacked_on=child_branch.bzrdir.root_transport.base)
519
self.assertEqual(child_branch.bzrdir.root_transport.base,
520
new_child.open_branch().get_stacked_on_url())
521
repo = new_child.open_repository()
522
self.assertTrue(repo._format.supports_external_lookups)
523
self.assertFalse(repo.supports_rich_root())
525
def test_sprout_upgrades_to_rich_root_format_if_needed(self):
526
child_branch, new_child_transport = self.prepare_default_stacking(
527
child_format='rich-root-pack')
528
new_child = child_branch.bzrdir.sprout(new_child_transport.base,
530
repo = new_child.open_repository()
531
self.assertTrue(repo._format.supports_external_lookups)
532
self.assertTrue(repo.supports_rich_root())
534
def test_add_fallback_repo_handles_absolute_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_add_fallback_repo_handles_relative_urls(self):
541
stack_on = self.make_branch('stack_on', format='1.6')
542
repo = self.make_repository('repo', format='1.6')
543
policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
544
policy._add_fallback(repo)
546
def test_configure_relative_branch_stacking_url(self):
547
stack_on = self.make_branch('stack_on', format='1.6')
548
stacked = self.make_branch('stack_on/stacked', format='1.6')
549
policy = bzrdir.UseExistingRepository(stacked.repository,
551
policy.configure_branch(stacked)
552
self.assertEqual('..', stacked.get_stacked_on_url())
554
def test_relative_branch_stacking_to_absolute(self):
555
stack_on = self.make_branch('stack_on', format='1.6')
556
stacked = self.make_branch('stack_on/stacked', format='1.6')
557
policy = bzrdir.UseExistingRepository(stacked.repository,
558
'.', self.get_readonly_url('stack_on'))
559
policy.configure_branch(stacked)
560
self.assertEqual(self.get_readonly_url('stack_on'),
561
stacked.get_stacked_on_url())
334
564
class ChrootedTests(TestCaseWithTransport):
355
588
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
356
589
self.assertEqual('g/p/q', relpath)
591
def test_open_containing_tree_branch_or_repository_empty(self):
592
self.assertRaises(errors.NotBranchError,
593
bzrdir.BzrDir.open_containing_tree_branch_or_repository,
594
self.get_readonly_url(''))
596
def test_open_containing_tree_branch_or_repository_all(self):
597
self.make_branch_and_tree('topdir')
598
tree, branch, repo, relpath = \
599
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
601
self.assertEqual(os.path.realpath('topdir'),
602
os.path.realpath(tree.basedir))
603
self.assertEqual(os.path.realpath('topdir'),
604
self.local_branch_path(branch))
606
osutils.realpath(os.path.join('topdir', '.bzr', 'repository')),
607
repo.bzrdir.transport.local_abspath('repository'))
608
self.assertEqual(relpath, 'foo')
610
def test_open_containing_tree_branch_or_repository_no_tree(self):
611
self.make_branch('branch')
612
tree, branch, repo, relpath = \
613
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
615
self.assertEqual(tree, None)
616
self.assertEqual(os.path.realpath('branch'),
617
self.local_branch_path(branch))
619
osutils.realpath(os.path.join('branch', '.bzr', 'repository')),
620
repo.bzrdir.transport.local_abspath('repository'))
621
self.assertEqual(relpath, 'foo')
623
def test_open_containing_tree_branch_or_repository_repo(self):
624
self.make_repository('repo')
625
tree, branch, repo, relpath = \
626
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
628
self.assertEqual(tree, None)
629
self.assertEqual(branch, None)
631
osutils.realpath(os.path.join('repo', '.bzr', 'repository')),
632
repo.bzrdir.transport.local_abspath('repository'))
633
self.assertEqual(relpath, '')
635
def test_open_containing_tree_branch_or_repository_shared_repo(self):
636
self.make_repository('shared', shared=True)
637
bzrdir.BzrDir.create_branch_convenience('shared/branch',
638
force_new_tree=False)
639
tree, branch, repo, relpath = \
640
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
642
self.assertEqual(tree, None)
643
self.assertEqual(os.path.realpath('shared/branch'),
644
self.local_branch_path(branch))
646
osutils.realpath(os.path.join('shared', '.bzr', 'repository')),
647
repo.bzrdir.transport.local_abspath('repository'))
648
self.assertEqual(relpath, '')
650
def test_open_containing_tree_branch_or_repository_branch_subdir(self):
651
self.make_branch_and_tree('foo')
652
self.build_tree(['foo/bar/'])
653
tree, branch, repo, relpath = \
654
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
656
self.assertEqual(os.path.realpath('foo'),
657
os.path.realpath(tree.basedir))
658
self.assertEqual(os.path.realpath('foo'),
659
self.local_branch_path(branch))
661
osutils.realpath(os.path.join('foo', '.bzr', 'repository')),
662
repo.bzrdir.transport.local_abspath('repository'))
663
self.assertEqual(relpath, 'bar')
665
def test_open_containing_tree_branch_or_repository_repo_subdir(self):
666
self.make_repository('bar')
667
self.build_tree(['bar/baz/'])
668
tree, branch, repo, relpath = \
669
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
671
self.assertEqual(tree, None)
672
self.assertEqual(branch, None)
674
osutils.realpath(os.path.join('bar', '.bzr', 'repository')),
675
repo.bzrdir.transport.local_abspath('repository'))
676
self.assertEqual(relpath, 'baz')
358
678
def test_open_containing_from_transport(self):
359
679
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
360
680
get_transport(self.get_readonly_url('')))
368
688
get_transport(self.get_readonly_url('g/p/q')))
369
689
self.assertEqual('g/p/q', relpath)
691
def test_open_containing_tree_or_branch(self):
692
self.make_branch_and_tree('topdir')
693
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
695
self.assertEqual(os.path.realpath('topdir'),
696
os.path.realpath(tree.basedir))
697
self.assertEqual(os.path.realpath('topdir'),
698
self.local_branch_path(branch))
699
self.assertIs(tree.bzrdir, branch.bzrdir)
700
self.assertEqual('foo', relpath)
701
# opening from non-local should not return the tree
702
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
703
self.get_readonly_url('topdir/foo'))
704
self.assertEqual(None, tree)
705
self.assertEqual('foo', relpath)
707
self.make_branch('topdir/foo')
708
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
710
self.assertIs(tree, None)
711
self.assertEqual(os.path.realpath('topdir/foo'),
712
self.local_branch_path(branch))
713
self.assertEqual('', relpath)
715
def test_open_tree_or_branch(self):
716
self.make_branch_and_tree('topdir')
717
tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir')
718
self.assertEqual(os.path.realpath('topdir'),
719
os.path.realpath(tree.basedir))
720
self.assertEqual(os.path.realpath('topdir'),
721
self.local_branch_path(branch))
722
self.assertIs(tree.bzrdir, branch.bzrdir)
723
# opening from non-local should not return the tree
724
tree, branch = bzrdir.BzrDir.open_tree_or_branch(
725
self.get_readonly_url('topdir'))
726
self.assertEqual(None, tree)
728
self.make_branch('topdir/foo')
729
tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir/foo')
730
self.assertIs(tree, None)
731
self.assertEqual(os.path.realpath('topdir/foo'),
732
self.local_branch_path(branch))
734
def test_open_from_transport(self):
735
# transport pointing at bzrdir should give a bzrdir with root transport
736
# set to the given transport
737
control = bzrdir.BzrDir.create(self.get_url())
738
transport = get_transport(self.get_url())
739
opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
740
self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
741
self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
743
def test_open_from_transport_no_bzrdir(self):
744
transport = get_transport(self.get_url())
745
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
748
def test_open_from_transport_bzrdir_in_parent(self):
749
control = bzrdir.BzrDir.create(self.get_url())
750
transport = get_transport(self.get_url())
751
transport.mkdir('subdir')
752
transport = transport.clone('subdir')
753
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
756
def test_sprout_recursive(self):
757
tree = self.make_branch_and_tree('tree1',
758
format='dirstate-with-subtree')
759
sub_tree = self.make_branch_and_tree('tree1/subtree',
760
format='dirstate-with-subtree')
761
sub_tree.set_root_id('subtree-root')
762
tree.add_reference(sub_tree)
763
self.build_tree(['tree1/subtree/file'])
765
tree.commit('Initial commit')
766
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
768
self.addCleanup(tree2.unlock)
769
self.failUnlessExists('tree2/subtree/file')
770
self.assertEqual('tree-reference', tree2.kind('subtree-root'))
772
def test_cloning_metadir(self):
773
"""Ensure that cloning metadir is suitable"""
774
bzrdir = self.make_bzrdir('bzrdir')
775
bzrdir.cloning_metadir()
776
branch = self.make_branch('branch', format='knit')
777
format = branch.bzrdir.cloning_metadir()
778
self.assertIsInstance(format.workingtree_format,
779
workingtree.WorkingTreeFormat3)
781
def test_sprout_recursive_treeless(self):
782
tree = self.make_branch_and_tree('tree1',
783
format='dirstate-with-subtree')
784
sub_tree = self.make_branch_and_tree('tree1/subtree',
785
format='dirstate-with-subtree')
786
tree.add_reference(sub_tree)
787
self.build_tree(['tree1/subtree/file'])
789
tree.commit('Initial commit')
790
tree.bzrdir.destroy_workingtree()
791
repo = self.make_repository('repo', shared=True,
792
format='dirstate-with-subtree')
793
repo.set_make_working_trees(False)
794
tree.bzrdir.sprout('repo/tree2')
795
self.failUnlessExists('repo/tree2/subtree')
796
self.failIfExists('repo/tree2/subtree/file')
798
def make_foo_bar_baz(self):
799
foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
800
bar = self.make_branch('foo/bar').bzrdir
801
baz = self.make_branch('baz').bzrdir
804
def test_find_bzrdirs(self):
805
foo, bar, baz = self.make_foo_bar_baz()
806
transport = get_transport(self.get_url())
807
self.assertEqualBzrdirs([baz, foo, bar],
808
bzrdir.BzrDir.find_bzrdirs(transport))
810
def test_find_bzrdirs_list_current(self):
811
def list_current(transport):
812
return [s for s in transport.list_dir('') if s != 'baz']
814
foo, bar, baz = self.make_foo_bar_baz()
815
transport = get_transport(self.get_url())
816
self.assertEqualBzrdirs([foo, bar],
817
bzrdir.BzrDir.find_bzrdirs(transport,
818
list_current=list_current))
821
def test_find_bzrdirs_evaluate(self):
822
def evaluate(bzrdir):
824
repo = bzrdir.open_repository()
825
except NoRepositoryPresent:
826
return True, bzrdir.root_transport.base
828
return False, bzrdir.root_transport.base
830
foo, bar, baz = self.make_foo_bar_baz()
831
transport = get_transport(self.get_url())
832
self.assertEqual([baz.root_transport.base, foo.root_transport.base],
833
list(bzrdir.BzrDir.find_bzrdirs(transport,
836
def assertEqualBzrdirs(self, first, second):
838
second = list(second)
839
self.assertEqual(len(first), len(second))
840
for x, y in zip(first, second):
841
self.assertEqual(x.root_transport.base, y.root_transport.base)
843
def test_find_branches(self):
844
root = self.make_repository('', shared=True)
845
foo, bar, baz = self.make_foo_bar_baz()
846
qux = self.make_bzrdir('foo/qux')
847
transport = get_transport(self.get_url())
848
branches = bzrdir.BzrDir.find_branches(transport)
849
self.assertEqual(baz.root_transport.base, branches[0].base)
850
self.assertEqual(foo.root_transport.base, branches[1].base)
851
self.assertEqual(bar.root_transport.base, branches[2].base)
853
# ensure this works without a top-level repo
854
branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
855
self.assertEqual(foo.root_transport.base, branches[0].base)
856
self.assertEqual(bar.root_transport.base, branches[1].base)
372
859
class TestMeta1DirFormat(TestCaseWithTransport):
373
860
"""Tests specific to the meta1 dir format."""
513
1089
result.open_branch()
514
1090
result.open_repository()
1092
def test_checkout_metadir(self):
1093
# checkout_metadir has reasonable working tree format even when no
1094
# working tree is present
1095
self.make_branch('branch-knit2', format='dirstate-with-subtree')
1096
my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1097
checkout_format = my_bzrdir.checkout_metadir()
1098
self.assertIsInstance(checkout_format.workingtree_format,
1099
workingtree.WorkingTreeFormat3)
1102
class TestHTTPRedirections(object):
1103
"""Test redirection between two http servers.
1105
This MUST be used by daughter classes that also inherit from
1106
TestCaseWithTwoWebservers.
1108
We can't inherit directly from TestCaseWithTwoWebservers or the
1109
test framework will try to create an instance which cannot
1110
run, its implementation being incomplete.
1113
def create_transport_readonly_server(self):
1114
return http_utils.HTTPServerRedirecting()
1116
def create_transport_secondary_server(self):
1117
return http_utils.HTTPServerRedirecting()
1120
super(TestHTTPRedirections, self).setUp()
1121
# The redirections will point to the new server
1122
self.new_server = self.get_readonly_server()
1123
# The requests to the old server will be redirected
1124
self.old_server = self.get_secondary_server()
1125
# Configure the redirections
1126
self.old_server.redirect_to(self.new_server.host, self.new_server.port)
1128
def test_loop(self):
1129
# Both servers redirect to each other creating a loop
1130
self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1131
# Starting from either server should loop
1132
old_url = self._qualified_url(self.old_server.host,
1133
self.old_server.port)
1134
oldt = self._transport(old_url)
1135
self.assertRaises(errors.NotBranchError,
1136
bzrdir.BzrDir.open_from_transport, oldt)
1137
new_url = self._qualified_url(self.new_server.host,
1138
self.new_server.port)
1139
newt = self._transport(new_url)
1140
self.assertRaises(errors.NotBranchError,
1141
bzrdir.BzrDir.open_from_transport, newt)
1143
def test_qualifier_preserved(self):
1144
wt = self.make_branch_and_tree('branch')
1145
old_url = self._qualified_url(self.old_server.host,
1146
self.old_server.port)
1147
start = self._transport(old_url).clone('branch')
1148
bdir = bzrdir.BzrDir.open_from_transport(start)
1149
# Redirection should preserve the qualifier, hence the transport class
1151
self.assertIsInstance(bdir.root_transport, type(start))
1154
class TestHTTPRedirections_urllib(TestHTTPRedirections,
1155
http_utils.TestCaseWithTwoWebservers):
1156
"""Tests redirections for urllib implementation"""
1158
_transport = HttpTransport_urllib
1160
def _qualified_url(self, host, port):
1161
result = 'http+urllib://%s:%s' % (host, port)
1162
self.permit_url(result)
1167
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1168
TestHTTPRedirections,
1169
http_utils.TestCaseWithTwoWebservers):
1170
"""Tests redirections for pycurl implementation"""
1172
def _qualified_url(self, host, port):
1173
result = 'http+pycurl://%s:%s' % (host, port)
1174
self.permit_url(result)
1178
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
1179
http_utils.TestCaseWithTwoWebservers):
1180
"""Tests redirections for the nosmart decorator"""
1182
_transport = NoSmartTransportDecorator
1184
def _qualified_url(self, host, port):
1185
result = 'nosmart+http://%s:%s' % (host, port)
1186
self.permit_url(result)
1190
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1191
http_utils.TestCaseWithTwoWebservers):
1192
"""Tests redirections for readonly decoratror"""
1194
_transport = ReadonlyTransportDecorator
1196
def _qualified_url(self, host, port):
1197
result = 'readonly+http://%s:%s' % (host, port)
1198
self.permit_url(result)
1202
class TestDotBzrHidden(TestCaseWithTransport):
1205
if sys.platform == 'win32':
1206
ls = [os.environ['COMSPEC'], '/C', 'dir', '/B']
1209
f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
1210
stderr=subprocess.PIPE)
1211
out, err = f.communicate()
1212
self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
1214
return out.splitlines()
1216
def test_dot_bzr_hidden(self):
1217
if sys.platform == 'win32' and not win32utils.has_win32file:
1218
raise TestSkipped('unable to make file hidden without pywin32 library')
1219
b = bzrdir.BzrDir.create('.')
1220
self.build_tree(['a'])
1221
self.assertEquals(['a'], self.get_ls())
1223
def test_dot_bzr_hidden_with_url(self):
1224
if sys.platform == 'win32' and not win32utils.has_win32file:
1225
raise TestSkipped('unable to make file hidden without pywin32 library')
1226
b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1227
self.build_tree(['a'])
1228
self.assertEquals(['a'], self.get_ls())
1231
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1232
"""Test BzrDirFormat implementation for TestBzrDirSprout."""
1234
def _open(self, transport):
1235
return _TestBzrDir(transport, self)
1238
class _TestBzrDir(bzrdir.BzrDirMeta1):
1239
"""Test BzrDir implementation for TestBzrDirSprout.
1241
When created a _TestBzrDir already has repository and a branch. The branch
1242
is a test double as well.
1245
def __init__(self, *args, **kwargs):
1246
super(_TestBzrDir, self).__init__(*args, **kwargs)
1247
self.test_branch = _TestBranch()
1248
self.test_branch.repository = self.create_repository()
1250
def open_branch(self, unsupported=False):
1251
return self.test_branch
1253
def cloning_metadir(self, require_stacking=False):
1254
return _TestBzrDirFormat()
1257
class _TestBranchFormat(bzrlib.branch.BranchFormat):
1258
"""Test Branch format for TestBzrDirSprout."""
1261
class _TestBranch(bzrlib.branch.Branch):
1262
"""Test Branch implementation for TestBzrDirSprout."""
1264
def __init__(self, *args, **kwargs):
1265
self._format = _TestBranchFormat()
1266
super(_TestBranch, self).__init__(*args, **kwargs)
1270
def sprout(self, *args, **kwargs):
1271
self.calls.append('sprout')
1272
return _TestBranch()
1274
def copy_content_into(self, destination, revision_id=None):
1275
self.calls.append('copy_content_into')
1277
def get_parent(self):
1280
def set_parent(self, parent):
1281
self._parent = parent
1284
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1286
def test_sprout_uses_branch_sprout(self):
1287
"""BzrDir.sprout calls Branch.sprout.
1289
Usually, BzrDir.sprout should delegate to the branch's sprout method
1290
for part of the work. This allows the source branch to control the
1291
choice of format for the new branch.
1293
There are exceptions, but this tests avoids them:
1294
- if there's no branch in the source bzrdir,
1295
- or if the stacking has been requested and the format needs to be
1296
overridden to satisfy that.
1298
# Make an instrumented bzrdir.
1299
t = self.get_transport('source')
1301
source_bzrdir = _TestBzrDirFormat().initialize_on_transport(t)
1302
# The instrumented bzrdir has a test_branch attribute that logs calls
1303
# made to the branch contained in that bzrdir. Initially the test
1304
# branch exists but no calls have been made to it.
1305
self.assertEqual([], source_bzrdir.test_branch.calls)
1308
target_url = self.get_url('target')
1309
result = source_bzrdir.sprout(target_url, recurse='no')
1311
# The bzrdir called the branch's sprout method.
1312
self.assertSubset(['sprout'], source_bzrdir.test_branch.calls)
1314
def test_sprout_parent(self):
1315
grandparent_tree = self.make_branch('grandparent')
1316
parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1317
branch_tree = parent.bzrdir.sprout('branch').open_branch()
1318
self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
1321
class TestBzrDirHooks(TestCaseWithMemoryTransport):
1323
def test_pre_open_called(self):
1325
bzrdir.BzrDir.hooks.install_named_hook('pre_open', calls.append, None)
1326
transport = self.get_transport('foo')
1327
url = transport.base
1328
self.assertRaises(errors.NotBranchError, bzrdir.BzrDir.open, url)
1329
self.assertEqual([transport.base], [t.base for t in calls])
1331
def test_pre_open_actual_exceptions_raised(self):
1333
def fail_once(transport):
1336
raise errors.BzrError("fail")
1337
bzrdir.BzrDir.hooks.install_named_hook('pre_open', fail_once, None)
1338
transport = self.get_transport('foo')
1339
url = transport.base
1340
err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1341
self.assertEqual('fail', err._preformatted_string)