/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_switch.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 2007-2010 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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Tests for breezy.switch."""
 
17
"""Tests for bzrlib.switch."""
18
18
 
19
19
 
20
20
import os
21
21
 
22
 
from breezy import (
 
22
from bzrlib import (
23
23
    branch,
24
24
    errors,
25
25
    merge as _mod_merge,
26
26
    switch,
27
27
    tests,
28
 
    workingtree,
29
 
    )
 
28
)
30
29
 
31
30
 
32
31
class TestSwitch(tests.TestCaseWithTransport):
35
34
        super(TestSwitch, self).setUp()
36
35
        self.lightweight = True
37
36
 
38
 
    @staticmethod
39
 
    def _master_if_present(branch):
40
 
        master = branch.get_master_branch()
41
 
        if master:
42
 
            return master
43
 
        else:
44
 
            return branch
45
 
 
46
37
    def _setup_tree(self):
47
38
        tree = self.make_branch_and_tree('branch-1')
48
39
        self.build_tree(['branch-1/file-1'])
50
41
        tree.commit('rev1')
51
42
        return tree
52
43
 
53
 
    def _setup_uncommitted(self, same_revision=False):
54
 
        tree = self._setup_tree()
55
 
        to_branch = tree.controldir.sprout('branch-2').open_branch()
56
 
        self.build_tree(['branch-1/file-2'])
57
 
        if not same_revision:
58
 
            tree.add('file-2')
59
 
            tree.remove('file-1')
60
 
            tree.commit('rev2')
61
 
        checkout = tree.branch.create_checkout('checkout',
62
 
                                               lightweight=self.lightweight)
63
 
        self.build_tree(['checkout/file-3'])
64
 
        checkout.add('file-3')
65
 
        return checkout, to_branch
66
 
 
67
 
    def test_switch_store_uncommitted(self):
68
 
        """Test switch updates tree and stores uncommitted changes."""
69
 
        checkout, to_branch = self._setup_uncommitted()
70
 
        self.assertPathDoesNotExist('checkout/file-1')
71
 
        self.assertPathExists('checkout/file-2')
72
 
        switch.switch(checkout.controldir, to_branch, store_uncommitted=True)
73
 
        self.assertPathExists('checkout/file-1')
74
 
        self.assertPathDoesNotExist('checkout/file-2')
75
 
        self.assertPathDoesNotExist('checkout/file-3')
76
 
 
77
 
    def test_switch_restore_uncommitted(self):
78
 
        """Test switch updates tree and restores uncommitted changes."""
79
 
        checkout, to_branch = self._setup_uncommitted()
80
 
        old_branch = self._master_if_present(checkout.branch)
81
 
        self.assertPathDoesNotExist('checkout/file-1')
82
 
        self.assertPathExists('checkout/file-2')
83
 
        self.assertPathExists('checkout/file-3')
84
 
        switch.switch(checkout.controldir, to_branch, store_uncommitted=True)
85
 
        checkout = workingtree.WorkingTree.open('checkout')
86
 
        switch.switch(checkout.controldir, old_branch, store_uncommitted=True)
87
 
        self.assertPathDoesNotExist('checkout/file-1')
88
 
        self.assertPathExists('checkout/file-2')
89
 
        self.assertPathExists('checkout/file-3')
90
 
 
91
 
    def test_switch_restore_uncommitted_same_revision(self):
92
 
        """Test switch updates tree and restores uncommitted changes."""
93
 
        checkout, to_branch = self._setup_uncommitted(same_revision=True)
94
 
        old_branch = self._master_if_present(checkout.branch)
95
 
        switch.switch(checkout.controldir, to_branch, store_uncommitted=True)
96
 
        checkout = workingtree.WorkingTree.open('checkout')
97
 
        switch.switch(checkout.controldir, old_branch, store_uncommitted=True)
98
 
        self.assertPathExists('checkout/file-3')
99
 
 
100
44
    def test_switch_updates(self):
101
45
        """Test switch updates tree and keeps uncommitted changes."""
102
 
        checkout, to_branch = self._setup_uncommitted()
103
 
        self.assertPathDoesNotExist('checkout/file-1')
104
 
        self.assertPathExists('checkout/file-2')
105
 
        switch.switch(checkout.controldir, to_branch)
106
 
        self.assertPathExists('checkout/file-1')
107
 
        self.assertPathDoesNotExist('checkout/file-2')
108
 
        self.assertPathExists('checkout/file-3')
 
46
        tree = self._setup_tree()
 
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')
 
52
        checkout = tree.branch.create_checkout('checkout',
 
53
            lightweight=self.lightweight)
 
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')
109
62
 
110
63
    def test_switch_after_branch_moved(self):
111
64
        """Test switch after the branch is moved."""
112
65
        tree = self._setup_tree()
113
66
        checkout = tree.branch.create_checkout('checkout',
114
 
                                               lightweight=self.lightweight)
 
67
            lightweight=self.lightweight)
115
68
        self.build_tree(['branch-1/file-2'])
116
69
        tree.add('file-2')
117
70
        tree.remove('file-1')
118
71
        tree.commit('rev2')
119
72
        self.build_tree(['checkout/file-3'])
120
73
        checkout.add('file-3')
 
74
        checkout_dir = checkout.bzrdir
121
75
        # rename the branch on disk, the checkout object is now invalid.
122
76
        os.rename('branch-1', 'branch-2')
123
77
        to_branch = branch.Branch.open('branch-2')
124
78
        # Check fails without --force
125
79
        err = self.assertRaises(
126
80
            (errors.BzrCommandError, errors.NotBranchError),
127
 
            switch.switch, checkout.controldir, to_branch)
 
81
            switch.switch, checkout.bzrdir, to_branch)
128
82
        if isinstance(err, errors.BzrCommandError):
129
83
            self.assertContainsRe(str(err),
130
 
                                  'Unable to connect to current master branch .*'
131
 
                                  'To switch anyway, use --force.')
132
 
        switch.switch(checkout.controldir, to_branch, force=True)
133
 
        self.assertPathDoesNotExist('checkout/file-1')
134
 
        self.assertPathExists('checkout/file-2')
135
 
        self.assertPathExists('checkout/file-3')
 
84
                'Unable to connect to current master branch .*'
 
85
                'To switch anyway, use --force.')
 
86
        switch.switch(checkout.bzrdir, to_branch, force=True)
 
87
        self.failIfExists('checkout/file-1')
 
88
        self.failUnlessExists('checkout/file-2')
 
89
        self.failUnlessExists('checkout/file-3')
136
90
 
137
91
    def test_switch_when_pending_merges(self):
138
92
        """Test graceful failure if pending merges are outstanding."""
139
93
        # Create 2 branches and a checkout
140
94
        tree = self._setup_tree()
141
 
        tree2 = tree.controldir.sprout('branch-2').open_workingtree()
 
95
        tree2 = tree.bzrdir.sprout('branch-2').open_workingtree()
142
96
        checkout = tree.branch.create_checkout('checkout',
143
 
                                               lightweight=self.lightweight)
 
97
            lightweight=self.lightweight)
144
98
        # Change tree2 and merge it into the checkout without committing
145
99
        self.build_tree(['branch-2/file-2'])
146
100
        tree2.add('file-2')
148
102
        checkout.merge_from_branch(tree2.branch)
149
103
        # Check the error reporting is as expected
150
104
        err = self.assertRaises(errors.BzrCommandError,
151
 
                                switch.switch, checkout.controldir, tree2.branch)
 
105
            switch.switch, checkout.bzrdir, tree2.branch)
152
106
        self.assertContainsRe(str(err),
153
 
                              "Pending merges must be committed or reverted before using switch")
 
107
            "Pending merges must be committed or reverted before using switch")
154
108
 
155
109
    def test_switch_with_revision(self):
156
110
        """Test switch when a revision is given."""
158
112
        tree = self.make_branch_and_tree('branch-1')
159
113
        self.build_tree(['branch-1/file-1'])
160
114
        tree.add('file-1')
161
 
        tree.commit(rev_id=b'rev1', message='rev1')
 
115
        tree.commit(rev_id='rev1', message='rev1')
162
116
        self.build_tree(['branch-1/file-2'])
163
117
        tree.add('file-2')
164
 
        tree.commit(rev_id=b'rev2', message='rev2')
 
118
        tree.commit(rev_id='rev2', message='rev2')
165
119
        # Check it out and switch to revision 1
166
120
        checkout = tree.branch.create_checkout('checkout',
167
 
                                               lightweight=self.lightweight)
168
 
        switch.switch(checkout.controldir, tree.branch, revision_id=b"rev1")
169
 
        self.assertPathExists('checkout/file-1')
170
 
        self.assertPathDoesNotExist('checkout/file-2')
 
121
            lightweight=self.lightweight)
 
122
        switch.switch(checkout.bzrdir, tree.branch, revision_id="rev1")
 
123
        self.failUnlessExists('checkout/file-1')
 
124
        self.failIfExists('checkout/file-2')
171
125
 
172
126
    def test_switch_changing_root_id(self):
173
127
        tree = self._setup_tree()
174
128
        tree2 = self.make_branch_and_tree('tree-2')
175
 
        tree2.set_root_id(b'custom-root-id')
 
129
        tree2.set_root_id('custom-root-id')
176
130
        self.build_tree(['tree-2/file-2'])
177
131
        tree2.add(['file-2'])
178
132
        tree2.commit('rev1b')
179
133
        checkout = tree.branch.create_checkout('checkout',
180
 
                                               lightweight=self.lightweight)
181
 
        switch.switch(checkout.controldir, tree2.branch)
182
 
        self.assertEqual(b'custom-root-id', tree2.path2id(''))
 
134
            lightweight=self.lightweight)
 
135
        switch.switch(checkout.bzrdir, tree2.branch)
 
136
        self.assertEqual('custom-root-id', tree2.get_root_id())
183
137
 
184
138
    def test_switch_configurable_file_merger(self):
185
139
        class DummyMerger(_mod_merge.ConfigurableFileMerger):
190
144
            'test factory')
191
145
        foo = self.make_branch('foo')
192
146
        checkout = foo.create_checkout('checkout', lightweight=True)
193
 
        self.build_tree_contents([('checkout/file', b'a')])
 
147
        self.build_tree_contents([('checkout/file', 'a')])
194
148
        checkout.add('file')
195
149
        checkout.commit('a')
196
 
        bar = foo.controldir.sprout('bar').open_workingtree()
197
 
        self.build_tree_contents([('bar/file', b'b')])
 
150
        bar = foo.bzrdir.sprout('bar').open_workingtree()
 
151
        self.build_tree_contents([('bar/file', 'b')])
198
152
        bar.commit('b')
199
 
        self.build_tree_contents([('checkout/file', b'c')])
200
 
        switch.switch(checkout.controldir, bar.branch)
 
153
        self.build_tree_contents([('checkout/file', 'c')])
 
154
        switch.switch(checkout.bzrdir, bar.branch)
201
155
 
202
156
 
203
157
class TestSwitchHeavyweight(TestSwitch):
209
163
    def test_switch_with_local_commits(self):
210
164
        """Test switch complains about local commits unless --force given."""
211
165
        tree = self._setup_tree()
212
 
        to_branch = tree.controldir.sprout('branch-2').open_branch()
 
166
        to_branch = tree.bzrdir.sprout('branch-2').open_branch()
213
167
        self.build_tree(['branch-1/file-2'])
214
168
        tree.add('file-2')
215
169
        tree.remove('file-1')
221
175
        self.build_tree(['checkout/file-4'])
222
176
        # Check the error reporting is as expected
223
177
        err = self.assertRaises(errors.BzrCommandError,
224
 
                                switch.switch, checkout.controldir, to_branch)
 
178
            switch.switch, checkout.bzrdir, to_branch)
225
179
        self.assertContainsRe(str(err),
226
 
                              'Cannot switch as local commits found in the checkout.')
 
180
            'Cannot switch as local commits found in the checkout.')
227
181
        # Check all is ok when force is given
228
 
        self.assertPathDoesNotExist('checkout/file-1')
229
 
        self.assertPathExists('checkout/file-2')
230
 
        switch.switch(checkout.controldir, to_branch, force=True)
231
 
        self.assertPathExists('checkout/file-1')
232
 
        self.assertPathDoesNotExist('checkout/file-2')
233
 
        self.assertPathDoesNotExist('checkout/file-3')
234
 
        self.assertPathExists('checkout/file-4')
 
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')
235
189
        # Check that the checkout is a true mirror of the bound branch
236
190
        self.assertEqual(to_branch.last_revision_info(),
237
191
                         checkout.branch.last_revision_info())