1
# Copyright (C) 2007, 2008, 2009 Canonical Ltd
2
# -*- coding: utf-8 -*-
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
"""Tests for the switch command of bzr."""
23
from bzrlib.workingtree import WorkingTree
24
from bzrlib.tests.blackbox import ExternalBase
25
from bzrlib.directory_service import directories
28
class TestSwitch(ExternalBase):
30
def _create_sample_tree(self):
31
tree = self.make_branch_and_tree('branch-1')
32
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
39
def test_switch_up_to_date_light_checkout(self):
40
self.make_branch_and_tree('branch')
41
self.run_bzr('branch branch branch2')
42
self.run_bzr('checkout --lightweight branch checkout')
44
out, err = self.run_bzr('switch ../branch2')
45
self.assertContainsRe(err, 'Tree is up to date at revision 0.\n')
46
self.assertContainsRe(err, 'Switched to branch: .*/branch2.\n')
47
self.assertEqual('', out)
49
def test_switch_out_of_date_light_checkout(self):
50
self.make_branch_and_tree('branch')
51
self.run_bzr('branch branch branch2')
52
self.build_tree(['branch2/file'])
53
self.run_bzr('add branch2/file')
54
self.run_bzr('commit -m add-file branch2')
55
self.run_bzr('checkout --lightweight branch checkout')
57
out, err = self.run_bzr('switch ../branch2')
58
#self.assertContainsRe(err, '\+N file')
59
self.assertContainsRe(err, 'Updated to revision 1.\n')
60
self.assertContainsRe(err, 'Switched to branch: .*/branch2.\n')
61
self.assertEqual('', out)
63
def _test_switch_nick(self, lightweight):
64
"""Check that the nick gets switched too."""
65
tree1 = self.make_branch_and_tree('branch1')
66
tree2 = self.make_branch_and_tree('branch2')
67
tree2.pull(tree1.branch)
68
checkout = tree1.branch.create_checkout('checkout',
69
lightweight=lightweight)
70
self.assertEqual(checkout.branch.nick, tree1.branch.nick)
71
self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
73
self.run_bzr('switch branch2', working_dir='checkout')
75
# we need to get the tree again, otherwise we don't get the new branch
76
checkout = WorkingTree.open('checkout')
77
self.assertEqual(checkout.branch.nick, tree2.branch.nick)
78
self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
81
def test_switch_nick(self):
82
self._test_switch_nick(lightweight=False)
84
def test_switch_nick_lightweight(self):
85
self._test_switch_nick(lightweight=True)
87
def _test_switch_explicit_nick(self, lightweight):
88
"""Check that the nick gets switched too."""
89
tree1 = self.make_branch_and_tree('branch1')
90
tree2 = self.make_branch_and_tree('branch2')
91
tree2.pull(tree1.branch)
92
checkout = tree1.branch.create_checkout('checkout',
93
lightweight=lightweight)
94
self.assertEqual(checkout.branch.nick, tree1.branch.nick)
95
checkout.branch.nick = "explicit_nick"
96
self.assertEqual(checkout.branch.nick, "explicit_nick")
97
self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
99
self.run_bzr('switch branch2', working_dir='checkout')
101
# we need to get the tree again, otherwise we don't get the new branch
102
checkout = WorkingTree.open('checkout')
103
self.assertEqual(checkout.branch.nick, tree2.branch.nick)
104
self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
107
def test_switch_explicit_nick(self):
108
self._test_switch_explicit_nick(lightweight=False)
110
def test_switch_explicit_nick_lightweight(self):
111
self._test_switch_explicit_nick(lightweight=True)
113
def test_switch_finds_relative_branch(self):
114
"""Switch will find 'foo' relative to the branch the checkout is of."""
115
self.build_tree(['repo/'])
116
tree1 = self.make_branch_and_tree('repo/brancha')
118
tree2 = self.make_branch_and_tree('repo/branchb')
119
tree2.pull(tree1.branch)
120
branchb_id = tree2.commit('bar')
121
checkout = tree1.branch.create_checkout('checkout', lightweight=True)
122
self.run_bzr(['switch', 'branchb'], working_dir='checkout')
123
self.assertEqual(branchb_id, checkout.last_revision())
124
checkout = checkout.bzrdir.open_workingtree()
125
self.assertEqual(tree2.branch.base, checkout.branch.base)
127
def test_switch_finds_relative_bound_branch(self):
128
"""Using switch on a heavy checkout should find master sibling
130
The behaviour of lighweight and heavy checkouts should be
131
consistentwhen using the convenient "switch to sibling" feature
132
Both should switch to a sibling of the branch
133
they are bound to, and not a sibling of themself"""
135
self.build_tree(['repo/',
137
tree1 = self.make_branch_and_tree('repo/brancha')
139
tree2 = self.make_branch_and_tree('repo/branchb')
140
tree2.pull(tree1.branch)
141
branchb_id = tree2.commit('bar')
142
checkout = tree1.branch.create_checkout('heavyco/a', lightweight=False)
143
self.run_bzr(['switch', 'branchb'], working_dir='heavyco/a')
144
self.assertEqual(branchb_id, checkout.last_revision())
145
self.assertEqual(tree2.branch.base, checkout.branch.get_bound_location())
147
def test_switch_revision(self):
148
tree = self._create_sample_tree()
149
checkout = tree.branch.create_checkout('checkout', lightweight=True)
150
self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
151
self.failUnlessExists('checkout/file-1')
152
self.failIfExists('checkout/file-2')
154
def test_switch_only_revision(self):
155
tree = self._create_sample_tree()
156
checkout = tree.branch.create_checkout('checkout', lightweight=True)
157
self.failUnlessExists('checkout/file-1')
158
self.failUnlessExists('checkout/file-2')
159
self.run_bzr(['switch', '-r1'], working_dir='checkout')
160
self.failUnlessExists('checkout/file-1')
161
self.failIfExists('checkout/file-2')
162
# Check that we don't accept a range
164
['bzr switch --revision takes exactly one revision identifier'],
165
['switch', '-r0..2'], working_dir='checkout')
167
def prepare_lightweight_switch(self):
168
branch = self.make_branch('branch')
169
branch.create_checkout('tree', lightweight=True)
170
os.rename('branch', 'branch1')
172
def test_switch_lightweight_after_branch_moved(self):
173
self.prepare_lightweight_switch()
174
self.run_bzr('switch --force ../branch1', working_dir='tree')
175
branch_location = WorkingTree.open('tree').branch.base
176
self.assertEndsWith(branch_location, 'branch1/')
178
def test_switch_lightweight_after_branch_moved_relative(self):
179
self.prepare_lightweight_switch()
180
self.run_bzr('switch --force branch1', working_dir='tree')
181
branch_location = WorkingTree.open('tree').branch.base
182
self.assertEndsWith(branch_location, 'branch1/')
184
def test_create_branch_no_branch(self):
185
self.prepare_lightweight_switch()
186
self.run_bzr_error(['cannot create branch without source branch'],
187
'switch --create-branch ../branch2', working_dir='tree')
189
def test_create_branch(self):
190
branch = self.make_branch('branch')
191
tree = branch.create_checkout('tree', lightweight=True)
192
tree.commit('one', rev_id='rev-1')
193
self.run_bzr('switch --create-branch ../branch2', working_dir='tree')
194
tree = WorkingTree.open('tree')
195
self.assertEndsWith(tree.branch.base, '/branch2/')
197
def test_create_branch_local(self):
198
branch = self.make_branch('branch')
199
tree = branch.create_checkout('tree', lightweight=True)
200
tree.commit('one', rev_id='rev-1')
201
self.run_bzr('switch --create-branch branch2', working_dir='tree')
202
tree = WorkingTree.open('tree')
203
# The new branch should have been created at the same level as
204
# 'branch', because we did not have a '/' segment
205
self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
207
def test_create_branch_short_name(self):
208
branch = self.make_branch('branch')
209
tree = branch.create_checkout('tree', lightweight=True)
210
tree.commit('one', rev_id='rev-1')
211
self.run_bzr('switch -b branch2', working_dir='tree')
212
tree = WorkingTree.open('tree')
213
# The new branch should have been created at the same level as
214
# 'branch', because we did not have a '/' segment
215
self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
217
def test_create_branch_directory_services(self):
218
branch = self.make_branch('branch')
219
tree = branch.create_checkout('tree', lightweight=True)
220
class FooLookup(object):
221
def look_up(self, name, url):
223
directories.register('foo:', FooLookup, 'Create branches named foo-')
224
self.addCleanup(directories.remove, 'foo:')
225
self.run_bzr('switch -b foo:branch2', working_dir='tree')
226
tree = WorkingTree.open('tree')
227
self.assertEndsWith(tree.branch.base, 'foo-branch2/')