/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2007-2012, 2016 Canonical Ltd
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
2
# -*- coding: utf-8 -*-
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
17
18
19
"""Tests for the switch command of bzr."""
20
21
import os
22
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
23
from breezy.controldir import ControlDir
24
from breezy import (
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
25
    osutils,
26
    urlutils,
27
    branch,
28
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
29
from breezy.workingtree import WorkingTree
30
from breezy.tests import (
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
31
    TestCaseWithTransport,
32
    script,
33
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
34
from breezy.tests.features import UnicodeFilenameFeature
35
from breezy.directory_service import directories
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
36
5816.6.15 by A. S. Budden
Added erroneously removed blank line.
37
5283.4.5 by Martin Pool
Update remaining subclasses of ExternalBase
38
class TestSwitch(TestCaseWithTransport):
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
39
3984.5.14 by Daniel Watkins
Extracted common setup code.
40
    def _create_sample_tree(self):
41
        tree = self.make_branch_and_tree('branch-1')
42
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
43
        tree.add('file-1')
44
        tree.commit('rev1')
45
        tree.add('file-2')
46
        tree.commit('rev2')
47
        return tree
48
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
49
    def test_switch_up_to_date_light_checkout(self):
50
        self.make_branch_and_tree('branch')
51
        self.run_bzr('branch branch branch2')
52
        self.run_bzr('checkout --lightweight branch checkout')
53
        os.chdir('checkout')
54
        out, err = self.run_bzr('switch ../branch2')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
55
        self.assertContainsRe(err, 'Tree is up to date at revision 0.\n')
7223.1.1 by Jelmer Vernooij
Display colocated branch name without full path when switching between colocated branches.
56
        self.assertContainsRe(err, 'Switched to branch at .*/branch2.\n')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
57
        self.assertEqual('', out)
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
58
59
    def test_switch_out_of_date_light_checkout(self):
60
        self.make_branch_and_tree('branch')
61
        self.run_bzr('branch branch branch2')
62
        self.build_tree(['branch2/file'])
63
        self.run_bzr('add branch2/file')
64
        self.run_bzr('commit -m add-file branch2')
65
        self.run_bzr('checkout --lightweight branch checkout')
66
        os.chdir('checkout')
67
        out, err = self.run_bzr('switch ../branch2')
68
        #self.assertContainsRe(err, '\+N  file')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
69
        self.assertContainsRe(err, 'Updated to revision 1.\n')
7223.1.1 by Jelmer Vernooij
Display colocated branch name without full path when switching between colocated branches.
70
        self.assertContainsRe(err, 'Switched to branch at .*/branch2.\n')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
71
        self.assertEqual('', out)
3246.5.1 by Robert Collins
* ``bzr switch`` will attempt to find branches to switch to relative to the
72
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
73
    def _test_switch_nick(self, lightweight):
74
        """Check that the nick gets switched too."""
75
        tree1 = self.make_branch_and_tree('branch1')
76
        tree2 = self.make_branch_and_tree('branch2')
77
        tree2.pull(tree1.branch)
7143.15.2 by Jelmer Vernooij
Run autopep8.
78
        checkout = tree1.branch.create_checkout('checkout',
79
                                                lightweight=lightweight)
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
80
        self.assertEqual(checkout.branch.nick, tree1.branch.nick)
81
        self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
82
                         False)
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
83
        self.run_bzr('switch branch2', working_dir='checkout')
84
85
        # we need to get the tree again, otherwise we don't get the new branch
86
        checkout = WorkingTree.open('checkout')
87
        self.assertEqual(checkout.branch.nick, tree2.branch.nick)
88
        self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
89
                         False)
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
90
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
91
    def test_switch_nick(self):
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
92
        self._test_switch_nick(lightweight=False)
93
94
    def test_switch_nick_lightweight(self):
95
        self._test_switch_nick(lightweight=True)
96
97
    def _test_switch_explicit_nick(self, lightweight):
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
98
        """Check that the nick gets switched too."""
99
        tree1 = self.make_branch_and_tree('branch1')
100
        tree2 = self.make_branch_and_tree('branch2')
101
        tree2.pull(tree1.branch)
7143.15.2 by Jelmer Vernooij
Run autopep8.
102
        checkout = tree1.branch.create_checkout('checkout',
103
                                                lightweight=lightweight)
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
104
        self.assertEqual(checkout.branch.nick, tree1.branch.nick)
105
        checkout.branch.nick = "explicit_nick"
106
        self.assertEqual(checkout.branch.nick, "explicit_nick")
107
        self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
108
                         "explicit_nick")
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
109
        self.run_bzr('switch branch2', working_dir='checkout')
110
111
        # we need to get the tree again, otherwise we don't get the new branch
112
        checkout = WorkingTree.open('checkout')
113
        self.assertEqual(checkout.branch.nick, tree2.branch.nick)
114
        self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
115
                         tree2.branch.nick)
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
116
117
    def test_switch_explicit_nick(self):
118
        self._test_switch_explicit_nick(lightweight=False)
119
120
    def test_switch_explicit_nick_lightweight(self):
121
        self._test_switch_explicit_nick(lightweight=True)
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
122
3246.5.1 by Robert Collins
* ``bzr switch`` will attempt to find branches to switch to relative to the
123
    def test_switch_finds_relative_branch(self):
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
124
        """Switch will find 'foo' relative to the branch the checkout is of."""
3246.5.1 by Robert Collins
* ``bzr switch`` will attempt to find branches to switch to relative to the
125
        self.build_tree(['repo/'])
126
        tree1 = self.make_branch_and_tree('repo/brancha')
127
        tree1.commit('foo')
128
        tree2 = self.make_branch_and_tree('repo/branchb')
129
        tree2.pull(tree1.branch)
130
        branchb_id = tree2.commit('bar')
7143.15.2 by Jelmer Vernooij
Run autopep8.
131
        checkout = tree1.branch.create_checkout('checkout', lightweight=True)
3246.5.1 by Robert Collins
* ``bzr switch`` will attempt to find branches to switch to relative to the
132
        self.run_bzr(['switch', 'branchb'], working_dir='checkout')
133
        self.assertEqual(branchb_id, checkout.last_revision())
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
134
        checkout = checkout.controldir.open_workingtree()
3246.5.1 by Robert Collins
* ``bzr switch`` will attempt to find branches to switch to relative to the
135
        self.assertEqual(tree2.branch.base, checkout.branch.base)
3602.3.1 by Adrian Wilkins
Test that `bzr switch` finds the sibling of the bound branch of heavy checkout.
136
137
    def test_switch_finds_relative_bound_branch(self):
3602.3.4 by Adrian Wilkins
Improved comments and documentation
138
        """Using switch on a heavy checkout should find master sibling
139
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
140
        The behaviour of lighweight and heavy checkouts should be
5816.6.1 by A. S. Budden
Test harness to stimulate Bug #513709: parent being set incorrectly in switch --create-branch.
141
        consistent when using the convenient "switch to sibling" feature
3602.3.4 by Adrian Wilkins
Improved comments and documentation
142
        Both should switch to a sibling of the branch
143
        they are bound to, and not a sibling of themself"""
144
3602.3.1 by Adrian Wilkins
Test that `bzr switch` finds the sibling of the bound branch of heavy checkout.
145
        self.build_tree(['repo/',
146
                         'heavyco/'])
147
        tree1 = self.make_branch_and_tree('repo/brancha')
148
        tree1.commit('foo')
149
        tree2 = self.make_branch_and_tree('repo/branchb')
150
        tree2.pull(tree1.branch)
151
        branchb_id = tree2.commit('bar')
152
        checkout = tree1.branch.create_checkout('heavyco/a', lightweight=False)
153
        self.run_bzr(['switch', 'branchb'], working_dir='heavyco/a')
6404.6.6 by Vincent Ladeuil
Use idioms coherently and add comments to make their purpose clearer.
154
        # Refresh checkout as 'switch' modified it
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
155
        checkout = checkout.controldir.open_workingtree()
3602.3.1 by Adrian Wilkins
Test that `bzr switch` finds the sibling of the bound branch of heavy checkout.
156
        self.assertEqual(branchb_id, checkout.last_revision())
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
157
        self.assertEqual(tree2.branch.base,
158
                         checkout.branch.get_bound_location())
4354.2.2 by Aaron Bentley
Enable switch --force for lightweight checkouts after moves.
159
5268.8.15 by Jelmer Vernooij
Fix switching to unicode branch names.
160
    def test_switch_finds_relative_unicode_branch(self):
161
        """Switch will find 'foo' relative to the branch the checkout is of."""
5268.8.18 by Jelmer Vernooij
Use UnicodeFilenameFeature.
162
        self.requireFeature(UnicodeFilenameFeature)
5268.8.15 by Jelmer Vernooij
Fix switching to unicode branch names.
163
        self.build_tree(['repo/'])
164
        tree1 = self.make_branch_and_tree('repo/brancha')
165
        tree1.commit('foo')
166
        tree2 = self.make_branch_and_tree(u'repo/branch\xe9')
167
        tree2.pull(tree1.branch)
168
        branchb_id = tree2.commit('bar')
7143.15.2 by Jelmer Vernooij
Run autopep8.
169
        checkout = tree1.branch.create_checkout('checkout', lightweight=True)
5268.8.15 by Jelmer Vernooij
Fix switching to unicode branch names.
170
        self.run_bzr(['switch', u'branch\xe9'], working_dir='checkout')
171
        self.assertEqual(branchb_id, checkout.last_revision())
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
172
        checkout = checkout.controldir.open_workingtree()
5268.8.15 by Jelmer Vernooij
Fix switching to unicode branch names.
173
        self.assertEqual(tree2.branch.base, checkout.branch.base)
174
175
    def test_switch_finds_relative_unicode_branch(self):
176
        """Switch will find 'foo' relative to the branch the checkout is of."""
5268.8.18 by Jelmer Vernooij
Use UnicodeFilenameFeature.
177
        self.requireFeature(UnicodeFilenameFeature)
5268.8.15 by Jelmer Vernooij
Fix switching to unicode branch names.
178
        self.build_tree(['repo/'])
179
        tree1 = self.make_branch_and_tree('repo/brancha')
180
        tree1.commit('foo')
181
        tree2 = self.make_branch_and_tree(u'repo/branch\xe9')
182
        tree2.pull(tree1.branch)
183
        branchb_id = tree2.commit('bar')
7143.15.2 by Jelmer Vernooij
Run autopep8.
184
        checkout = tree1.branch.create_checkout('checkout', lightweight=True)
5268.8.15 by Jelmer Vernooij
Fix switching to unicode branch names.
185
        self.run_bzr(['switch', u'branch\xe9'], working_dir='checkout')
186
        self.assertEqual(branchb_id, checkout.last_revision())
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
187
        checkout = checkout.controldir.open_workingtree()
5268.8.15 by Jelmer Vernooij
Fix switching to unicode branch names.
188
        self.assertEqual(tree2.branch.base, checkout.branch.base)
189
3984.5.2 by Daniel Watkins
Added blackbox test.
190
    def test_switch_revision(self):
3984.5.14 by Daniel Watkins
Extracted common setup code.
191
        tree = self._create_sample_tree()
3984.5.2 by Daniel Watkins
Added blackbox test.
192
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
193
        self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
194
        self.assertPathExists('checkout/file-1')
195
        self.assertPathDoesNotExist('checkout/file-2')
3984.5.9 by Daniel Watkins
Added test for allowing only revisions to be passed to switch.
196
6437.7.5 by Jelmer Vernooij
"bzr switch -b" in a standalone tree will now create a colocated branch.
197
    def test_switch_into_colocated(self):
198
        # Create a new colocated branch from an existing non-colocated branch.
199
        tree = self.make_branch_and_tree('.', format='development-colo')
200
        self.build_tree(['file-1', 'file-2'])
201
        tree.add('file-1')
202
        revid1 = tree.commit('rev1')
203
        tree.add('file-2')
204
        revid2 = tree.commit('rev2')
205
        self.run_bzr(['switch', '-b', 'anotherbranch'])
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
206
        self.assertEqual(
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
207
            {'', 'anotherbranch'},
7490.13.2 by Jelmer Vernooij
Fix reference handling.
208
            set(tree.branch.controldir.branch_names()))
6437.7.5 by Jelmer Vernooij
"bzr switch -b" in a standalone tree will now create a colocated branch.
209
6437.7.6 by Jelmer Vernooij
Add test for --force argument used in standalone branch.
210
    def test_switch_into_unrelated_colocated(self):
211
        # Create a new colocated branch from an existing non-colocated branch.
212
        tree = self.make_branch_and_tree('.', format='development-colo')
213
        self.build_tree(['file-1', 'file-2'])
214
        tree.add('file-1')
215
        revid1 = tree.commit('rev1')
216
        tree.add('file-2')
217
        revid2 = tree.commit('rev2')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
218
        tree.controldir.create_branch(name='foo')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
219
        self.run_bzr_error(['Cannot switch a branch, only a checkout.'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
220
                           'switch foo')
6437.7.6 by Jelmer Vernooij
Add test for --force argument used in standalone branch.
221
        self.run_bzr(['switch', '--force', 'foo'])
222
5268.8.12 by Jelmer Vernooij
support creating new colocated branches from 'bzr switch -b'.
223
    def test_switch_existing_colocated(self):
5268.8.6 by Jelmer Vernooij
More fixes to test.
224
        # Create a branch branch-1 that initially is a checkout of 'foo'
225
        # Use switch to change it to 'anotherbranch'
226
        repo = self.make_repository('branch-1', format='development-colo')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
227
        target_branch = repo.controldir.create_branch(name='foo')
228
        repo.controldir.set_branch_reference(target_branch)
229
        tree = repo.controldir.create_workingtree()
5268.8.5 by Jelmer Vernooij
Add tests for switching to colocated branches.
230
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
231
        tree.add('file-1')
232
        revid1 = tree.commit('rev1')
233
        tree.add('file-2')
234
        revid2 = tree.commit('rev2')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
235
        otherbranch = tree.controldir.create_branch(name='anotherbranch')
5268.8.5 by Jelmer Vernooij
Add tests for switching to colocated branches.
236
        otherbranch.generate_revision_history(revid1)
237
        self.run_bzr(['switch', 'anotherbranch'], working_dir='branch-1')
5268.8.10 by Jelmer Vernooij
bzr switch can now switch to colocated branches.
238
        tree = WorkingTree.open("branch-1")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
239
        self.assertEqual(tree.last_revision(), revid1)
240
        self.assertEqual(tree.branch.control_url, otherbranch.control_url)
5268.8.5 by Jelmer Vernooij
Add tests for switching to colocated branches.
241
5268.8.12 by Jelmer Vernooij
support creating new colocated branches from 'bzr switch -b'.
242
    def test_switch_new_colocated(self):
243
        # Create a branch branch-1 that initially is a checkout of 'foo'
244
        # Use switch to create 'anotherbranch' which derives from that
245
        repo = self.make_repository('branch-1', format='development-colo')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
246
        target_branch = repo.controldir.create_branch(name='foo')
247
        repo.controldir.set_branch_reference(target_branch)
248
        tree = repo.controldir.create_workingtree()
5268.8.12 by Jelmer Vernooij
support creating new colocated branches from 'bzr switch -b'.
249
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
250
        tree.add('file-1')
251
        revid1 = tree.commit('rev1')
252
        self.run_bzr(['switch', '-b', 'anotherbranch'], working_dir='branch-1')
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
253
        bzrdir = ControlDir.open("branch-1")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
254
        self.assertEqual(
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
255
            {b.name for b in bzrdir.list_branches()},
256
            {"foo", "anotherbranch"})
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
257
        self.assertEqual(bzrdir.open_branch().name, "anotherbranch")
258
        self.assertEqual(bzrdir.open_branch().last_revision(), revid1)
5268.8.12 by Jelmer Vernooij
support creating new colocated branches from 'bzr switch -b'.
259
5268.8.17 by Jelmer Vernooij
Support switching to new unicode colocated branch.
260
    def test_switch_new_colocated_unicode(self):
261
        # Create a branch branch-1 that initially is a checkout of 'foo'
262
        # Use switch to create 'branch\xe9' which derives from that
5268.8.18 by Jelmer Vernooij
Use UnicodeFilenameFeature.
263
        self.requireFeature(UnicodeFilenameFeature)
5268.8.17 by Jelmer Vernooij
Support switching to new unicode colocated branch.
264
        repo = self.make_repository('branch-1', format='development-colo')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
265
        target_branch = repo.controldir.create_branch(name='foo')
266
        repo.controldir.set_branch_reference(target_branch)
267
        tree = repo.controldir.create_workingtree()
5268.8.17 by Jelmer Vernooij
Support switching to new unicode colocated branch.
268
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
269
        tree.add('file-1')
270
        revid1 = tree.commit('rev1')
271
        self.run_bzr(['switch', '-b', u'branch\xe9'], working_dir='branch-1')
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
272
        bzrdir = ControlDir.open("branch-1")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
273
        self.assertEqual(
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
274
            {b.name for b in bzrdir.list_branches()},
275
            {"foo", u"branch\xe9"})
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
276
        self.assertEqual(bzrdir.open_branch().name, u"branch\xe9")
277
        self.assertEqual(bzrdir.open_branch().last_revision(), revid1)
5268.8.17 by Jelmer Vernooij
Support switching to new unicode colocated branch.
278
3984.5.9 by Daniel Watkins
Added test for allowing only revisions to be passed to switch.
279
    def test_switch_only_revision(self):
3984.5.14 by Daniel Watkins
Extracted common setup code.
280
        tree = self._create_sample_tree()
3984.5.9 by Daniel Watkins
Added test for allowing only revisions to be passed to switch.
281
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
282
        self.assertPathExists('checkout/file-1')
283
        self.assertPathExists('checkout/file-2')
3984.5.9 by Daniel Watkins
Added test for allowing only revisions to be passed to switch.
284
        self.run_bzr(['switch', '-r1'], working_dir='checkout')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
285
        self.assertPathExists('checkout/file-1')
286
        self.assertPathDoesNotExist('checkout/file-2')
3984.5.15 by Daniel Watkins
Add to test to ensure that we don't accept a range of revisions.
287
        # Check that we don't accept a range
3984.5.17 by Daniel Watkins
Fixed incorrect call of run_bzr_error.
288
        self.run_bzr_error(
6622.1.29 by Jelmer Vernooij
Fix some more tests.
289
            ['brz switch --revision takes exactly one revision identifier'],
3984.5.17 by Daniel Watkins
Fixed incorrect call of run_bzr_error.
290
            ['switch', '-r0..2'], working_dir='checkout')
3984.5.19 by Andrew Bennetts
Merge lp:bzr, resolving conflicts.
291
4354.2.2 by Aaron Bentley
Enable switch --force for lightweight checkouts after moves.
292
    def prepare_lightweight_switch(self):
293
        branch = self.make_branch('branch')
294
        branch.create_checkout('tree', lightweight=True)
5186.2.2 by Martin Pool
wrap os.rename to insert the source and destination filenames in any exception that may be raised
295
        osutils.rename('branch', 'branch1')
4354.2.2 by Aaron Bentley
Enable switch --force for lightweight checkouts after moves.
296
297
    def test_switch_lightweight_after_branch_moved(self):
298
        self.prepare_lightweight_switch()
6538.1.30 by Aaron Bentley
Make *not* storing on switch the default.
299
        self.run_bzr('switch --force ../branch1', working_dir='tree')
4354.2.2 by Aaron Bentley
Enable switch --force for lightweight checkouts after moves.
300
        branch_location = WorkingTree.open('tree').branch.base
301
        self.assertEndsWith(branch_location, 'branch1/')
302
303
    def test_switch_lightweight_after_branch_moved_relative(self):
304
        self.prepare_lightweight_switch()
6538.1.30 by Aaron Bentley
Make *not* storing on switch the default.
305
        self.run_bzr('switch --force branch1',
6538.1.18 by Aaron Bentley
Add --with-changes flag to switch.
306
                     working_dir='tree')
4354.2.2 by Aaron Bentley
Enable switch --force for lightweight checkouts after moves.
307
        branch_location = WorkingTree.open('tree').branch.base
308
        self.assertEndsWith(branch_location, 'branch1/')
4520.1.1 by John Arbash Meinel
'bzr switch -b' can now be used to create the branch while you switch to it.
309
310
    def test_create_branch_no_branch(self):
311
        self.prepare_lightweight_switch()
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
312
        self.run_bzr_error(['cannot create branch without source branch'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
313
                           'switch --create-branch ../branch2', working_dir='tree')
4520.1.1 by John Arbash Meinel
'bzr switch -b' can now be used to create the branch while you switch to it.
314
315
    def test_create_branch(self):
316
        branch = self.make_branch('branch')
317
        tree = branch.create_checkout('tree', lightweight=True)
6855.4.1 by Jelmer Vernooij
Yet more bees.
318
        tree.commit('one', rev_id=b'rev-1')
4520.1.1 by John Arbash Meinel
'bzr switch -b' can now be used to create the branch while you switch to it.
319
        self.run_bzr('switch --create-branch ../branch2', working_dir='tree')
320
        tree = WorkingTree.open('tree')
321
        self.assertEndsWith(tree.branch.base, '/branch2/')
322
323
    def test_create_branch_local(self):
324
        branch = self.make_branch('branch')
325
        tree = branch.create_checkout('tree', lightweight=True)
6855.4.1 by Jelmer Vernooij
Yet more bees.
326
        tree.commit('one', rev_id=b'rev-1')
4520.1.1 by John Arbash Meinel
'bzr switch -b' can now be used to create the branch while you switch to it.
327
        self.run_bzr('switch --create-branch branch2', working_dir='tree')
328
        tree = WorkingTree.open('tree')
329
        # The new branch should have been created at the same level as
330
        # 'branch', because we did not have a '/' segment
331
        self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
332
333
    def test_create_branch_short_name(self):
334
        branch = self.make_branch('branch')
335
        tree = branch.create_checkout('tree', lightweight=True)
6855.4.1 by Jelmer Vernooij
Yet more bees.
336
        tree.commit('one', rev_id=b'rev-1')
4520.1.1 by John Arbash Meinel
'bzr switch -b' can now be used to create the branch while you switch to it.
337
        self.run_bzr('switch -b branch2', working_dir='tree')
338
        tree = WorkingTree.open('tree')
339
        # The new branch should have been created at the same level as
340
        # 'branch', because we did not have a '/' segment
341
        self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
4879.2.1 by Neil Martinsen-Burrell
switch should use directory services when creating a branch
342
343
    def test_create_branch_directory_services(self):
344
        branch = self.make_branch('branch')
345
        tree = branch.create_checkout('tree', lightweight=True)
7143.15.2 by Jelmer Vernooij
Run autopep8.
346
4879.2.1 by Neil Martinsen-Burrell
switch should use directory services when creating a branch
347
        class FooLookup(object):
7268.11.2 by Jelmer Vernooij
Add purpose argument.
348
            def look_up(self, name, url, purpose=None):
7143.15.2 by Jelmer Vernooij
Run autopep8.
349
                return 'foo-' + name
4879.2.1 by Neil Martinsen-Burrell
switch should use directory services when creating a branch
350
        directories.register('foo:', FooLookup, 'Create branches named foo-')
4879.2.2 by Neil Martinsen-Burrell
add test cleanup per JAMs review
351
        self.addCleanup(directories.remove, 'foo:')
4879.2.1 by Neil Martinsen-Burrell
switch should use directory services when creating a branch
352
        self.run_bzr('switch -b foo:branch2', working_dir='tree')
353
        tree = WorkingTree.open('tree')
354
        self.assertEndsWith(tree.branch.base, 'foo-branch2/')
5107.3.6 by Marco Pantaleoni
Documented behaviour of 'post_branch_init' for lightweight checkouts.
355
356
    def test_switch_with_post_switch_hook(self):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
357
        from breezy import branch as _mod_branch
5107.3.6 by Marco Pantaleoni
Documented behaviour of 'post_branch_init' for lightweight checkouts.
358
        calls = []
359
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
7143.15.2 by Jelmer Vernooij
Run autopep8.
360
                                                    calls.append, None)
5107.3.6 by Marco Pantaleoni
Documented behaviour of 'post_branch_init' for lightweight checkouts.
361
        self.make_branch_and_tree('branch')
362
        self.run_bzr('branch branch branch2')
363
        self.run_bzr('checkout branch checkout')
364
        os.chdir('checkout')
365
        self.assertLength(0, calls)
366
        out, err = self.run_bzr('switch ../branch2')
367
        self.assertLength(1, calls)
368
369
    def test_switch_lightweight_co_with_post_switch_hook(self):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
370
        from breezy import branch as _mod_branch
5107.3.6 by Marco Pantaleoni
Documented behaviour of 'post_branch_init' for lightweight checkouts.
371
        calls = []
372
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
7143.15.2 by Jelmer Vernooij
Run autopep8.
373
                                                    calls.append, None)
5107.3.6 by Marco Pantaleoni
Documented behaviour of 'post_branch_init' for lightweight checkouts.
374
        self.make_branch_and_tree('branch')
375
        self.run_bzr('branch branch branch2')
376
        self.run_bzr('checkout --lightweight branch checkout')
377
        os.chdir('checkout')
378
        self.assertLength(0, calls)
379
        out, err = self.run_bzr('switch ../branch2')
380
        self.assertLength(1, calls)
5171.3.13 by Martin von Gagern
Add --directory option to 7 more commands.
381
382
    def test_switch_lightweight_directory(self):
383
        """Test --directory option"""
384
385
        # create a source branch
386
        a_tree = self.make_branch_and_tree('a')
6855.4.1 by Jelmer Vernooij
Yet more bees.
387
        self.build_tree_contents([('a/a', b'initial\n')])
5171.3.13 by Martin von Gagern
Add --directory option to 7 more commands.
388
        a_tree.add('a')
389
        a_tree.commit(message='initial')
390
391
        # clone and add a differing revision
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
392
        b_tree = a_tree.controldir.sprout('b').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
393
        self.build_tree_contents([('b/a', b'initial\nmore\n')])
5171.3.13 by Martin von Gagern
Add --directory option to 7 more commands.
394
        b_tree.commit(message='more')
395
396
        self.run_bzr('checkout --lightweight a checkout')
397
        self.run_bzr('switch --directory checkout b')
7029.4.2 by Jelmer Vernooij
Fix more merge tests.
398
        self.assertFileEqual(b'initial\nmore\n', 'checkout/a')
5816.6.7 by A. S. Budden
Moved test harnesses into test_switch and test_branch as these are the commands that are being tested.
399
6015.7.2 by John Arbash Meinel
Bug #812285. Add an effort test to show that things have improved.
400
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
401
class TestSwitchParentLocationBase(TestCaseWithTransport):
5816.6.7 by A. S. Budden
Moved test harnesses into test_switch and test_branch as these are the commands that are being tested.
402
403
    def setUp(self):
404
        """Set up a repository and branch ready for testing."""
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
405
        super(TestSwitchParentLocationBase, self).setUp()
5816.6.7 by A. S. Budden
Moved test harnesses into test_switch and test_branch as these are the commands that are being tested.
406
        self.script_runner = script.ScriptRunner()
407
        self.script_runner.run_script(self, '''
7385.2.1 by Jelmer Vernooij
Rename init-repo to init-shared-repo.
408
                $ brz init-shared-repo --no-trees repo
5816.6.7 by A. S. Budden
Moved test harnesses into test_switch and test_branch as these are the commands that are being tested.
409
                Shared repository...
410
                Location:
411
                  shared repository: repo
6622.1.29 by Jelmer Vernooij
Fix some more tests.
412
                $ brz init repo/trunk
5816.6.7 by A. S. Budden
Moved test harnesses into test_switch and test_branch as these are the commands that are being tested.
413
                Created a repository branch...
414
                Using shared repository: ...
415
                ''')
416
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
417
    def assertParent(self, expected_parent, branch):
5816.6.11 by A. S. Budden
Refactored assertParentCorrect to check the full path.
418
        """Verify that the parent is not None and is set correctly."""
419
        actual_parent = branch.get_parent()
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
420
        self.assertIsSameRealPath(urlutils.local_path_to_url(expected_parent),
421
                                  branch.get_parent())
422
423
424
class TestSwitchParentLocation(TestSwitchParentLocationBase):
425
426
    def _checkout_and_switch(self, option=''):
5816.6.8 by A. S. Budden
Refactored to use common generation code for lightweight and heavyweight.
427
        self.script_runner.run_script(self, '''
6622.1.29 by Jelmer Vernooij
Fix some more tests.
428
                $ brz checkout %(option)s repo/trunk checkout
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
429
                $ cd checkout
6622.1.29 by Jelmer Vernooij
Fix some more tests.
430
                $ brz switch --create-branch switched
5816.6.8 by A. S. Budden
Refactored to use common generation code for lightweight and heavyweight.
431
                2>Tree is up to date at revision 0.
7223.1.1 by Jelmer Vernooij
Display colocated branch name without full path when switching between colocated branches.
432
                2>Switched to branch at .../switched/
5816.6.11 by A. S. Budden
Refactored assertParentCorrect to check the full path.
433
                $ cd ..
5816.6.9 by A. S. Budden
Use locals() instead of kwargs so that parameters are more explicit.
434
                ''' % locals())
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
435
        bound_branch = branch.Branch.open_containing('checkout')[0]
436
        master_branch = branch.Branch.open_containing('repo/switched')[0]
5816.6.12 by A. S. Budden
Check parent branch of both the checkout (light or heavy) and the branch to which it is connected.
437
        return (bound_branch, master_branch)
5816.6.8 by A. S. Budden
Refactored to use common generation code for lightweight and heavyweight.
438
5816.6.7 by A. S. Budden
Moved test harnesses into test_switch and test_branch as these are the commands that are being tested.
439
    def test_switch_parent_lightweight(self):
6622.1.29 by Jelmer Vernooij
Fix some more tests.
440
        """Lightweight checkout using brz switch."""
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
441
        bb, mb = self._checkout_and_switch(option='--lightweight')
442
        self.assertParent('repo/trunk', bb)
443
        self.assertParent('repo/trunk', mb)
5816.6.7 by A. S. Budden
Moved test harnesses into test_switch and test_branch as these are the commands that are being tested.
444
445
    def test_switch_parent_heavyweight(self):
6622.1.29 by Jelmer Vernooij
Fix some more tests.
446
        """Heavyweight checkout using brz switch."""
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
447
        bb, mb = self._checkout_and_switch()
448
        self.assertParent('repo/trunk', bb)
449
        self.assertParent('repo/trunk', mb)
5816.6.11 by A. S. Budden
Refactored assertParentCorrect to check the full path.
450
6015.7.2 by John Arbash Meinel
Bug #812285. Add an effort test to show that things have improved.
451
452
class TestSwitchDoesntOpenMasterBranch(TestCaseWithTransport):
453
    # See https://bugs.launchpad.net/bzr/+bug/812285
6622.1.29 by Jelmer Vernooij
Fix some more tests.
454
    # "brz switch --create-branch" can point the new branch's parent to the
6015.7.2 by John Arbash Meinel
Bug #812285. Add an effort test to show that things have improved.
455
    # master branch, but it doesn't have to open it to do so.
456
457
    def test_switch_create_doesnt_open_master_branch(self):
458
        master = self.make_branch_and_tree('master')
459
        master.commit('one')
460
        # Note: not a lightweight checkout
461
        checkout = master.branch.create_checkout('checkout')
462
        opened = []
7143.15.2 by Jelmer Vernooij
Run autopep8.
463
6015.7.2 by John Arbash Meinel
Bug #812285. Add an effort test to show that things have improved.
464
        def open_hook(branch):
465
            # Just append the final directory of the branch
466
            name = branch.base.rstrip('/').rsplit('/', 1)[1]
467
            opened.append(name)
468
        branch.Branch.hooks.install_named_hook('open', open_hook,
469
                                               'open_hook_logger')
470
        self.run_bzr('switch --create-branch -d checkout feature')
471
        # We only open the master branch 1 time.
472
        # This test should be cleaner to write, but see bug:
473
        #  https://bugs.launchpad.net/bzr/+bug/812295
474
        self.assertEqual(1, opened.count('master'))
6366.1.5 by Jelmer Vernooij
add test for hpss call / connection count of 'bzr switch'.
475
476
6538.1.17 by Aaron Bentley
Switch command stores/restores uncommitted changes.
477
class TestSwitchUncommitted(TestCaseWithTransport):
478
6538.1.18 by Aaron Bentley
Add --with-changes flag to switch.
479
    def prepare(self):
6538.1.17 by Aaron Bentley
Switch command stores/restores uncommitted changes.
480
        tree = self.make_branch_and_tree('orig')
481
        tree.commit('')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
482
        tree.branch.controldir.sprout('new')
6538.1.17 by Aaron Bentley
Switch command stores/restores uncommitted changes.
483
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
484
        self.build_tree(['checkout/a'])
485
        self.assertPathExists('checkout/a')
486
        checkout.add('a')
6538.1.34 by Aaron Bentley
Update blackbox test to show storing and restoring in one switch.
487
        return checkout
6538.1.18 by Aaron Bentley
Add --with-changes flag to switch.
488
6538.1.30 by Aaron Bentley
Make *not* storing on switch the default.
489
    def test_store_and_restore_uncommitted(self):
6538.1.34 by Aaron Bentley
Update blackbox test to show storing and restoring in one switch.
490
        checkout = self.prepare()
6538.1.30 by Aaron Bentley
Make *not* storing on switch the default.
491
        self.run_bzr(['switch', '--store', '-d', 'checkout', 'new'])
6538.1.34 by Aaron Bentley
Update blackbox test to show storing and restoring in one switch.
492
        self.build_tree(['checkout/b'])
493
        checkout.add('b')
6538.1.30 by Aaron Bentley
Make *not* storing on switch the default.
494
        self.assertPathDoesNotExist('checkout/a')
6538.1.34 by Aaron Bentley
Update blackbox test to show storing and restoring in one switch.
495
        self.assertPathExists('checkout/b')
6538.1.30 by Aaron Bentley
Make *not* storing on switch the default.
496
        self.run_bzr(['switch', '--store', '-d', 'checkout', 'orig'])
497
        self.assertPathExists('checkout/a')
6538.1.34 by Aaron Bentley
Update blackbox test to show storing and restoring in one switch.
498
        self.assertPathDoesNotExist('checkout/b')
6538.1.30 by Aaron Bentley
Make *not* storing on switch the default.
499
500
    def test_does_not_store(self):
6538.1.18 by Aaron Bentley
Add --with-changes flag to switch.
501
        self.prepare()
6538.1.17 by Aaron Bentley
Switch command stores/restores uncommitted changes.
502
        self.run_bzr(['switch', '-d', 'checkout', 'new'])
6538.1.30 by Aaron Bentley
Make *not* storing on switch the default.
503
        self.assertPathExists('checkout/a')
504
505
    def test_does_not_restore_changes(self):
506
        self.prepare()
507
        self.run_bzr(['switch', '--store', '-d', 'checkout', 'new'])
6538.1.17 by Aaron Bentley
Switch command stores/restores uncommitted changes.
508
        self.assertPathDoesNotExist('checkout/a')
509
        self.run_bzr(['switch', '-d', 'checkout', 'orig'])
6538.1.18 by Aaron Bentley
Add --with-changes flag to switch.
510
        self.assertPathDoesNotExist('checkout/a')
6573.2.1 by Spundun Bhatt
Added blackbox unittests for bug https://bugs.launchpad.net/bzr/+bug/1018628
511
6929.8.1 by Jelmer Vernooij
merge tests.
512
6573.2.1 by Spundun Bhatt
Added blackbox unittests for bug https://bugs.launchpad.net/bzr/+bug/1018628
513
class TestSwitchStandAloneCorruption(TestCaseWithTransport):
6929.8.1 by Jelmer Vernooij
merge tests.
514
6573.2.1 by Spundun Bhatt
Added blackbox unittests for bug https://bugs.launchpad.net/bzr/+bug/1018628
515
    def test_empty_tree_switch(self):
6929.8.2 by Jelmer Vernooij
Fix new switch tests.
516
        """switch . on an empty tree gets infinite recursion
517
518
        Inspired by: https://bugs.launchpad.net/bzr/+bug/1018628
6573.2.1 by Spundun Bhatt
Added blackbox unittests for bug https://bugs.launchpad.net/bzr/+bug/1018628
519
        """
520
        self.script_runner = script.ScriptRunner()
521
        self.script_runner.run_script(self, '''
6929.8.2 by Jelmer Vernooij
Fix new switch tests.
522
            $ brz init
523
            Created a standalone tree (format: 2a)
524
            $ brz switch .
525
            2>brz: ERROR: switching would create a branch reference loop. Use the "bzr up" command to switch to a different revision.
526
            ''')
6573.2.1 by Spundun Bhatt
Added blackbox unittests for bug https://bugs.launchpad.net/bzr/+bug/1018628
527
528
    def test_switch_on_previous_rev(self):
6929.8.2 by Jelmer Vernooij
Fix new switch tests.
529
        """switch to previous rev in a standalone directory
530
531
        Inspired by: https://bugs.launchpad.net/brz/+bug/1018628
6573.2.1 by Spundun Bhatt
Added blackbox unittests for bug https://bugs.launchpad.net/bzr/+bug/1018628
532
        """
533
        self.script_runner = script.ScriptRunner()
534
        self.script_runner.run_script(self, '''
6929.8.2 by Jelmer Vernooij
Fix new switch tests.
535
           $ brz init
536
           Created a standalone tree (format: 2a)
537
           $ brz commit -m 1 --unchanged
538
           $ brz commit -m 2 --unchanged
539
           $ brz switch -r 1
540
           2>brz: ERROR: switching would create a branch reference loop. Use the "bzr up" command to switch to a different revision.''',
7143.15.2 by Jelmer Vernooij
Run autopep8.
541
                                      null_output_matches_anything=True)
6573.2.1 by Spundun Bhatt
Added blackbox unittests for bug https://bugs.launchpad.net/bzr/+bug/1018628
542
6573.2.2 by Spundun Bhatt
Even a simple switch test locks the colo repo.
543
    def test_switch_create_colo_locks_repo_path(self):
6573.2.1 by Spundun Bhatt
Added blackbox unittests for bug https://bugs.launchpad.net/bzr/+bug/1018628
544
        self.script_runner = script.ScriptRunner()
545
        self.script_runner.run_script(self, '''
6929.8.2 by Jelmer Vernooij
Fix new switch tests.
546
            $ mkdir mywork
547
            $ cd mywork
548
            $ brz init
549
            Created a standalone tree (format: 2a)
550
            $ echo A > a && brz add a && brz commit -m A
551
            $ brz switch -b br1
552
            $ cd ..
553
            $ mv mywork mywork1
554
            $ cd mywork1
555
            $ brz branches
7236.1.3 by Jelmer Vernooij
Fix test, simplify logic.
556
              br1
6929.8.2 by Jelmer Vernooij
Fix new switch tests.
557
            ''', null_output_matches_anything=True)
7143.4.1 by Jelmer Vernooij
Fix behaviour when specifying a revision to 'bzr switch -b blah'.
558
559
    def test_switch_to_new_branch_on_old_rev(self):
560
        """switch to previous rev in a standalone directory
561
562
        Inspired by: https://bugs.launchpad.net/brz/+bug/933362
563
        """
564
        self.script_runner = script.ScriptRunner()
565
        self.script_runner.run_script(self, '''
566
           $ brz init
567
           Created a standalone tree (format: 2a)
568
           $ brz switch -b trunk
569
           2>Tree is up to date at revision 0.
7223.1.1 by Jelmer Vernooij
Display colocated branch name without full path when switching between colocated branches.
570
           2>Switched to branch trunk
7143.4.1 by Jelmer Vernooij
Fix behaviour when specifying a revision to 'bzr switch -b blah'.
571
           $ brz commit -m 1 --unchanged
572
           2>Committing to: ...
573
           2>Committed revision 1.
574
           $ brz commit -m 2 --unchanged
575
           2>Committing to: ...
576
           2>Committed revision 2.
577
           $ brz switch -b blah -r1
578
           2>Updated to revision 1.
7223.1.1 by Jelmer Vernooij
Display colocated branch name without full path when switching between colocated branches.
579
           2>Switched to branch blah
7143.4.1 by Jelmer Vernooij
Fix behaviour when specifying a revision to 'bzr switch -b blah'.
580
           $ brz branches
581
           * blah
582
             trunk
583
           $ brz st
584
           ''')