243
246
self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
245
248
def test_set_revision_history(self):
246
tree = self.make_branch_and_memory_tree('.',
247
format=self.get_format_name())
251
tree.commit('foo', rev_id='foo')
252
tree.commit('bar', rev_id='bar')
253
tree.branch.set_revision_history(['foo', 'bar'])
254
tree.branch.set_revision_history(['foo'])
255
self.assertRaises(errors.NotLefthandHistory,
256
tree.branch.set_revision_history, ['bar'])
249
builder = self.make_branch_builder('.', format=self.get_format_name())
250
builder.build_snapshot('foo', None,
251
[('add', ('', None, 'directory', None))],
253
builder.build_snapshot('bar', None, [], message='bar')
254
branch = builder.get_branch()
256
self.addCleanup(branch.unlock)
257
branch.set_revision_history(['foo', 'bar'])
258
branch.set_revision_history(['foo'])
259
self.assertRaises(errors.NotLefthandHistory,
260
branch.set_revision_history, ['bar'])
260
262
def do_checkout_test(self, lightweight=False):
261
263
tree = self.make_branch_and_tree('source',
334
336
return _mod_branch.BzrBranch7
336
338
def get_format_name(self):
339
341
def get_format_name_subtree(self):
340
342
return "development-subtree"
342
def test_set_stacked_on_unstackable_repo(self):
344
def test_set_stacked_on_url_unstackable_repo(self):
343
345
repo = self.make_repository('a', format='dirstate-tags')
344
346
control = repo.bzrdir
345
347
branch = _mod_branch.BzrBranchFormat7().initialize(control)
346
348
target = self.make_branch('b')
347
349
self.assertRaises(errors.UnstackableRepositoryFormat,
348
branch.set_stacked_on, target.base)
350
branch.set_stacked_on_url, target.base)
352
def test_clone_stacked_on_unstackable_repo(self):
353
repo = self.make_repository('a', format='dirstate-tags')
354
control = repo.bzrdir
355
branch = _mod_branch.BzrBranchFormat7().initialize(control)
356
# Calling clone should not raise UnstackableRepositoryFormat.
357
cloned_bzrdir = control.clone('cloned')
350
359
def _test_default_stacked_location(self):
351
360
branch = self.make_branch('a', format=self.get_format_name())
352
self.assertRaises(errors.NotStacked, branch.get_stacked_on)
361
self.assertRaises(errors.NotStacked, branch.get_stacked_on_url)
354
363
def test_stack_and_unstack(self):
355
364
branch = self.make_branch('a', format=self.get_format_name())
356
365
target = self.make_branch_and_tree('b', format=self.get_format_name())
357
branch.set_stacked_on(target.branch.base)
358
self.assertEqual(target.branch.base, branch.get_stacked_on())
366
branch.set_stacked_on_url(target.branch.base)
367
self.assertEqual(target.branch.base, branch.get_stacked_on_url())
359
368
revid = target.commit('foo')
360
369
self.assertTrue(branch.repository.has_revision(revid))
361
branch.set_stacked_on(None)
362
self.assertRaises(errors.NotStacked, branch.get_stacked_on)
370
branch.set_stacked_on_url(None)
371
self.assertRaises(errors.NotStacked, branch.get_stacked_on_url)
363
372
self.assertFalse(branch.repository.has_revision(revid))
365
374
def test_open_opens_stacked_reference(self):
366
375
branch = self.make_branch('a', format=self.get_format_name())
367
376
target = self.make_branch_and_tree('b', format=self.get_format_name())
368
branch.set_stacked_on(target.branch.base)
377
branch.set_stacked_on_url(target.branch.base)
369
378
branch = branch.bzrdir.open_branch()
370
379
revid = target.commit('foo')
371
380
self.assertTrue(branch.repository.has_revision(revid))
383
class BzrBranch8(TestCaseWithTransport):
385
def make_branch(self, location, format=None):
387
format = bzrdir.format_registry.make_bzrdir('1.9')
388
format.set_branch_format(_mod_branch.BzrBranchFormat8())
389
return TestCaseWithTransport.make_branch(self, location, format=format)
391
def create_branch_with_reference(self):
392
branch = self.make_branch('branch')
393
branch._set_all_reference_info({'file-id': ('path', 'location')})
397
def instrument_branch(branch, gets):
398
old_get = branch._transport.get
399
def get(*args, **kwargs):
400
gets.append((args, kwargs))
401
return old_get(*args, **kwargs)
402
branch._transport.get = get
404
def test_reference_info_caching_read_locked(self):
406
branch = self.create_branch_with_reference()
408
self.addCleanup(branch.unlock)
409
self.instrument_branch(branch, gets)
410
branch.get_reference_info('file-id')
411
branch.get_reference_info('file-id')
412
self.assertEqual(1, len(gets))
414
def test_reference_info_caching_read_unlocked(self):
416
branch = self.create_branch_with_reference()
417
self.instrument_branch(branch, gets)
418
branch.get_reference_info('file-id')
419
branch.get_reference_info('file-id')
420
self.assertEqual(2, len(gets))
422
def test_reference_info_caching_write_locked(self):
424
branch = self.make_branch('branch')
426
self.instrument_branch(branch, gets)
427
self.addCleanup(branch.unlock)
428
branch._set_all_reference_info({'file-id': ('path2', 'location2')})
429
path, location = branch.get_reference_info('file-id')
430
self.assertEqual(0, len(gets))
431
self.assertEqual('path2', path)
432
self.assertEqual('location2', location)
434
def test_reference_info_caches_cleared(self):
435
branch = self.make_branch('branch')
437
branch.set_reference_info('file-id', 'path2', 'location2')
439
doppelganger = Branch.open('branch')
440
doppelganger.set_reference_info('file-id', 'path3', 'location3')
441
self.assertEqual(('path3', 'location3'),
442
branch.get_reference_info('file-id'))
374
444
class TestBranchReference(TestCaseWithTransport):
375
445
"""Tests for the branch reference facility."""
432
503
# it's still supported
433
504
a = "%d revisions pulled" % r
434
505
self.assertEqual(a, "10 revisions pulled")
509
class _StubLockable(object):
510
"""Helper for TestRunWithWriteLockedTarget."""
512
def __init__(self, calls, unlock_exc=None):
514
self.unlock_exc = unlock_exc
516
def lock_write(self):
517
self.calls.append('lock_write')
520
self.calls.append('unlock')
521
if self.unlock_exc is not None:
522
raise self.unlock_exc
525
class _ErrorFromCallable(Exception):
526
"""Helper for TestRunWithWriteLockedTarget."""
529
class _ErrorFromUnlock(Exception):
530
"""Helper for TestRunWithWriteLockedTarget."""
533
class TestRunWithWriteLockedTarget(TestCase):
534
"""Tests for _run_with_write_locked_target."""
540
def func_that_returns_ok(self):
541
self._calls.append('func called')
544
def func_that_raises(self):
545
self._calls.append('func called')
546
raise _ErrorFromCallable()
548
def test_success_unlocks(self):
549
lockable = _StubLockable(self._calls)
550
result = _run_with_write_locked_target(
551
lockable, self.func_that_returns_ok)
552
self.assertEqual('ok', result)
553
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
555
def test_exception_unlocks_and_propagates(self):
556
lockable = _StubLockable(self._calls)
557
self.assertRaises(_ErrorFromCallable,
558
_run_with_write_locked_target, lockable, self.func_that_raises)
559
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
561
def test_callable_succeeds_but_error_during_unlock(self):
562
lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
563
self.assertRaises(_ErrorFromUnlock,
564
_run_with_write_locked_target, lockable, self.func_that_returns_ok)
565
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
567
def test_error_during_unlock_does_not_mask_original_error(self):
568
lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
569
self.assertRaises(_ErrorFromCallable,
570
_run_with_write_locked_target, lockable, self.func_that_raises)
571
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)