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

  • Committer: Jelmer Vernooij
  • Date: 2018-09-12 18:20:02 UTC
  • mto: This revision was merged to the branch mainline in revision 7107.
  • Revision ID: jelmer@jelmer.uk-20180912182002-5z6dvag2frdvr9y2
Fix remaining deprecation warning test.

Show diffs side-by-side

added added

removed removed

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