1
# Copyright (C) 2006 by Canonical Ltd
1
# Copyright (C) 2006 Canonical Ltd
2
2
# -*- coding: utf-8 -*-
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
6
6
# the Free Software Foundation; either version 2 of the License, or
7
7
# (at your option) any later version.
9
9
# This program is distributed in the hope that it will be useful,
10
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
12
# GNU General Public License for more details.
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
19
"""Tests for the update command of bzr."""
22
from bzrlib.tests import TestSkipped
23
from bzrlib import branch, bzrdir
23
24
from bzrlib.tests.blackbox import ExternalBase
25
from bzrlib.workingtree import WorkingTree
26
28
class TestUpdate(ExternalBase):
28
30
def test_update_standalone_trivial(self):
30
out, err = self.runbzr('update')
31
self.assertEqual('Tree is up to date.\n', err)
31
self.make_branch_and_tree('.')
32
out, err = self.run_bzr('update')
33
self.assertEqual('Tree is up to date at revision 0.\n', err)
34
self.assertEqual('', out)
36
def test_update_standalone_trivial_with_alias_up(self):
37
self.make_branch_and_tree('.')
38
out, err = self.run_bzr('up')
39
self.assertEqual('Tree is up to date at revision 0.\n', err)
40
self.assertEqual('', out)
42
def test_update_up_to_date_light_checkout(self):
43
self.make_branch_and_tree('branch')
44
self.run_bzr('checkout --lightweight branch checkout')
45
out, err = self.run_bzr('update checkout')
46
self.assertEqual('Tree is up to date at revision 0.\n', err)
32
47
self.assertEqual('', out)
34
49
def test_update_up_to_date_checkout(self):
35
50
self.make_branch_and_tree('branch')
36
self.runbzr('checkout branch checkout')
37
out, err = self.runbzr('update checkout')
38
self.assertEqual('Tree is up to date.\n', err)
51
self.run_bzr('checkout branch checkout')
52
out, err = self.run_bzr('update checkout')
53
self.assertEqual('Tree is up to date at revision 0.\n', err)
39
54
self.assertEqual('', out)
41
56
def test_update_out_of_date_standalone_tree(self):
42
57
# FIXME the default format has to change for this to pass
43
58
# because it currently uses the branch last-revision marker.
44
raise TestSkipped('default format too old')
45
59
self.make_branch_and_tree('branch')
47
self.runbzr('checkout branch checkout')
61
self.run_bzr('checkout --lightweight branch checkout')
48
62
self.build_tree(['checkout/file'])
49
self.runbzr('add checkout/file')
50
self.runbzr('commit -m add-file checkout')
63
self.run_bzr('add checkout/file')
64
self.run_bzr('commit -m add-file checkout')
51
65
# now branch should be out of date
52
out,err = self.runbzr('update branch')
53
self.assertEqual('Updated to revision 1.\n', out)
54
self.assertEqual('', err)
66
out,err = self.run_bzr('update branch')
67
self.assertEqual('', out)
68
self.assertContainsRe(err, '\+N file')
69
self.assertEndsWith(err, 'All changes applied successfully.\n'
70
'Updated to revision 1.\n')
55
71
self.failUnlessExists('branch/file')
57
def test_update_out_of_date_checkout(self):
73
def test_update_out_of_date_light_checkout(self):
58
74
self.make_branch_and_tree('branch')
59
75
# make two checkouts
60
self.runbzr('checkout branch checkout')
61
self.runbzr('checkout branch checkout2')
76
self.run_bzr('checkout --lightweight branch checkout')
77
self.run_bzr('checkout --lightweight branch checkout2')
62
78
self.build_tree(['checkout/file'])
63
self.runbzr('add checkout/file')
64
self.runbzr('commit -m add-file checkout')
79
self.run_bzr('add checkout/file')
80
self.run_bzr('commit -m add-file checkout')
65
81
# now checkout2 should be out of date
66
out,err = self.runbzr('update checkout2')
67
self.assertEqual('All changes applied successfully.\n'
68
'Updated to revision 1.\n',
82
out,err = self.run_bzr('update checkout2')
83
self.assertContainsRe(err, '\+N file')
84
self.assertEndsWith(err, 'All changes applied successfully.\n'
85
'Updated to revision 1.\n')
70
86
self.assertEqual('', out)
72
88
def test_update_conflicts_returns_2(self):
73
89
self.make_branch_and_tree('branch')
74
90
# make two checkouts
75
self.runbzr('checkout branch checkout')
91
self.run_bzr('checkout --lightweight branch checkout')
76
92
self.build_tree(['checkout/file'])
77
self.runbzr('add checkout/file')
78
self.runbzr('commit -m add-file checkout')
79
self.runbzr('checkout branch checkout2')
93
self.run_bzr('add checkout/file')
94
self.run_bzr('commit -m add-file checkout')
95
self.run_bzr('checkout --lightweight branch checkout2')
80
96
# now alter file in checkout
81
97
a_file = file('checkout/file', 'wt')
82
98
a_file.write('Foo')
84
self.runbzr('commit -m checnge-file checkout')
100
self.run_bzr('commit -m checnge-file checkout')
85
101
# now checkout2 should be out of date
86
102
# make a local change to file
87
103
a_file = file('checkout2/file', 'wt')
88
104
a_file.write('Bar')
90
out,err = self.runbzr('update checkout2', retcode=1)
106
out,err = self.run_bzr('update checkout2', retcode=1)
107
self.assertContainsRe(err, 'M file')
91
108
self.assertEqual(['1 conflicts encountered.',
92
109
'Updated to revision 2.'],
110
err.split('\n')[-3:-1])
94
111
self.assertContainsRe(err, 'Text conflict in file\n')
95
112
self.assertEqual('', out)
114
def test_smoke_update_checkout_bound_branch_local_commits(self):
115
# smoke test for doing an update of a checkout of a bound
116
# branch with local commits.
117
master = self.make_branch_and_tree('master')
118
# make a bound branch
119
self.run_bzr('checkout master child')
120
# get an object form of child
121
child = WorkingTree.open('child')
123
self.run_bzr('checkout --lightweight child checkout')
124
# get an object form of the checkout to manipulate
125
wt = WorkingTree.open('checkout')
127
a_file = file('master/file', 'wt')
131
master_tip = master.commit('add file')
133
a_file = file('child/file_b', 'wt')
136
child.add(['file_b'])
137
child_tip = child.commit('add file_b', local=True)
139
a_file = file('checkout/file_c', 'wt')
144
# now, update checkout ->
145
# get all three files and a pending merge.
146
out, err = self.run_bzr('update checkout')
147
self.assertEqual('', out)
148
self.assertContainsRe(err, '\+N file')
149
self.assertContainsRe(err, '\+N file_b')
150
self.assertContainsRe(err, 'Updated to revision 1.\n'
151
'Your local commits will now show as'
153
self.assertEqual([master_tip, child_tip], wt.get_parent_ids())
154
self.failUnlessExists('checkout/file')
155
self.failUnlessExists('checkout/file_b')
156
self.failUnlessExists('checkout/file_c')
157
self.assertTrue(wt.has_filename('file_c'))
159
def test_update_with_merges(self):
160
# Test that 'bzr update' works correctly when you have
161
# an update in the master tree, and a lightweight checkout
162
# which has merged another branch
163
master = self.make_branch_and_tree('master')
164
self.build_tree(['master/file'])
166
master.commit('one', rev_id='m1')
168
self.build_tree(['checkout1/'])
169
checkout_dir = bzrdir.BzrDirMetaFormat1().initialize('checkout1')
170
branch.BranchReferenceFormat().initialize(checkout_dir, master.branch)
171
checkout1 = checkout_dir.create_workingtree('m1')
173
# Create a second branch, with an extra commit
174
other = master.bzrdir.sprout('other').open_workingtree()
175
self.build_tree(['other/file2'])
177
other.commit('other2', rev_id='o2')
179
# Create a new commit in the master branch
180
self.build_tree(['master/file3'])
181
master.add(['file3'])
182
master.commit('f3', rev_id='m2')
184
# Merge the other branch into checkout
185
os.chdir('checkout1')
186
self.run_bzr('merge ../other')
188
self.assertEqual(['o2'], checkout1.get_parent_ids()[1:])
190
# At this point, 'commit' should fail, because we are out of date
191
self.run_bzr_error(["please run 'bzr update'"],
194
# This should not report about local commits being pending
195
# merges, because they were real merges
196
out, err = self.run_bzr('update')
197
self.assertEqual('', out)
198
self.assertEndsWith(err, 'All changes applied successfully.\n'
199
'Updated to revision 2.\n')
200
self.assertContainsRe(err, r'\+N file3')
201
# The pending merges should still be there
202
self.assertEqual(['o2'], checkout1.get_parent_ids()[1:])
204
def test_readonly_lightweight_update(self):
205
"""Update a light checkout of a readonly branch"""
206
tree = self.make_branch_and_tree('branch')
207
readonly_branch = branch.Branch.open(self.get_readonly_url('branch'))
208
checkout = readonly_branch.create_checkout('checkout',
210
tree.commit('empty commit')
211
self.run_bzr('update checkout')