1116
1117
self.failUnless(len(text))
1120
class TestBreakLock(TestCaseWithBzrDir):
1123
super(TestBreakLock, self).setUp()
1124
# we want a UI factory that accepts canned input for the tests:
1125
# while SilentUIFactory still accepts stdin, we need to customise
1127
self.old_factory = bzrlib.ui.ui_factory
1128
bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
1131
bzrlib.ui.ui_factory = self.old_factory
1133
def test_break_lock_empty(self):
1134
# break lock on an empty bzrdir should work silently.
1135
dir = self.make_bzrdir('.')
1138
except NotImplementedError:
1141
def test_break_lock_repository(self):
1142
# break lock with just a repo should unlock the repo.
1143
repo = self.make_repository('.')
1145
# only one yes needed here: it should only be unlocking
1147
bzrlib.ui.ui_factory.stdin = StringIO("y\n")
1149
repo.bzrdir.break_lock()
1150
except NotImplementedError:
1151
# this bzrdir does not implement break_lock - so we cant test it.
1154
lock_repo = repo.bzrdir.open_repository()
1155
lock_repo.lock_write()
1157
self.assertRaises(errors.LockBroken, repo.unlock)
1159
def test_break_lock_branch(self):
1160
# break lock with just a repo should unlock the branch.
1161
# and not directly try the repository.
1162
# we test this by making a branch reference to a branch
1163
# and repository in another bzrdir
1164
# for pre-metadir formats this will fail, thats ok.
1165
master = self.make_branch('branch')
1166
thisdir = self.make_bzrdir('this')
1168
bzrlib.branch.BranchReferenceFormat().initialize(
1170
except errors.IncompatibleFormat:
1172
unused_repo = thisdir.create_repository()
1174
unused_repo.lock_write()
1175
# two yes's : branch and repository. If the repo in this
1176
# dir is inappropriately accessed, 3 will be needed, and
1177
# we'll see that because the stream will be fully consumed
1178
bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\n")
1179
master.bzrdir.break_lock()
1180
# only two ys should have been read
1181
self.assertEqual("y\n", bzrlib.ui.ui_factory.stdin.read())
1182
# we should be able to lock a newly opened branch now
1183
branch = master.bzrdir.open_branch()
1186
# we should not be able to lock the repository in thisdir as its still
1187
# held by the explicit lock we took, and the break lock should not have
1189
repo = thisdir.open_repository()
1190
self.assertRaises(errors.LockContention, repo.lock_write)
1191
unused_repo.unlock()
1192
self.assertRaises(errors.LockBroken, master.unlock)
1194
def test_break_lock_tree(self):
1195
# break lock with a tree should unlock the tree but not try the
1196
# branch explicitly. However this is very hard to test for as we
1197
# dont have a tree reference class, nor is one needed;
1198
# the worst case if this code unlocks twice is an extra question
1200
tree = self.make_branch_and_tree('.')
1202
# three yes's : tree, branch and repository.
1203
bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\ny\n")
1205
tree.bzrdir.break_lock()
1206
except NotImplementedError:
1207
# bzrdir does not support break_lock
1210
self.assertEqual("y\n", bzrlib.ui.ui_factory.stdin.read())
1211
lock_tree = tree.bzrdir.open_workingtree()
1212
lock_tree.lock_write()
1214
self.assertRaises(errors.LockBroken, tree.unlock)
1119
1217
class ChrootedBzrDirTests(ChrootedTestCase):
1121
1219
def test_find_repository_no_repository(self):