~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-07-07 04:03:32 UTC
  • mfrom: (5335.2.3 doc)
  • Revision ID: pqm@pqm.ubuntu.com-20100707040332-we60v2hsd39rumlr
(mbp) developer docs about testing (Martin Pool)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 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
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
18
18
 
19
19
"""Tests for the switch command of bzr."""
20
20
 
21
21
import os
22
22
 
 
23
from bzrlib import osutils
23
24
from bzrlib.workingtree import WorkingTree
24
 
from bzrlib.tests.blackbox import ExternalBase
25
 
 
26
 
 
27
 
class TestSwitch(ExternalBase):
 
25
from bzrlib.tests import TestCaseWithTransport
 
26
from bzrlib.directory_service import directories
 
27
 
 
28
 
 
29
class TestSwitch(TestCaseWithTransport):
 
30
 
 
31
    def _create_sample_tree(self):
 
32
        tree = self.make_branch_and_tree('branch-1')
 
33
        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
 
34
        tree.add('file-1')
 
35
        tree.commit('rev1')
 
36
        tree.add('file-2')
 
37
        tree.commit('rev2')
 
38
        return tree
28
39
 
29
40
    def test_switch_up_to_date_light_checkout(self):
30
41
        self.make_branch_and_tree('branch')
117
128
    def test_switch_finds_relative_bound_branch(self):
118
129
        """Using switch on a heavy checkout should find master sibling
119
130
 
120
 
        The behaviour of lighweight and heavy checkouts should be 
 
131
        The behaviour of lighweight and heavy checkouts should be
121
132
        consistentwhen using the convenient "switch to sibling" feature
122
133
        Both should switch to a sibling of the branch
123
134
        they are bound to, and not a sibling of themself"""
133
144
        self.run_bzr(['switch', 'branchb'], working_dir='heavyco/a')
134
145
        self.assertEqual(branchb_id, checkout.last_revision())
135
146
        self.assertEqual(tree2.branch.base, checkout.branch.get_bound_location())
 
147
 
 
148
    def test_switch_revision(self):
 
149
        tree = self._create_sample_tree()
 
150
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
 
151
        self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
 
152
        self.failUnlessExists('checkout/file-1')
 
153
        self.failIfExists('checkout/file-2')
 
154
 
 
155
    def test_switch_only_revision(self):
 
156
        tree = self._create_sample_tree()
 
157
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
 
158
        self.failUnlessExists('checkout/file-1')
 
159
        self.failUnlessExists('checkout/file-2')
 
160
        self.run_bzr(['switch', '-r1'], working_dir='checkout')
 
161
        self.failUnlessExists('checkout/file-1')
 
162
        self.failIfExists('checkout/file-2')
 
163
        # Check that we don't accept a range
 
164
        self.run_bzr_error(
 
165
            ['bzr switch --revision takes exactly one revision identifier'],
 
166
            ['switch', '-r0..2'], working_dir='checkout')
 
167
 
 
168
    def prepare_lightweight_switch(self):
 
169
        branch = self.make_branch('branch')
 
170
        branch.create_checkout('tree', lightweight=True)
 
171
        osutils.rename('branch', 'branch1')
 
172
 
 
173
    def test_switch_lightweight_after_branch_moved(self):
 
174
        self.prepare_lightweight_switch()
 
175
        self.run_bzr('switch --force ../branch1', working_dir='tree')
 
176
        branch_location = WorkingTree.open('tree').branch.base
 
177
        self.assertEndsWith(branch_location, 'branch1/')
 
178
 
 
179
    def test_switch_lightweight_after_branch_moved_relative(self):
 
180
        self.prepare_lightweight_switch()
 
181
        self.run_bzr('switch --force branch1', working_dir='tree')
 
182
        branch_location = WorkingTree.open('tree').branch.base
 
183
        self.assertEndsWith(branch_location, 'branch1/')
 
184
 
 
185
    def test_create_branch_no_branch(self):
 
186
        self.prepare_lightweight_switch()
 
187
        self.run_bzr_error(['cannot create branch without source branch'],
 
188
            'switch --create-branch ../branch2', working_dir='tree')
 
189
 
 
190
    def test_create_branch(self):
 
191
        branch = self.make_branch('branch')
 
192
        tree = branch.create_checkout('tree', lightweight=True)
 
193
        tree.commit('one', rev_id='rev-1')
 
194
        self.run_bzr('switch --create-branch ../branch2', working_dir='tree')
 
195
        tree = WorkingTree.open('tree')
 
196
        self.assertEndsWith(tree.branch.base, '/branch2/')
 
197
 
 
198
    def test_create_branch_local(self):
 
199
        branch = self.make_branch('branch')
 
200
        tree = branch.create_checkout('tree', lightweight=True)
 
201
        tree.commit('one', rev_id='rev-1')
 
202
        self.run_bzr('switch --create-branch branch2', working_dir='tree')
 
203
        tree = WorkingTree.open('tree')
 
204
        # The new branch should have been created at the same level as
 
205
        # 'branch', because we did not have a '/' segment
 
206
        self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
 
207
 
 
208
    def test_create_branch_short_name(self):
 
209
        branch = self.make_branch('branch')
 
210
        tree = branch.create_checkout('tree', lightweight=True)
 
211
        tree.commit('one', rev_id='rev-1')
 
212
        self.run_bzr('switch -b branch2', working_dir='tree')
 
213
        tree = WorkingTree.open('tree')
 
214
        # The new branch should have been created at the same level as
 
215
        # 'branch', because we did not have a '/' segment
 
216
        self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
 
217
 
 
218
    def test_create_branch_directory_services(self):
 
219
        branch = self.make_branch('branch')
 
220
        tree = branch.create_checkout('tree', lightweight=True)
 
221
        class FooLookup(object):
 
222
            def look_up(self, name, url):
 
223
                return 'foo-'+name
 
224
        directories.register('foo:', FooLookup, 'Create branches named foo-')
 
225
        self.addCleanup(directories.remove, 'foo:')
 
226
        self.run_bzr('switch -b foo:branch2', working_dir='tree')
 
227
        tree = WorkingTree.open('tree')
 
228
        self.assertEndsWith(tree.branch.base, 'foo-branch2/')
 
229
 
 
230
    def test_switch_with_post_switch_hook(self):
 
231
        from bzrlib import branch as _mod_branch
 
232
        calls = []
 
233
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
 
234
            calls.append, None)
 
235
        self.make_branch_and_tree('branch')
 
236
        self.run_bzr('branch branch branch2')
 
237
        self.run_bzr('checkout branch checkout')
 
238
        os.chdir('checkout')
 
239
        self.assertLength(0, calls)
 
240
        out, err = self.run_bzr('switch ../branch2')
 
241
        self.assertLength(1, calls)
 
242
 
 
243
    def test_switch_lightweight_co_with_post_switch_hook(self):
 
244
        from bzrlib import branch as _mod_branch
 
245
        calls = []
 
246
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
 
247
            calls.append, None)
 
248
        self.make_branch_and_tree('branch')
 
249
        self.run_bzr('branch branch branch2')
 
250
        self.run_bzr('checkout --lightweight branch checkout')
 
251
        os.chdir('checkout')
 
252
        self.assertLength(0, calls)
 
253
        out, err = self.run_bzr('switch ../branch2')
 
254
        self.assertLength(1, calls)
 
255
 
 
256
    def test_switch_lightweight_directory(self):
 
257
        """Test --directory option"""
 
258
 
 
259
        # create a source branch
 
260
        a_tree = self.make_branch_and_tree('a')
 
261
        self.build_tree_contents([('a/a', 'initial\n')])
 
262
        a_tree.add('a')
 
263
        a_tree.commit(message='initial')
 
264
 
 
265
        # clone and add a differing revision
 
266
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
 
267
        self.build_tree_contents([('b/a', 'initial\nmore\n')])
 
268
        b_tree.commit(message='more')
 
269
 
 
270
        self.run_bzr('checkout --lightweight a checkout')
 
271
        self.run_bzr('switch --directory checkout b')
 
272
        self.assertFileEqual('initial\nmore\n', 'checkout/a')