712
712
self.assertEqual("No revisions to pull.\n", f.getvalue())
715
class _StubLockable(object):
716
"""Helper for TestRunWithWriteLockedTarget."""
718
def __init__(self, calls, unlock_exc=None):
720
self.unlock_exc = unlock_exc
722
def lock_write(self):
723
self.calls.append('lock_write')
726
self.calls.append('unlock')
727
if self.unlock_exc is not None:
728
raise self.unlock_exc
731
class _ErrorFromCallable(Exception):
732
"""Helper for TestRunWithWriteLockedTarget."""
735
class _ErrorFromUnlock(Exception):
736
"""Helper for TestRunWithWriteLockedTarget."""
739
class TestRunWithWriteLockedTarget(tests.TestCase):
740
"""Tests for _run_with_write_locked_target."""
743
tests.TestCase.setUp(self)
746
def func_that_returns_ok(self):
747
self._calls.append('func called')
750
def func_that_raises(self):
751
self._calls.append('func called')
752
raise _ErrorFromCallable()
754
def test_success_unlocks(self):
755
lockable = _StubLockable(self._calls)
756
result = _mod_branch._run_with_write_locked_target(
757
lockable, self.func_that_returns_ok)
758
self.assertEqual('ok', result)
759
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
761
def test_exception_unlocks_and_propagates(self):
762
lockable = _StubLockable(self._calls)
763
self.assertRaises(_ErrorFromCallable,
764
_mod_branch._run_with_write_locked_target,
765
lockable, self.func_that_raises)
766
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
768
def test_callable_succeeds_but_error_during_unlock(self):
769
lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
770
self.assertRaises(_ErrorFromUnlock,
771
_mod_branch._run_with_write_locked_target,
772
lockable, self.func_that_returns_ok)
773
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
775
def test_error_during_unlock_does_not_mask_original_error(self):
776
lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
777
self.assertRaises(_ErrorFromCallable,
778
_mod_branch._run_with_write_locked_target,
779
lockable, self.func_that_raises)
780
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)