/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 brzlib/tests/test_switch.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 brzlib.switch."""
18
18
 
19
19
 
20
20
import os
21
21
 
22
 
from breezy import (
 
22
from brzlib import (
23
23
    branch,
24
24
    errors,
 
25
    lock,
25
26
    merge as _mod_merge,
26
27
    switch,
27
28
    tests,
52
53
 
53
54
    def _setup_uncommitted(self, same_revision=False):
54
55
        tree = self._setup_tree()
55
 
        to_branch = tree.controldir.sprout('branch-2').open_branch()
 
56
        to_branch = tree.bzrdir.sprout('branch-2').open_branch()
56
57
        self.build_tree(['branch-1/file-2'])
57
58
        if not same_revision:
58
59
            tree.add('file-2')
59
60
            tree.remove('file-1')
60
61
            tree.commit('rev2')
61
62
        checkout = tree.branch.create_checkout('checkout',
62
 
                                               lightweight=self.lightweight)
 
63
            lightweight=self.lightweight)
63
64
        self.build_tree(['checkout/file-3'])
64
65
        checkout.add('file-3')
65
66
        return checkout, to_branch
69
70
        checkout, to_branch = self._setup_uncommitted()
70
71
        self.assertPathDoesNotExist('checkout/file-1')
71
72
        self.assertPathExists('checkout/file-2')
72
 
        switch.switch(checkout.controldir, to_branch, store_uncommitted=True)
 
73
        switch.switch(checkout.bzrdir, to_branch, store_uncommitted=True)
73
74
        self.assertPathExists('checkout/file-1')
74
75
        self.assertPathDoesNotExist('checkout/file-2')
75
76
        self.assertPathDoesNotExist('checkout/file-3')
81
82
        self.assertPathDoesNotExist('checkout/file-1')
82
83
        self.assertPathExists('checkout/file-2')
83
84
        self.assertPathExists('checkout/file-3')
84
 
        switch.switch(checkout.controldir, to_branch, store_uncommitted=True)
 
85
        switch.switch(checkout.bzrdir, to_branch, store_uncommitted=True)
85
86
        checkout = workingtree.WorkingTree.open('checkout')
86
 
        switch.switch(checkout.controldir, old_branch, store_uncommitted=True)
 
87
        switch.switch(checkout.bzrdir, old_branch, store_uncommitted=True)
87
88
        self.assertPathDoesNotExist('checkout/file-1')
88
89
        self.assertPathExists('checkout/file-2')
89
90
        self.assertPathExists('checkout/file-3')
92
93
        """Test switch updates tree and restores uncommitted changes."""
93
94
        checkout, to_branch = self._setup_uncommitted(same_revision=True)
94
95
        old_branch = self._master_if_present(checkout.branch)
95
 
        switch.switch(checkout.controldir, to_branch, store_uncommitted=True)
 
96
        switch.switch(checkout.bzrdir, to_branch, store_uncommitted=True)
96
97
        checkout = workingtree.WorkingTree.open('checkout')
97
 
        switch.switch(checkout.controldir, old_branch, store_uncommitted=True)
 
98
        switch.switch(checkout.bzrdir, old_branch, store_uncommitted=True)
98
99
        self.assertPathExists('checkout/file-3')
99
100
 
100
101
    def test_switch_updates(self):
102
103
        checkout, to_branch = self._setup_uncommitted()
103
104
        self.assertPathDoesNotExist('checkout/file-1')
104
105
        self.assertPathExists('checkout/file-2')
105
 
        switch.switch(checkout.controldir, to_branch)
 
106
        switch.switch(checkout.bzrdir, to_branch)
106
107
        self.assertPathExists('checkout/file-1')
107
108
        self.assertPathDoesNotExist('checkout/file-2')
108
109
        self.assertPathExists('checkout/file-3')
111
112
        """Test switch after the branch is moved."""
112
113
        tree = self._setup_tree()
113
114
        checkout = tree.branch.create_checkout('checkout',
114
 
                                               lightweight=self.lightweight)
 
115
            lightweight=self.lightweight)
115
116
        self.build_tree(['branch-1/file-2'])
116
117
        tree.add('file-2')
117
118
        tree.remove('file-1')
118
119
        tree.commit('rev2')
119
120
        self.build_tree(['checkout/file-3'])
120
121
        checkout.add('file-3')
 
122
        checkout_dir = checkout.bzrdir
121
123
        # rename the branch on disk, the checkout object is now invalid.
122
124
        os.rename('branch-1', 'branch-2')
123
125
        to_branch = branch.Branch.open('branch-2')
124
126
        # Check fails without --force
125
127
        err = self.assertRaises(
126
128
            (errors.BzrCommandError, errors.NotBranchError),
127
 
            switch.switch, checkout.controldir, to_branch)
 
129
            switch.switch, checkout.bzrdir, to_branch)
128
130
        if isinstance(err, errors.BzrCommandError):
129
131
            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)
 
132
                'Unable to connect to current master branch .*'
 
133
                'To switch anyway, use --force.')
 
134
        switch.switch(checkout.bzrdir, to_branch, force=True)
133
135
        self.assertPathDoesNotExist('checkout/file-1')
134
136
        self.assertPathExists('checkout/file-2')
135
137
        self.assertPathExists('checkout/file-3')
138
140
        """Test graceful failure if pending merges are outstanding."""
139
141
        # Create 2 branches and a checkout
140
142
        tree = self._setup_tree()
141
 
        tree2 = tree.controldir.sprout('branch-2').open_workingtree()
 
143
        tree2 = tree.bzrdir.sprout('branch-2').open_workingtree()
142
144
        checkout = tree.branch.create_checkout('checkout',
143
 
                                               lightweight=self.lightweight)
 
145
            lightweight=self.lightweight)
144
146
        # Change tree2 and merge it into the checkout without committing
145
147
        self.build_tree(['branch-2/file-2'])
146
148
        tree2.add('file-2')
148
150
        checkout.merge_from_branch(tree2.branch)
149
151
        # Check the error reporting is as expected
150
152
        err = self.assertRaises(errors.BzrCommandError,
151
 
                                switch.switch, checkout.controldir, tree2.branch)
 
153
            switch.switch, checkout.bzrdir, tree2.branch)
152
154
        self.assertContainsRe(str(err),
153
 
                              "Pending merges must be committed or reverted before using switch")
 
155
            "Pending merges must be committed or reverted before using switch")
154
156
 
155
157
    def test_switch_with_revision(self):
156
158
        """Test switch when a revision is given."""
158
160
        tree = self.make_branch_and_tree('branch-1')
159
161
        self.build_tree(['branch-1/file-1'])
160
162
        tree.add('file-1')
161
 
        tree.commit(rev_id=b'rev1', message='rev1')
 
163
        tree.commit(rev_id='rev1', message='rev1')
162
164
        self.build_tree(['branch-1/file-2'])
163
165
        tree.add('file-2')
164
 
        tree.commit(rev_id=b'rev2', message='rev2')
 
166
        tree.commit(rev_id='rev2', message='rev2')
165
167
        # Check it out and switch to revision 1
166
168
        checkout = tree.branch.create_checkout('checkout',
167
 
                                               lightweight=self.lightweight)
168
 
        switch.switch(checkout.controldir, tree.branch, revision_id=b"rev1")
 
169
            lightweight=self.lightweight)
 
170
        switch.switch(checkout.bzrdir, tree.branch, revision_id="rev1")
169
171
        self.assertPathExists('checkout/file-1')
170
172
        self.assertPathDoesNotExist('checkout/file-2')
171
173
 
172
174
    def test_switch_changing_root_id(self):
173
175
        tree = self._setup_tree()
174
176
        tree2 = self.make_branch_and_tree('tree-2')
175
 
        tree2.set_root_id(b'custom-root-id')
 
177
        tree2.set_root_id('custom-root-id')
176
178
        self.build_tree(['tree-2/file-2'])
177
179
        tree2.add(['file-2'])
178
180
        tree2.commit('rev1b')
179
181
        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(''))
 
182
            lightweight=self.lightweight)
 
183
        switch.switch(checkout.bzrdir, tree2.branch)
 
184
        self.assertEqual('custom-root-id', tree2.get_root_id())
183
185
 
184
186
    def test_switch_configurable_file_merger(self):
185
187
        class DummyMerger(_mod_merge.ConfigurableFileMerger):
190
192
            'test factory')
191
193
        foo = self.make_branch('foo')
192
194
        checkout = foo.create_checkout('checkout', lightweight=True)
193
 
        self.build_tree_contents([('checkout/file', b'a')])
 
195
        self.build_tree_contents([('checkout/file', 'a')])
194
196
        checkout.add('file')
195
197
        checkout.commit('a')
196
 
        bar = foo.controldir.sprout('bar').open_workingtree()
197
 
        self.build_tree_contents([('bar/file', b'b')])
 
198
        bar = foo.bzrdir.sprout('bar').open_workingtree()
 
199
        self.build_tree_contents([('bar/file', 'b')])
198
200
        bar.commit('b')
199
 
        self.build_tree_contents([('checkout/file', b'c')])
200
 
        switch.switch(checkout.controldir, bar.branch)
 
201
        self.build_tree_contents([('checkout/file', 'c')])
 
202
        switch.switch(checkout.bzrdir, bar.branch)
201
203
 
202
204
 
203
205
class TestSwitchHeavyweight(TestSwitch):
209
211
    def test_switch_with_local_commits(self):
210
212
        """Test switch complains about local commits unless --force given."""
211
213
        tree = self._setup_tree()
212
 
        to_branch = tree.controldir.sprout('branch-2').open_branch()
 
214
        to_branch = tree.bzrdir.sprout('branch-2').open_branch()
213
215
        self.build_tree(['branch-1/file-2'])
214
216
        tree.add('file-2')
215
217
        tree.remove('file-1')
221
223
        self.build_tree(['checkout/file-4'])
222
224
        # Check the error reporting is as expected
223
225
        err = self.assertRaises(errors.BzrCommandError,
224
 
                                switch.switch, checkout.controldir, to_branch)
 
226
            switch.switch, checkout.bzrdir, to_branch)
225
227
        self.assertContainsRe(str(err),
226
 
                              'Cannot switch as local commits found in the checkout.')
 
228
            'Cannot switch as local commits found in the checkout.')
227
229
        # Check all is ok when force is given
228
230
        self.assertPathDoesNotExist('checkout/file-1')
229
231
        self.assertPathExists('checkout/file-2')
230
 
        switch.switch(checkout.controldir, to_branch, force=True)
 
232
        switch.switch(checkout.bzrdir, to_branch, force=True)
231
233
        self.assertPathExists('checkout/file-1')
232
234
        self.assertPathDoesNotExist('checkout/file-2')
233
235
        self.assertPathDoesNotExist('checkout/file-3')