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