22
import bzrlib.errors as errors
21
23
from bzrlib.branch import Branch
22
24
from bzrlib.bzrdir import BzrDir
23
from bzrlib.tests import TestCaseInTempDir
25
class TestBreakLock(TestCaseInTempDir):
25
from bzrlib.tests.blackbox import ExternalBase
28
class TestBreakLock(ExternalBase):
30
# General principal for break-lock: All the elements that might be locked
31
# by a bzr operation on PATH, are candidates that break-lock may unlock.
32
# so pathologically if we have a lightweight checkout A, of branch B, which
33
# is bound to location C, the following things should be checked for locks
37
# wt.branch.repository
38
# wt.branch.get_master_branch()
39
# wt.branch.get_master_branch().repository
40
# so for smoke tests all we need is a bound branch with a checkout of that
41
# and we can then use different urls to test individual cases, for as much
42
# granularity as needed.
45
super(TestBreakLock, self).setUp()
48
'master-repo/master-branch/',
52
bzrlib.bzrdir.BzrDir.create('master-repo').create_repository()
53
self.master_branch = bzrlib.bzrdir.BzrDir.create_branch_convenience(
54
'master-repo/master-branch')
55
bzrlib.bzrdir.BzrDir.create('repo').create_repository()
56
local_branch = bzrlib.bzrdir.BzrDir.create_branch_convenience('repo/branch')
57
local_branch.bind(self.master_branch)
58
checkoutdir = bzrlib.bzrdir.BzrDir.create('checkout')
59
bzrlib.branch.BranchReferenceFormat().initialize(
60
checkoutdir, local_branch)
61
self.wt = checkoutdir.create_workingtree()
26
63
def test_break_lock_help(self):
27
self.run_bzr('break-lock', '--help')
30
def test_show_no_lock(self):
31
wt = BzrDir.create_standalone_workingtree('.')
32
# out, err = self.run_bzr('break-lock', '--show', '.', retcode=3)
33
# shouldn't see any information
34
# self.assertContainsRe(err, 'not locked')
64
out, err = self.run_bzr('break-lock', '--help')
65
# shouldn't fail and should not produce error output
66
self.assertEqual('', err)
68
def test_break_lock_everything_locked(self):
69
### if everything is locked, we should be able to unlock the lot.
73
self.master_branch.lock_write()
75
# we need 5 yes's - wt, branch, repo, bound branch, bound repo.
76
self.run_bzr('break-lock', 'checkout', stdin="y\ny\ny\ny\ny\n")
77
# a new tree instance should be lockable
78
wt = bzrlib.workingtree.WorkingTree.open('checkout')
81
# and a new instance of the master branch
82
mb = wt.branch.get_master_branch()
85
self.assertRaises(errors.LockBroken, self.wt.unlock)
86
self.assertRaises(errors.LockBroken, self.master_branch.unlock)
88
def test_saying_no_leaves_it_locked(self):
89
### if 'no' is answered, objects should remain locked.
91
self.master_branch.lock_write()
93
# we need 5 yes's - wt, branch, repo, bound branch, bound repo.
94
self.run_bzr('break-lock', 'checkout', stdin="n\nn\nn\nn\nn\n")
95
# a new tree instance should not be lockable
96
wt = bzrlib.workingtree.WorkingTree.open('checkout')
97
self.assertRaises(errors.LockContention, wt.lock_write)
98
# and a new instance of the master branch
99
mb = wt.branch.get_master_branch()
100
self.assertRaises(errors.LockContention, mb.lock_write)
101
# unlock our branches normally.
103
self.master_branch.unlock()
106
class TestBreakLockOldBranch(ExternalBase):
108
def test_break_lock_format_5_bzrdir(self):
109
# break lock on a format 5 bzrdir should just return
110
self.make_branch_and_tree('foo', format=bzrlib.bzrdir.BzrDirFormat5())
111
out, err = self.run_bzr('break-lock', 'foo')
112
self.assertEqual('', out)
113
self.assertEqual('', err)