~bzr-pqm/bzr/bzr.dev

4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2007-2010 Canonical Ltd
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
2
# -*- coding: utf-8 -*-
3
#
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.
8
#
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.
13
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
17
18
19
"""Tests for the switch command of bzr."""
20
21
import os
22
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
23
from bzrlib.workingtree import WorkingTree
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
24
from bzrlib.tests.blackbox import ExternalBase
4879.2.1 by Neil Martinsen-Burrell
switch should use directory services when creating a branch
25
from bzrlib.directory_service import directories
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
26
27
28
class TestSwitch(ExternalBase):
29
3984.5.14 by Daniel Watkins
Extracted common setup code.
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'])
33
        tree.add('file-1')
34
        tree.commit('rev1')
35
        tree.add('file-2')
36
        tree.commit('rev2')
37
        return tree
38
2999.1.4 by Ian Clatworthy
more review tweaks including commit of blackbox tests
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')
43
        os.chdir('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)
48
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')
56
        os.chdir('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)
3246.5.1 by Robert Collins
* ``bzr switch`` will attempt to find branches to switch to relative to the
62
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
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(),
72
            False)
73
        self.run_bzr('switch branch2', working_dir='checkout')
74
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(),
79
            False)
80
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
81
    def test_switch_nick(self):
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
82
        self._test_switch_nick(lightweight=False)
83
84
    def test_switch_nick_lightweight(self):
85
        self._test_switch_nick(lightweight=True)
86
87
    def _test_switch_explicit_nick(self, lightweight):
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
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)
3565.6.7 by Marius Kruger
* checkouts now use master nick when no explicit nick is set.
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(),
98
            "explicit_nick")
99
        self.run_bzr('switch branch2', working_dir='checkout')
100
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(),
105
            tree2.branch.nick)
106
107
    def test_switch_explicit_nick(self):
108
        self._test_switch_explicit_nick(lightweight=False)
109
110
    def test_switch_explicit_nick_lightweight(self):
111
        self._test_switch_explicit_nick(lightweight=True)
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
112
3246.5.1 by Robert Collins
* ``bzr switch`` will attempt to find branches to switch to relative to the
113
    def test_switch_finds_relative_branch(self):
3565.6.1 by Marius Kruger
Let 'bzr switch' update the nick too.
114
        """Switch will find 'foo' relative to the branch the checkout is of."""
3246.5.1 by Robert Collins
* ``bzr switch`` will attempt to find branches to switch to relative to the
115
        self.build_tree(['repo/'])
116
        tree1 = self.make_branch_and_tree('repo/brancha')
117
        tree1.commit('foo')
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)
3602.3.1 by Adrian Wilkins
Test that `bzr switch` finds the sibling of the bound branch of heavy checkout.
126
127
    def test_switch_finds_relative_bound_branch(self):
3602.3.4 by Adrian Wilkins
Improved comments and documentation
128
        """Using switch on a heavy checkout should find master sibling
129
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
130
        The behaviour of lighweight and heavy checkouts should be
3602.3.4 by Adrian Wilkins
Improved comments and documentation
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"""
134
3602.3.1 by Adrian Wilkins
Test that `bzr switch` finds the sibling of the bound branch of heavy checkout.
135
        self.build_tree(['repo/',
136
                         'heavyco/'])
137
        tree1 = self.make_branch_and_tree('repo/brancha')
138
        tree1.commit('foo')
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())
4354.2.2 by Aaron Bentley
Enable switch --force for lightweight checkouts after moves.
146
3984.5.2 by Daniel Watkins
Added blackbox test.
147
    def test_switch_revision(self):
3984.5.14 by Daniel Watkins
Extracted common setup code.
148
        tree = self._create_sample_tree()
3984.5.2 by Daniel Watkins
Added blackbox test.
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')
3984.5.9 by Daniel Watkins
Added test for allowing only revisions to be passed to switch.
153
154
    def test_switch_only_revision(self):
3984.5.14 by Daniel Watkins
Extracted common setup code.
155
        tree = self._create_sample_tree()
3984.5.9 by Daniel Watkins
Added test for allowing only revisions to be passed to switch.
156
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
3984.5.10 by Daniel Watkins
Toughened test slightly.
157
        self.failUnlessExists('checkout/file-1')
158
        self.failUnlessExists('checkout/file-2')
3984.5.9 by Daniel Watkins
Added test for allowing only revisions to be passed to switch.
159
        self.run_bzr(['switch', '-r1'], working_dir='checkout')
160
        self.failUnlessExists('checkout/file-1')
161
        self.failIfExists('checkout/file-2')
3984.5.15 by Daniel Watkins
Add to test to ensure that we don't accept a range of revisions.
162
        # Check that we don't accept a range
3984.5.17 by Daniel Watkins
Fixed incorrect call of run_bzr_error.
163
        self.run_bzr_error(
164
            ['bzr switch --revision takes exactly one revision identifier'],
165
            ['switch', '-r0..2'], working_dir='checkout')
3984.5.19 by Andrew Bennetts
Merge lp:bzr, resolving conflicts.
166
4354.2.2 by Aaron Bentley
Enable switch --force for lightweight checkouts after moves.
167
    def prepare_lightweight_switch(self):
168
        branch = self.make_branch('branch')
169
        branch.create_checkout('tree', lightweight=True)
170
        os.rename('branch', 'branch1')
171
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/')
177
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/')
4520.1.1 by John Arbash Meinel
'bzr switch -b' can now be used to create the branch while you switch to it.
183
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')
188
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/')
196
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)
206
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)
4879.2.1 by Neil Martinsen-Burrell
switch should use directory services when creating a branch
216
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):
222
                return 'foo-'+name
223
        directories.register('foo:', FooLookup, 'Create branches named foo-')
4879.2.2 by Neil Martinsen-Burrell
add test cleanup per JAMs review
224
        self.addCleanup(directories.remove, 'foo:')
4879.2.1 by Neil Martinsen-Burrell
switch should use directory services when creating a branch
225
        self.run_bzr('switch -b foo:branch2', working_dir='tree')
226
        tree = WorkingTree.open('tree')
227
        self.assertEndsWith(tree.branch.base, 'foo-branch2/')