~bzr-pqm/bzr/bzr.dev

1553.5.34 by Martin Pool
Stub lock-breaking command
1
# Copyright (C) 2006 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1553.5.34 by Martin Pool
Stub lock-breaking command
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1553.5.34 by Martin Pool
Stub lock-breaking command
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1553.5.34 by Martin Pool
Stub lock-breaking command
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
16
17
"""Tests for lock-breaking user interface"""
18
19
import os
20
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
21
import bzrlib
22
import bzrlib.errors as errors
1553.5.34 by Martin Pool
Stub lock-breaking command
23
from bzrlib.branch import Branch
1553.5.35 by Martin Pool
Start break-lock --show
24
from bzrlib.bzrdir import BzrDir
1687.1.1 by Robert Collins
Document what the break-lock commands should be like
25
from bzrlib.tests.blackbox import ExternalBase
26
27
28
class TestBreakLock(ExternalBase):
29
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
34
    # to break:
35
    # wt = WorkingTree(A)
36
    # wt.branch
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.
43
44
    def setUp(self):
45
        super(TestBreakLock, self).setUp()
46
        self.build_tree(
47
            ['master-repo/',
48
             'master-repo/master-branch/',
49
             'repo/',
50
             'repo/branch/',
51
             'checkout/'])
52
        bzrlib.bzrdir.BzrDir.create('master-repo').create_repository()
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
53
        self.master_branch = bzrlib.bzrdir.BzrDir.create_branch_convenience(
1687.1.1 by Robert Collins
Document what the break-lock commands should be like
54
            'master-repo/master-branch')
55
        bzrlib.bzrdir.BzrDir.create('repo').create_repository()
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
56
        local_branch = bzrlib.bzrdir.BzrDir.create_branch_convenience('repo/branch')
1687.1.1 by Robert Collins
Document what the break-lock commands should be like
57
        local_branch.bind(self.master_branch)
58
        checkoutdir = bzrlib.bzrdir.BzrDir.create('checkout')
59
        bzrlib.branch.BranchReferenceFormat().initialize(
60
            checkoutdir, local_branch)
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
61
        self.wt = checkoutdir.create_workingtree()
1687.1.1 by Robert Collins
Document what the break-lock commands should be like
62
1553.5.34 by Martin Pool
Stub lock-breaking command
63
    def test_break_lock_help(self):
1687.1.1 by Robert Collins
Document what the break-lock commands should be like
64
        out, err = self.run_bzr('break-lock', '--help')
65
        # shouldn't fail and should not produce error output
66
        self.assertEqual('', err)
67
68
    def test_break_lock_everything_locked(self):
69
        ### if everything is locked, we should be able to unlock the lot.
70
        # sketch of test:
71
        # lock the lot:
72
        self.wt.lock_write()
73
        self.master_branch.lock_write()
74
        # run the break-lock
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
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')
79
        wt.lock_write()
80
        wt.unlock()
81
        # and a new instance of the master branch 
82
        mb = wt.branch.get_master_branch()
83
        mb.lock_write()
84
        mb.unlock()
85
        self.assertRaises(errors.LockBroken, self.wt.unlock)
86
        self.assertRaises(errors.LockBroken, self.master_branch.unlock)
1687.1.16 by Robert Collins
Ensure that answering no to break lock leaves the locks in place. This is currently simple - ask for everything always.
87
88
    def test_saying_no_leaves_it_locked(self):
89
        ### if 'no' is answered, objects should remain locked.
90
        self.wt.lock_write()
91
        self.master_branch.lock_write()
92
        # run the break-lock
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.
102
        self.wt.unlock()
103
        self.master_branch.unlock()
1687.1.17 by Robert Collins
Test break lock on old format branches.
104
105
106
class TestBreakLockOldBranch(ExternalBase):
107
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)