~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-01-15 04:49:48 UTC
  • mfrom: (3984.5.22 switch-r-183559)
  • Revision ID: pqm@pqm.ubuntu.com-20100115044948-yxz5m3vchxapbq22
(andrew) Add --revision option to 'bzr switch'. (#184559)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007, 2008, 2009 Canonical Ltd
 
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
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
 
 
19
"""Tests for the switch command of bzr."""
 
20
 
 
21
import os
 
22
 
 
23
from bzrlib.workingtree import WorkingTree
 
24
from bzrlib.tests.blackbox import ExternalBase
 
25
from bzrlib.directory_service import directories
 
26
 
 
27
 
 
28
class TestSwitch(ExternalBase):
 
29
 
 
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
 
 
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)
 
62
 
 
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
 
 
81
    def test_switch_nick(self):
 
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):
 
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(),
 
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)
 
112
 
 
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')
 
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)
 
126
 
 
127
    def test_switch_finds_relative_bound_branch(self):
 
128
        """Using switch on a heavy checkout should find master sibling
 
129
 
 
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"""
 
134
 
 
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())
 
146
 
 
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')
 
153
 
 
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
 
163
        self.run_bzr_error(
 
164
            ['bzr switch --revision takes exactly one revision identifier'],
 
165
            ['switch', '-r0..2'], working_dir='checkout')
 
166
 
 
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/')
 
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)
 
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-')
 
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/')