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 test_switch_up_to_date_light_checkout(self):
31
self.make_branch_and_tree('branch')
32
self.run_bzr('branch branch branch2')
33
self.run_bzr('checkout --lightweight branch checkout')
35
out, err = self.run_bzr('switch ../branch2')
36
self.assertContainsRe(err, 'Tree is up to date at revision 0.\n')
37
self.assertContainsRe(err, 'Switched to branch: .*/branch2.\n')
38
self.assertEqual('', out)
40
def test_switch_out_of_date_light_checkout(self):
41
self.make_branch_and_tree('branch')
42
self.run_bzr('branch branch branch2')
43
self.build_tree(['branch2/file'])
44
self.run_bzr('add branch2/file')
45
self.run_bzr('commit -m add-file branch2')
46
self.run_bzr('checkout --lightweight branch checkout')
48
out, err = self.run_bzr('switch ../branch2')
49
#self.assertContainsRe(err, '\+N file')
50
self.assertContainsRe(err, 'Updated to revision 1.\n')
51
self.assertContainsRe(err, 'Switched to branch: .*/branch2.\n')
52
self.assertEqual('', out)
54
def _test_switch_nick(self, lightweight):
55
"""Check that the nick gets switched too."""
56
tree1 = self.make_branch_and_tree('branch1')
57
tree2 = self.make_branch_and_tree('branch2')
58
tree2.pull(tree1.branch)
59
checkout = tree1.branch.create_checkout('checkout',
60
lightweight=lightweight)
61
self.assertEqual(checkout.branch.nick, tree1.branch.nick)
62
self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
64
self.run_bzr('switch branch2', working_dir='checkout')
66
# we need to get the tree again, otherwise we don't get the new branch
67
checkout = WorkingTree.open('checkout')
68
self.assertEqual(checkout.branch.nick, tree2.branch.nick)
69
self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
72
def test_switch_nick(self):
73
self._test_switch_nick(lightweight=False)
75
def test_switch_nick_lightweight(self):
76
self._test_switch_nick(lightweight=True)
78
def _test_switch_explicit_nick(self, lightweight):
79
"""Check that the nick gets switched too."""
80
tree1 = self.make_branch_and_tree('branch1')
81
tree2 = self.make_branch_and_tree('branch2')
82
tree2.pull(tree1.branch)
83
checkout = tree1.branch.create_checkout('checkout',
84
lightweight=lightweight)
85
self.assertEqual(checkout.branch.nick, tree1.branch.nick)
86
checkout.branch.nick = "explicit_nick"
87
self.assertEqual(checkout.branch.nick, "explicit_nick")
88
self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
90
self.run_bzr('switch branch2', working_dir='checkout')
92
# we need to get the tree again, otherwise we don't get the new branch
93
checkout = WorkingTree.open('checkout')
94
self.assertEqual(checkout.branch.nick, tree2.branch.nick)
95
self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
98
def test_switch_explicit_nick(self):
99
self._test_switch_explicit_nick(lightweight=False)
101
def test_switch_explicit_nick_lightweight(self):
102
self._test_switch_explicit_nick(lightweight=True)
104
def test_switch_finds_relative_branch(self):
105
"""Switch will find 'foo' relative to the branch the checkout is of."""
106
self.build_tree(['repo/'])
107
tree1 = self.make_branch_and_tree('repo/brancha')
109
tree2 = self.make_branch_and_tree('repo/branchb')
110
tree2.pull(tree1.branch)
111
branchb_id = tree2.commit('bar')
112
checkout = tree1.branch.create_checkout('checkout', lightweight=True)
113
self.run_bzr(['switch', 'branchb'], working_dir='checkout')
114
self.assertEqual(branchb_id, checkout.last_revision())
115
checkout = checkout.bzrdir.open_workingtree()
116
self.assertEqual(tree2.branch.base, checkout.branch.base)
118
def test_switch_finds_relative_bound_branch(self):
119
"""Using switch on a heavy checkout should find master sibling
121
The behaviour of lighweight and heavy checkouts should be
122
consistentwhen using the convenient "switch to sibling" feature
123
Both should switch to a sibling of the branch
124
they are bound to, and not a sibling of themself"""
126
self.build_tree(['repo/',
128
tree1 = self.make_branch_and_tree('repo/brancha')
130
tree2 = self.make_branch_and_tree('repo/branchb')
131
tree2.pull(tree1.branch)
132
branchb_id = tree2.commit('bar')
133
checkout = tree1.branch.create_checkout('heavyco/a', lightweight=False)
134
self.run_bzr(['switch', 'branchb'], working_dir='heavyco/a')
135
self.assertEqual(branchb_id, checkout.last_revision())
136
self.assertEqual(tree2.branch.base, checkout.branch.get_bound_location())
138
def prepare_lightweight_switch(self):
139
branch = self.make_branch('branch')
140
branch.create_checkout('tree', lightweight=True)
141
os.rename('branch', 'branch1')
143
def test_switch_lightweight_after_branch_moved(self):
144
self.prepare_lightweight_switch()
145
self.run_bzr('switch --force ../branch1', working_dir='tree')
146
branch_location = WorkingTree.open('tree').branch.base
147
self.assertEndsWith(branch_location, 'branch1/')
149
def test_switch_lightweight_after_branch_moved_relative(self):
150
self.prepare_lightweight_switch()
151
self.run_bzr('switch --force branch1', working_dir='tree')
152
branch_location = WorkingTree.open('tree').branch.base
153
self.assertEndsWith(branch_location, 'branch1/')
155
def test_create_branch_no_branch(self):
156
self.prepare_lightweight_switch()
157
self.run_bzr_error(['cannot create branch without source branch'],
158
'switch --create-branch ../branch2', working_dir='tree')
160
def test_create_branch(self):
161
branch = self.make_branch('branch')
162
tree = branch.create_checkout('tree', lightweight=True)
163
tree.commit('one', rev_id='rev-1')
164
self.run_bzr('switch --create-branch ../branch2', working_dir='tree')
165
tree = WorkingTree.open('tree')
166
self.assertEndsWith(tree.branch.base, '/branch2/')
168
def test_create_branch_local(self):
169
branch = self.make_branch('branch')
170
tree = branch.create_checkout('tree', lightweight=True)
171
tree.commit('one', rev_id='rev-1')
172
self.run_bzr('switch --create-branch branch2', working_dir='tree')
173
tree = WorkingTree.open('tree')
174
# The new branch should have been created at the same level as
175
# 'branch', because we did not have a '/' segment
176
self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
178
def test_create_branch_short_name(self):
179
branch = self.make_branch('branch')
180
tree = branch.create_checkout('tree', lightweight=True)
181
tree.commit('one', rev_id='rev-1')
182
self.run_bzr('switch -b branch2', working_dir='tree')
183
tree = WorkingTree.open('tree')
184
# The new branch should have been created at the same level as
185
# 'branch', because we did not have a '/' segment
186
self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
188
def test_create_branch_directory_services(self):
189
branch = self.make_branch('branch')
190
tree = branch.create_checkout('tree', lightweight=True)
191
class FooLookup(object):
192
def look_up(self, name, url):
194
directories.register('foo:', FooLookup, 'Create branches named foo-')
195
self.addCleanup(directories.remove, 'foo:')
196
self.run_bzr('switch -b foo:branch2', working_dir='tree')
197
tree = WorkingTree.open('tree')
198
self.assertEndsWith(tree.branch.base, 'foo-branch2/')