/brz/remove-bazaar

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

« back to all changes in this revision

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

  • Committer: Jelmer Vernooij
  • Date: 2020-11-19 18:28:52 UTC
  • mto: This revision was merged to the branch mainline in revision 7526.
  • Revision ID: jelmer@jelmer.uk-20201119182852-4za22s5mhakix4dq
Remove sixish data type.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2012, 2016 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 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):
 
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
 
 
38
class TestSwitch(TestCaseWithTransport):
30
39
 
31
40
    def _create_sample_tree(self):
32
41
        tree = self.make_branch_and_tree('branch-1')
44
53
        os.chdir('checkout')
45
54
        out, err = self.run_bzr('switch ../branch2')
46
55
        self.assertContainsRe(err, 'Tree is up to date at revision 0.\n')
47
 
        self.assertContainsRe(err, 'Switched to branch: .*/branch2.\n')
 
56
        self.assertContainsRe(err, 'Switched to branch at .*/branch2.\n')
48
57
        self.assertEqual('', out)
49
58
 
50
59
    def test_switch_out_of_date_light_checkout(self):
58
67
        out, err = self.run_bzr('switch ../branch2')
59
68
        #self.assertContainsRe(err, '\+N  file')
60
69
        self.assertContainsRe(err, 'Updated to revision 1.\n')
61
 
        self.assertContainsRe(err, 'Switched to branch: .*/branch2.\n')
 
70
        self.assertContainsRe(err, 'Switched to branch at .*/branch2.\n')
62
71
        self.assertEqual('', out)
63
72
 
64
73
    def _test_switch_nick(self, lightweight):
66
75
        tree1 = self.make_branch_and_tree('branch1')
67
76
        tree2 = self.make_branch_and_tree('branch2')
68
77
        tree2.pull(tree1.branch)
69
 
        checkout =  tree1.branch.create_checkout('checkout',
70
 
            lightweight=lightweight)
 
78
        checkout = tree1.branch.create_checkout('checkout',
 
79
                                                lightweight=lightweight)
71
80
        self.assertEqual(checkout.branch.nick, tree1.branch.nick)
72
81
        self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
73
 
            False)
 
82
                         False)
74
83
        self.run_bzr('switch branch2', working_dir='checkout')
75
84
 
76
85
        # we need to get the tree again, otherwise we don't get the new branch
77
86
        checkout = WorkingTree.open('checkout')
78
87
        self.assertEqual(checkout.branch.nick, tree2.branch.nick)
79
88
        self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
80
 
            False)
 
89
                         False)
81
90
 
82
91
    def test_switch_nick(self):
83
92
        self._test_switch_nick(lightweight=False)
90
99
        tree1 = self.make_branch_and_tree('branch1')
91
100
        tree2 = self.make_branch_and_tree('branch2')
92
101
        tree2.pull(tree1.branch)
93
 
        checkout =  tree1.branch.create_checkout('checkout',
94
 
            lightweight=lightweight)
 
102
        checkout = tree1.branch.create_checkout('checkout',
 
103
                                                lightweight=lightweight)
95
104
        self.assertEqual(checkout.branch.nick, tree1.branch.nick)
96
105
        checkout.branch.nick = "explicit_nick"
97
106
        self.assertEqual(checkout.branch.nick, "explicit_nick")
98
107
        self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
99
 
            "explicit_nick")
 
108
                         "explicit_nick")
100
109
        self.run_bzr('switch branch2', working_dir='checkout')
101
110
 
102
111
        # we need to get the tree again, otherwise we don't get the new branch
103
112
        checkout = WorkingTree.open('checkout')
104
113
        self.assertEqual(checkout.branch.nick, tree2.branch.nick)
105
114
        self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
106
 
            tree2.branch.nick)
 
115
                         tree2.branch.nick)
107
116
 
108
117
    def test_switch_explicit_nick(self):
109
118
        self._test_switch_explicit_nick(lightweight=False)
119
128
        tree2 = self.make_branch_and_tree('repo/branchb')
120
129
        tree2.pull(tree1.branch)
121
130
        branchb_id = tree2.commit('bar')
122
 
        checkout =  tree1.branch.create_checkout('checkout', lightweight=True)
 
131
        checkout = tree1.branch.create_checkout('checkout', lightweight=True)
123
132
        self.run_bzr(['switch', 'branchb'], working_dir='checkout')
124
133
        self.assertEqual(branchb_id, checkout.last_revision())
125
 
        checkout = checkout.bzrdir.open_workingtree()
 
134
        checkout = checkout.controldir.open_workingtree()
126
135
        self.assertEqual(tree2.branch.base, checkout.branch.base)
127
136
 
128
137
    def test_switch_finds_relative_bound_branch(self):
129
138
        """Using switch on a heavy checkout should find master sibling
130
139
 
131
140
        The behaviour of lighweight and heavy checkouts should be
132
 
        consistentwhen using the convenient "switch to sibling" feature
 
141
        consistent when using the convenient "switch to sibling" feature
133
142
        Both should switch to a sibling of the branch
134
143
        they are bound to, and not a sibling of themself"""
135
144
 
142
151
        branchb_id = tree2.commit('bar')
143
152
        checkout = tree1.branch.create_checkout('heavyco/a', lightweight=False)
144
153
        self.run_bzr(['switch', 'branchb'], working_dir='heavyco/a')
145
 
        self.assertEqual(branchb_id, checkout.last_revision())
146
 
        self.assertEqual(tree2.branch.base, checkout.branch.get_bound_location())
 
154
        # Refresh checkout as 'switch' modified it
 
155
        checkout = checkout.controldir.open_workingtree()
 
156
        self.assertEqual(branchb_id, checkout.last_revision())
 
157
        self.assertEqual(tree2.branch.base,
 
158
                         checkout.branch.get_bound_location())
 
159
 
 
160
    def test_switch_finds_relative_unicode_branch(self):
 
161
        """Switch will find 'foo' relative to the branch the checkout is of."""
 
162
        self.requireFeature(UnicodeFilenameFeature)
 
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')
 
169
        checkout = tree1.branch.create_checkout('checkout', lightweight=True)
 
170
        self.run_bzr(['switch', u'branch\xe9'], working_dir='checkout')
 
171
        self.assertEqual(branchb_id, checkout.last_revision())
 
172
        checkout = checkout.controldir.open_workingtree()
 
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."""
 
177
        self.requireFeature(UnicodeFilenameFeature)
 
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')
 
184
        checkout = tree1.branch.create_checkout('checkout', lightweight=True)
 
185
        self.run_bzr(['switch', u'branch\xe9'], working_dir='checkout')
 
186
        self.assertEqual(branchb_id, checkout.last_revision())
 
187
        checkout = checkout.controldir.open_workingtree()
 
188
        self.assertEqual(tree2.branch.base, checkout.branch.base)
147
189
 
148
190
    def test_switch_revision(self):
149
191
        tree = self._create_sample_tree()
150
192
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
151
193
        self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
152
 
        self.failUnlessExists('checkout/file-1')
153
 
        self.failIfExists('checkout/file-2')
 
194
        self.assertPathExists('checkout/file-1')
 
195
        self.assertPathDoesNotExist('checkout/file-2')
 
196
 
 
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'])
 
206
        self.assertEqual(
 
207
            {'', 'anotherbranch'},
 
208
            set(tree.branch.controldir.branch_names()))
 
209
 
 
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')
 
218
        tree.controldir.create_branch(name='foo')
 
219
        self.run_bzr_error(['Cannot switch a branch, only a checkout.'],
 
220
                           'switch foo')
 
221
        self.run_bzr(['switch', '--force', 'foo'])
 
222
 
 
223
    def test_switch_existing_colocated(self):
 
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')
 
227
        target_branch = repo.controldir.create_branch(name='foo')
 
228
        repo.controldir.set_branch_reference(target_branch)
 
229
        tree = repo.controldir.create_workingtree()
 
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')
 
235
        otherbranch = tree.controldir.create_branch(name='anotherbranch')
 
236
        otherbranch.generate_revision_history(revid1)
 
237
        self.run_bzr(['switch', 'anotherbranch'], working_dir='branch-1')
 
238
        tree = WorkingTree.open("branch-1")
 
239
        self.assertEqual(tree.last_revision(), revid1)
 
240
        self.assertEqual(tree.branch.control_url, otherbranch.control_url)
 
241
 
 
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')
 
246
        target_branch = repo.controldir.create_branch(name='foo')
 
247
        repo.controldir.set_branch_reference(target_branch)
 
248
        tree = repo.controldir.create_workingtree()
 
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')
 
253
        bzrdir = ControlDir.open("branch-1")
 
254
        self.assertEqual(
 
255
            {b.name for b in bzrdir.list_branches()},
 
256
            {"foo", "anotherbranch"})
 
257
        self.assertEqual(bzrdir.open_branch().name, "anotherbranch")
 
258
        self.assertEqual(bzrdir.open_branch().last_revision(), revid1)
 
259
 
 
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
 
263
        self.requireFeature(UnicodeFilenameFeature)
 
264
        repo = self.make_repository('branch-1', format='development-colo')
 
265
        target_branch = repo.controldir.create_branch(name='foo')
 
266
        repo.controldir.set_branch_reference(target_branch)
 
267
        tree = repo.controldir.create_workingtree()
 
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')
 
272
        bzrdir = ControlDir.open("branch-1")
 
273
        self.assertEqual(
 
274
            {b.name for b in bzrdir.list_branches()},
 
275
            {"foo", u"branch\xe9"})
 
276
        self.assertEqual(bzrdir.open_branch().name, u"branch\xe9")
 
277
        self.assertEqual(bzrdir.open_branch().last_revision(), revid1)
154
278
 
155
279
    def test_switch_only_revision(self):
156
280
        tree = self._create_sample_tree()
157
281
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
158
 
        self.failUnlessExists('checkout/file-1')
159
 
        self.failUnlessExists('checkout/file-2')
 
282
        self.assertPathExists('checkout/file-1')
 
283
        self.assertPathExists('checkout/file-2')
160
284
        self.run_bzr(['switch', '-r1'], working_dir='checkout')
161
 
        self.failUnlessExists('checkout/file-1')
162
 
        self.failIfExists('checkout/file-2')
 
285
        self.assertPathExists('checkout/file-1')
 
286
        self.assertPathDoesNotExist('checkout/file-2')
163
287
        # Check that we don't accept a range
164
288
        self.run_bzr_error(
165
 
            ['bzr switch --revision takes exactly one revision identifier'],
 
289
            ['brz switch --revision takes exactly one revision identifier'],
166
290
            ['switch', '-r0..2'], working_dir='checkout')
167
291
 
168
292
    def prepare_lightweight_switch(self):
178
302
 
179
303
    def test_switch_lightweight_after_branch_moved_relative(self):
180
304
        self.prepare_lightweight_switch()
181
 
        self.run_bzr('switch --force branch1', working_dir='tree')
 
305
        self.run_bzr('switch --force branch1',
 
306
                     working_dir='tree')
182
307
        branch_location = WorkingTree.open('tree').branch.base
183
308
        self.assertEndsWith(branch_location, 'branch1/')
184
309
 
185
310
    def test_create_branch_no_branch(self):
186
311
        self.prepare_lightweight_switch()
187
312
        self.run_bzr_error(['cannot create branch without source branch'],
188
 
            'switch --create-branch ../branch2', working_dir='tree')
 
313
                           'switch --create-branch ../branch2', working_dir='tree')
189
314
 
190
315
    def test_create_branch(self):
191
316
        branch = self.make_branch('branch')
192
317
        tree = branch.create_checkout('tree', lightweight=True)
193
 
        tree.commit('one', rev_id='rev-1')
 
318
        tree.commit('one', rev_id=b'rev-1')
194
319
        self.run_bzr('switch --create-branch ../branch2', working_dir='tree')
195
320
        tree = WorkingTree.open('tree')
196
321
        self.assertEndsWith(tree.branch.base, '/branch2/')
198
323
    def test_create_branch_local(self):
199
324
        branch = self.make_branch('branch')
200
325
        tree = branch.create_checkout('tree', lightweight=True)
201
 
        tree.commit('one', rev_id='rev-1')
 
326
        tree.commit('one', rev_id=b'rev-1')
202
327
        self.run_bzr('switch --create-branch branch2', working_dir='tree')
203
328
        tree = WorkingTree.open('tree')
204
329
        # The new branch should have been created at the same level as
208
333
    def test_create_branch_short_name(self):
209
334
        branch = self.make_branch('branch')
210
335
        tree = branch.create_checkout('tree', lightweight=True)
211
 
        tree.commit('one', rev_id='rev-1')
 
336
        tree.commit('one', rev_id=b'rev-1')
212
337
        self.run_bzr('switch -b branch2', working_dir='tree')
213
338
        tree = WorkingTree.open('tree')
214
339
        # The new branch should have been created at the same level as
218
343
    def test_create_branch_directory_services(self):
219
344
        branch = self.make_branch('branch')
220
345
        tree = branch.create_checkout('tree', lightweight=True)
 
346
 
221
347
        class FooLookup(object):
222
 
            def look_up(self, name, url):
223
 
                return 'foo-'+name
 
348
            def look_up(self, name, url, purpose=None):
 
349
                return 'foo-' + name
224
350
        directories.register('foo:', FooLookup, 'Create branches named foo-')
225
351
        self.addCleanup(directories.remove, 'foo:')
226
352
        self.run_bzr('switch -b foo:branch2', working_dir='tree')
228
354
        self.assertEndsWith(tree.branch.base, 'foo-branch2/')
229
355
 
230
356
    def test_switch_with_post_switch_hook(self):
231
 
        from bzrlib import branch as _mod_branch
 
357
        from breezy import branch as _mod_branch
232
358
        calls = []
233
359
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
234
 
            calls.append, None)
 
360
                                                    calls.append, None)
235
361
        self.make_branch_and_tree('branch')
236
362
        self.run_bzr('branch branch branch2')
237
363
        self.run_bzr('checkout branch checkout')
241
367
        self.assertLength(1, calls)
242
368
 
243
369
    def test_switch_lightweight_co_with_post_switch_hook(self):
244
 
        from bzrlib import branch as _mod_branch
 
370
        from breezy import branch as _mod_branch
245
371
        calls = []
246
372
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
247
 
            calls.append, None)
 
373
                                                    calls.append, None)
248
374
        self.make_branch_and_tree('branch')
249
375
        self.run_bzr('branch branch branch2')
250
376
        self.run_bzr('checkout --lightweight branch checkout')
252
378
        self.assertLength(0, calls)
253
379
        out, err = self.run_bzr('switch ../branch2')
254
380
        self.assertLength(1, calls)
 
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')
 
387
        self.build_tree_contents([('a/a', b'initial\n')])
 
388
        a_tree.add('a')
 
389
        a_tree.commit(message='initial')
 
390
 
 
391
        # clone and add a differing revision
 
392
        b_tree = a_tree.controldir.sprout('b').open_workingtree()
 
393
        self.build_tree_contents([('b/a', b'initial\nmore\n')])
 
394
        b_tree.commit(message='more')
 
395
 
 
396
        self.run_bzr('checkout --lightweight a checkout')
 
397
        self.run_bzr('switch --directory checkout b')
 
398
        self.assertFileEqual(b'initial\nmore\n', 'checkout/a')
 
399
 
 
400
 
 
401
class TestSwitchParentLocationBase(TestCaseWithTransport):
 
402
 
 
403
    def setUp(self):
 
404
        """Set up a repository and branch ready for testing."""
 
405
        super(TestSwitchParentLocationBase, self).setUp()
 
406
        self.script_runner = script.ScriptRunner()
 
407
        self.script_runner.run_script(self, '''
 
408
                $ brz init-shared-repo --no-trees repo
 
409
                Shared repository...
 
410
                Location:
 
411
                  shared repository: repo
 
412
                $ brz init repo/trunk
 
413
                Created a repository branch...
 
414
                Using shared repository: ...
 
415
                ''')
 
416
 
 
417
    def assertParent(self, expected_parent, branch):
 
418
        """Verify that the parent is not None and is set correctly."""
 
419
        actual_parent = branch.get_parent()
 
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=''):
 
427
        self.script_runner.run_script(self, '''
 
428
                $ brz checkout %(option)s repo/trunk checkout
 
429
                $ cd checkout
 
430
                $ brz switch --create-branch switched
 
431
                2>Tree is up to date at revision 0.
 
432
                2>Switched to branch at .../switched/
 
433
                $ cd ..
 
434
                ''' % locals())
 
435
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
436
        master_branch = branch.Branch.open_containing('repo/switched')[0]
 
437
        return (bound_branch, master_branch)
 
438
 
 
439
    def test_switch_parent_lightweight(self):
 
440
        """Lightweight checkout using brz switch."""
 
441
        bb, mb = self._checkout_and_switch(option='--lightweight')
 
442
        self.assertParent('repo/trunk', bb)
 
443
        self.assertParent('repo/trunk', mb)
 
444
 
 
445
    def test_switch_parent_heavyweight(self):
 
446
        """Heavyweight checkout using brz switch."""
 
447
        bb, mb = self._checkout_and_switch()
 
448
        self.assertParent('repo/trunk', bb)
 
449
        self.assertParent('repo/trunk', mb)
 
450
 
 
451
 
 
452
class TestSwitchDoesntOpenMasterBranch(TestCaseWithTransport):
 
453
    # See https://bugs.launchpad.net/bzr/+bug/812285
 
454
    # "brz switch --create-branch" can point the new branch's parent to the
 
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 = []
 
463
 
 
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'))
 
475
 
 
476
 
 
477
class TestSwitchUncommitted(TestCaseWithTransport):
 
478
 
 
479
    def prepare(self):
 
480
        tree = self.make_branch_and_tree('orig')
 
481
        tree.commit('')
 
482
        tree.branch.controldir.sprout('new')
 
483
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
 
484
        self.build_tree(['checkout/a'])
 
485
        self.assertPathExists('checkout/a')
 
486
        checkout.add('a')
 
487
        return checkout
 
488
 
 
489
    def test_store_and_restore_uncommitted(self):
 
490
        checkout = self.prepare()
 
491
        self.run_bzr(['switch', '--store', '-d', 'checkout', 'new'])
 
492
        self.build_tree(['checkout/b'])
 
493
        checkout.add('b')
 
494
        self.assertPathDoesNotExist('checkout/a')
 
495
        self.assertPathExists('checkout/b')
 
496
        self.run_bzr(['switch', '--store', '-d', 'checkout', 'orig'])
 
497
        self.assertPathExists('checkout/a')
 
498
        self.assertPathDoesNotExist('checkout/b')
 
499
 
 
500
    def test_does_not_store(self):
 
501
        self.prepare()
 
502
        self.run_bzr(['switch', '-d', 'checkout', 'new'])
 
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'])
 
508
        self.assertPathDoesNotExist('checkout/a')
 
509
        self.run_bzr(['switch', '-d', 'checkout', 'orig'])
 
510
        self.assertPathDoesNotExist('checkout/a')
 
511
 
 
512
 
 
513
class TestSwitchStandAloneCorruption(TestCaseWithTransport):
 
514
 
 
515
    def test_empty_tree_switch(self):
 
516
        """switch . on an empty tree gets infinite recursion
 
517
 
 
518
        Inspired by: https://bugs.launchpad.net/bzr/+bug/1018628
 
519
        """
 
520
        self.script_runner = script.ScriptRunner()
 
521
        self.script_runner.run_script(self, '''
 
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
            ''')
 
527
 
 
528
    def test_switch_on_previous_rev(self):
 
529
        """switch to previous rev in a standalone directory
 
530
 
 
531
        Inspired by: https://bugs.launchpad.net/brz/+bug/1018628
 
532
        """
 
533
        self.script_runner = script.ScriptRunner()
 
534
        self.script_runner.run_script(self, '''
 
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.''',
 
541
                                      null_output_matches_anything=True)
 
542
 
 
543
    def test_switch_create_colo_locks_repo_path(self):
 
544
        self.script_runner = script.ScriptRunner()
 
545
        self.script_runner.run_script(self, '''
 
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
 
556
              br1
 
557
            ''', null_output_matches_anything=True)
 
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.
 
570
           2>Switched to branch trunk
 
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.
 
579
           2>Switched to branch blah
 
580
           $ brz branches
 
581
           * blah
 
582
             trunk
 
583
           $ brz st
 
584
           ''')