1
# Copyright (C) 2006, 2007, 2009-2012, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for the 'checkout' CLI command."""
22
branch as _mod_branch,
27
from breezy.bzr import (
30
from breezy.tests import (
31
TestCaseWithTransport,
33
from breezy.tests.matchers import ContainsNoVfsCalls
34
from breezy.tests.features import (
39
class TestCheckout(TestCaseWithTransport):
42
super(TestCheckout, self).setUp()
43
tree = controldir.ControlDir.create_standalone_workingtree('branch')
44
tree.commit('1', rev_id=b'1', allow_pointless=True)
45
self.build_tree(['branch/added_in_2'])
46
tree.add('added_in_2')
47
tree.commit('2', rev_id=b'2')
49
def test_checkout_makes_bound_branch(self):
50
self.run_bzr('checkout branch checkout')
51
# if we have a checkout, the branch base should be 'branch'
52
source = controldir.ControlDir.open('branch')
53
result = controldir.ControlDir.open('checkout')
54
self.assertEqual(source.open_branch().controldir.root_transport.base,
55
result.open_branch().get_bound_location())
57
def test_checkout_light_makes_checkout(self):
58
self.run_bzr('checkout --lightweight branch checkout')
59
# if we have a checkout, the branch base should be 'branch'
60
source = controldir.ControlDir.open('branch')
61
result = controldir.ControlDir.open('checkout')
62
self.assertEqual(source.open_branch().controldir.root_transport.base,
63
result.open_branch().controldir.root_transport.base)
65
def test_checkout_dash_r(self):
66
out, err = self.run_bzr(['checkout', '-r', '-2', 'branch', 'checkout'])
67
# the working tree should now be at revision '1' with the content
69
result = controldir.ControlDir.open('checkout')
70
self.assertEqual(['1'], result.open_workingtree().get_parent_ids())
71
self.assertPathDoesNotExist('checkout/added_in_2')
73
def test_checkout_light_dash_r(self):
74
out, err = self.run_bzr(['checkout', '--lightweight', '-r', '-2',
75
'branch', 'checkout'])
76
# the working tree should now be at revision '1' with the content
78
result = controldir.ControlDir.open('checkout')
79
self.assertEqual(['1'], result.open_workingtree().get_parent_ids())
80
self.assertPathDoesNotExist('checkout/added_in_2')
82
def test_checkout_into_empty_dir(self):
83
self.make_controldir('checkout')
84
out, err = self.run_bzr(['checkout', 'branch', 'checkout'])
85
result = controldir.ControlDir.open('checkout')
86
tree = result.open_workingtree()
87
branch = result.open_branch()
89
def test_checkout_reconstitutes_working_trees(self):
90
# doing a 'brz checkout' in the directory of a branch with no tree
91
# or a 'brz checkout path' with path the name of a directory with
92
# a branch with no tree will reconsistute the tree.
93
os.mkdir('treeless-branch')
94
branch = controldir.ControlDir.create_branch_convenience(
97
format=bzrdir.BzrDirMetaFormat1())
98
# check no tree was created
99
self.assertRaises(errors.NoWorkingTree, branch.controldir.open_workingtree)
100
out, err = self.run_bzr('checkout treeless-branch')
101
# we should have a tree now
102
branch.controldir.open_workingtree()
104
out, err = self.run_bzr('diff treeless-branch')
106
# now test with no parameters
107
branch = controldir.ControlDir.create_branch_convenience(
109
force_new_tree=False,
110
format=bzrdir.BzrDirMetaFormat1())
111
# check no tree was created
112
self.assertRaises(errors.NoWorkingTree, branch.controldir.open_workingtree)
113
out, err = self.run_bzr('checkout')
114
# we should have a tree now
115
branch.controldir.open_workingtree()
117
out, err = self.run_bzr('diff')
119
def _test_checkout_existing_dir(self, lightweight):
120
source = self.make_branch_and_tree('source')
121
self.build_tree_contents([('source/file1', b'content1'),
122
('source/file2', b'content2'),])
123
source.add(['file1', 'file2'])
124
source.commit('added files')
125
self.build_tree_contents([('target/', b''),
126
('target/file1', b'content1'),
127
('target/file2', b'content3'),])
128
cmd = ['checkout', 'source', 'target']
130
cmd.append('--lightweight')
131
self.run_bzr('checkout source target')
132
# files with unique content should be moved
133
self.assertPathExists('target/file2.moved')
134
# files with content matching tree should not be moved
135
self.assertPathDoesNotExist('target/file1.moved')
137
def test_checkout_existing_dir_heavy(self):
138
self._test_checkout_existing_dir(False)
140
def test_checkout_existing_dir_lightweight(self):
141
self._test_checkout_existing_dir(True)
143
def test_checkout_in_branch_with_r(self):
144
branch = _mod_branch.Branch.open('branch')
145
branch.controldir.destroy_workingtree()
146
self.run_bzr('checkout -r 1', working_dir='branch')
147
tree = workingtree.WorkingTree.open('branch')
148
self.assertEqual('1', tree.last_revision())
149
branch.controldir.destroy_workingtree()
150
self.run_bzr('checkout -r 0', working_dir='branch')
151
self.assertEqual('null:', tree.last_revision())
153
def test_checkout_files_from(self):
154
branch = _mod_branch.Branch.open('branch')
155
self.run_bzr(['checkout', 'branch', 'branch2', '--files-from',
158
def test_checkout_hardlink(self):
159
self.requireFeature(HardlinkFeature)
160
source = self.make_branch_and_tree('source')
161
self.build_tree(['source/file1'])
163
source.commit('added file')
164
out, err = self.run_bzr('checkout source target --hardlink')
165
source_stat = os.stat('source/file1')
166
target_stat = os.stat('target/file1')
167
self.assertEqual(source_stat, target_stat)
169
def test_checkout_hardlink_files_from(self):
170
self.requireFeature(HardlinkFeature)
171
source = self.make_branch_and_tree('source')
172
self.build_tree(['source/file1'])
174
source.commit('added file')
175
source.controldir.sprout('second')
176
out, err = self.run_bzr('checkout source target --hardlink'
177
' --files-from second')
178
second_stat = os.stat('second/file1')
179
target_stat = os.stat('target/file1')
180
self.assertEqual(second_stat, target_stat)
182
def test_colo_checkout(self):
183
source = self.make_branch_and_tree('source', format='development-colo')
184
self.build_tree(['source/file1'])
186
source.commit('added file')
187
target = source.controldir.sprout('file:second,branch=somebranch',
188
create_tree_if_local=False)
189
out, err = self.run_bzr('checkout file:,branch=somebranch .',
190
working_dir='second')
191
# We should always be creating a lighweight checkout for colocated
194
target.open_branch(name='somebranch').user_url,
195
target.get_branch_reference(name=""))
198
class TestSmartServerCheckout(TestCaseWithTransport):
200
def test_heavyweight_checkout(self):
201
self.setup_smart_server_with_call_log()
202
t = self.make_branch_and_tree('from')
203
for count in range(9):
204
t.commit(message='commit %d' % count)
205
self.reset_smart_call_log()
206
out, err = self.run_bzr(['checkout', self.get_url('from'), 'target'])
207
# This figure represent the amount of work to perform this use case. It
208
# is entirely ok to reduce this number if a test fails due to rpc_count
209
# being too low. If rpc_count increases, more network roundtrips have
210
# become necessary for this use case. Please do not adjust this number
211
# upwards without agreement from bzr's network support maintainers.
212
self.assertLength(10, self.hpss_calls)
213
self.assertLength(1, self.hpss_connections)
214
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
216
def test_lightweight_checkout(self):
217
self.setup_smart_server_with_call_log()
218
t = self.make_branch_and_tree('from')
219
for count in range(9):
220
t.commit(message='commit %d' % count)
221
self.reset_smart_call_log()
222
out, err = self.run_bzr(['checkout', '--lightweight', self.get_url('from'),
224
# This figure represent the amount of work to perform this use case. It
225
# is entirely ok to reduce this number if a test fails due to rpc_count
226
# being too low. If rpc_count increases, more network roundtrips have
227
# become necessary for this use case. Please do not adjust this number
228
# upwards without agreement from bzr's network support maintainers.
229
self.assertLength(13, self.hpss_calls)
230
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)