1
# Copyright (C) 2006 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from cStringIO import StringIO
22
import bzrlib.errors as errors
23
from bzrlib.tests import TestNotApplicable
24
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
27
class TestBreakLock(TestCaseWithWorkingTree):
30
super(TestBreakLock, self).setUp()
31
self.unused_workingtree = self.make_branch_and_tree('.')
32
self.workingtree = self.unused_workingtree.bzrdir.open_workingtree()
33
# we want a UI factory that accepts canned input for the tests:
34
# while SilentUIFactory still accepts stdin, we need to customise
36
self.old_factory = bzrlib.ui.ui_factory
37
self.addCleanup(self.restoreFactory)
38
bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
40
def restoreFactory(self):
41
bzrlib.ui.ui_factory = self.old_factory
43
def test_unlocked(self):
44
# break lock when nothing is locked should just return
46
self.workingtree.break_lock()
47
except NotImplementedError:
50
def test_unlocked_repo_locked(self):
51
# break lock on the workingtree should try on the branch even
52
# if the workingtree isn't locked - and the easiest way
53
# to see if that happened is to lock the repo.
54
self.workingtree.branch.repository.lock_write()
55
stdin = StringIO("y\n")
56
bzrlib.ui.ui_factory.stdin = stdin
58
self.unused_workingtree.break_lock()
59
except NotImplementedError:
60
# workingtree does not support break_lock
61
self.workingtree.branch.repository.unlock()
63
remaining = stdin.read()
64
if remaining == 'y\n':
65
raise TestNotApplicable("repository does not physically lock.")
66
self.assertRaises(errors.LockBroken,
67
self.workingtree.branch.repository.unlock)
69
def test_locked(self):
70
# break_lock when locked should
71
self.workingtree.lock_write()
72
bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\n")
74
self.unused_workingtree.break_lock()
75
except (NotImplementedError, errors.LockActive):
76
# workingtree does not support break_lock,
77
# or does not support breaking a lock held by an alive
79
self.workingtree.unlock()
81
self.assertRaises(errors.LockBroken, self.workingtree.unlock)