1
# Copyright (C) 2006-2010 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
18
"""Tests for the update command of bzr."""
31
from bzrlib.tests.script import ScriptRunner
34
class TestUpdate(tests.TestCaseWithTransport):
36
def test_update_standalone_trivial(self):
37
self.make_branch_and_tree('.')
38
out, err = self.run_bzr('update')
40
'Tree is up to date at revision 0 of branch %s\n' % self.test_dir,
42
self.assertEqual('', out)
44
def test_update_quiet(self):
45
self.make_branch_and_tree('.')
46
out, err = self.run_bzr('update --quiet')
47
self.assertEqual('', err)
48
self.assertEqual('', out)
50
def test_update_standalone_trivial_with_alias_up(self):
51
self.make_branch_and_tree('.')
52
out, err = self.run_bzr('up')
53
self.assertEqual('Tree is up to date at revision 0 of branch %s\n'
56
self.assertEqual('', out)
58
def test_update_up_to_date_light_checkout(self):
59
self.make_branch_and_tree('branch')
60
self.run_bzr('checkout --lightweight branch checkout')
61
out, err = self.run_bzr('update checkout')
62
self.assertEqual('Tree is up to date at revision 0 of branch %s\n'
63
% osutils.pathjoin(self.test_dir, 'branch'),
65
self.assertEqual('', out)
67
def test_update_up_to_date_checkout(self):
68
self.make_branch_and_tree('branch')
69
self.run_bzr('checkout branch checkout')
71
sr.run_script(self, '''
73
2>Tree is up to date at revision 0 of branch .../branch
76
def test_update_out_of_date_standalone_tree(self):
77
# FIXME the default format has to change for this to pass
78
# because it currently uses the branch last-revision marker.
79
self.make_branch_and_tree('branch')
81
self.run_bzr('checkout --lightweight branch checkout')
82
self.build_tree(['checkout/file'])
83
self.run_bzr('add checkout/file')
84
self.run_bzr('commit -m add-file checkout')
85
# now branch should be out of date
86
out,err = self.run_bzr('update branch')
87
self.assertEqual('', out)
88
self.assertEqualDiff("""+N file
89
All changes applied successfully.
90
Updated to revision 1 of branch %s
91
""" % osutils.pathjoin(self.test_dir, 'branch',),
93
self.failUnlessExists('branch/file')
95
def test_update_out_of_date_light_checkout(self):
96
self.make_branch_and_tree('branch')
98
self.run_bzr('checkout --lightweight branch checkout')
99
self.run_bzr('checkout --lightweight branch checkout2')
100
self.build_tree(['checkout/file'])
101
self.run_bzr('add checkout/file')
102
self.run_bzr('commit -m add-file checkout')
103
# now checkout2 should be out of date
104
out,err = self.run_bzr('update checkout2')
105
self.assertEqualDiff('''+N file
106
All changes applied successfully.
107
Updated to revision 1 of branch %s
108
''' % osutils.pathjoin(self.test_dir, 'branch',),
110
self.assertEqual('', out)
112
def test_update_conflicts_returns_2(self):
113
self.make_branch_and_tree('branch')
115
self.run_bzr('checkout --lightweight branch checkout')
116
self.build_tree(['checkout/file'])
117
self.run_bzr('add checkout/file')
118
self.run_bzr('commit -m add-file checkout')
119
self.run_bzr('checkout --lightweight branch checkout2')
120
# now alter file in checkout
121
a_file = file('checkout/file', 'wt')
124
self.run_bzr('commit -m checnge-file checkout')
125
# now checkout2 should be out of date
126
# make a local change to file
127
a_file = file('checkout2/file', 'wt')
130
out,err = self.run_bzr('update checkout2', retcode=1)
131
self.assertEqualDiff(''' M file
132
Text conflict in file
133
1 conflicts encountered.
134
Updated to revision 2 of branch %s
135
''' % osutils.pathjoin(self.test_dir, 'branch',),
137
self.assertEqual('', out)
139
def test_smoke_update_checkout_bound_branch_local_commits(self):
140
# smoke test for doing an update of a checkout of a bound
141
# branch with local commits.
142
master = self.make_branch_and_tree('master')
143
# make a bound branch
144
self.run_bzr('checkout master child')
145
# get an object form of child
146
child = workingtree.WorkingTree.open('child')
148
self.run_bzr('checkout --lightweight child checkout')
149
# get an object form of the checkout to manipulate
150
wt = workingtree.WorkingTree.open('checkout')
152
a_file = file('master/file', 'wt')
156
master_tip = master.commit('add file')
158
a_file = file('child/file_b', 'wt')
161
child.add(['file_b'])
162
child_tip = child.commit('add file_b', local=True)
164
a_file = file('checkout/file_c', 'wt')
169
# now, update checkout ->
170
# get all three files and a pending merge.
171
out, err = self.run_bzr('update checkout')
172
self.assertEqual('', out)
173
self.assertEqualDiff("""+N file_b
174
All changes applied successfully.
176
All changes applied successfully.
177
Updated to revision 1 of branch %s
178
Your local commits will now show as pending merges with 'bzr status', and can be committed with 'bzr commit'.
179
""" % osutils.pathjoin(self.test_dir, 'master',),
181
self.assertEqual([master_tip, child_tip], wt.get_parent_ids())
182
self.failUnlessExists('checkout/file')
183
self.failUnlessExists('checkout/file_b')
184
self.failUnlessExists('checkout/file_c')
185
self.assertTrue(wt.has_filename('file_c'))
187
def test_update_with_merges(self):
188
# Test that 'bzr update' works correctly when you have
189
# an update in the master tree, and a lightweight checkout
190
# which has merged another branch
191
master = self.make_branch_and_tree('master')
192
self.build_tree(['master/file'])
194
master.commit('one', rev_id='m1')
196
self.build_tree(['checkout1/'])
197
checkout_dir = bzrdir.BzrDirMetaFormat1().initialize('checkout1')
198
branch.BranchReferenceFormat().initialize(checkout_dir,
199
target_branch=master.branch)
200
checkout1 = checkout_dir.create_workingtree('m1')
202
# Create a second branch, with an extra commit
203
other = master.bzrdir.sprout('other').open_workingtree()
204
self.build_tree(['other/file2'])
206
other.commit('other2', rev_id='o2')
208
# Create a new commit in the master branch
209
self.build_tree(['master/file3'])
210
master.add(['file3'])
211
master.commit('f3', rev_id='m2')
213
# Merge the other branch into checkout
214
os.chdir('checkout1')
215
self.run_bzr('merge ../other')
217
self.assertEqual(['o2'], checkout1.get_parent_ids()[1:])
219
# At this point, 'commit' should fail, because we are out of date
220
self.run_bzr_error(["please run 'bzr update'"],
223
# This should not report about local commits being pending
224
# merges, because they were real merges
225
out, err = self.run_bzr('update')
226
self.assertEqual('', out)
227
self.assertEqualDiff('''+N file3
228
All changes applied successfully.
229
Updated to revision 2 of branch %s
230
''' % osutils.pathjoin(self.test_dir, 'master',),
232
# The pending merges should still be there
233
self.assertEqual(['o2'], checkout1.get_parent_ids()[1:])
235
def test_readonly_lightweight_update(self):
236
"""Update a light checkout of a readonly branch"""
237
tree = self.make_branch_and_tree('branch')
238
readonly_branch = branch.Branch.open(self.get_readonly_url('branch'))
239
checkout = readonly_branch.create_checkout('checkout',
241
tree.commit('empty commit')
242
self.run_bzr('update checkout')
244
def test_update_dash_r(self):
245
master = self.make_branch_and_tree('master')
247
self.build_tree(['./file1'])
248
master.add(['file1'])
249
master.commit('one', rev_id='m1')
250
self.build_tree(['./file2'])
251
master.add(['file2'])
252
master.commit('two', rev_id='m2')
255
sr.run_script(self, '''
258
2>All changes applied successfully.
259
2>Updated to revision 1 of .../master
261
self.failUnlessExists('./file1')
262
self.failIfExists('./file2')
263
self.assertEquals(['m1'], master.get_parent_ids())
265
def test_update_dash_r_outside_history(self):
266
"""Ensure that we can update -r to dotted revisions.
268
master = self.make_branch_and_tree('master')
269
self.build_tree(['master/file1'])
270
master.add(['file1'])
271
master.commit('one', rev_id='m1')
273
# Create a second branch, with extra commits
274
other = master.bzrdir.sprout('other').open_workingtree()
275
self.build_tree(['other/file2', 'other/file3'])
277
other.commit('other2', rev_id='o2')
279
other.commit('other3', rev_id='o3')
282
self.run_bzr('merge ../other')
283
master.commit('merge', rev_id='merge')
285
# Switch to o2. file3 was added only in o3 and should be deleted.
286
out, err = self.run_bzr('update -r revid:o2')
287
self.assertContainsRe(err, '-D\s+file3')
288
self.assertContainsRe(err, 'All changes applied successfully\.')
289
self.assertContainsRe(err, 'Updated to revision 1.1.1 of branch .*')
291
# Switch back to latest
292
out, err = self.run_bzr('update')
293
self.assertContainsRe(err, '\+N\s+file3')
294
self.assertContainsRe(err, 'All changes applied successfully\.')
295
self.assertContainsRe(err, 'Updated to revision 2 of branch .*')
297
def test_update_dash_r_in_master(self):
298
# Test that 'bzr update' works correctly when you have
299
# an update in the master tree,
300
master = self.make_branch_and_tree('master')
301
self.build_tree(['master/file1'])
302
master.add(['file1'])
303
master.commit('one', rev_id='m1')
305
self.run_bzr('checkout master checkout')
307
# add a revision in the master.
308
self.build_tree(['master/file2'])
309
master.add(['file2'])
310
master.commit('two', rev_id='m2')
314
sr.run_script(self, '''
315
$ bzr update -r revid:m2
317
2>All changes applied successfully.
318
2>Updated to revision 2 of branch .../master
321
def test_update_checkout_prevent_double_merge(self):
322
""""Launchpad bug 113809 in bzr "update performs two merges"
323
https://launchpad.net/bugs/113809"""
324
master = self.make_branch_and_tree('master')
325
self.build_tree_contents([('master/file', 'initial contents\n')])
327
master.commit('one', rev_id='m1')
329
checkout = master.branch.create_checkout('checkout')
330
lightweight = checkout.branch.create_checkout('lightweight',
333
# time to create a mess
334
# add a commit to the master
335
self.build_tree_contents([('master/file', 'master\n')])
336
master.commit('two', rev_id='m2')
337
self.build_tree_contents([('master/file', 'master local changes\n')])
339
# local commit on the checkout
340
self.build_tree_contents([('checkout/file', 'checkout\n')])
341
checkout.commit('tree', rev_id='c2', local=True)
342
self.build_tree_contents([('checkout/file',
343
'checkout local changes\n')])
346
self.build_tree_contents([('lightweight/file',
347
'lightweight local changes\n')])
349
# now update (and get conflicts)
350
out, err = self.run_bzr('update lightweight', retcode=1)
351
self.assertEqual('', out)
352
# NB: these conflicts are actually in the source code
353
self.assertFileEqual('''\
355
lightweight local changes
363
self.build_tree_contents([('lightweight/file',
364
'lightweight+checkout\n')])
365
self.run_bzr('resolve lightweight/file')
367
# check we get the second conflict
368
out, err = self.run_bzr('update lightweight', retcode=1)
369
self.assertEqual('', out)
370
# NB: these conflicts are actually in the source code
371
self.assertFileEqual('''\