1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for branch break-lock behaviour."""
19
from cStringIO import StringIO
22
import bzrlib.errors as errors
23
from bzrlib.tests import TestCase, TestCaseWithTransport, TestNotApplicable
24
from bzrlib.tests.branch_implementations.test_branch import TestCaseWithBranch
27
class TestBreakLock(TestCaseWithBranch):
30
super(TestBreakLock, self).setUp()
31
self.unused_branch = self.make_branch('branch')
32
self.branch = self.unused_branch.bzrdir.open_branch()
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.branch.break_lock()
47
except NotImplementedError:
50
def test_unlocked_repo_locked(self):
51
# break lock on the branch should try on the repository even
52
# if the branch isn't locked
53
token = self.branch.repository.lock_write()
55
self.branch.repository.unlock()
56
raise TestNotApplicable('Repository does not use physical locks.')
57
self.branch.repository.leave_lock_in_place()
58
self.branch.repository.unlock()
59
other_instance = self.branch.repository.bzrdir.open_repository()
60
if not other_instance.get_physical_lock_status():
61
raise TestNotApplicable("Repository does not lock persistently.")
62
bzrlib.ui.ui_factory.stdin = StringIO("y\n")
64
self.unused_branch.break_lock()
65
except NotImplementedError:
66
# branch does not support break_lock
67
self.branch.repository.unlock()
69
self.assertRaises(errors.LockBroken, self.branch.repository.unlock)
71
def test_locked(self):
72
# break_lock when locked should unlock the branch and repo
73
self.branch.lock_write()
74
bzrlib.ui.ui_factory.stdin = StringIO("y\ny\n")
76
self.unused_branch.break_lock()
77
except NotImplementedError:
78
# branch does not support break_lock
81
self.assertRaises(errors.LockBroken, self.branch.unlock)
83
def test_unlocks_master_branch(self):
84
# break_lock when when the master branch is locked should offer to
86
master = self.make_branch('master')
88
self.branch.bind(master)
89
except errors.UpgradeRequired:
90
# this branch does not support binding.
93
bzrlib.ui.ui_factory.stdin = StringIO("y\ny\n")
95
self.unused_branch.break_lock()
96
except NotImplementedError:
97
# branch does not support break_lock
100
self.assertRaises(errors.LockBroken, master.unlock)
101
# can we lock it now ?