42
66
old_format = bzrdir.BzrDirFormat.get_default_format()
43
67
# default is BzrDirFormat6
44
68
self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
45
bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
69
bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
46
70
# creating a bzr dir should now create an instrumented dir.
48
72
result = bzrdir.BzrDir.create('memory:///')
49
73
self.failUnless(isinstance(result, SampleBzrDir))
51
bzrdir.BzrDirFormat.set_default_format(old_format)
75
bzrdir.BzrDirFormat._set_default_format(old_format)
52
76
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
79
class TestFormatRegistry(TestCase):
81
def make_format_registry(self):
82
my_format_registry = bzrdir.BzrDirFormatRegistry()
83
my_format_registry.register('weave', bzrdir.BzrDirFormat6,
84
'Pre-0.8 format. Slower and does not support checkouts or shared'
85
' repositories', deprecated=True)
86
my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
87
'BzrDirFormat6', 'Format registered lazily', deprecated=True)
88
my_format_registry.register_metadir('knit',
89
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
92
my_format_registry.set_default('knit')
93
my_format_registry.register_metadir(
95
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
96
'Experimental successor to knit. Use at your own risk.',
97
branch_format='bzrlib.branch.BzrBranchFormat6',
99
my_format_registry.register_metadir(
101
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
102
'Experimental successor to knit. Use at your own risk.',
103
branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
104
my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
105
'Pre-0.8 format. Slower and does not support checkouts or shared'
106
' repositories', hidden=True)
107
my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
108
'BzrDirFormat6', 'Format registered lazily', deprecated=True,
110
return my_format_registry
112
def test_format_registry(self):
113
my_format_registry = self.make_format_registry()
114
my_bzrdir = my_format_registry.make_bzrdir('lazy')
115
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
116
my_bzrdir = my_format_registry.make_bzrdir('weave')
117
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
118
my_bzrdir = my_format_registry.make_bzrdir('default')
119
self.assertIsInstance(my_bzrdir.repository_format,
120
knitrepo.RepositoryFormatKnit1)
121
my_bzrdir = my_format_registry.make_bzrdir('knit')
122
self.assertIsInstance(my_bzrdir.repository_format,
123
knitrepo.RepositoryFormatKnit1)
124
my_bzrdir = my_format_registry.make_bzrdir('branch6')
125
self.assertIsInstance(my_bzrdir.get_branch_format(),
126
bzrlib.branch.BzrBranchFormat6)
128
def test_get_help(self):
129
my_format_registry = self.make_format_registry()
130
self.assertEqual('Format registered lazily',
131
my_format_registry.get_help('lazy'))
132
self.assertEqual('Format using knits',
133
my_format_registry.get_help('knit'))
134
self.assertEqual('Format using knits',
135
my_format_registry.get_help('default'))
136
self.assertEqual('Pre-0.8 format. Slower and does not support'
137
' checkouts or shared repositories',
138
my_format_registry.get_help('weave'))
140
def test_help_topic(self):
141
topics = help_topics.HelpTopicRegistry()
142
topics.register('formats', self.make_format_registry().help_topic,
144
topic = topics.get_detail('formats')
145
new, rest = topic.split('Experimental formats')
146
experimental, deprecated = rest.split('Deprecated formats')
147
self.assertContainsRe(new, 'These formats can be used')
148
self.assertContainsRe(new,
149
':knit:\n \(native\) \(default\) Format using knits\n')
150
self.assertContainsRe(experimental,
151
':branch6:\n \(native\) Experimental successor to knit')
152
self.assertContainsRe(deprecated,
153
':lazy:\n \(native\) Format registered lazily\n')
154
self.assertNotContainsRe(new, 'hidden')
156
def test_set_default_repository(self):
157
default_factory = bzrdir.format_registry.get('default')
158
old_default = [k for k, v in bzrdir.format_registry.iteritems()
159
if v == default_factory and k != 'default'][0]
160
bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
162
self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
163
bzrdir.format_registry.get('default'))
165
repository.RepositoryFormat.get_default_format().__class__,
166
knitrepo.RepositoryFormatKnit3)
168
bzrdir.format_registry.set_default_repository(old_default)
170
def test_aliases(self):
171
a_registry = bzrdir.BzrDirFormatRegistry()
172
a_registry.register('weave', bzrdir.BzrDirFormat6,
173
'Pre-0.8 format. Slower and does not support checkouts or shared'
174
' repositories', deprecated=True)
175
a_registry.register('weavealias', bzrdir.BzrDirFormat6,
176
'Pre-0.8 format. Slower and does not support checkouts or shared'
177
' repositories', deprecated=True, alias=True)
178
self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
55
181
class SampleBranch(bzrlib.branch.Branch):
56
182
"""A dummy branch for guess what, dummy use."""
153
278
# now open_downlevel should fail too.
154
279
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
281
def test_create_branch_and_repo_uses_default(self):
190
282
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)
283
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
285
self.assertTrue(isinstance(branch, SampleBranch))
199
287
def test_create_branch_and_repo_under_shared(self):
200
288
# creating a branch and repo in a shared repo uses the
201
289
# 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)
290
format = bzrdir.format_registry.make_bzrdir('knit')
291
self.make_repository('.', shared=True, format=format)
292
branch = bzrdir.BzrDir.create_branch_and_repo(
293
self.get_url('child'), format=format)
294
self.assertRaises(errors.NoRepositoryPresent,
295
branch.bzrdir.open_repository)
212
297
def test_create_branch_and_repo_under_shared_force_new(self):
213
298
# creating a branch and repo in a shared repo can be forced to
214
299
# 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)
300
format = bzrdir.format_registry.make_bzrdir('knit')
301
self.make_repository('.', shared=True, format=format)
302
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
305
branch.bzrdir.open_repository()
225
307
def test_create_standalone_working_tree(self):
226
308
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)
309
# note this is deliberately readonly, as this failure should
310
# occur before any writes.
311
self.assertRaises(errors.NotLocalUrl,
312
bzrdir.BzrDir.create_standalone_workingtree,
313
self.get_readonly_url(), format=format)
314
tree = bzrdir.BzrDir.create_standalone_workingtree('.',
316
self.assertEqual('A tree', tree)
240
318
def test_create_standalone_working_tree_under_shared_repo(self):
241
319
# 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)
320
format = bzrdir.format_registry.make_bzrdir('knit')
321
self.make_repository('.', shared=True, format=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('child'), format=format)
327
tree = bzrdir.BzrDir.create_standalone_workingtree('child',
329
tree.bzrdir.open_repository()
256
331
def test_create_branch_convenience(self):
257
332
# 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)
333
format = bzrdir.format_registry.make_bzrdir('knit')
334
branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
335
branch.bzrdir.open_workingtree()
336
branch.bzrdir.open_repository()
338
def test_create_branch_convenience_possible_transports(self):
339
"""Check that the optional 'possible_transports' is recognized"""
340
format = bzrdir.format_registry.make_bzrdir('knit')
341
t = self.get_transport()
342
branch = bzrdir.BzrDir.create_branch_convenience(
343
'.', format=format, possible_transports=[t])
344
branch.bzrdir.open_workingtree()
345
branch.bzrdir.open_repository()
267
347
def test_create_branch_convenience_root(self):
268
348
"""Creating a branch at the root of a fs should work."""
269
self.transport_server = MemoryServer
349
self.vfs_transport_factory = MemoryServer
270
350
# 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)
351
format = bzrdir.format_registry.make_bzrdir('knit')
352
branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
354
self.assertRaises(errors.NoWorkingTree,
355
branch.bzrdir.open_workingtree)
356
branch.bzrdir.open_repository()
281
358
def test_create_branch_convenience_under_shared_repo(self):
282
359
# inside a repo the default convenience output is a branch+ follow the
283
360
# 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)
361
format = bzrdir.format_registry.make_bzrdir('knit')
362
self.make_repository('.', shared=True, format=format)
363
branch = bzrdir.BzrDir.create_branch_convenience('child',
365
branch.bzrdir.open_workingtree()
366
self.assertRaises(errors.NoRepositoryPresent,
367
branch.bzrdir.open_repository)
295
369
def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
296
370
# inside a repo the default convenience output is a branch+ follow the
297
371
# 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)
372
format = bzrdir.format_registry.make_bzrdir('knit')
373
self.make_repository('.', shared=True, format=format)
374
branch = bzrdir.BzrDir.create_branch_convenience('child',
375
force_new_tree=False, format=format)
376
self.assertRaises(errors.NoWorkingTree,
377
branch.bzrdir.open_workingtree)
378
self.assertRaises(errors.NoRepositoryPresent,
379
branch.bzrdir.open_repository)
311
381
def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
312
382
# inside a repo the default convenience output is a branch+ follow the
313
383
# 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)
384
format = bzrdir.format_registry.make_bzrdir('knit')
385
repo = self.make_repository('.', shared=True, format=format)
386
repo.set_make_working_trees(False)
387
branch = bzrdir.BzrDir.create_branch_convenience('child',
389
self.assertRaises(errors.NoWorkingTree,
390
branch.bzrdir.open_workingtree)
391
self.assertRaises(errors.NoRepositoryPresent,
392
branch.bzrdir.open_repository)
327
394
def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
328
395
# inside a repo the default convenience output is a branch+ follow the
329
396
# 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)
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',
401
force_new_tree=True, format=format)
402
branch.bzrdir.open_workingtree()
403
self.assertRaises(errors.NoRepositoryPresent,
404
branch.bzrdir.open_repository)
343
406
def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
344
407
# inside a repo the default convenience output is overridable to give
345
408
# 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)
409
format = bzrdir.format_registry.make_bzrdir('knit')
410
self.make_repository('.', shared=True, format=format)
411
branch = bzrdir.BzrDir.create_branch_convenience('child',
412
force_new_repo=True, format=format)
413
branch.bzrdir.open_repository()
414
branch.bzrdir.open_workingtree()
417
class TestRepositoryAcquisitionPolicy(TestCaseWithTransport):
419
def test_acquire_repository_standalone(self):
420
"""The default acquisition policy should create a standalone branch."""
421
my_bzrdir = self.make_bzrdir('.')
422
repo_policy = my_bzrdir.determine_repository_policy()
423
repo = repo_policy.acquire_repository()
424
self.assertEqual(repo.bzrdir.root_transport.base,
425
my_bzrdir.root_transport.base)
426
self.assertFalse(repo.is_shared())
429
def test_determine_stacking_policy(self):
430
parent_bzrdir = self.make_bzrdir('.')
431
child_bzrdir = self.make_bzrdir('child')
432
parent_bzrdir.get_config().set_default_stack_on('http://example.org')
433
repo_policy = child_bzrdir.determine_repository_policy()
434
self.assertEqual('http://example.org', repo_policy._stack_on)
436
def test_determine_stacking_policy_relative(self):
437
parent_bzrdir = self.make_bzrdir('.')
438
child_bzrdir = self.make_bzrdir('child')
439
parent_bzrdir.get_config().set_default_stack_on('child2')
440
repo_policy = child_bzrdir.determine_repository_policy()
441
self.assertEqual('child2', repo_policy._stack_on)
442
self.assertEqual(parent_bzrdir.root_transport.base,
443
repo_policy._stack_on_pwd)
445
def prepare_default_stacking(self):
446
parent_bzrdir = self.make_bzrdir('.')
447
child_branch = self.make_branch('child', format='development1')
448
parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
449
new_child_transport = parent_bzrdir.transport.clone('child2')
450
return child_branch, new_child_transport
452
def test_clone_on_transport_obeys_stacking_policy(self):
453
child_branch, new_child_transport = self.prepare_default_stacking()
454
new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
455
self.assertEqual(child_branch.base,
456
new_child.open_branch().get_stacked_on_url())
458
def test_sprout_obeys_stacking_policy(self):
459
child_branch, new_child_transport = self.prepare_default_stacking()
460
new_child = child_branch.bzrdir.sprout(new_child_transport.base)
461
self.assertEqual(child_branch.base,
462
new_child.open_branch().get_stacked_on_url())
464
def test_add_fallback_repo_handles_absolute_urls(self):
465
stack_on = self.make_branch('stack_on', format='development1')
466
repo = self.make_repository('repo', format='development1')
467
policy = bzrdir.UseExistingRepository(repo, stack_on.base)
468
policy._add_fallback(repo)
470
def test_add_fallback_repo_handles_relative_urls(self):
471
stack_on = self.make_branch('stack_on', format='development1')
472
repo = self.make_repository('repo', format='development1')
473
policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
474
policy._add_fallback(repo)
476
def test_configure_relative_branch_stacking_url(self):
477
stack_on = self.make_branch('stack_on', format='development1')
478
stacked = self.make_branch('stack_on/stacked', format='development1')
479
policy = bzrdir.UseExistingRepository(stacked.repository,
481
policy.configure_branch(stacked)
482
self.assertEqual('..', stacked.get_stacked_on_url())
484
def test_relative_branch_stacking_to_absolute(self):
485
stack_on = self.make_branch('stack_on', format='development1')
486
stacked = self.make_branch('stack_on/stacked', format='development1')
487
policy = bzrdir.UseExistingRepository(stacked.repository,
488
'.', self.get_readonly_url('stack_on'))
489
policy.configure_branch(stacked)
490
self.assertEqual(self.get_readonly_url('stack_on'),
491
stacked.get_stacked_on_url())
358
494
class ChrootedTests(TestCaseWithTransport):
379
518
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
380
519
self.assertEqual('g/p/q', relpath)
521
def test_open_containing_tree_branch_or_repository_empty(self):
522
self.assertRaises(errors.NotBranchError,
523
bzrdir.BzrDir.open_containing_tree_branch_or_repository,
524
self.get_readonly_url(''))
526
def test_open_containing_tree_branch_or_repository_all(self):
527
self.make_branch_and_tree('topdir')
528
tree, branch, repo, relpath = \
529
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
531
self.assertEqual(os.path.realpath('topdir'),
532
os.path.realpath(tree.basedir))
533
self.assertEqual(os.path.realpath('topdir'),
534
self.local_branch_path(branch))
536
osutils.realpath(os.path.join('topdir', '.bzr', 'repository')),
537
repo.bzrdir.transport.local_abspath('repository'))
538
self.assertEqual(relpath, 'foo')
540
def test_open_containing_tree_branch_or_repository_no_tree(self):
541
self.make_branch('branch')
542
tree, branch, repo, relpath = \
543
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
545
self.assertEqual(tree, None)
546
self.assertEqual(os.path.realpath('branch'),
547
self.local_branch_path(branch))
549
osutils.realpath(os.path.join('branch', '.bzr', 'repository')),
550
repo.bzrdir.transport.local_abspath('repository'))
551
self.assertEqual(relpath, 'foo')
553
def test_open_containing_tree_branch_or_repository_repo(self):
554
self.make_repository('repo')
555
tree, branch, repo, relpath = \
556
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
558
self.assertEqual(tree, None)
559
self.assertEqual(branch, None)
561
osutils.realpath(os.path.join('repo', '.bzr', 'repository')),
562
repo.bzrdir.transport.local_abspath('repository'))
563
self.assertEqual(relpath, '')
565
def test_open_containing_tree_branch_or_repository_shared_repo(self):
566
self.make_repository('shared', shared=True)
567
bzrdir.BzrDir.create_branch_convenience('shared/branch',
568
force_new_tree=False)
569
tree, branch, repo, relpath = \
570
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
572
self.assertEqual(tree, None)
573
self.assertEqual(os.path.realpath('shared/branch'),
574
self.local_branch_path(branch))
576
osutils.realpath(os.path.join('shared', '.bzr', 'repository')),
577
repo.bzrdir.transport.local_abspath('repository'))
578
self.assertEqual(relpath, '')
580
def test_open_containing_tree_branch_or_repository_branch_subdir(self):
581
self.make_branch_and_tree('foo')
582
self.build_tree(['foo/bar/'])
583
tree, branch, repo, relpath = \
584
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
586
self.assertEqual(os.path.realpath('foo'),
587
os.path.realpath(tree.basedir))
588
self.assertEqual(os.path.realpath('foo'),
589
self.local_branch_path(branch))
591
osutils.realpath(os.path.join('foo', '.bzr', 'repository')),
592
repo.bzrdir.transport.local_abspath('repository'))
593
self.assertEqual(relpath, 'bar')
595
def test_open_containing_tree_branch_or_repository_repo_subdir(self):
596
self.make_repository('bar')
597
self.build_tree(['bar/baz/'])
598
tree, branch, repo, relpath = \
599
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
601
self.assertEqual(tree, None)
602
self.assertEqual(branch, None)
604
osutils.realpath(os.path.join('bar', '.bzr', 'repository')),
605
repo.bzrdir.transport.local_abspath('repository'))
606
self.assertEqual(relpath, 'baz')
382
608
def test_open_containing_from_transport(self):
383
609
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
384
610
get_transport(self.get_readonly_url('')))
414
683
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
686
def test_sprout_recursive(self):
687
tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
688
sub_tree = self.make_branch_and_tree('tree1/subtree',
689
format='dirstate-with-subtree')
690
tree.add_reference(sub_tree)
691
self.build_tree(['tree1/subtree/file'])
693
tree.commit('Initial commit')
694
tree.bzrdir.sprout('tree2')
695
self.failUnlessExists('tree2/subtree/file')
697
def test_cloning_metadir(self):
698
"""Ensure that cloning metadir is suitable"""
699
bzrdir = self.make_bzrdir('bzrdir')
700
bzrdir.cloning_metadir()
701
branch = self.make_branch('branch', format='knit')
702
format = branch.bzrdir.cloning_metadir()
703
self.assertIsInstance(format.workingtree_format,
704
workingtree.WorkingTreeFormat3)
706
def test_sprout_recursive_treeless(self):
707
tree = self.make_branch_and_tree('tree1',
708
format='dirstate-with-subtree')
709
sub_tree = self.make_branch_and_tree('tree1/subtree',
710
format='dirstate-with-subtree')
711
tree.add_reference(sub_tree)
712
self.build_tree(['tree1/subtree/file'])
714
tree.commit('Initial commit')
715
tree.bzrdir.destroy_workingtree()
716
repo = self.make_repository('repo', shared=True,
717
format='dirstate-with-subtree')
718
repo.set_make_working_trees(False)
719
tree.bzrdir.sprout('repo/tree2')
720
self.failUnlessExists('repo/tree2/subtree')
721
self.failIfExists('repo/tree2/subtree/file')
723
def make_foo_bar_baz(self):
724
foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
725
bar = self.make_branch('foo/bar').bzrdir
726
baz = self.make_branch('baz').bzrdir
729
def test_find_bzrdirs(self):
730
foo, bar, baz = self.make_foo_bar_baz()
731
transport = get_transport(self.get_url())
732
self.assertEqualBzrdirs([baz, foo, bar],
733
bzrdir.BzrDir.find_bzrdirs(transport))
735
def test_find_bzrdirs_list_current(self):
736
def list_current(transport):
737
return [s for s in transport.list_dir('') if s != 'baz']
739
foo, bar, baz = self.make_foo_bar_baz()
740
transport = get_transport(self.get_url())
741
self.assertEqualBzrdirs([foo, bar],
742
bzrdir.BzrDir.find_bzrdirs(transport,
743
list_current=list_current))
746
def test_find_bzrdirs_evaluate(self):
747
def evaluate(bzrdir):
749
repo = bzrdir.open_repository()
750
except NoRepositoryPresent:
751
return True, bzrdir.root_transport.base
753
return False, bzrdir.root_transport.base
755
foo, bar, baz = self.make_foo_bar_baz()
756
transport = get_transport(self.get_url())
757
self.assertEqual([baz.root_transport.base, foo.root_transport.base],
758
list(bzrdir.BzrDir.find_bzrdirs(transport,
761
def assertEqualBzrdirs(self, first, second):
763
second = list(second)
764
self.assertEqual(len(first), len(second))
765
for x, y in zip(first, second):
766
self.assertEqual(x.root_transport.base, y.root_transport.base)
768
def test_find_branches(self):
769
root = self.make_repository('', shared=True)
770
foo, bar, baz = self.make_foo_bar_baz()
771
qux = self.make_bzrdir('foo/qux')
772
transport = get_transport(self.get_url())
773
branches = bzrdir.BzrDir.find_branches(transport)
774
self.assertEqual(baz.root_transport.base, branches[0].base)
775
self.assertEqual(foo.root_transport.base, branches[1].base)
776
self.assertEqual(bar.root_transport.base, branches[2].base)
778
# ensure this works without a top-level repo
779
branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
780
self.assertEqual(foo.root_transport.base, branches[0].base)
781
self.assertEqual(bar.root_transport.base, branches[1].base)
418
784
class TestMeta1DirFormat(TestCaseWithTransport):
419
785
"""Tests specific to the meta1 dir format."""
624
1009
result.open_branch()
625
1010
result.open_repository()
1012
def test_checkout_metadir(self):
1013
# checkout_metadir has reasonable working tree format even when no
1014
# working tree is present
1015
self.make_branch('branch-knit2', format='dirstate-with-subtree')
1016
my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1017
checkout_format = my_bzrdir.checkout_metadir()
1018
self.assertIsInstance(checkout_format.workingtree_format,
1019
workingtree.WorkingTreeFormat3)
1022
class TestHTTPRedirectionLoop(object):
1023
"""Test redirection loop between two http servers.
1025
This MUST be used by daughter classes that also inherit from
1026
TestCaseWithTwoWebservers.
1028
We can't inherit directly from TestCaseWithTwoWebservers or the
1029
test framework will try to create an instance which cannot
1030
run, its implementation being incomplete.
1033
# Should be defined by daughter classes to ensure redirection
1034
# still use the same transport implementation (not currently
1035
# enforced as it's a bit tricky to get right (see the FIXME
1036
# in BzrDir.open_from_transport for the unique use case so
1040
def create_transport_readonly_server(self):
1041
return HTTPServerRedirecting()
1043
def create_transport_secondary_server(self):
1044
return HTTPServerRedirecting()
1047
# Both servers redirect to each server creating a loop
1048
super(TestHTTPRedirectionLoop, self).setUp()
1049
# The redirections will point to the new server
1050
self.new_server = self.get_readonly_server()
1051
# The requests to the old server will be redirected
1052
self.old_server = self.get_secondary_server()
1053
# Configure the redirections
1054
self.old_server.redirect_to(self.new_server.host, self.new_server.port)
1055
self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1057
def _qualified_url(self, host, port):
1058
return 'http+%s://%s:%s' % (self._qualifier, host, port)
1060
def test_loop(self):
1061
# Starting from either server should loop
1062
old_url = self._qualified_url(self.old_server.host,
1063
self.old_server.port)
1064
oldt = self._transport(old_url)
1065
self.assertRaises(errors.NotBranchError,
1066
bzrdir.BzrDir.open_from_transport, oldt)
1067
new_url = self._qualified_url(self.new_server.host,
1068
self.new_server.port)
1069
newt = self._transport(new_url)
1070
self.assertRaises(errors.NotBranchError,
1071
bzrdir.BzrDir.open_from_transport, newt)
1074
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
1075
TestCaseWithTwoWebservers):
1076
"""Tests redirections for urllib implementation"""
1078
_qualifier = 'urllib'
1079
_transport = HttpTransport_urllib
1083
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1084
TestHTTPRedirectionLoop,
1085
TestCaseWithTwoWebservers):
1086
"""Tests redirections for pycurl implementation"""
1088
_qualifier = 'pycurl'
1091
class TestDotBzrHidden(TestCaseWithTransport):
1094
if sys.platform == 'win32':
1095
ls = [os.environ['COMSPEC'], '/C', 'dir', '/B']
1098
f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
1099
stderr=subprocess.PIPE)
1100
out, err = f.communicate()
1101
self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
1103
return out.splitlines()
1105
def test_dot_bzr_hidden(self):
1106
if sys.platform == 'win32' and not win32utils.has_win32file:
1107
raise TestSkipped('unable to make file hidden without pywin32 library')
1108
b = bzrdir.BzrDir.create('.')
1109
self.build_tree(['a'])
1110
self.assertEquals(['a'], self.get_ls())
1112
def test_dot_bzr_hidden_with_url(self):
1113
if sys.platform == 'win32' and not win32utils.has_win32file:
1114
raise TestSkipped('unable to make file hidden without pywin32 library')
1115
b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1116
self.build_tree(['a'])
1117
self.assertEquals(['a'], self.get_ls())
1120
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1121
"""Test BzrDirFormat implementation for TestBzrDirSprout."""
1123
def _open(self, transport):
1124
return _TestBzrDir(transport, self)
1127
class _TestBzrDir(bzrdir.BzrDirMeta1):
1128
"""Test BzrDir implementation for TestBzrDirSprout.
1130
When created a _TestBzrDir already has repository and a branch. The branch
1131
is a test double as well.
1134
def __init__(self, *args, **kwargs):
1135
super(_TestBzrDir, self).__init__(*args, **kwargs)
1136
self.test_branch = _TestBranch()
1137
self.test_branch.repository = self.create_repository()
1139
def open_branch(self, unsupported=False):
1140
return self.test_branch
1142
def cloning_metadir(self):
1143
return _TestBzrDirFormat()
1146
class _TestBranch(bzrlib.branch.Branch):
1147
"""Test Branch implementation for TestBzrDirSprout."""
1149
def __init__(self, *args, **kwargs):
1150
super(_TestBranch, self).__init__(*args, **kwargs)
1153
def sprout(self, *args, **kwargs):
1154
self.calls.append('sprout')
1157
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1159
def test_sprout_uses_branch_sprout(self):
1160
"""BzrDir.sprout calls Branch.sprout.
1162
Usually, BzrDir.sprout should delegate to the branch's sprout method
1163
for part of the work. This allows the source branch to control the
1164
choice of format for the new branch.
1166
There are exceptions, but this tests avoids them:
1167
- if there's no branch in the source bzrdir,
1168
- or if the stacking has been requested and the format needs to be
1169
overridden to satisfy that.
1171
# Make an instrumented bzrdir.
1172
t = self.get_transport('source')
1174
source_bzrdir = _TestBzrDirFormat().initialize_on_transport(t)
1175
# The instrumented bzrdir has a test_branch attribute that logs calls
1176
# made to the branch contained in that bzrdir. Initially the test
1177
# branch exists but no calls have been made to it.
1178
self.assertEqual([], source_bzrdir.test_branch.calls)
1181
target_url = self.get_url('target')
1182
result = source_bzrdir.sprout(target_url, recurse='no')
1184
# The bzrdir called the branch's sprout method.
1185
self.assertEqual(['sprout'], source_bzrdir.test_branch.calls)