/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_switch.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2007-2010 Canonical Ltd
2
2
# -*- coding: utf-8 -*-
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
20
20
 
21
21
import os
22
22
 
23
 
from breezy.controldir import ControlDir
24
 
from breezy import (
25
 
    osutils,
26
 
    urlutils,
27
 
    branch,
28
 
    )
29
 
from breezy.workingtree import WorkingTree
30
 
from breezy.tests import (
31
 
    TestCaseWithTransport,
32
 
    script,
33
 
    )
34
 
from breezy.tests.features import UnicodeFilenameFeature
35
 
from breezy.directory_service import directories
36
 
 
37
 
from breezy.tests.matchers import ContainsNoVfsCalls
38
 
 
39
 
 
40
 
class TestSwitch(TestCaseWithTransport):
 
23
from bzrlib import osutils
 
24
from bzrlib.workingtree import WorkingTree
 
25
from bzrlib.tests.blackbox import ExternalBase
 
26
from bzrlib.directory_service import directories
 
27
 
 
28
 
 
29
class TestSwitch(ExternalBase):
41
30
 
42
31
    def _create_sample_tree(self):
43
32
        tree = self.make_branch_and_tree('branch-1')
55
44
        os.chdir('checkout')
56
45
        out, err = self.run_bzr('switch ../branch2')
57
46
        self.assertContainsRe(err, 'Tree is up to date at revision 0.\n')
58
 
        self.assertContainsRe(err, 'Switched to branch at .*/branch2.\n')
 
47
        self.assertContainsRe(err, 'Switched to branch: .*/branch2.\n')
59
48
        self.assertEqual('', out)
60
49
 
61
50
    def test_switch_out_of_date_light_checkout(self):
69
58
        out, err = self.run_bzr('switch ../branch2')
70
59
        #self.assertContainsRe(err, '\+N  file')
71
60
        self.assertContainsRe(err, 'Updated to revision 1.\n')
72
 
        self.assertContainsRe(err, 'Switched to branch at .*/branch2.\n')
 
61
        self.assertContainsRe(err, 'Switched to branch: .*/branch2.\n')
73
62
        self.assertEqual('', out)
74
63
 
75
64
    def _test_switch_nick(self, lightweight):
77
66
        tree1 = self.make_branch_and_tree('branch1')
78
67
        tree2 = self.make_branch_and_tree('branch2')
79
68
        tree2.pull(tree1.branch)
80
 
        checkout = tree1.branch.create_checkout('checkout',
81
 
                                                lightweight=lightweight)
 
69
        checkout =  tree1.branch.create_checkout('checkout',
 
70
            lightweight=lightweight)
82
71
        self.assertEqual(checkout.branch.nick, tree1.branch.nick)
83
72
        self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
84
 
                         False)
 
73
            False)
85
74
        self.run_bzr('switch branch2', working_dir='checkout')
86
75
 
87
76
        # we need to get the tree again, otherwise we don't get the new branch
88
77
        checkout = WorkingTree.open('checkout')
89
78
        self.assertEqual(checkout.branch.nick, tree2.branch.nick)
90
79
        self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
91
 
                         False)
 
80
            False)
92
81
 
93
82
    def test_switch_nick(self):
94
83
        self._test_switch_nick(lightweight=False)
101
90
        tree1 = self.make_branch_and_tree('branch1')
102
91
        tree2 = self.make_branch_and_tree('branch2')
103
92
        tree2.pull(tree1.branch)
104
 
        checkout = tree1.branch.create_checkout('checkout',
105
 
                                                lightweight=lightweight)
 
93
        checkout =  tree1.branch.create_checkout('checkout',
 
94
            lightweight=lightweight)
106
95
        self.assertEqual(checkout.branch.nick, tree1.branch.nick)
107
96
        checkout.branch.nick = "explicit_nick"
108
97
        self.assertEqual(checkout.branch.nick, "explicit_nick")
109
98
        self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
110
 
                         "explicit_nick")
 
99
            "explicit_nick")
111
100
        self.run_bzr('switch branch2', working_dir='checkout')
112
101
 
113
102
        # we need to get the tree again, otherwise we don't get the new branch
114
103
        checkout = WorkingTree.open('checkout')
115
104
        self.assertEqual(checkout.branch.nick, tree2.branch.nick)
116
105
        self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
117
 
                         tree2.branch.nick)
 
106
            tree2.branch.nick)
118
107
 
119
108
    def test_switch_explicit_nick(self):
120
109
        self._test_switch_explicit_nick(lightweight=False)
130
119
        tree2 = self.make_branch_and_tree('repo/branchb')
131
120
        tree2.pull(tree1.branch)
132
121
        branchb_id = tree2.commit('bar')
133
 
        checkout = tree1.branch.create_checkout('checkout', lightweight=True)
 
122
        checkout =  tree1.branch.create_checkout('checkout', lightweight=True)
134
123
        self.run_bzr(['switch', 'branchb'], working_dir='checkout')
135
124
        self.assertEqual(branchb_id, checkout.last_revision())
136
 
        checkout = checkout.controldir.open_workingtree()
 
125
        checkout = checkout.bzrdir.open_workingtree()
137
126
        self.assertEqual(tree2.branch.base, checkout.branch.base)
138
127
 
139
128
    def test_switch_finds_relative_bound_branch(self):
140
129
        """Using switch on a heavy checkout should find master sibling
141
130
 
142
131
        The behaviour of lighweight and heavy checkouts should be
143
 
        consistent when using the convenient "switch to sibling" feature
 
132
        consistentwhen using the convenient "switch to sibling" feature
144
133
        Both should switch to a sibling of the branch
145
134
        they are bound to, and not a sibling of themself"""
146
135
 
153
142
        branchb_id = tree2.commit('bar')
154
143
        checkout = tree1.branch.create_checkout('heavyco/a', lightweight=False)
155
144
        self.run_bzr(['switch', 'branchb'], working_dir='heavyco/a')
156
 
        # Refresh checkout as 'switch' modified it
157
 
        checkout = checkout.controldir.open_workingtree()
158
 
        self.assertEqual(branchb_id, checkout.last_revision())
159
 
        self.assertEqual(tree2.branch.base,
160
 
                         checkout.branch.get_bound_location())
161
 
 
162
 
    def test_switch_finds_relative_unicode_branch(self):
163
 
        """Switch will find 'foo' relative to the branch the checkout is of."""
164
 
        self.requireFeature(UnicodeFilenameFeature)
165
 
        self.build_tree(['repo/'])
166
 
        tree1 = self.make_branch_and_tree('repo/brancha')
167
 
        tree1.commit('foo')
168
 
        tree2 = self.make_branch_and_tree(u'repo/branch\xe9')
169
 
        tree2.pull(tree1.branch)
170
 
        branchb_id = tree2.commit('bar')
171
 
        checkout = tree1.branch.create_checkout('checkout', lightweight=True)
172
 
        self.run_bzr(['switch', u'branch\xe9'], working_dir='checkout')
173
 
        self.assertEqual(branchb_id, checkout.last_revision())
174
 
        checkout = checkout.controldir.open_workingtree()
175
 
        self.assertEqual(tree2.branch.base, checkout.branch.base)
176
 
 
177
 
    def test_switch_finds_relative_unicode_branch(self):
178
 
        """Switch will find 'foo' relative to the branch the checkout is of."""
179
 
        self.requireFeature(UnicodeFilenameFeature)
180
 
        self.build_tree(['repo/'])
181
 
        tree1 = self.make_branch_and_tree('repo/brancha')
182
 
        tree1.commit('foo')
183
 
        tree2 = self.make_branch_and_tree(u'repo/branch\xe9')
184
 
        tree2.pull(tree1.branch)
185
 
        branchb_id = tree2.commit('bar')
186
 
        checkout = tree1.branch.create_checkout('checkout', lightweight=True)
187
 
        self.run_bzr(['switch', u'branch\xe9'], working_dir='checkout')
188
 
        self.assertEqual(branchb_id, checkout.last_revision())
189
 
        checkout = checkout.controldir.open_workingtree()
190
 
        self.assertEqual(tree2.branch.base, checkout.branch.base)
 
145
        self.assertEqual(branchb_id, checkout.last_revision())
 
146
        self.assertEqual(tree2.branch.base, checkout.branch.get_bound_location())
191
147
 
192
148
    def test_switch_revision(self):
193
149
        tree = self._create_sample_tree()
194
150
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
195
151
        self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
196
 
        self.assertPathExists('checkout/file-1')
197
 
        self.assertPathDoesNotExist('checkout/file-2')
198
 
 
199
 
    def test_switch_into_colocated(self):
200
 
        # Create a new colocated branch from an existing non-colocated branch.
201
 
        tree = self.make_branch_and_tree('.', format='development-colo')
202
 
        self.build_tree(['file-1', 'file-2'])
203
 
        tree.add('file-1')
204
 
        revid1 = tree.commit('rev1')
205
 
        tree.add('file-2')
206
 
        revid2 = tree.commit('rev2')
207
 
        self.run_bzr(['switch', '-b', 'anotherbranch'])
208
 
        self.assertEqual(
209
 
            {'', 'anotherbranch'},
210
 
            set(tree.branch.controldir.branch_names()))
211
 
 
212
 
    def test_switch_into_unrelated_colocated(self):
213
 
        # Create a new colocated branch from an existing non-colocated branch.
214
 
        tree = self.make_branch_and_tree('.', format='development-colo')
215
 
        self.build_tree(['file-1', 'file-2'])
216
 
        tree.add('file-1')
217
 
        revid1 = tree.commit('rev1')
218
 
        tree.add('file-2')
219
 
        revid2 = tree.commit('rev2')
220
 
        tree.controldir.create_branch(name='foo')
221
 
        self.run_bzr_error(['Cannot switch a branch, only a checkout.'],
222
 
                           'switch foo')
223
 
        self.run_bzr(['switch', '--force', 'foo'])
224
 
 
225
 
    def test_switch_existing_colocated(self):
226
 
        # Create a branch branch-1 that initially is a checkout of 'foo'
227
 
        # Use switch to change it to 'anotherbranch'
228
 
        repo = self.make_repository('branch-1', format='development-colo')
229
 
        target_branch = repo.controldir.create_branch(name='foo')
230
 
        repo.controldir.set_branch_reference(target_branch)
231
 
        tree = repo.controldir.create_workingtree()
232
 
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
233
 
        tree.add('file-1')
234
 
        revid1 = tree.commit('rev1')
235
 
        tree.add('file-2')
236
 
        revid2 = tree.commit('rev2')
237
 
        otherbranch = tree.controldir.create_branch(name='anotherbranch')
238
 
        otherbranch.generate_revision_history(revid1)
239
 
        self.run_bzr(['switch', 'anotherbranch'], working_dir='branch-1')
240
 
        tree = WorkingTree.open("branch-1")
241
 
        self.assertEqual(tree.last_revision(), revid1)
242
 
        self.assertEqual(tree.branch.control_url, otherbranch.control_url)
243
 
 
244
 
    def test_switch_new_colocated(self):
245
 
        # Create a branch branch-1 that initially is a checkout of 'foo'
246
 
        # Use switch to create 'anotherbranch' which derives from that
247
 
        repo = self.make_repository('branch-1', format='development-colo')
248
 
        target_branch = repo.controldir.create_branch(name='foo')
249
 
        repo.controldir.set_branch_reference(target_branch)
250
 
        tree = repo.controldir.create_workingtree()
251
 
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
252
 
        tree.add('file-1')
253
 
        revid1 = tree.commit('rev1')
254
 
        self.run_bzr(['switch', '-b', 'anotherbranch'], working_dir='branch-1')
255
 
        bzrdir = ControlDir.open("branch-1")
256
 
        self.assertEqual(
257
 
            {b.name for b in bzrdir.list_branches()},
258
 
            {"foo", "anotherbranch"})
259
 
        self.assertEqual(bzrdir.open_branch().name, "anotherbranch")
260
 
        self.assertEqual(bzrdir.open_branch().last_revision(), revid1)
261
 
 
262
 
    def test_switch_new_colocated_unicode(self):
263
 
        # Create a branch branch-1 that initially is a checkout of 'foo'
264
 
        # Use switch to create 'branch\xe9' which derives from that
265
 
        self.requireFeature(UnicodeFilenameFeature)
266
 
        repo = self.make_repository('branch-1', format='development-colo')
267
 
        target_branch = repo.controldir.create_branch(name='foo')
268
 
        repo.controldir.set_branch_reference(target_branch)
269
 
        tree = repo.controldir.create_workingtree()
270
 
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
271
 
        tree.add('file-1')
272
 
        revid1 = tree.commit('rev1')
273
 
        self.run_bzr(['switch', '-b', u'branch\xe9'], working_dir='branch-1')
274
 
        bzrdir = ControlDir.open("branch-1")
275
 
        self.assertEqual(
276
 
            {b.name for b in bzrdir.list_branches()},
277
 
            {"foo", u"branch\xe9"})
278
 
        self.assertEqual(bzrdir.open_branch().name, u"branch\xe9")
279
 
        self.assertEqual(bzrdir.open_branch().last_revision(), revid1)
 
152
        self.failUnlessExists('checkout/file-1')
 
153
        self.failIfExists('checkout/file-2')
280
154
 
281
155
    def test_switch_only_revision(self):
282
156
        tree = self._create_sample_tree()
283
157
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
284
 
        self.assertPathExists('checkout/file-1')
285
 
        self.assertPathExists('checkout/file-2')
 
158
        self.failUnlessExists('checkout/file-1')
 
159
        self.failUnlessExists('checkout/file-2')
286
160
        self.run_bzr(['switch', '-r1'], working_dir='checkout')
287
 
        self.assertPathExists('checkout/file-1')
288
 
        self.assertPathDoesNotExist('checkout/file-2')
 
161
        self.failUnlessExists('checkout/file-1')
 
162
        self.failIfExists('checkout/file-2')
289
163
        # Check that we don't accept a range
290
164
        self.run_bzr_error(
291
 
            ['brz switch --revision takes exactly one revision identifier'],
 
165
            ['bzr switch --revision takes exactly one revision identifier'],
292
166
            ['switch', '-r0..2'], working_dir='checkout')
293
167
 
294
168
    def prepare_lightweight_switch(self):
304
178
 
305
179
    def test_switch_lightweight_after_branch_moved_relative(self):
306
180
        self.prepare_lightweight_switch()
307
 
        self.run_bzr('switch --force branch1',
308
 
                     working_dir='tree')
 
181
        self.run_bzr('switch --force branch1', working_dir='tree')
309
182
        branch_location = WorkingTree.open('tree').branch.base
310
183
        self.assertEndsWith(branch_location, 'branch1/')
311
184
 
312
185
    def test_create_branch_no_branch(self):
313
186
        self.prepare_lightweight_switch()
314
187
        self.run_bzr_error(['cannot create branch without source branch'],
315
 
                           'switch --create-branch ../branch2', working_dir='tree')
 
188
            'switch --create-branch ../branch2', working_dir='tree')
316
189
 
317
190
    def test_create_branch(self):
318
191
        branch = self.make_branch('branch')
319
192
        tree = branch.create_checkout('tree', lightweight=True)
320
 
        tree.commit('one', rev_id=b'rev-1')
 
193
        tree.commit('one', rev_id='rev-1')
321
194
        self.run_bzr('switch --create-branch ../branch2', working_dir='tree')
322
195
        tree = WorkingTree.open('tree')
323
196
        self.assertEndsWith(tree.branch.base, '/branch2/')
325
198
    def test_create_branch_local(self):
326
199
        branch = self.make_branch('branch')
327
200
        tree = branch.create_checkout('tree', lightweight=True)
328
 
        tree.commit('one', rev_id=b'rev-1')
 
201
        tree.commit('one', rev_id='rev-1')
329
202
        self.run_bzr('switch --create-branch branch2', working_dir='tree')
330
203
        tree = WorkingTree.open('tree')
331
204
        # The new branch should have been created at the same level as
335
208
    def test_create_branch_short_name(self):
336
209
        branch = self.make_branch('branch')
337
210
        tree = branch.create_checkout('tree', lightweight=True)
338
 
        tree.commit('one', rev_id=b'rev-1')
 
211
        tree.commit('one', rev_id='rev-1')
339
212
        self.run_bzr('switch -b branch2', working_dir='tree')
340
213
        tree = WorkingTree.open('tree')
341
214
        # The new branch should have been created at the same level as
345
218
    def test_create_branch_directory_services(self):
346
219
        branch = self.make_branch('branch')
347
220
        tree = branch.create_checkout('tree', lightweight=True)
348
 
 
349
221
        class FooLookup(object):
350
 
            def look_up(self, name, url, purpose=None):
351
 
                return 'foo-' + name
 
222
            def look_up(self, name, url):
 
223
                return 'foo-'+name
352
224
        directories.register('foo:', FooLookup, 'Create branches named foo-')
353
225
        self.addCleanup(directories.remove, 'foo:')
354
226
        self.run_bzr('switch -b foo:branch2', working_dir='tree')
356
228
        self.assertEndsWith(tree.branch.base, 'foo-branch2/')
357
229
 
358
230
    def test_switch_with_post_switch_hook(self):
359
 
        from breezy import branch as _mod_branch
 
231
        from bzrlib import branch as _mod_branch
360
232
        calls = []
361
233
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
362
 
                                                    calls.append, None)
 
234
            calls.append, None)
363
235
        self.make_branch_and_tree('branch')
364
236
        self.run_bzr('branch branch branch2')
365
237
        self.run_bzr('checkout branch checkout')
369
241
        self.assertLength(1, calls)
370
242
 
371
243
    def test_switch_lightweight_co_with_post_switch_hook(self):
372
 
        from breezy import branch as _mod_branch
 
244
        from bzrlib import branch as _mod_branch
373
245
        calls = []
374
246
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
375
 
                                                    calls.append, None)
 
247
            calls.append, None)
376
248
        self.make_branch_and_tree('branch')
377
249
        self.run_bzr('branch branch branch2')
378
250
        self.run_bzr('checkout --lightweight branch checkout')
380
252
        self.assertLength(0, calls)
381
253
        out, err = self.run_bzr('switch ../branch2')
382
254
        self.assertLength(1, calls)
383
 
 
384
 
    def test_switch_lightweight_directory(self):
385
 
        """Test --directory option"""
386
 
 
387
 
        # create a source branch
388
 
        a_tree = self.make_branch_and_tree('a')
389
 
        self.build_tree_contents([('a/a', b'initial\n')])
390
 
        a_tree.add('a')
391
 
        a_tree.commit(message='initial')
392
 
 
393
 
        # clone and add a differing revision
394
 
        b_tree = a_tree.controldir.sprout('b').open_workingtree()
395
 
        self.build_tree_contents([('b/a', b'initial\nmore\n')])
396
 
        b_tree.commit(message='more')
397
 
 
398
 
        self.run_bzr('checkout --lightweight a checkout')
399
 
        self.run_bzr('switch --directory checkout b')
400
 
        self.assertFileEqual(b'initial\nmore\n', 'checkout/a')
401
 
 
402
 
 
403
 
class TestSwitchParentLocationBase(TestCaseWithTransport):
404
 
 
405
 
    def setUp(self):
406
 
        """Set up a repository and branch ready for testing."""
407
 
        super(TestSwitchParentLocationBase, self).setUp()
408
 
        self.script_runner = script.ScriptRunner()
409
 
        self.script_runner.run_script(self, '''
410
 
                $ brz init-shared-repo --no-trees repo
411
 
                Shared repository...
412
 
                Location:
413
 
                  shared repository: repo
414
 
                $ brz init repo/trunk
415
 
                Created a repository branch...
416
 
                Using shared repository: ...
417
 
                ''')
418
 
 
419
 
    def assertParent(self, expected_parent, branch):
420
 
        """Verify that the parent is not None and is set correctly."""
421
 
        actual_parent = branch.get_parent()
422
 
        self.assertIsSameRealPath(urlutils.local_path_to_url(expected_parent),
423
 
                                  branch.get_parent())
424
 
 
425
 
 
426
 
class TestSwitchParentLocation(TestSwitchParentLocationBase):
427
 
 
428
 
    def _checkout_and_switch(self, option=''):
429
 
        self.script_runner.run_script(self, '''
430
 
                $ brz checkout %(option)s repo/trunk checkout
431
 
                $ cd checkout
432
 
                $ brz switch --create-branch switched
433
 
                2>Tree is up to date at revision 0.
434
 
                2>Switched to branch at .../switched/
435
 
                $ cd ..
436
 
                ''' % locals())
437
 
        bound_branch = branch.Branch.open_containing('checkout')[0]
438
 
        master_branch = branch.Branch.open_containing('repo/switched')[0]
439
 
        return (bound_branch, master_branch)
440
 
 
441
 
    def test_switch_parent_lightweight(self):
442
 
        """Lightweight checkout using brz switch."""
443
 
        bb, mb = self._checkout_and_switch(option='--lightweight')
444
 
        self.assertParent('repo/trunk', bb)
445
 
        self.assertParent('repo/trunk', mb)
446
 
 
447
 
    def test_switch_parent_heavyweight(self):
448
 
        """Heavyweight checkout using brz switch."""
449
 
        bb, mb = self._checkout_and_switch()
450
 
        self.assertParent('repo/trunk', bb)
451
 
        self.assertParent('repo/trunk', mb)
452
 
 
453
 
 
454
 
class TestSwitchDoesntOpenMasterBranch(TestCaseWithTransport):
455
 
    # See https://bugs.launchpad.net/bzr/+bug/812285
456
 
    # "brz switch --create-branch" can point the new branch's parent to the
457
 
    # master branch, but it doesn't have to open it to do so.
458
 
 
459
 
    def test_switch_create_doesnt_open_master_branch(self):
460
 
        master = self.make_branch_and_tree('master')
461
 
        master.commit('one')
462
 
        # Note: not a lightweight checkout
463
 
        checkout = master.branch.create_checkout('checkout')
464
 
        opened = []
465
 
 
466
 
        def open_hook(branch):
467
 
            # Just append the final directory of the branch
468
 
            name = branch.base.rstrip('/').rsplit('/', 1)[1]
469
 
            opened.append(name)
470
 
        branch.Branch.hooks.install_named_hook('open', open_hook,
471
 
                                               'open_hook_logger')
472
 
        self.run_bzr('switch --create-branch -d checkout feature')
473
 
        # We only open the master branch 1 time.
474
 
        # This test should be cleaner to write, but see bug:
475
 
        #  https://bugs.launchpad.net/bzr/+bug/812295
476
 
        self.assertEqual(1, opened.count('master'))
477
 
 
478
 
 
479
 
class TestSmartServerSwitch(TestCaseWithTransport):
480
 
 
481
 
    def test_switch_lightweight(self):
482
 
        self.setup_smart_server_with_call_log()
483
 
        t = self.make_branch_and_tree('from')
484
 
        for count in range(9):
485
 
            t.commit(message='commit %d' % count)
486
 
        out, err = self.run_bzr(['checkout', '--lightweight', self.get_url('from'),
487
 
                                 'target'])
488
 
        self.reset_smart_call_log()
489
 
        self.run_bzr(['switch', self.get_url('from')], working_dir='target')
490
 
        # This figure represent the amount of work to perform this use case. It
491
 
        # is entirely ok to reduce this number if a test fails due to rpc_count
492
 
        # being too low. If rpc_count increases, more network roundtrips have
493
 
        # become necessary for this use case. Please do not adjust this number
494
 
        # upwards without agreement from bzr's network support maintainers.
495
 
        self.assertLength(21, self.hpss_calls)
496
 
        self.assertLength(3, self.hpss_connections)
497
 
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
498
 
 
499
 
 
500
 
class TestSwitchUncommitted(TestCaseWithTransport):
501
 
 
502
 
    def prepare(self):
503
 
        tree = self.make_branch_and_tree('orig')
504
 
        tree.commit('')
505
 
        tree.branch.controldir.sprout('new')
506
 
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
507
 
        self.build_tree(['checkout/a'])
508
 
        self.assertPathExists('checkout/a')
509
 
        checkout.add('a')
510
 
        return checkout
511
 
 
512
 
    def test_store_and_restore_uncommitted(self):
513
 
        checkout = self.prepare()
514
 
        self.run_bzr(['switch', '--store', '-d', 'checkout', 'new'])
515
 
        self.build_tree(['checkout/b'])
516
 
        checkout.add('b')
517
 
        self.assertPathDoesNotExist('checkout/a')
518
 
        self.assertPathExists('checkout/b')
519
 
        self.run_bzr(['switch', '--store', '-d', 'checkout', 'orig'])
520
 
        self.assertPathExists('checkout/a')
521
 
        self.assertPathDoesNotExist('checkout/b')
522
 
 
523
 
    def test_does_not_store(self):
524
 
        self.prepare()
525
 
        self.run_bzr(['switch', '-d', 'checkout', 'new'])
526
 
        self.assertPathExists('checkout/a')
527
 
 
528
 
    def test_does_not_restore_changes(self):
529
 
        self.prepare()
530
 
        self.run_bzr(['switch', '--store', '-d', 'checkout', 'new'])
531
 
        self.assertPathDoesNotExist('checkout/a')
532
 
        self.run_bzr(['switch', '-d', 'checkout', 'orig'])
533
 
        self.assertPathDoesNotExist('checkout/a')
534
 
 
535
 
 
536
 
class TestSwitchStandAloneCorruption(TestCaseWithTransport):
537
 
 
538
 
    def test_empty_tree_switch(self):
539
 
        """switch . on an empty tree gets infinite recursion
540
 
 
541
 
        Inspired by: https://bugs.launchpad.net/bzr/+bug/1018628
542
 
        """
543
 
        self.script_runner = script.ScriptRunner()
544
 
        self.script_runner.run_script(self, '''
545
 
            $ brz init
546
 
            Created a standalone tree (format: 2a)
547
 
            $ brz switch .
548
 
            2>brz: ERROR: switching would create a branch reference loop. Use the "bzr up" command to switch to a different revision.
549
 
            ''')
550
 
 
551
 
    def test_switch_on_previous_rev(self):
552
 
        """switch to previous rev in a standalone directory
553
 
 
554
 
        Inspired by: https://bugs.launchpad.net/brz/+bug/1018628
555
 
        """
556
 
        self.script_runner = script.ScriptRunner()
557
 
        self.script_runner.run_script(self, '''
558
 
           $ brz init
559
 
           Created a standalone tree (format: 2a)
560
 
           $ brz commit -m 1 --unchanged
561
 
           $ brz commit -m 2 --unchanged
562
 
           $ brz switch -r 1
563
 
           2>brz: ERROR: switching would create a branch reference loop. Use the "bzr up" command to switch to a different revision.''',
564
 
                                      null_output_matches_anything=True)
565
 
 
566
 
    def test_switch_create_colo_locks_repo_path(self):
567
 
        self.script_runner = script.ScriptRunner()
568
 
        self.script_runner.run_script(self, '''
569
 
            $ mkdir mywork
570
 
            $ cd mywork
571
 
            $ brz init
572
 
            Created a standalone tree (format: 2a)
573
 
            $ echo A > a && brz add a && brz commit -m A
574
 
            $ brz switch -b br1
575
 
            $ cd ..
576
 
            $ mv mywork mywork1
577
 
            $ cd mywork1
578
 
            $ brz branches
579
 
              br1
580
 
            ''', null_output_matches_anything=True)
581
 
 
582
 
    def test_switch_to_new_branch_on_old_rev(self):
583
 
        """switch to previous rev in a standalone directory
584
 
 
585
 
        Inspired by: https://bugs.launchpad.net/brz/+bug/933362
586
 
        """
587
 
        self.script_runner = script.ScriptRunner()
588
 
        self.script_runner.run_script(self, '''
589
 
           $ brz init
590
 
           Created a standalone tree (format: 2a)
591
 
           $ brz switch -b trunk
592
 
           2>Tree is up to date at revision 0.
593
 
           2>Switched to branch trunk
594
 
           $ brz commit -m 1 --unchanged
595
 
           2>Committing to: ...
596
 
           2>Committed revision 1.
597
 
           $ brz commit -m 2 --unchanged
598
 
           2>Committing to: ...
599
 
           2>Committed revision 2.
600
 
           $ brz switch -b blah -r1
601
 
           2>Updated to revision 1.
602
 
           2>Switched to branch blah
603
 
           $ brz branches
604
 
           * blah
605
 
             trunk
606
 
           $ brz st
607
 
           ''')