~bzr-pqm/bzr/bzr.dev

4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2007-2010 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
5268.8.12 by Jelmer Vernooij
support creating new colocated branches from 'bzr switch -b'.
23
from bzrlib.bzrdir import BzrDir
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.
24
from bzrlib import (
25
        osutils,
5816.6.11 by A. S. Budden
Refactored assertParentCorrect to check the full path.
26
        urlutils,
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.
27
        branch,
28
        )
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
29
from bzrlib.workingtree import WorkingTree
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.
30
from bzrlib.tests import (
31
        TestCaseWithTransport,
32
        script,
33
        )
5268.8.18 by Jelmer Vernooij
Use UnicodeFilenameFeature.
34
from bzrlib.tests.features import UnicodeFilenameFeature
4879.2.1 by Neil Martinsen-Burrell
switch should use directory services when creating a branch
35
from bzrlib.directory_service import directories
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
36
6366.1.5 by Jelmer Vernooij
add test for hpss call / connection count of 'bzr switch'.
37
from bzrlib.tests.matchers import ContainsNoVfsCalls
38
5816.6.15 by A. S. Budden
Added erroneously removed blank line.
39
5283.4.5 by Martin Pool
Update remaining subclasses of ExternalBase
40
class TestSwitch(TestCaseWithTransport):
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
41
3984.5.14 by Daniel Watkins
Extracted common setup code.
42
    def _create_sample_tree(self):
43
        tree = self.make_branch_and_tree('branch-1')
44
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
45
        tree.add('file-1')
46
        tree.commit('rev1')
47
        tree.add('file-2')
48
        tree.commit('rev2')
49
        return tree
50
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
51
    def test_switch_up_to_date_light_checkout(self):
52
        self.make_branch_and_tree('branch')
53
        self.run_bzr('branch branch branch2')
54
        self.run_bzr('checkout --lightweight branch checkout')
55
        os.chdir('checkout')
56
        out, err = self.run_bzr('switch ../branch2')
57
        self.assertContainsRe(err, 'Tree is up to date at revision 0.\n')
58
        self.assertContainsRe(err, 'Switched to branch: .*/branch2.\n')
59
        self.assertEqual('', out)
60
61
    def test_switch_out_of_date_light_checkout(self):
62
        self.make_branch_and_tree('branch')
63
        self.run_bzr('branch branch branch2')
64
        self.build_tree(['branch2/file'])
65
        self.run_bzr('add branch2/file')
66
        self.run_bzr('commit -m add-file branch2')
67
        self.run_bzr('checkout --lightweight branch checkout')
68
        os.chdir('checkout')
69
        out, err = self.run_bzr('switch ../branch2')
70
        #self.assertContainsRe(err, '\+N  file')
71
        self.assertContainsRe(err, 'Updated to revision 1.\n')
72
        self.assertContainsRe(err, 'Switched to branch: .*/branch2.\n')
73
        self.assertEqual('', out)
3246.5.1 by Robert Collins
* ``bzr switch`` will attempt to find branches to switch to relative to the
74
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
75
    def _test_switch_nick(self, lightweight):
76
        """Check that the nick gets switched too."""
77
        tree1 = self.make_branch_and_tree('branch1')
78
        tree2 = self.make_branch_and_tree('branch2')
79
        tree2.pull(tree1.branch)
80
        checkout =  tree1.branch.create_checkout('checkout',
81
            lightweight=lightweight)
82
        self.assertEqual(checkout.branch.nick, tree1.branch.nick)
83
        self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
84
            False)
85
        self.run_bzr('switch branch2', working_dir='checkout')
86
87
        # we need to get the tree again, otherwise we don't get the new branch
88
        checkout = WorkingTree.open('checkout')
89
        self.assertEqual(checkout.branch.nick, tree2.branch.nick)
90
        self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
91
            False)
92
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
93
    def test_switch_nick(self):
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
94
        self._test_switch_nick(lightweight=False)
95
96
    def test_switch_nick_lightweight(self):
97
        self._test_switch_nick(lightweight=True)
98
99
    def _test_switch_explicit_nick(self, lightweight):
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
100
        """Check that the nick gets switched too."""
101
        tree1 = self.make_branch_and_tree('branch1')
102
        tree2 = self.make_branch_and_tree('branch2')
103
        tree2.pull(tree1.branch)
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
104
        checkout =  tree1.branch.create_checkout('checkout',
105
            lightweight=lightweight)
106
        self.assertEqual(checkout.branch.nick, tree1.branch.nick)
107
        checkout.branch.nick = "explicit_nick"
108
        self.assertEqual(checkout.branch.nick, "explicit_nick")
109
        self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
110
            "explicit_nick")
111
        self.run_bzr('switch branch2', working_dir='checkout')
112
113
        # we need to get the tree again, otherwise we don't get the new branch
114
        checkout = WorkingTree.open('checkout')
115
        self.assertEqual(checkout.branch.nick, tree2.branch.nick)
116
        self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
117
            tree2.branch.nick)
118
119
    def test_switch_explicit_nick(self):
120
        self._test_switch_explicit_nick(lightweight=False)
121
122
    def test_switch_explicit_nick_lightweight(self):
123
        self._test_switch_explicit_nick(lightweight=True)
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
124
3246.5.1 by Robert Collins
* ``bzr switch`` will attempt to find branches to switch to relative to the
125
    def test_switch_finds_relative_branch(self):
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
126
        """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
127
        self.build_tree(['repo/'])
128
        tree1 = self.make_branch_and_tree('repo/brancha')
129
        tree1.commit('foo')
130
        tree2 = self.make_branch_and_tree('repo/branchb')
131
        tree2.pull(tree1.branch)
132
        branchb_id = tree2.commit('bar')
133
        checkout =  tree1.branch.create_checkout('checkout', lightweight=True)
134
        self.run_bzr(['switch', 'branchb'], working_dir='checkout')
135
        self.assertEqual(branchb_id, checkout.last_revision())
136
        checkout = checkout.bzrdir.open_workingtree()
137
        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.
138
139
    def test_switch_finds_relative_bound_branch(self):
3602.3.4 by Adrian Wilkins
Improved comments and documentation
140
        """Using switch on a heavy checkout should find master sibling
141
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
142
        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.
143
        consistent when using the convenient "switch to sibling" feature
3602.3.4 by Adrian Wilkins
Improved comments and documentation
144
        Both should switch to a sibling of the branch
145
        they are bound to, and not a sibling of themself"""
146
3602.3.1 by Adrian Wilkins
Test that `bzr switch` finds the sibling of the bound branch of heavy checkout.
147
        self.build_tree(['repo/',
148
                         'heavyco/'])
149
        tree1 = self.make_branch_and_tree('repo/brancha')
150
        tree1.commit('foo')
151
        tree2 = self.make_branch_and_tree('repo/branchb')
152
        tree2.pull(tree1.branch)
153
        branchb_id = tree2.commit('bar')
154
        checkout = tree1.branch.create_checkout('heavyco/a', lightweight=False)
155
        self.run_bzr(['switch', 'branchb'], working_dir='heavyco/a')
156
        self.assertEqual(branchb_id, checkout.last_revision())
157
        self.assertEqual(tree2.branch.base, checkout.branch.get_bound_location())
4354.2.2 by Aaron Bentley
Enable switch --force for lightweight checkouts after moves.
158
5268.8.15 by Jelmer Vernooij
Fix switching to unicode branch names.
159
    def test_switch_finds_relative_unicode_branch(self):
160
        """Switch will find 'foo' relative to the branch the checkout is of."""
5268.8.18 by Jelmer Vernooij
Use UnicodeFilenameFeature.
161
        self.requireFeature(UnicodeFilenameFeature)
5268.8.15 by Jelmer Vernooij
Fix switching to unicode branch names.
162
        self.build_tree(['repo/'])
163
        tree1 = self.make_branch_and_tree('repo/brancha')
164
        tree1.commit('foo')
165
        tree2 = self.make_branch_and_tree(u'repo/branch\xe9')
166
        tree2.pull(tree1.branch)
167
        branchb_id = tree2.commit('bar')
168
        checkout =  tree1.branch.create_checkout('checkout', lightweight=True)
169
        self.run_bzr(['switch', u'branch\xe9'], working_dir='checkout')
170
        self.assertEqual(branchb_id, checkout.last_revision())
171
        checkout = checkout.bzrdir.open_workingtree()
172
        self.assertEqual(tree2.branch.base, checkout.branch.base)
173
3984.5.2 by Daniel Watkins
Added blackbox test.
174
    def test_switch_revision(self):
3984.5.14 by Daniel Watkins
Extracted common setup code.
175
        tree = self._create_sample_tree()
3984.5.2 by Daniel Watkins
Added blackbox test.
176
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
177
        self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
178
        self.assertPathExists('checkout/file-1')
179
        self.assertPathDoesNotExist('checkout/file-2')
3984.5.9 by Daniel Watkins
Added test for allowing only revisions to be passed to switch.
180
6437.7.5 by Jelmer Vernooij
"bzr switch -b" in a standalone tree will now create a colocated branch.
181
    def test_switch_into_colocated(self):
182
        # Create a new colocated branch from an existing non-colocated branch.
183
        tree = self.make_branch_and_tree('.', format='development-colo')
184
        self.build_tree(['file-1', 'file-2'])
185
        tree.add('file-1')
186
        revid1 = tree.commit('rev1')
187
        tree.add('file-2')
188
        revid2 = tree.commit('rev2')
189
        self.run_bzr(['switch', '-b', 'anotherbranch'])
190
        self.assertEquals(
191
            ['', 'anotherbranch'],
192
            tree.branch.bzrdir.get_branches().keys())
193
6437.7.6 by Jelmer Vernooij
Add test for --force argument used in standalone branch.
194
    def test_switch_into_unrelated_colocated(self):
195
        # Create a new colocated branch from an existing non-colocated branch.
196
        tree = self.make_branch_and_tree('.', format='development-colo')
197
        self.build_tree(['file-1', 'file-2'])
198
        tree.add('file-1')
199
        revid1 = tree.commit('rev1')
200
        tree.add('file-2')
201
        revid2 = tree.commit('rev2')
202
        tree.bzrdir.create_branch(name='foo')
203
        self.run_bzr_error(['Cannot switch a branch, only a checkout.'],
204
            'switch foo')
205
        self.run_bzr(['switch', '--force', 'foo'])
206
5268.8.12 by Jelmer Vernooij
support creating new colocated branches from 'bzr switch -b'.
207
    def test_switch_existing_colocated(self):
5268.8.6 by Jelmer Vernooij
More fixes to test.
208
        # Create a branch branch-1 that initially is a checkout of 'foo'
209
        # Use switch to change it to 'anotherbranch'
210
        repo = self.make_repository('branch-1', format='development-colo')
211
        target_branch = repo.bzrdir.create_branch(name='foo')
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
212
        repo.bzrdir.set_branch_reference(target_branch)
5268.8.6 by Jelmer Vernooij
More fixes to test.
213
        tree = repo.bzrdir.create_workingtree()
5268.8.5 by Jelmer Vernooij
Add tests for switching to colocated branches.
214
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
215
        tree.add('file-1')
216
        revid1 = tree.commit('rev1')
217
        tree.add('file-2')
218
        revid2 = tree.commit('rev2')
5268.8.6 by Jelmer Vernooij
More fixes to test.
219
        otherbranch = tree.bzrdir.create_branch(name='anotherbranch')
5268.8.5 by Jelmer Vernooij
Add tests for switching to colocated branches.
220
        otherbranch.generate_revision_history(revid1)
221
        self.run_bzr(['switch', 'anotherbranch'], working_dir='branch-1')
5268.8.10 by Jelmer Vernooij
bzr switch can now switch to colocated branches.
222
        tree = WorkingTree.open("branch-1")
5268.8.5 by Jelmer Vernooij
Add tests for switching to colocated branches.
223
        self.assertEquals(tree.last_revision(), revid1)
5268.8.10 by Jelmer Vernooij
bzr switch can now switch to colocated branches.
224
        self.assertEquals(tree.branch.control_url, otherbranch.control_url)
5268.8.5 by Jelmer Vernooij
Add tests for switching to colocated branches.
225
5268.8.12 by Jelmer Vernooij
support creating new colocated branches from 'bzr switch -b'.
226
    def test_switch_new_colocated(self):
227
        # Create a branch branch-1 that initially is a checkout of 'foo'
228
        # Use switch to create 'anotherbranch' which derives from that
229
        repo = self.make_repository('branch-1', format='development-colo')
230
        target_branch = repo.bzrdir.create_branch(name='foo')
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
231
        repo.bzrdir.set_branch_reference(target_branch)
5268.8.12 by Jelmer Vernooij
support creating new colocated branches from 'bzr switch -b'.
232
        tree = repo.bzrdir.create_workingtree()
233
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
234
        tree.add('file-1')
235
        revid1 = tree.commit('rev1')
236
        self.run_bzr(['switch', '-b', 'anotherbranch'], working_dir='branch-1')
237
        bzrdir = BzrDir.open("branch-1")
238
        self.assertEquals(
239
            set([b.name for b in bzrdir.list_branches()]),
240
            set(["foo", "anotherbranch"]))
241
        self.assertEquals(bzrdir.open_branch().name, "anotherbranch")
242
        self.assertEquals(bzrdir.open_branch().last_revision(), revid1)
243
5268.8.17 by Jelmer Vernooij
Support switching to new unicode colocated branch.
244
    def test_switch_new_colocated_unicode(self):
245
        # Create a branch branch-1 that initially is a checkout of 'foo'
246
        # Use switch to create 'branch\xe9' which derives from that
5268.8.18 by Jelmer Vernooij
Use UnicodeFilenameFeature.
247
        self.requireFeature(UnicodeFilenameFeature)
5268.8.17 by Jelmer Vernooij
Support switching to new unicode colocated branch.
248
        repo = self.make_repository('branch-1', format='development-colo')
249
        target_branch = repo.bzrdir.create_branch(name='foo')
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
250
        repo.bzrdir.set_branch_reference(target_branch)
5268.8.17 by Jelmer Vernooij
Support switching to new unicode colocated branch.
251
        tree = repo.bzrdir.create_workingtree()
252
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
253
        tree.add('file-1')
254
        revid1 = tree.commit('rev1')
255
        self.run_bzr(['switch', '-b', u'branch\xe9'], working_dir='branch-1')
256
        bzrdir = BzrDir.open("branch-1")
257
        self.assertEquals(
258
            set([b.name for b in bzrdir.list_branches()]),
259
            set(["foo", u"branch\xe9"]))
260
        self.assertEquals(bzrdir.open_branch().name, u"branch\xe9")
261
        self.assertEquals(bzrdir.open_branch().last_revision(), revid1)
262
3984.5.9 by Daniel Watkins
Added test for allowing only revisions to be passed to switch.
263
    def test_switch_only_revision(self):
3984.5.14 by Daniel Watkins
Extracted common setup code.
264
        tree = self._create_sample_tree()
3984.5.9 by Daniel Watkins
Added test for allowing only revisions to be passed to switch.
265
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
266
        self.assertPathExists('checkout/file-1')
267
        self.assertPathExists('checkout/file-2')
3984.5.9 by Daniel Watkins
Added test for allowing only revisions to be passed to switch.
268
        self.run_bzr(['switch', '-r1'], working_dir='checkout')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
269
        self.assertPathExists('checkout/file-1')
270
        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.
271
        # Check that we don't accept a range
3984.5.17 by Daniel Watkins
Fixed incorrect call of run_bzr_error.
272
        self.run_bzr_error(
273
            ['bzr switch --revision takes exactly one revision identifier'],
274
            ['switch', '-r0..2'], working_dir='checkout')
3984.5.19 by Andrew Bennetts
Merge lp:bzr, resolving conflicts.
275
4354.2.2 by Aaron Bentley
Enable switch --force for lightweight checkouts after moves.
276
    def prepare_lightweight_switch(self):
277
        branch = self.make_branch('branch')
278
        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
279
        osutils.rename('branch', 'branch1')
4354.2.2 by Aaron Bentley
Enable switch --force for lightweight checkouts after moves.
280
281
    def test_switch_lightweight_after_branch_moved(self):
282
        self.prepare_lightweight_switch()
283
        self.run_bzr('switch --force ../branch1', working_dir='tree')
284
        branch_location = WorkingTree.open('tree').branch.base
285
        self.assertEndsWith(branch_location, 'branch1/')
286
287
    def test_switch_lightweight_after_branch_moved_relative(self):
288
        self.prepare_lightweight_switch()
289
        self.run_bzr('switch --force branch1', working_dir='tree')
290
        branch_location = WorkingTree.open('tree').branch.base
291
        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.
292
293
    def test_create_branch_no_branch(self):
294
        self.prepare_lightweight_switch()
295
        self.run_bzr_error(['cannot create branch without source branch'],
296
            'switch --create-branch ../branch2', working_dir='tree')
297
298
    def test_create_branch(self):
299
        branch = self.make_branch('branch')
300
        tree = branch.create_checkout('tree', lightweight=True)
301
        tree.commit('one', rev_id='rev-1')
302
        self.run_bzr('switch --create-branch ../branch2', working_dir='tree')
303
        tree = WorkingTree.open('tree')
304
        self.assertEndsWith(tree.branch.base, '/branch2/')
305
306
    def test_create_branch_local(self):
307
        branch = self.make_branch('branch')
308
        tree = branch.create_checkout('tree', lightweight=True)
309
        tree.commit('one', rev_id='rev-1')
310
        self.run_bzr('switch --create-branch branch2', working_dir='tree')
311
        tree = WorkingTree.open('tree')
312
        # The new branch should have been created at the same level as
313
        # 'branch', because we did not have a '/' segment
314
        self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
315
316
    def test_create_branch_short_name(self):
317
        branch = self.make_branch('branch')
318
        tree = branch.create_checkout('tree', lightweight=True)
319
        tree.commit('one', rev_id='rev-1')
320
        self.run_bzr('switch -b branch2', working_dir='tree')
321
        tree = WorkingTree.open('tree')
322
        # The new branch should have been created at the same level as
323
        # 'branch', because we did not have a '/' segment
324
        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
325
326
    def test_create_branch_directory_services(self):
327
        branch = self.make_branch('branch')
328
        tree = branch.create_checkout('tree', lightweight=True)
329
        class FooLookup(object):
330
            def look_up(self, name, url):
331
                return 'foo-'+name
332
        directories.register('foo:', FooLookup, 'Create branches named foo-')
4879.2.2 by Neil Martinsen-Burrell
add test cleanup per JAMs review
333
        self.addCleanup(directories.remove, 'foo:')
4879.2.1 by Neil Martinsen-Burrell
switch should use directory services when creating a branch
334
        self.run_bzr('switch -b foo:branch2', working_dir='tree')
335
        tree = WorkingTree.open('tree')
336
        self.assertEndsWith(tree.branch.base, 'foo-branch2/')
5107.3.6 by Marco Pantaleoni
Documented behaviour of 'post_branch_init' for lightweight checkouts.
337
338
    def test_switch_with_post_switch_hook(self):
339
        from bzrlib import branch as _mod_branch
340
        calls = []
341
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
342
            calls.append, None)
343
        self.make_branch_and_tree('branch')
344
        self.run_bzr('branch branch branch2')
345
        self.run_bzr('checkout branch checkout')
346
        os.chdir('checkout')
347
        self.assertLength(0, calls)
348
        out, err = self.run_bzr('switch ../branch2')
349
        self.assertLength(1, calls)
350
351
    def test_switch_lightweight_co_with_post_switch_hook(self):
352
        from bzrlib import branch as _mod_branch
353
        calls = []
354
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
355
            calls.append, None)
356
        self.make_branch_and_tree('branch')
357
        self.run_bzr('branch branch branch2')
358
        self.run_bzr('checkout --lightweight branch checkout')
359
        os.chdir('checkout')
360
        self.assertLength(0, calls)
361
        out, err = self.run_bzr('switch ../branch2')
362
        self.assertLength(1, calls)
5171.3.13 by Martin von Gagern
Add --directory option to 7 more commands.
363
364
    def test_switch_lightweight_directory(self):
365
        """Test --directory option"""
366
367
        # create a source branch
368
        a_tree = self.make_branch_and_tree('a')
369
        self.build_tree_contents([('a/a', 'initial\n')])
370
        a_tree.add('a')
371
        a_tree.commit(message='initial')
372
373
        # clone and add a differing revision
374
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
375
        self.build_tree_contents([('b/a', 'initial\nmore\n')])
376
        b_tree.commit(message='more')
377
378
        self.run_bzr('checkout --lightweight a checkout')
379
        self.run_bzr('switch --directory checkout b')
380
        self.assertFileEqual('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.
381
6015.7.2 by John Arbash Meinel
Bug #812285. Add an effort test to show that things have improved.
382
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
383
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.
384
385
    def setUp(self):
386
        """Set up a repository and branch ready for testing."""
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
387
        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.
388
        self.script_runner = script.ScriptRunner()
389
        self.script_runner.run_script(self, '''
390
                $ bzr init-repo --no-trees repo
391
                Shared repository...
392
                Location:
393
                  shared repository: repo
394
                $ bzr init repo/trunk
395
                Created a repository branch...
396
                Using shared repository: ...
397
                ''')
398
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
399
    def assertParent(self, expected_parent, branch):
5816.6.11 by A. S. Budden
Refactored assertParentCorrect to check the full path.
400
        """Verify that the parent is not None and is set correctly."""
401
        actual_parent = branch.get_parent()
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
402
        self.assertIsSameRealPath(urlutils.local_path_to_url(expected_parent),
403
                                  branch.get_parent())
404
405
406
class TestSwitchParentLocation(TestSwitchParentLocationBase):
407
408
    def _checkout_and_switch(self, option=''):
5816.6.8 by A. S. Budden
Refactored to use common generation code for lightweight and heavyweight.
409
        self.script_runner.run_script(self, '''
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
410
                $ bzr checkout %(option)s repo/trunk checkout
411
                $ cd checkout
412
                $ bzr switch --create-branch switched
5816.6.8 by A. S. Budden
Refactored to use common generation code for lightweight and heavyweight.
413
                2>Tree is up to date at revision 0.
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
414
                2>Switched to branch:...switched...
5816.6.11 by A. S. Budden
Refactored assertParentCorrect to check the full path.
415
                $ cd ..
5816.6.9 by A. S. Budden
Use locals() instead of kwargs so that parameters are more explicit.
416
                ''' % locals())
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
417
        bound_branch = branch.Branch.open_containing('checkout')[0]
418
        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.
419
        return (bound_branch, master_branch)
5816.6.8 by A. S. Budden
Refactored to use common generation code for lightweight and heavyweight.
420
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.
421
    def test_switch_parent_lightweight(self):
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
422
        """Lightweight checkout using bzr switch."""
423
        bb, mb = self._checkout_and_switch(option='--lightweight')
424
        self.assertParent('repo/trunk', bb)
425
        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.
426
427
    def test_switch_parent_heavyweight(self):
5816.7.1 by Vincent Ladeuil
Remove duplication and simplify.
428
        """Heavyweight checkout using bzr switch."""
429
        bb, mb = self._checkout_and_switch()
430
        self.assertParent('repo/trunk', bb)
431
        self.assertParent('repo/trunk', mb)
5816.6.11 by A. S. Budden
Refactored assertParentCorrect to check the full path.
432
6015.7.2 by John Arbash Meinel
Bug #812285. Add an effort test to show that things have improved.
433
434
class TestSwitchDoesntOpenMasterBranch(TestCaseWithTransport):
435
    # See https://bugs.launchpad.net/bzr/+bug/812285
436
    # "bzr switch --create-branch" can point the new branch's parent to the
437
    # master branch, but it doesn't have to open it to do so.
438
439
    def test_switch_create_doesnt_open_master_branch(self):
440
        master = self.make_branch_and_tree('master')
441
        master.commit('one')
442
        # Note: not a lightweight checkout
443
        checkout = master.branch.create_checkout('checkout')
444
        opened = []
445
        def open_hook(branch):
446
            # Just append the final directory of the branch
447
            name = branch.base.rstrip('/').rsplit('/', 1)[1]
448
            opened.append(name)
449
        branch.Branch.hooks.install_named_hook('open', open_hook,
450
                                               'open_hook_logger')
451
        self.run_bzr('switch --create-branch -d checkout feature')
452
        # We only open the master branch 1 time.
453
        # This test should be cleaner to write, but see bug:
454
        #  https://bugs.launchpad.net/bzr/+bug/812295
455
        self.assertEqual(1, opened.count('master'))
6366.1.5 by Jelmer Vernooij
add test for hpss call / connection count of 'bzr switch'.
456
457
458
class TestSmartServerSwitch(TestCaseWithTransport):
459
460
    def test_switch_lightweight(self):
461
        self.setup_smart_server_with_call_log()
462
        t = self.make_branch_and_tree('from')
463
        for count in range(9):
464
            t.commit(message='commit %d' % count)
465
        out, err = self.run_bzr(['checkout', '--lightweight', self.get_url('from'),
466
            'target'])
467
        self.reset_smart_call_log()
468
        self.run_bzr(['switch', self.get_url('from')], working_dir='target')
469
        # This figure represent the amount of work to perform this use case. It
470
        # is entirely ok to reduce this number if a test fails due to rpc_count
471
        # being too low. If rpc_count increases, more network roundtrips have
472
        # become necessary for this use case. Please do not adjust this number
473
        # upwards without agreement from bzr's network support maintainers.
6366.1.6 by Jelmer Vernooij
Merge bzr.dev.
474
        self.assertLength(24, self.hpss_calls)
475
        self.assertLength(5, self.hpss_connections)
6366.1.5 by Jelmer Vernooij
add test for hpss call / connection count of 'bzr switch'.
476
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)