1
# Copyright (C) 2005, 2006 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
"""Black-box tests for bzr diff.
25
from bzrlib import workingtree
26
from bzrlib.branch import Branch
27
from bzrlib.tests import TestSkipped
28
from bzrlib.tests.blackbox import ExternalBase
31
def subst_dates(string):
32
"""Replace date strings with constant values."""
33
return re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
34
'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
37
class DiffBase(ExternalBase):
38
"""Base class with common setup method"""
40
def make_example_branch(self):
41
# FIXME: copied from test_too_much -- share elsewhere?
42
tree = self.make_branch_and_tree('.')
43
open('hello', 'wb').write('foo\n')
46
open('goodbye', 'wb').write('baz\n')
51
class TestDiff(DiffBase):
54
self.make_example_branch()
55
file('hello', 'wt').write('hello world!')
56
self.runbzr('commit -m fixing hello')
57
output = self.runbzr('diff -r 2..3', backtick=1, retcode=1)
58
self.assert_('\n+hello world!' in output)
59
output = self.runbzr('diff -r last:3..last:1', backtick=1, retcode=1)
60
self.assert_('\n+baz' in output)
61
file('moo', 'wb').write('moo')
62
self.runbzr('add moo')
66
def test_diff_prefix(self):
67
"""diff --prefix appends to filenames in output"""
68
self.make_example_branch()
69
file('hello', 'wb').write('hello world!\n')
70
out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
71
self.assertEquals(err, '')
72
self.assertEqualDiff(subst_dates(out), '''\
73
=== modified file 'hello'
74
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
75
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
82
def test_diff_p1(self):
83
"""diff -p1 produces lkml-style diffs"""
84
self.make_example_branch()
85
file('hello', 'wb').write('hello world!\n')
86
out, err = self.runbzr('diff -p1', retcode=1)
87
self.assertEquals(err, '')
88
self.assertEqualDiff(subst_dates(out), '''\
89
=== modified file 'hello'
90
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
91
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
98
def test_diff_p0(self):
99
"""diff -p0 produces diffs with no prefix"""
100
self.make_example_branch()
101
file('hello', 'wb').write('hello world!\n')
102
out, err = self.runbzr('diff -p0', retcode=1)
103
self.assertEquals(err, '')
104
self.assertEqualDiff(subst_dates(out), '''\
105
=== modified file 'hello'
106
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
107
+++ hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
114
def test_diff_nonexistent(self):
115
# Get an error from a file that does not exist at all
117
self.make_example_branch()
118
out, err = self.runbzr('diff does-not-exist', retcode=3)
119
self.assertContainsRe(err, 'not versioned.*does-not-exist')
121
def test_diff_unversioned(self):
122
# Get an error when diffing a non-versioned file.
124
self.make_example_branch()
125
self.build_tree(['unversioned-file'])
126
out, err = self.runbzr('diff unversioned-file', retcode=3)
127
self.assertContainsRe(err, 'not versioned.*unversioned-file')
129
# TODO: What should diff say for a file deleted in working tree?
131
def example_branches(self):
132
self.build_tree(['branch1/', 'branch1/file'], line_endings='binary')
133
self.capture('init branch1')
134
self.capture('add branch1/file')
135
self.run_bzr_captured(['commit', '-m', 'add file', 'branch1'])
136
self.capture('branch branch1 branch2')
137
print >> open('branch2/file', 'wb'), 'new content'
138
self.run_bzr_captured(['commit', '-m', 'update file', 'branch2'])
140
def test_diff_branches(self):
141
self.example_branches()
142
# should open branch1 and diff against branch2,
143
out, err = self.run_bzr_captured(['diff', '-r', 'branch:branch2',
146
self.assertEquals('', err)
147
self.assertEquals("=== modified file 'file'\n"
148
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
149
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
152
"+contents of branch1/file\n"
153
"\n", subst_dates(out))
154
out, err = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
156
self.assertEquals('', err)
157
self.assertEqualDiff("=== modified file 'file'\n"
158
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
159
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
162
"+contents of branch1/file\n"
163
"\n", subst_dates(out))
165
def test_diff_revno_branches(self):
166
self.example_branches()
167
print >> open('branch2/file', 'wb'), 'even newer content'
168
self.run_bzr_captured(['commit', '-m',
169
'update file once more', 'branch2'])
171
out, err = self.run_bzr_captured(['diff', '-r',
172
'revno:1:branch2..revno:1:branch1'],
174
self.assertEquals('', err)
175
self.assertEquals('', out)
176
out, err = self.run_bzr_captured(['diff', '-r',
177
'revno:2:branch2..revno:1:branch1'],
179
self.assertEquals('', err)
180
self.assertEqualDiff("=== modified file 'file'\n"
181
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
182
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
185
"+contents of branch1/file\n"
186
"\n", subst_dates(out))
188
def example_branch2(self):
189
self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
190
self.capture('init branch1')
191
self.capture('add branch1/file1')
192
print >> open('branch1/file1', 'wb'), 'original line'
193
self.run_bzr_captured(['commit', '-m', 'first commit', 'branch1'])
195
print >> open('branch1/file1', 'wb'), 'repo line'
196
self.run_bzr_captured(['commit', '-m', 'second commit', 'branch1'])
198
def test_diff_to_working_tree(self):
199
self.example_branch2()
201
print >> open('branch1/file1', 'wb'), 'new line'
202
output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'],
204
self.assertTrue('\n-original line\n+new line\n' in output[0])
206
def test_diff_across_rename(self):
207
"""The working tree path should always be considered for diffing"""
208
self.make_example_branch()
209
self.run_bzr('diff', '-r', '0..1', 'hello', retcode=1)
210
wt = workingtree.WorkingTree.open_containing('.')[0]
211
wt.rename_one('hello', 'hello1')
212
self.run_bzr('diff', 'hello1', retcode=1)
213
self.run_bzr('diff', '-r', '0..1', 'hello1', retcode=1)
216
class TestCheckoutDiff(TestDiff):
218
def make_example_branch(self):
219
super(TestCheckoutDiff, self).make_example_branch()
220
self.runbzr('checkout . checkout')
223
def example_branch2(self):
224
super(TestCheckoutDiff, self).example_branch2()
225
os.mkdir('checkouts')
226
self.runbzr('checkout branch1 checkouts/branch1')
227
os.chdir('checkouts')
229
def example_branches(self):
230
super(TestCheckoutDiff, self).example_branches()
231
os.mkdir('checkouts')
232
self.runbzr('checkout branch1 checkouts/branch1')
233
self.runbzr('checkout branch2 checkouts/branch2')
234
os.chdir('checkouts')
237
class TestDiffLabels(DiffBase):
239
def test_diff_label_removed(self):
240
super(TestDiffLabels, self).make_example_branch()
241
self.runbzr('remove hello')
242
diff = self.run_bzr_captured(['diff'], retcode=1)
243
self.assertTrue("=== removed file 'hello'" in diff[0])
245
def test_diff_label_added(self):
246
super(TestDiffLabels, self).make_example_branch()
247
file('barbar', 'wt').write('barbar')
248
self.runbzr('add barbar')
249
diff = self.run_bzr_captured(['diff'], retcode=1)
250
self.assertTrue("=== added file 'barbar'" in diff[0])
252
def test_diff_label_modified(self):
253
super(TestDiffLabels, self).make_example_branch()
254
file('hello', 'wt').write('barbar')
255
diff = self.run_bzr_captured(['diff'], retcode=1)
256
self.assertTrue("=== modified file 'hello'" in diff[0])
258
def test_diff_label_renamed(self):
259
super(TestDiffLabels, self).make_example_branch()
260
self.runbzr('rename hello gruezi')
261
diff = self.run_bzr_captured(['diff'], retcode=1)
262
self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])
265
class TestExternalDiff(DiffBase):
267
def test_external_diff(self):
268
"""Test that we can spawn an external diff process"""
269
# We have to use run_bzr_subprocess, because we need to
270
# test writing directly to stdout, (there was a bug in
271
# subprocess.py that we had to workaround).
272
# However, if 'diff' may not be available
273
self.make_example_branch()
274
orig_progress = os.environ.get('BZR_PROGRESS_BAR')
276
os.environ['BZR_PROGRESS_BAR'] = 'none'
277
out, err = self.run_bzr_subprocess('diff', '-r', '1',
278
'--diff-options', '-ub',
279
universal_newlines=True,
282
if orig_progress is None:
283
del os.environ['BZR_PROGRESS_BAR']
285
os.environ['BZR_PROGRESS_BAR'] = orig_progress
287
if 'Diff is not installed on this machine' in err:
288
raise TestSkipped("No external 'diff' is available")
289
self.assertEqual('', err)
290
# We have to skip the stuff in the middle, because it depends
292
self.assertStartsWith(out, "=== added file 'goodbye'\n"
293
"--- goodbye\t1970-01-01 00:00:00 +0000\n"
295
self.assertEndsWith(out, "\n@@ -0,0 +1 @@\n"
299
class TestDiffOutput(DiffBase):
301
def test_diff_output(self):
302
# check that output doesn't mangle line-endings
303
self.make_example_branch()
304
file('hello', 'wb').write('hello world!\n')
305
output = self.run_bzr_subprocess('diff', retcode=1)[0]
306
self.assert_('\n+hello world!\n' in output)