~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Jelmer Vernooij
  • Date: 2011-05-16 13:39:39 UTC
  • mto: (5923.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 5925.
  • Revision ID: jelmer@samba.org-20110516133939-8u1pc9utas3uw1lt
Require a unicode prompt to be passed into all methods that prompt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for the Branch facility that are not interface  tests.
18
18
 
19
 
For interface tests see `tests/per_branch/*.py`.
 
19
For interface tests see tests/per_branch/*.py.
20
20
 
21
21
For concrete class tests see this file, and for meta-branch tests
22
22
also see this file.
711
711
        r.report(f)
712
712
        self.assertEqual("No revisions to pull.\n", f.getvalue())
713
713
 
 
714
 
 
715
class _StubLockable(object):
 
716
    """Helper for TestRunWithWriteLockedTarget."""
 
717
 
 
718
    def __init__(self, calls, unlock_exc=None):
 
719
        self.calls = calls
 
720
        self.unlock_exc = unlock_exc
 
721
 
 
722
    def lock_write(self):
 
723
        self.calls.append('lock_write')
 
724
 
 
725
    def unlock(self):
 
726
        self.calls.append('unlock')
 
727
        if self.unlock_exc is not None:
 
728
            raise self.unlock_exc
 
729
 
 
730
 
 
731
class _ErrorFromCallable(Exception):
 
732
    """Helper for TestRunWithWriteLockedTarget."""
 
733
 
 
734
 
 
735
class _ErrorFromUnlock(Exception):
 
736
    """Helper for TestRunWithWriteLockedTarget."""
 
737
 
 
738
 
 
739
class TestRunWithWriteLockedTarget(tests.TestCase):
 
740
    """Tests for _run_with_write_locked_target."""
 
741
 
 
742
    def setUp(self):
 
743
        tests.TestCase.setUp(self)
 
744
        self._calls = []
 
745
 
 
746
    def func_that_returns_ok(self):
 
747
        self._calls.append('func called')
 
748
        return 'ok'
 
749
 
 
750
    def func_that_raises(self):
 
751
        self._calls.append('func called')
 
752
        raise _ErrorFromCallable()
 
753
 
 
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)
 
760
 
 
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)
 
767
 
 
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)
 
774
 
 
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)