~bzr-pqm/bzr/bzr.dev

4634.123.7 by John Arbash Meinel
Add direct tests that 'bzr switch' can handle root-id changes.
1
# Copyright (C) 2007-2010 Canonical Ltd
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
2
#
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.
7
#
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.
12
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
16
17
"""Tests for bzrlib.switch."""
18
19
20
import os
21
5162.3.1 by Aaron Bentley
Fix switch/merge when a ConfigurableFileMerger is used.
22
from bzrlib import (
23
    branch,
24
    errors,
25
    merge as _mod_merge,
26
    switch,
27
    tests,
28
)
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
29
30
31
class TestSwitch(tests.TestCaseWithTransport):
32
3078.2.2 by Ian Clatworthy
get switch tests passing on heavyweight checkouts
33
    def setUp(self):
34
        super(TestSwitch, self).setUp()
35
        self.lightweight = True
36
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
37
    def _setup_tree(self):
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
38
        tree = self.make_branch_and_tree('branch-1')
39
        self.build_tree(['branch-1/file-1'])
40
        tree.add('file-1')
41
        tree.commit('rev1')
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
42
        return tree
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
43
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
44
    def test_switch_updates(self):
45
        """Test switch updates tree and keeps uncommitted changes."""
46
        tree = self._setup_tree()
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
47
        to_branch = tree.bzrdir.sprout('branch-2').open_branch()
48
        self.build_tree(['branch-1/file-2'])
49
        tree.add('file-2')
50
        tree.remove('file-1')
51
        tree.commit('rev2')
3078.2.2 by Ian Clatworthy
get switch tests passing on heavyweight checkouts
52
        checkout = tree.branch.create_checkout('checkout',
53
            lightweight=self.lightweight)
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
54
        self.build_tree(['checkout/file-3'])
55
        checkout.add('file-3')
56
        self.failIfExists('checkout/file-1')
57
        self.failUnlessExists('checkout/file-2')
58
        switch.switch(checkout.bzrdir, to_branch)
59
        self.failUnlessExists('checkout/file-1')
60
        self.failIfExists('checkout/file-2')
61
        self.failUnlessExists('checkout/file-3')
62
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
63
    def test_switch_after_branch_moved(self):
64
        """Test switch after the branch is moved."""
65
        tree = self._setup_tree()
3078.2.2 by Ian Clatworthy
get switch tests passing on heavyweight checkouts
66
        checkout = tree.branch.create_checkout('checkout',
67
            lightweight=self.lightweight)
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
68
        self.build_tree(['branch-1/file-2'])
69
        tree.add('file-2')
70
        tree.remove('file-1')
71
        tree.commit('rev2')
3044.1.4 by Martin Pool
Set default format to pack-0.92
72
        self.build_tree(['checkout/file-3'])
73
        checkout.add('file-3')
74
        checkout_dir = checkout.bzrdir
75
        # rename the branch on disk, the checkout object is now invalid.
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
76
        os.rename('branch-1', 'branch-2')
77
        to_branch = branch.Branch.open('branch-2')
3078.2.5 by Ian Clatworthy
make switch fail without --force if branch missing
78
        # Check fails without --force
4340.1.1 by Jelmer Vernooij
Mention --force when bzr switch fails to open the current master branch.
79
        err = self.assertRaises(
80
            (errors.BzrCommandError, errors.NotBranchError),
3078.2.5 by Ian Clatworthy
make switch fail without --force if branch missing
81
            switch.switch, checkout.bzrdir, to_branch)
4340.1.1 by Jelmer Vernooij
Mention --force when bzr switch fails to open the current master branch.
82
        if isinstance(err, errors.BzrCommandError):
83
            self.assertContainsRe(str(err),
84
                'Unable to connect to current master branch .*'
85
                'To switch anyway, use --force.')
3078.2.5 by Ian Clatworthy
make switch fail without --force if branch missing
86
        switch.switch(checkout.bzrdir, to_branch, force=True)
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
87
        self.failIfExists('checkout/file-1')
88
        self.failUnlessExists('checkout/file-2')
89
        self.failUnlessExists('checkout/file-3')
90
91
    def test_switch_when_pending_merges(self):
92
        """Test graceful failure if pending merges are outstanding."""
93
        # Create 2 branches and a checkout
94
        tree = self._setup_tree()
95
        tree2 = tree.bzrdir.sprout('branch-2').open_workingtree()
3078.2.2 by Ian Clatworthy
get switch tests passing on heavyweight checkouts
96
        checkout = tree.branch.create_checkout('checkout',
97
            lightweight=self.lightweight)
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
98
        # Change tree2 and merge it into the checkout without committing
99
        self.build_tree(['branch-2/file-2'])
100
        tree2.add('file-2')
101
        tree2.commit('rev2')
102
        checkout.merge_from_branch(tree2.branch)
103
        # Check the error reporting is as expected
104
        err = self.assertRaises(errors.BzrCommandError,
105
            switch.switch, checkout.bzrdir, tree2.branch)
106
        self.assertContainsRe(str(err),
107
            "Pending merges must be committed or reverted before using switch")
3078.2.2 by Ian Clatworthy
get switch tests passing on heavyweight checkouts
108
3984.5.1 by Daniel Watkins
Added whitebox test.
109
    def test_switch_with_revision(self):
110
        """Test switch when a revision is given."""
111
        # Create a tree with 2 revisions
3984.5.3 by Daniel Watkins
Updated testcase.
112
        tree = self.make_branch_and_tree('branch-1')
113
        self.build_tree(['branch-1/file-1'])
114
        tree.add('file-1')
115
        tree.commit(rev_id='rev1', message='rev1')
3984.5.1 by Daniel Watkins
Added whitebox test.
116
        self.build_tree(['branch-1/file-2'])
117
        tree.add('file-2')
3984.5.3 by Daniel Watkins
Updated testcase.
118
        tree.commit(rev_id='rev2', message='rev2')
3984.5.1 by Daniel Watkins
Added whitebox test.
119
        # Check it out and switch to revision 1
120
        checkout = tree.branch.create_checkout('checkout',
3984.5.21 by Andrew Bennetts
Merge lp:bzr.
121
            lightweight=self.lightweight)
3984.5.12 by Daniel Watkins
Convert test back to taking a revision_id.
122
        switch.switch(checkout.bzrdir, tree.branch, revision_id="rev1")
3984.5.1 by Daniel Watkins
Added whitebox test.
123
        self.failUnlessExists('checkout/file-1')
124
        self.failIfExists('checkout/file-2')
125
4634.123.7 by John Arbash Meinel
Add direct tests that 'bzr switch' can handle root-id changes.
126
    def test_switch_changing_root_id(self):
127
        tree = self._setup_tree()
128
        tree2 = self.make_branch_and_tree('tree-2')
129
        tree2.set_root_id('custom-root-id')
130
        self.build_tree(['tree-2/file-2'])
131
        tree2.add(['file-2'])
132
        tree2.commit('rev1b')
133
        checkout = tree.branch.create_checkout('checkout',
134
            lightweight=self.lightweight)
135
        switch.switch(checkout.bzrdir, tree2.branch)
136
        self.assertEqual('custom-root-id', tree2.get_root_id())
137
5162.3.1 by Aaron Bentley
Fix switch/merge when a ConfigurableFileMerger is used.
138
    def test_switch_configurable_file_merger(self):
139
        class DummyMerger(_mod_merge.ConfigurableFileMerger):
140
            name_prefix = 'file'
141
142
        _mod_merge.Merger.hooks.install_named_hook(
143
            'merge_file_content', DummyMerger,
144
            'test factory')
145
        foo = self.make_branch('foo')
146
        checkout = foo.create_checkout('checkout', lightweight=True)
147
        self.build_tree_contents([('checkout/file', 'a')])
148
        checkout.add('file')
149
        checkout.commit('a')
150
        bar = foo.bzrdir.sprout('bar').open_workingtree()
151
        self.build_tree_contents([('bar/file', 'b')])
152
        bar.commit('b')
153
        self.build_tree_contents([('checkout/file', 'c')])
154
        switch.switch(checkout.bzrdir, bar.branch)
155
3078.2.2 by Ian Clatworthy
get switch tests passing on heavyweight checkouts
156
157
class TestSwitchHeavyweight(TestSwitch):
158
159
    def setUp(self):
160
        super(TestSwitchHeavyweight, self).setUp()
161
        self.lightweight = False
3078.2.4 by Ian Clatworthy
Add test for local commits handling
162
163
    def test_switch_with_local_commits(self):
164
        """Test switch complains about local commits unless --force given."""
165
        tree = self._setup_tree()
166
        to_branch = tree.bzrdir.sprout('branch-2').open_branch()
167
        self.build_tree(['branch-1/file-2'])
168
        tree.add('file-2')
169
        tree.remove('file-1')
170
        tree.commit('rev2')
171
        checkout = tree.branch.create_checkout('checkout')
172
        self.build_tree(['checkout/file-3'])
173
        checkout.add('file-3')
174
        checkout.commit(message='local only commit', local=True)
175
        self.build_tree(['checkout/file-4'])
176
        # Check the error reporting is as expected
177
        err = self.assertRaises(errors.BzrCommandError,
178
            switch.switch, checkout.bzrdir, to_branch)
179
        self.assertContainsRe(str(err),
180
            'Cannot switch as local commits found in the checkout.')
181
        # Check all is ok when force is given
182
        self.failIfExists('checkout/file-1')
183
        self.failUnlessExists('checkout/file-2')
184
        switch.switch(checkout.bzrdir, to_branch, force=True)
185
        self.failUnlessExists('checkout/file-1')
186
        self.failIfExists('checkout/file-2')
187
        self.failIfExists('checkout/file-3')
188
        self.failUnlessExists('checkout/file-4')
189
        # Check that the checkout is a true mirror of the bound branch
3445.2.1 by John Arbash Meinel
Add tests for Branch.missing_revisions and deprecate it.
190
        self.assertEqual(to_branch.last_revision_info(),
191
                         checkout.branch.last_revision_info())