~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: 2011-09-26 15:21:01 UTC
  • mfrom: (6165.2.3 avoid-revision-history)
  • mto: This revision was merged to the branch mainline in revision 6216.
  • Revision ID: jelmer@samba.org-20110926152101-afcxw1hikybyivfd
merge avoid-revision-history.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2008, 2009 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 bzrlib import (
 
24
        osutils,
 
25
        urlutils,
 
26
        branch,
 
27
        )
23
28
from bzrlib.workingtree import WorkingTree
24
 
from bzrlib.tests.blackbox import ExternalBase
 
29
from bzrlib.tests import (
 
30
        TestCaseWithTransport,
 
31
        script,
 
32
        )
25
33
from bzrlib.directory_service import directories
26
34
 
27
35
 
28
 
class TestSwitch(ExternalBase):
 
36
class TestSwitch(TestCaseWithTransport):
29
37
 
30
38
    def _create_sample_tree(self):
31
39
        tree = self.make_branch_and_tree('branch-1')
128
136
        """Using switch on a heavy checkout should find master sibling
129
137
 
130
138
        The behaviour of lighweight and heavy checkouts should be
131
 
        consistentwhen using the convenient "switch to sibling" feature
 
139
        consistent when using the convenient "switch to sibling" feature
132
140
        Both should switch to a sibling of the branch
133
141
        they are bound to, and not a sibling of themself"""
134
142
 
148
156
        tree = self._create_sample_tree()
149
157
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
150
158
        self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
151
 
        self.failUnlessExists('checkout/file-1')
152
 
        self.failIfExists('checkout/file-2')
 
159
        self.assertPathExists('checkout/file-1')
 
160
        self.assertPathDoesNotExist('checkout/file-2')
153
161
 
154
162
    def test_switch_only_revision(self):
155
163
        tree = self._create_sample_tree()
156
164
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
157
 
        self.failUnlessExists('checkout/file-1')
158
 
        self.failUnlessExists('checkout/file-2')
 
165
        self.assertPathExists('checkout/file-1')
 
166
        self.assertPathExists('checkout/file-2')
159
167
        self.run_bzr(['switch', '-r1'], working_dir='checkout')
160
 
        self.failUnlessExists('checkout/file-1')
161
 
        self.failIfExists('checkout/file-2')
 
168
        self.assertPathExists('checkout/file-1')
 
169
        self.assertPathDoesNotExist('checkout/file-2')
162
170
        # Check that we don't accept a range
163
171
        self.run_bzr_error(
164
172
            ['bzr switch --revision takes exactly one revision identifier'],
167
175
    def prepare_lightweight_switch(self):
168
176
        branch = self.make_branch('branch')
169
177
        branch.create_checkout('tree', lightweight=True)
170
 
        os.rename('branch', 'branch1')
 
178
        osutils.rename('branch', 'branch1')
171
179
 
172
180
    def test_switch_lightweight_after_branch_moved(self):
173
181
        self.prepare_lightweight_switch()
225
233
        self.run_bzr('switch -b foo:branch2', working_dir='tree')
226
234
        tree = WorkingTree.open('tree')
227
235
        self.assertEndsWith(tree.branch.base, 'foo-branch2/')
 
236
 
 
237
    def test_switch_with_post_switch_hook(self):
 
238
        from bzrlib import branch as _mod_branch
 
239
        calls = []
 
240
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
 
241
            calls.append, None)
 
242
        self.make_branch_and_tree('branch')
 
243
        self.run_bzr('branch branch branch2')
 
244
        self.run_bzr('checkout branch checkout')
 
245
        os.chdir('checkout')
 
246
        self.assertLength(0, calls)
 
247
        out, err = self.run_bzr('switch ../branch2')
 
248
        self.assertLength(1, calls)
 
249
 
 
250
    def test_switch_lightweight_co_with_post_switch_hook(self):
 
251
        from bzrlib import branch as _mod_branch
 
252
        calls = []
 
253
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
 
254
            calls.append, None)
 
255
        self.make_branch_and_tree('branch')
 
256
        self.run_bzr('branch branch branch2')
 
257
        self.run_bzr('checkout --lightweight branch checkout')
 
258
        os.chdir('checkout')
 
259
        self.assertLength(0, calls)
 
260
        out, err = self.run_bzr('switch ../branch2')
 
261
        self.assertLength(1, calls)
 
262
 
 
263
    def test_switch_lightweight_directory(self):
 
264
        """Test --directory option"""
 
265
 
 
266
        # create a source branch
 
267
        a_tree = self.make_branch_and_tree('a')
 
268
        self.build_tree_contents([('a/a', 'initial\n')])
 
269
        a_tree.add('a')
 
270
        a_tree.commit(message='initial')
 
271
 
 
272
        # clone and add a differing revision
 
273
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
 
274
        self.build_tree_contents([('b/a', 'initial\nmore\n')])
 
275
        b_tree.commit(message='more')
 
276
 
 
277
        self.run_bzr('checkout --lightweight a checkout')
 
278
        self.run_bzr('switch --directory checkout b')
 
279
        self.assertFileEqual('initial\nmore\n', 'checkout/a')
 
280
 
 
281
 
 
282
class TestSwitchParentLocationBase(TestCaseWithTransport):
 
283
 
 
284
    def setUp(self):
 
285
        """Set up a repository and branch ready for testing."""
 
286
        super(TestSwitchParentLocationBase, self).setUp()
 
287
        self.script_runner = script.ScriptRunner()
 
288
        self.script_runner.run_script(self, '''
 
289
                $ bzr init-repo --no-trees repo
 
290
                Shared repository...
 
291
                Location:
 
292
                  shared repository: repo
 
293
                $ bzr init repo/trunk
 
294
                Created a repository branch...
 
295
                Using shared repository: ...
 
296
                ''')
 
297
 
 
298
    def assertParent(self, expected_parent, branch):
 
299
        """Verify that the parent is not None and is set correctly."""
 
300
        actual_parent = branch.get_parent()
 
301
        self.assertIsSameRealPath(urlutils.local_path_to_url(expected_parent),
 
302
                                  branch.get_parent())
 
303
 
 
304
 
 
305
class TestSwitchParentLocation(TestSwitchParentLocationBase):
 
306
 
 
307
    def _checkout_and_switch(self, option=''):
 
308
        self.script_runner.run_script(self, '''
 
309
                $ bzr checkout %(option)s repo/trunk checkout
 
310
                $ cd checkout
 
311
                $ bzr switch --create-branch switched
 
312
                2>Tree is up to date at revision 0.
 
313
                2>Switched to branch:...switched...
 
314
                $ cd ..
 
315
                ''' % locals())
 
316
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
317
        master_branch = branch.Branch.open_containing('repo/switched')[0]
 
318
        return (bound_branch, master_branch)
 
319
 
 
320
    def test_switch_parent_lightweight(self):
 
321
        """Lightweight checkout using bzr switch."""
 
322
        bb, mb = self._checkout_and_switch(option='--lightweight')
 
323
        self.assertParent('repo/trunk', bb)
 
324
        self.assertParent('repo/trunk', mb)
 
325
 
 
326
    def test_switch_parent_heavyweight(self):
 
327
        """Heavyweight checkout using bzr switch."""
 
328
        bb, mb = self._checkout_and_switch()
 
329
        self.assertParent('repo/trunk', bb)
 
330
        self.assertParent('repo/trunk', mb)
 
331
 
 
332
 
 
333
class TestSwitchDoesntOpenMasterBranch(TestCaseWithTransport):
 
334
    # See https://bugs.launchpad.net/bzr/+bug/812285
 
335
    # "bzr switch --create-branch" can point the new branch's parent to the
 
336
    # master branch, but it doesn't have to open it to do so.
 
337
 
 
338
    def test_switch_create_doesnt_open_master_branch(self):
 
339
        master = self.make_branch_and_tree('master')
 
340
        master.commit('one')
 
341
        # Note: not a lightweight checkout
 
342
        checkout = master.branch.create_checkout('checkout')
 
343
        opened = []
 
344
        def open_hook(branch):
 
345
            # Just append the final directory of the branch
 
346
            name = branch.base.rstrip('/').rsplit('/', 1)[1]
 
347
            opened.append(name)
 
348
        branch.Branch.hooks.install_named_hook('open', open_hook,
 
349
                                               'open_hook_logger')
 
350
        self.run_bzr('switch --create-branch -d checkout feature')
 
351
        # We only open the master branch 1 time.
 
352
        # This test should be cleaner to write, but see bug:
 
353
        #  https://bugs.launchpad.net/bzr/+bug/812295
 
354
        self.assertEqual(1, opened.count('master'))