~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_switch.py

  • Committer: Aaron Bentley
  • Date: 2008-11-23 16:38:39 UTC
  • mto: This revision was merged to the branch mainline in revision 3892.
  • Revision ID: aaron@aaronbentley.com-20081123163839-ew27m17130fz85v6
Update documentation

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for bzrlib.switch."""
18
18
 
19
19
 
20
20
import os
21
21
 
22
 
from bzrlib import (
23
 
    branch,
24
 
    errors,
25
 
    lock,
26
 
    merge as _mod_merge,
27
 
    switch,
28
 
    tests,
29
 
    workingtree,
30
 
    )
 
22
from bzrlib import branch, errors, switch, tests
31
23
 
32
24
 
33
25
class TestSwitch(tests.TestCaseWithTransport):
36
28
        super(TestSwitch, self).setUp()
37
29
        self.lightweight = True
38
30
 
39
 
    @staticmethod
40
 
    def _master_if_present(branch):
41
 
        master = branch.get_master_branch()
42
 
        if master:
43
 
            return master
44
 
        else:
45
 
            return branch
46
 
 
47
31
    def _setup_tree(self):
48
32
        tree = self.make_branch_and_tree('branch-1')
49
33
        self.build_tree(['branch-1/file-1'])
51
35
        tree.commit('rev1')
52
36
        return tree
53
37
 
54
 
    def _setup_uncommitted(self, same_revision=False):
55
 
        tree = self._setup_tree()
56
 
        to_branch = tree.bzrdir.sprout('branch-2').open_branch()
57
 
        self.build_tree(['branch-1/file-2'])
58
 
        if not same_revision:
59
 
            tree.add('file-2')
60
 
            tree.remove('file-1')
61
 
            tree.commit('rev2')
62
 
        checkout = tree.branch.create_checkout('checkout',
63
 
            lightweight=self.lightweight)
64
 
        self.build_tree(['checkout/file-3'])
65
 
        checkout.add('file-3')
66
 
        return checkout, to_branch
67
 
 
68
 
    def test_switch_store_uncommitted(self):
69
 
        """Test switch updates tree and stores uncommitted changes."""
70
 
        checkout, to_branch = self._setup_uncommitted()
71
 
        self.assertPathDoesNotExist('checkout/file-1')
72
 
        self.assertPathExists('checkout/file-2')
73
 
        switch.switch(checkout.bzrdir, to_branch, store_uncommitted=True)
74
 
        self.assertPathExists('checkout/file-1')
75
 
        self.assertPathDoesNotExist('checkout/file-2')
76
 
        self.assertPathDoesNotExist('checkout/file-3')
77
 
 
78
 
    def test_switch_restore_uncommitted(self):
79
 
        """Test switch updates tree and restores uncommitted changes."""
80
 
        checkout, to_branch = self._setup_uncommitted()
81
 
        old_branch = self._master_if_present(checkout.branch)
82
 
        self.assertPathDoesNotExist('checkout/file-1')
83
 
        self.assertPathExists('checkout/file-2')
84
 
        self.assertPathExists('checkout/file-3')
85
 
        switch.switch(checkout.bzrdir, to_branch, store_uncommitted=True)
86
 
        checkout = workingtree.WorkingTree.open('checkout')
87
 
        switch.switch(checkout.bzrdir, old_branch, store_uncommitted=True)
88
 
        self.assertPathDoesNotExist('checkout/file-1')
89
 
        self.assertPathExists('checkout/file-2')
90
 
        self.assertPathExists('checkout/file-3')
91
 
 
92
 
    def test_switch_restore_uncommitted_same_revision(self):
93
 
        """Test switch updates tree and restores uncommitted changes."""
94
 
        checkout, to_branch = self._setup_uncommitted(same_revision=True)
95
 
        old_branch = self._master_if_present(checkout.branch)
96
 
        switch.switch(checkout.bzrdir, to_branch, store_uncommitted=True)
97
 
        checkout = workingtree.WorkingTree.open('checkout')
98
 
        switch.switch(checkout.bzrdir, old_branch, store_uncommitted=True)
99
 
        self.assertPathExists('checkout/file-3')
100
 
 
101
38
    def test_switch_updates(self):
102
39
        """Test switch updates tree and keeps uncommitted changes."""
103
 
        checkout, to_branch = self._setup_uncommitted()
104
 
        self.assertPathDoesNotExist('checkout/file-1')
105
 
        self.assertPathExists('checkout/file-2')
 
40
        tree = self._setup_tree()
 
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')
 
46
        checkout = tree.branch.create_checkout('checkout',
 
47
            lightweight=self.lightweight)
 
48
        self.build_tree(['checkout/file-3'])
 
49
        checkout.add('file-3')
 
50
        self.failIfExists('checkout/file-1')
 
51
        self.failUnlessExists('checkout/file-2')
106
52
        switch.switch(checkout.bzrdir, to_branch)
107
 
        self.assertPathExists('checkout/file-1')
108
 
        self.assertPathDoesNotExist('checkout/file-2')
109
 
        self.assertPathExists('checkout/file-3')
 
53
        self.failUnlessExists('checkout/file-1')
 
54
        self.failIfExists('checkout/file-2')
 
55
        self.failUnlessExists('checkout/file-3')
110
56
 
111
57
    def test_switch_after_branch_moved(self):
112
58
        """Test switch after the branch is moved."""
124
70
        os.rename('branch-1', 'branch-2')
125
71
        to_branch = branch.Branch.open('branch-2')
126
72
        # Check fails without --force
127
 
        err = self.assertRaises(
128
 
            (errors.BzrCommandError, errors.NotBranchError),
 
73
        err = self.assertRaises((errors.NotBranchError,
 
74
            errors.BoundBranchConnectionFailure),
129
75
            switch.switch, checkout.bzrdir, to_branch)
130
 
        if isinstance(err, errors.BzrCommandError):
131
 
            self.assertContainsRe(str(err),
132
 
                'Unable to connect to current master branch .*'
133
 
                'To switch anyway, use --force.')
134
76
        switch.switch(checkout.bzrdir, to_branch, force=True)
135
 
        self.assertPathDoesNotExist('checkout/file-1')
136
 
        self.assertPathExists('checkout/file-2')
137
 
        self.assertPathExists('checkout/file-3')
 
77
        self.failIfExists('checkout/file-1')
 
78
        self.failUnlessExists('checkout/file-2')
 
79
        self.failUnlessExists('checkout/file-3')
138
80
 
139
81
    def test_switch_when_pending_merges(self):
140
82
        """Test graceful failure if pending merges are outstanding."""
154
96
        self.assertContainsRe(str(err),
155
97
            "Pending merges must be committed or reverted before using switch")
156
98
 
157
 
    def test_switch_with_revision(self):
158
 
        """Test switch when a revision is given."""
159
 
        # Create a tree with 2 revisions
160
 
        tree = self.make_branch_and_tree('branch-1')
161
 
        self.build_tree(['branch-1/file-1'])
162
 
        tree.add('file-1')
163
 
        tree.commit(rev_id='rev1', message='rev1')
164
 
        self.build_tree(['branch-1/file-2'])
165
 
        tree.add('file-2')
166
 
        tree.commit(rev_id='rev2', message='rev2')
167
 
        # Check it out and switch to revision 1
168
 
        checkout = tree.branch.create_checkout('checkout',
169
 
            lightweight=self.lightweight)
170
 
        switch.switch(checkout.bzrdir, tree.branch, revision_id="rev1")
171
 
        self.assertPathExists('checkout/file-1')
172
 
        self.assertPathDoesNotExist('checkout/file-2')
173
 
 
174
 
    def test_switch_changing_root_id(self):
175
 
        tree = self._setup_tree()
176
 
        tree2 = self.make_branch_and_tree('tree-2')
177
 
        tree2.set_root_id('custom-root-id')
178
 
        self.build_tree(['tree-2/file-2'])
179
 
        tree2.add(['file-2'])
180
 
        tree2.commit('rev1b')
181
 
        checkout = tree.branch.create_checkout('checkout',
182
 
            lightweight=self.lightweight)
183
 
        switch.switch(checkout.bzrdir, tree2.branch)
184
 
        self.assertEqual('custom-root-id', tree2.get_root_id())
185
 
 
186
 
    def test_switch_configurable_file_merger(self):
187
 
        class DummyMerger(_mod_merge.ConfigurableFileMerger):
188
 
            name_prefix = 'file'
189
 
 
190
 
        _mod_merge.Merger.hooks.install_named_hook(
191
 
            'merge_file_content', DummyMerger,
192
 
            'test factory')
193
 
        foo = self.make_branch('foo')
194
 
        checkout = foo.create_checkout('checkout', lightweight=True)
195
 
        self.build_tree_contents([('checkout/file', 'a')])
196
 
        checkout.add('file')
197
 
        checkout.commit('a')
198
 
        bar = foo.bzrdir.sprout('bar').open_workingtree()
199
 
        self.build_tree_contents([('bar/file', 'b')])
200
 
        bar.commit('b')
201
 
        self.build_tree_contents([('checkout/file', 'c')])
202
 
        switch.switch(checkout.bzrdir, bar.branch)
203
 
 
204
99
 
205
100
class TestSwitchHeavyweight(TestSwitch):
206
101
 
227
122
        self.assertContainsRe(str(err),
228
123
            'Cannot switch as local commits found in the checkout.')
229
124
        # Check all is ok when force is given
230
 
        self.assertPathDoesNotExist('checkout/file-1')
231
 
        self.assertPathExists('checkout/file-2')
 
125
        self.failIfExists('checkout/file-1')
 
126
        self.failUnlessExists('checkout/file-2')
232
127
        switch.switch(checkout.bzrdir, to_branch, force=True)
233
 
        self.assertPathExists('checkout/file-1')
234
 
        self.assertPathDoesNotExist('checkout/file-2')
235
 
        self.assertPathDoesNotExist('checkout/file-3')
236
 
        self.assertPathExists('checkout/file-4')
 
128
        self.failUnlessExists('checkout/file-1')
 
129
        self.failIfExists('checkout/file-2')
 
130
        self.failIfExists('checkout/file-3')
 
131
        self.failUnlessExists('checkout/file-4')
237
132
        # Check that the checkout is a true mirror of the bound branch
238
133
        self.assertEqual(to_branch.last_revision_info(),
239
134
                         checkout.branch.last_revision_info())