1
1
# Copyright (C) 2005, 2006 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35
37
class TestDiff(ExternalBase):
37
def make_example_branch(test):
39
def make_example_branch(self):
38
40
# FIXME: copied from test_too_much -- share elsewhere?
40
file('hello', 'wt').write('foo\n')
41
test.runbzr('add hello')
42
test.runbzr('commit -m setup hello')
43
file('goodbye', 'wt').write('baz\n')
44
test.runbzr('add goodbye')
45
test.runbzr('commit -m setup goodbye')
41
tree = self.make_branch_and_tree('.')
42
open('hello', 'wb').write('foo\n')
45
open('goodbye', 'wb').write('baz\n')
47
49
def test_diff(self):
48
50
self.make_example_branch()
60
62
def test_diff_prefix(self):
61
63
"""diff --prefix appends to filenames in output"""
62
64
self.make_example_branch()
63
file('hello', 'wt').write('hello world!\n')
65
file('hello', 'wb').write('hello world!\n')
64
66
out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
65
67
self.assertEquals(err, '')
66
68
self.assertEqualDiff(subst_dates(out), '''\
76
78
def test_diff_p1(self):
77
79
"""diff -p1 produces lkml-style diffs"""
78
80
self.make_example_branch()
79
file('hello', 'wt').write('hello world!\n')
81
file('hello', 'wb').write('hello world!\n')
80
82
out, err = self.runbzr('diff -p1', retcode=1)
81
83
self.assertEquals(err, '')
82
84
self.assertEqualDiff(subst_dates(out), '''\
92
94
def test_diff_p0(self):
93
95
"""diff -p0 produces diffs with no prefix"""
94
96
self.make_example_branch()
95
file('hello', 'wt').write('hello world!\n')
97
file('hello', 'wb').write('hello world!\n')
96
98
out, err = self.runbzr('diff -p0', retcode=1)
97
99
self.assertEquals(err, '')
98
100
self.assertEqualDiff(subst_dates(out), '''\
146
148
"+contents of branch1/file\n"
147
149
"\n", subst_dates(out))
148
out, ett = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
150
out, err = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
152
self.assertEquals('', err)
153
self.assertEqualDiff("=== modified file 'file'\n"
154
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
155
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
158
"+contents of branch1/file\n"
159
"\n", subst_dates(out))
161
def test_diff_revno_branches(self):
162
self.example_branches()
163
print >> open('branch2/file', 'wb'), 'even newer content'
164
self.run_bzr_captured(['commit', '-m',
165
'update file once more', 'branch2'])
167
out, err = self.run_bzr_captured(['diff', '-r',
168
'revno:1:branch2..revno:1:branch1'],
170
self.assertEquals('', err)
171
self.assertEquals('', out)
172
out, err = self.run_bzr_captured(['diff', '-r',
173
'revno:2:branch2..revno:1:branch1'],
150
175
self.assertEquals('', err)
151
176
self.assertEqualDiff("=== modified file 'file'\n"
170
195
self.example_branch2()
172
197
print >> open('branch1/file1', 'wb'), 'new line'
173
output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'], retcode=1)
198
output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'],
174
200
self.assertTrue('\n-original line\n+new line\n' in output[0])
202
def test_diff_across_rename(self):
203
"""The working tree path should always be considered for diffing"""
204
self.make_example_branch()
205
self.run_bzr('diff', '-r', '0..1', 'hello', retcode=1)
206
wt = workingtree.WorkingTree.open_containing('.')[0]
207
wt.rename_one('hello', 'hello1')
208
self.run_bzr('diff', 'hello1', retcode=1)
209
self.run_bzr('diff', '-r', '0..1', 'hello1', retcode=1)
177
212
class TestCheckoutDiff(TestDiff):
221
256
self.runbzr('rename hello gruezi')
222
257
diff = self.run_bzr_captured(['diff'], retcode=1)
223
258
self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])
261
class TestExternalDiff(TestDiff):
263
def test_external_diff(self):
264
"""Test that we can spawn an external diff process"""
265
# We have to use run_bzr_subprocess, because we need to
266
# test writing directly to stdout, (there was a bug in
267
# subprocess.py that we had to workaround).
268
# However, if 'diff' may not be available
269
self.make_example_branch()
270
orig_progress = os.environ.get('BZR_PROGRESS_BAR')
272
os.environ['BZR_PROGRESS_BAR'] = 'none'
273
out, err = self.run_bzr_subprocess('diff', '-r', '1',
274
'--diff-options', '-ub',
277
if orig_progress is None:
278
del os.environ['BZR_PROGRESS_BAR']
280
os.environ['BZR_PROGRESS_BAR'] = orig_progress
282
if 'Diff is not installed on this machine' in err:
283
raise TestSkipped("No external 'diff' is available")
284
self.assertEqual('', err)
285
# We have to skip the stuff in the middle, because it depends
287
self.assertStartsWith(out, "=== added file 'goodbye'\n"
288
"--- goodbye\t1970-01-01 00:00:00 +0000\n"
290
self.assertEndsWith(out, "\n@@ -0,0 +1 @@\n"