~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/bzrdir_implementations/test_bzrdir.py

  • Committer: Robert Collins
  • Date: 2006-05-04 13:55:32 UTC
  • mto: (1697.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 1701.
  • Revision ID: robertc@robertcollins.net-20060504135532-f0152b27530fd30c
Hook in the full break-lock ui.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for bzrdir implementations - tests a bzrdir format."""
18
18
 
 
19
from cStringIO import StringIO
19
20
import os
20
21
from stat import *
21
22
import sys
1116
1117
        self.failUnless(len(text))
1117
1118
 
1118
1119
 
 
1120
class TestBreakLock(TestCaseWithBzrDir):
 
1121
 
 
1122
    def setUp(self):
 
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
 
1126
        # ours
 
1127
        self.old_factory = bzrlib.ui.ui_factory
 
1128
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
 
1129
 
 
1130
    def tearDown(self):
 
1131
        bzrlib.ui.ui_factory = self.old_factory
 
1132
 
 
1133
    def test_break_lock_empty(self):
 
1134
        # break lock on an empty bzrdir should work silently.
 
1135
        dir = self.make_bzrdir('.')
 
1136
        try:
 
1137
            dir.break_lock()
 
1138
        except NotImplementedError:
 
1139
            pass
 
1140
 
 
1141
    def test_break_lock_repository(self):
 
1142
        # break lock with just a repo should unlock the repo.
 
1143
        repo = self.make_repository('.')
 
1144
        repo.lock_write()
 
1145
        # only one yes needed here: it should only be unlocking
 
1146
        # the repo
 
1147
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
 
1148
        try:
 
1149
            repo.bzrdir.break_lock()
 
1150
        except NotImplementedError:
 
1151
            # this bzrdir does not implement break_lock - so we cant test it.
 
1152
            repo.unlock()
 
1153
            return
 
1154
        lock_repo = repo.bzrdir.open_repository()
 
1155
        lock_repo.lock_write()
 
1156
        lock_repo.unlock()
 
1157
        self.assertRaises(errors.LockBroken, repo.unlock)
 
1158
 
 
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')
 
1167
        try:
 
1168
            bzrlib.branch.BranchReferenceFormat().initialize(
 
1169
                thisdir, master)
 
1170
        except errors.IncompatibleFormat:
 
1171
            return
 
1172
        unused_repo = thisdir.create_repository()
 
1173
        master.lock_write()
 
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()
 
1184
        branch.lock_write()
 
1185
        branch.unlock()
 
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
 
1188
        # touched it.
 
1189
        repo = thisdir.open_repository()
 
1190
        self.assertRaises(errors.LockContention, repo.lock_write)
 
1191
        unused_repo.unlock()
 
1192
        self.assertRaises(errors.LockBroken, master.unlock)
 
1193
 
 
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
 
1199
        # being asked.
 
1200
        tree = self.make_branch_and_tree('.')
 
1201
        tree.lock_write()
 
1202
        # three yes's : tree, branch and repository.
 
1203
        bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\ny\n")
 
1204
        try:
 
1205
            tree.bzrdir.break_lock()
 
1206
        except NotImplementedError:
 
1207
            # bzrdir does not support break_lock
 
1208
            tree.unlock()
 
1209
            return
 
1210
        self.assertEqual("y\n", bzrlib.ui.ui_factory.stdin.read())
 
1211
        lock_tree = tree.bzrdir.open_workingtree()
 
1212
        lock_tree.lock_write()
 
1213
        lock_tree.unlock()
 
1214
        self.assertRaises(errors.LockBroken, tree.unlock)
 
1215
 
 
1216
 
1119
1217
class ChrootedBzrDirTests(ChrootedTestCase):
1120
1218
 
1121
1219
    def test_find_repository_no_repository(self):