1
# Copyright (C) 2005, 2006 by 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Tests for the commit CLI of bzr."""
24
from bzrlib.branch import Branch
25
from bzrlib.bzrdir import BzrDir
26
from bzrlib.errors import BzrCommandError
27
from bzrlib.tests.blackbox import ExternalBase
28
from bzrlib.workingtree import WorkingTree
31
class TestCommit(ExternalBase):
33
def test_05_empty_commit(self):
34
"""Commit of tree with no versioned files should fail"""
35
# If forced, it should succeed, but this is not tested here.
37
self.build_tree(['hello.txt'])
38
self.runbzr("commit -m empty", retcode=3)
40
def test_10_verbose_commit(self):
41
"""Add one file and examine verbose commit output"""
43
self.build_tree(['hello.txt'])
44
self.runbzr("add hello.txt")
45
out,err = self.run_bzr("commit", "-m", "added")
46
self.assertEqual('', out)
47
self.assertEqual('added hello.txt\n'
48
'Committed revision 1.\n',
51
def prepare_simple_history(self):
52
"""Prepare and return a working tree with one commit of one file"""
53
# Commit with modified file should say so
54
wt = BzrDir.create_standalone_workingtree('.')
55
self.build_tree(['hello.txt', 'extra.txt'])
57
wt.commit(message='added')
60
def test_verbose_commit_modified(self):
61
# Verbose commit of modified file should say so
62
wt = self.prepare_simple_history()
63
self.build_tree_contents([('hello.txt', 'new contents')])
64
out, err = self.run_bzr("commit", "-m", "modified")
65
self.assertEqual('', out)
66
self.assertEqual('modified hello.txt\n'
67
'Committed revision 2.\n',
70
def test_verbose_commit_renamed(self):
71
# Verbose commit of renamed file should say so
72
wt = self.prepare_simple_history()
73
wt.rename_one('hello.txt', 'gutentag.txt')
74
out, err = self.run_bzr("commit", "-m", "renamed")
75
self.assertEqual('', out)
76
self.assertEqual('renamed hello.txt => gutentag.txt\n'
77
'Committed revision 2.\n',
80
def test_verbose_commit_moved(self):
81
# Verbose commit of file moved to new directory should say so
82
wt = self.prepare_simple_history()
85
wt.rename_one('hello.txt', 'subdir/hello.txt')
86
out, err = self.run_bzr("commit", "-m", "renamed")
87
self.assertEqual('', out)
88
self.assertEqualDiff('added subdir\n'
89
'renamed hello.txt => subdir/hello.txt\n'
90
'Committed revision 2.\n',
93
def test_verbose_commit_with_unknown(self):
94
"""Unknown files should not be listed by default in verbose output"""
95
# Is that really the best policy?
96
wt = BzrDir.create_standalone_workingtree('.')
97
self.build_tree(['hello.txt', 'extra.txt'])
99
out,err = self.run_bzr("commit", "-m", "added")
100
self.assertEqual('', out)
101
self.assertEqual('added hello.txt\n'
102
'Committed revision 1.\n',
105
def test_verbose_commit_with_unchanged(self):
106
"""Unchanged files should not be listed by default in verbose output"""
108
self.build_tree(['hello.txt', 'unchanged.txt'])
109
self.runbzr('add unchanged.txt')
110
self.runbzr('commit -m unchanged unchanged.txt')
111
self.runbzr("add hello.txt")
112
out,err = self.run_bzr("commit", "-m", "added")
113
self.assertEqual('', out)
114
self.assertEqual('added hello.txt\n'
115
'Committed revision 2.\n',
118
def test_commit_merge_reports_all_modified_files(self):
119
# the commit command should show all the files that are shown by
120
# bzr diff or bzr status when committing, even when they were not
121
# changed by the user but rather through doing a merge.
122
this_tree = self.make_branch_and_tree('this')
123
# we need a bunch of files and dirs, to perform one action on each.
126
'this/dirtoreparent/',
129
'this/filetoreparent',
146
this_tree.commit('create_files')
147
other_dir = this_tree.bzrdir.sprout('other')
148
other_tree = other_dir.open_workingtree()
149
other_tree.lock_write()
150
# perform the needed actions on the files and dirs.
152
other_tree.rename_one('dirtorename', 'renameddir')
153
other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
154
other_tree.rename_one('filetorename', 'renamedfile')
155
other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
156
other_tree.remove(['dirtoremove', 'filetoremove'])
157
self.build_tree_contents([
159
('other/filetomodify', 'new content'),
160
('other/newfile', 'new file content')])
161
other_tree.add('newfile')
162
other_tree.add('newdir/')
163
other_tree.commit('modify all sample files and dirs.')
166
self.merge(other_tree.branch, this_tree)
168
out,err = self.run_bzr("commit", "-m", "added")
170
self.assertEqual('', out)
171
self.assertEqualDiff(
172
'modified filetomodify\n'
175
'renamed dirtorename => renameddir\n'
176
'renamed dirtoreparent => renameddir/reparenteddir\n'
177
'renamed filetoreparent => renameddir/reparentedfile\n'
178
'renamed filetorename => renamedfile\n'
179
'deleted dirtoremove\n'
180
'deleted filetoremove\n'
181
'Committed revision 2.\n',
184
def test_empty_commit_message(self):
186
file('foo.c', 'wt').write('int main() {}')
187
self.runbzr(['add', 'foo.c'])
188
self.runbzr(["commit", "-m", ""] , retcode=3)
190
def test_other_branch_commit(self):
191
# this branch is to ensure consistent behaviour, whether we're run
192
# inside a branch, or not.
193
os.mkdir('empty_branch')
194
os.chdir('empty_branch')
199
file('foo.c', 'wt').write('int main() {}')
200
file('bar.c', 'wt').write('int main() {}')
202
self.runbzr(['add', 'branch/foo.c'])
203
self.runbzr(['add', 'branch'])
204
# can't commit files in different trees; sane error
205
self.runbzr('commit -m newstuff branch/foo.c .', retcode=3)
206
self.runbzr('commit -m newstuff branch/foo.c')
207
self.runbzr('commit -m newstuff branch')
208
self.runbzr('commit -m newstuff branch', retcode=3)
210
def test_out_of_date_tree_commit(self):
211
# check we get an error code and a clear message committing with an out
213
self.make_branch_and_tree('branch')
215
self.runbzr('checkout --lightweight branch checkout')
216
# commit to the original branch to make the checkout out of date
217
self.runbzr('commit --unchanged -m message branch')
218
# now commit to the checkout should emit
219
# ERROR: Out of date with the branch, 'bzr update' is suggested
220
output = self.runbzr('commit --unchanged -m checkout_message '
221
'checkout', retcode=3)
222
self.assertEqual(output,
224
"bzr: ERROR: Working tree is out of date, please run "
227
def test_local_commit_unbound(self):
228
# a --local commit on an unbound branch is an error
229
self.make_branch_and_tree('.')
230
out, err = self.run_bzr('commit', '--local', retcode=3)
231
self.assertEqualDiff('', out)
232
self.assertEqualDiff('bzr: ERROR: Cannot perform local-only commits '
233
'on unbound branches.\n', err)
235
def test_commit_a_text_merge_in_a_checkout(self):
236
# checkouts perform multiple actions in a transaction across bond
237
# branches and their master, and have been observed to fail in the
238
# past. This is a user story reported to fail in bug #43959 where
239
# a merge done in a checkout (using the update command) failed to
241
self.run_bzr('init', 'trunk')
243
self.run_bzr('checkout', 'trunk', 'u1')
244
self.build_tree_contents([('u1/hosts', 'initial contents')])
245
self.run_bzr('add', 'u1/hosts')
246
self.run_bzr('commit', '-m', 'add hosts', 'u1')
248
self.run_bzr('checkout', 'trunk', 'u2')
249
self.build_tree_contents([('u2/hosts', 'altered in u2')])
250
self.run_bzr('commit', '-m', 'checkin from u2', 'u2')
252
# make an offline commits
253
self.build_tree_contents([('u1/hosts', 'first offline change in u1')])
254
self.run_bzr('commit', '-m', 'checkin offline', '--local', 'u1')
256
# now try to pull in online work from u2, and then commit our offline
258
# retcode 1 as we expect a text conflict
259
self.run_bzr('update', 'u1', retcode=1)
260
self.run_bzr('resolved', 'u1/hosts')
261
# add a text change here to represent resolving the merge conflicts in
262
# favour of a new version of the file not identical to either the u1
263
# version or the u2 version.
264
self.build_tree_contents([('u1/hosts', 'merge resolution\n')])
265
self.run_bzr('commit', '-m', 'checkin merge of the offline work from u1', 'u1')