~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Jelmer Vernooij
  • Date: 2012-01-27 16:27:26 UTC
  • mto: This revision was merged to the branch mainline in revision 6449.
  • Revision ID: jelmer@samba.org-20120127162726-f3qlvm7xkfg460ck
Run subtree tests with development-subtree rather than deprecated dirstate-with-subtree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
import os
22
22
 
23
 
from bzrlib import osutils
 
23
from bzrlib.bzrdir import BzrDir
 
24
from bzrlib import (
 
25
        osutils,
 
26
        urlutils,
 
27
        branch,
 
28
        )
24
29
from bzrlib.workingtree import WorkingTree
25
 
from bzrlib.tests.blackbox import ExternalBase
 
30
from bzrlib.tests import (
 
31
        TestCaseWithTransport,
 
32
        script,
 
33
        )
 
34
from bzrlib.tests.features import UnicodeFilenameFeature
26
35
from bzrlib.directory_service import directories
27
36
 
28
 
 
29
 
class TestSwitch(ExternalBase):
 
37
from bzrlib.tests.matchers import ContainsNoVfsCalls
 
38
 
 
39
 
 
40
class TestSwitch(TestCaseWithTransport):
30
41
 
31
42
    def _create_sample_tree(self):
32
43
        tree = self.make_branch_and_tree('branch-1')
129
140
        """Using switch on a heavy checkout should find master sibling
130
141
 
131
142
        The behaviour of lighweight and heavy checkouts should be
132
 
        consistentwhen using the convenient "switch to sibling" feature
 
143
        consistent when using the convenient "switch to sibling" feature
133
144
        Both should switch to a sibling of the branch
134
145
        they are bound to, and not a sibling of themself"""
135
146
 
145
156
        self.assertEqual(branchb_id, checkout.last_revision())
146
157
        self.assertEqual(tree2.branch.base, checkout.branch.get_bound_location())
147
158
 
 
159
    def test_switch_finds_relative_unicode_branch(self):
 
160
        """Switch will find 'foo' relative to the branch the checkout is of."""
 
161
        self.requireFeature(UnicodeFilenameFeature)
 
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
 
148
174
    def test_switch_revision(self):
149
175
        tree = self._create_sample_tree()
150
176
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
151
177
        self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
152
 
        self.failUnlessExists('checkout/file-1')
153
 
        self.failIfExists('checkout/file-2')
 
178
        self.assertPathExists('checkout/file-1')
 
179
        self.assertPathDoesNotExist('checkout/file-2')
 
180
 
 
181
    def test_switch_existing_colocated(self):
 
182
        # Create a branch branch-1 that initially is a checkout of 'foo'
 
183
        # Use switch to change it to 'anotherbranch'
 
184
        repo = self.make_repository('branch-1', format='development-colo')
 
185
        target_branch = repo.bzrdir.create_branch(name='foo')
 
186
        branch.BranchReferenceFormat().initialize(
 
187
            repo.bzrdir, target_branch=target_branch)
 
188
        tree = repo.bzrdir.create_workingtree()
 
189
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
 
190
        tree.add('file-1')
 
191
        revid1 = tree.commit('rev1')
 
192
        tree.add('file-2')
 
193
        revid2 = tree.commit('rev2')
 
194
        otherbranch = tree.bzrdir.create_branch(name='anotherbranch')
 
195
        otherbranch.generate_revision_history(revid1)
 
196
        self.run_bzr(['switch', 'anotherbranch'], working_dir='branch-1')
 
197
        tree = WorkingTree.open("branch-1")
 
198
        self.assertEquals(tree.last_revision(), revid1)
 
199
        self.assertEquals(tree.branch.control_url, otherbranch.control_url)
 
200
 
 
201
    def test_switch_new_colocated(self):
 
202
        # Create a branch branch-1 that initially is a checkout of 'foo'
 
203
        # Use switch to create 'anotherbranch' which derives from that
 
204
        repo = self.make_repository('branch-1', format='development-colo')
 
205
        target_branch = repo.bzrdir.create_branch(name='foo')
 
206
        branch.BranchReferenceFormat().initialize(
 
207
            repo.bzrdir, target_branch=target_branch)
 
208
        tree = repo.bzrdir.create_workingtree()
 
209
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
 
210
        tree.add('file-1')
 
211
        revid1 = tree.commit('rev1')
 
212
        self.run_bzr(['switch', '-b', 'anotherbranch'], working_dir='branch-1')
 
213
        bzrdir = BzrDir.open("branch-1")
 
214
        self.assertEquals(
 
215
            set([b.name for b in bzrdir.list_branches()]),
 
216
            set(["foo", "anotherbranch"]))
 
217
        self.assertEquals(bzrdir.open_branch().name, "anotherbranch")
 
218
        self.assertEquals(bzrdir.open_branch().last_revision(), revid1)
 
219
 
 
220
    def test_switch_new_colocated_unicode(self):
 
221
        # Create a branch branch-1 that initially is a checkout of 'foo'
 
222
        # Use switch to create 'branch\xe9' which derives from that
 
223
        self.requireFeature(UnicodeFilenameFeature)
 
224
        repo = self.make_repository('branch-1', format='development-colo')
 
225
        target_branch = repo.bzrdir.create_branch(name='foo')
 
226
        branch.BranchReferenceFormat().initialize(
 
227
            repo.bzrdir, target_branch=target_branch)
 
228
        tree = repo.bzrdir.create_workingtree()
 
229
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
 
230
        tree.add('file-1')
 
231
        revid1 = tree.commit('rev1')
 
232
        self.run_bzr(['switch', '-b', u'branch\xe9'], working_dir='branch-1')
 
233
        bzrdir = BzrDir.open("branch-1")
 
234
        self.assertEquals(
 
235
            set([b.name for b in bzrdir.list_branches()]),
 
236
            set(["foo", u"branch\xe9"]))
 
237
        self.assertEquals(bzrdir.open_branch().name, u"branch\xe9")
 
238
        self.assertEquals(bzrdir.open_branch().last_revision(), revid1)
154
239
 
155
240
    def test_switch_only_revision(self):
156
241
        tree = self._create_sample_tree()
157
242
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
158
 
        self.failUnlessExists('checkout/file-1')
159
 
        self.failUnlessExists('checkout/file-2')
 
243
        self.assertPathExists('checkout/file-1')
 
244
        self.assertPathExists('checkout/file-2')
160
245
        self.run_bzr(['switch', '-r1'], working_dir='checkout')
161
 
        self.failUnlessExists('checkout/file-1')
162
 
        self.failIfExists('checkout/file-2')
 
246
        self.assertPathExists('checkout/file-1')
 
247
        self.assertPathDoesNotExist('checkout/file-2')
163
248
        # Check that we don't accept a range
164
249
        self.run_bzr_error(
165
250
            ['bzr switch --revision takes exactly one revision identifier'],
252
337
        self.assertLength(0, calls)
253
338
        out, err = self.run_bzr('switch ../branch2')
254
339
        self.assertLength(1, calls)
 
340
 
 
341
    def test_switch_lightweight_directory(self):
 
342
        """Test --directory option"""
 
343
 
 
344
        # create a source branch
 
345
        a_tree = self.make_branch_and_tree('a')
 
346
        self.build_tree_contents([('a/a', 'initial\n')])
 
347
        a_tree.add('a')
 
348
        a_tree.commit(message='initial')
 
349
 
 
350
        # clone and add a differing revision
 
351
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
 
352
        self.build_tree_contents([('b/a', 'initial\nmore\n')])
 
353
        b_tree.commit(message='more')
 
354
 
 
355
        self.run_bzr('checkout --lightweight a checkout')
 
356
        self.run_bzr('switch --directory checkout b')
 
357
        self.assertFileEqual('initial\nmore\n', 'checkout/a')
 
358
 
 
359
 
 
360
class TestSwitchParentLocationBase(TestCaseWithTransport):
 
361
 
 
362
    def setUp(self):
 
363
        """Set up a repository and branch ready for testing."""
 
364
        super(TestSwitchParentLocationBase, self).setUp()
 
365
        self.script_runner = script.ScriptRunner()
 
366
        self.script_runner.run_script(self, '''
 
367
                $ bzr init-repo --no-trees repo
 
368
                Shared repository...
 
369
                Location:
 
370
                  shared repository: repo
 
371
                $ bzr init repo/trunk
 
372
                Created a repository branch...
 
373
                Using shared repository: ...
 
374
                ''')
 
375
 
 
376
    def assertParent(self, expected_parent, branch):
 
377
        """Verify that the parent is not None and is set correctly."""
 
378
        actual_parent = branch.get_parent()
 
379
        self.assertIsSameRealPath(urlutils.local_path_to_url(expected_parent),
 
380
                                  branch.get_parent())
 
381
 
 
382
 
 
383
class TestSwitchParentLocation(TestSwitchParentLocationBase):
 
384
 
 
385
    def _checkout_and_switch(self, option=''):
 
386
        self.script_runner.run_script(self, '''
 
387
                $ bzr checkout %(option)s repo/trunk checkout
 
388
                $ cd checkout
 
389
                $ bzr switch --create-branch switched
 
390
                2>Tree is up to date at revision 0.
 
391
                2>Switched to branch:...switched...
 
392
                $ cd ..
 
393
                ''' % locals())
 
394
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
395
        master_branch = branch.Branch.open_containing('repo/switched')[0]
 
396
        return (bound_branch, master_branch)
 
397
 
 
398
    def test_switch_parent_lightweight(self):
 
399
        """Lightweight checkout using bzr switch."""
 
400
        bb, mb = self._checkout_and_switch(option='--lightweight')
 
401
        self.assertParent('repo/trunk', bb)
 
402
        self.assertParent('repo/trunk', mb)
 
403
 
 
404
    def test_switch_parent_heavyweight(self):
 
405
        """Heavyweight checkout using bzr switch."""
 
406
        bb, mb = self._checkout_and_switch()
 
407
        self.assertParent('repo/trunk', bb)
 
408
        self.assertParent('repo/trunk', mb)
 
409
 
 
410
 
 
411
class TestSwitchDoesntOpenMasterBranch(TestCaseWithTransport):
 
412
    # See https://bugs.launchpad.net/bzr/+bug/812285
 
413
    # "bzr switch --create-branch" can point the new branch's parent to the
 
414
    # master branch, but it doesn't have to open it to do so.
 
415
 
 
416
    def test_switch_create_doesnt_open_master_branch(self):
 
417
        master = self.make_branch_and_tree('master')
 
418
        master.commit('one')
 
419
        # Note: not a lightweight checkout
 
420
        checkout = master.branch.create_checkout('checkout')
 
421
        opened = []
 
422
        def open_hook(branch):
 
423
            # Just append the final directory of the branch
 
424
            name = branch.base.rstrip('/').rsplit('/', 1)[1]
 
425
            opened.append(name)
 
426
        branch.Branch.hooks.install_named_hook('open', open_hook,
 
427
                                               'open_hook_logger')
 
428
        self.run_bzr('switch --create-branch -d checkout feature')
 
429
        # We only open the master branch 1 time.
 
430
        # This test should be cleaner to write, but see bug:
 
431
        #  https://bugs.launchpad.net/bzr/+bug/812295
 
432
        self.assertEqual(1, opened.count('master'))
 
433
 
 
434
 
 
435
class TestSmartServerSwitch(TestCaseWithTransport):
 
436
 
 
437
    def test_switch_lightweight(self):
 
438
        self.setup_smart_server_with_call_log()
 
439
        t = self.make_branch_and_tree('from')
 
440
        for count in range(9):
 
441
            t.commit(message='commit %d' % count)
 
442
        out, err = self.run_bzr(['checkout', '--lightweight', self.get_url('from'),
 
443
            'target'])
 
444
        self.reset_smart_call_log()
 
445
        self.run_bzr(['switch', self.get_url('from')], working_dir='target')
 
446
        # This figure represent the amount of work to perform this use case. It
 
447
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
448
        # being too low. If rpc_count increases, more network roundtrips have
 
449
        # become necessary for this use case. Please do not adjust this number
 
450
        # upwards without agreement from bzr's network support maintainers.
 
451
        self.assertLength(24, self.hpss_calls)
 
452
        self.assertLength(5, self.hpss_connections)
 
453
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)