23
from bzrlib import osutils
23
24
from bzrlib.workingtree import WorkingTree
24
from bzrlib.tests.blackbox import ExternalBase
27
class TestSwitch(ExternalBase):
25
from bzrlib.tests import TestCaseWithTransport
26
from bzrlib.directory_service import directories
29
class TestSwitch(TestCaseWithTransport):
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'])
29
40
def test_switch_up_to_date_light_checkout(self):
30
41
self.make_branch_and_tree('branch')
134
145
self.assertEqual(branchb_id, checkout.last_revision())
135
146
self.assertEqual(tree2.branch.base, checkout.branch.get_bound_location())
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')
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
165
['bzr switch --revision takes exactly one revision identifier'],
166
['switch', '-r0..2'], working_dir='checkout')
137
168
def prepare_lightweight_switch(self):
138
169
branch = self.make_branch('branch')
139
170
branch.create_checkout('tree', lightweight=True)
140
os.rename('branch', 'branch1')
171
osutils.rename('branch', 'branch1')
142
173
def test_switch_lightweight_after_branch_moved(self):
143
174
self.prepare_lightweight_switch()
183
214
# The new branch should have been created at the same level as
184
215
# 'branch', because we did not have a '/' segment
185
216
self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
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):
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/')
230
def test_switch_with_post_switch_hook(self):
231
from bzrlib import branch as _mod_branch
233
_mod_branch.Branch.hooks.install_named_hook('post_switch',
235
self.make_branch_and_tree('branch')
236
self.run_bzr('branch branch branch2')
237
self.run_bzr('checkout branch checkout')
239
self.assertLength(0, calls)
240
out, err = self.run_bzr('switch ../branch2')
241
self.assertLength(1, calls)
243
def test_switch_lightweight_co_with_post_switch_hook(self):
244
from bzrlib import branch as _mod_branch
246
_mod_branch.Branch.hooks.install_named_hook('post_switch',
248
self.make_branch_and_tree('branch')
249
self.run_bzr('branch branch branch2')
250
self.run_bzr('checkout --lightweight branch checkout')
252
self.assertLength(0, calls)
253
out, err = self.run_bzr('switch ../branch2')
254
self.assertLength(1, calls)
256
def test_switch_lightweight_directory(self):
257
"""Test --directory option"""
259
# create a source branch
260
a_tree = self.make_branch_and_tree('a')
261
self.build_tree_contents([('a/a', 'initial\n')])
263
a_tree.commit(message='initial')
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')
270
self.run_bzr('checkout --lightweight a checkout')
271
self.run_bzr('switch --directory checkout b')
272
self.assertFileEqual('initial\nmore\n', 'checkout/a')