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
self.addCleanup(self.restoreFactory)
1129
bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
1131
def restoreFactory(self):
1132
bzrlib.ui.ui_factory = self.old_factory
1134
def test_break_lock_empty(self):
1135
# break lock on an empty bzrdir should work silently.
1136
dir = self.make_bzrdir('.')
1139
except NotImplementedError:
1142
def test_break_lock_repository(self):
1143
# break lock with just a repo should unlock the repo.
1144
repo = self.make_repository('.')
1146
# only one yes needed here: it should only be unlocking
1148
bzrlib.ui.ui_factory.stdin = StringIO("y\n")
1150
repo.bzrdir.break_lock()
1151
except NotImplementedError:
1152
# this bzrdir does not implement break_lock - so we cant test it.
1155
lock_repo = repo.bzrdir.open_repository()
1156
lock_repo.lock_write()
1158
self.assertRaises(errors.LockBroken, repo.unlock)
1160
def test_break_lock_branch(self):
1161
# break lock with just a repo should unlock the branch.
1162
# and not directly try the repository.
1163
# we test this by making a branch reference to a branch
1164
# and repository in another bzrdir
1165
# for pre-metadir formats this will fail, thats ok.
1166
master = self.make_branch('branch')
1167
thisdir = self.make_bzrdir('this')
1169
bzrlib.branch.BranchReferenceFormat().initialize(
1171
except errors.IncompatibleFormat:
1173
unused_repo = thisdir.create_repository()
1175
unused_repo.lock_write()
1176
# two yes's : branch and repository. If the repo in this
1177
# dir is inappropriately accessed, 3 will be needed, and
1178
# we'll see that because the stream will be fully consumed
1179
bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\n")
1180
master.bzrdir.break_lock()
1181
# only two ys should have been read
1182
self.assertEqual("y\n", bzrlib.ui.ui_factory.stdin.read())
1183
# we should be able to lock a newly opened branch now
1184
branch = master.bzrdir.open_branch()
1187
# we should not be able to lock the repository in thisdir as its still
1188
# held by the explicit lock we took, and the break lock should not have
1190
repo = thisdir.open_repository()
1191
self.assertRaises(errors.LockContention, repo.lock_write)
1192
unused_repo.unlock()
1193
self.assertRaises(errors.LockBroken, master.unlock)
1195
def test_break_lock_tree(self):
1196
# break lock with a tree should unlock the tree but not try the
1197
# branch explicitly. However this is very hard to test for as we
1198
# dont have a tree reference class, nor is one needed;
1199
# the worst case if this code unlocks twice is an extra question
1201
tree = self.make_branch_and_tree('.')
1203
# three yes's : tree, branch and repository.
1204
bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\ny\n")
1206
tree.bzrdir.break_lock()
1207
except NotImplementedError:
1208
# bzrdir does not support break_lock
1211
self.assertEqual("y\n", bzrlib.ui.ui_factory.stdin.read())
1212
lock_tree = tree.bzrdir.open_workingtree()
1213
lock_tree.lock_write()
1215
self.assertRaises(errors.LockBroken, tree.unlock)
1119
1218
class ChrootedBzrDirTests(ChrootedTestCase):
1121
1220
def test_find_repository_no_repository(self):