1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
1
# Copyright (C) 2005, 2006 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
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
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Black-box tests for bzr diff."""
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Black-box tests for bzr diff.
27
from bzrlib.diff import (
29
format_registry as diff_format_registry,
31
from bzrlib.tests import (
25
from bzrlib import workingtree
26
from bzrlib.branch import Branch
27
from bzrlib.tests import TestSkipped
28
from bzrlib.tests.blackbox import ExternalBase
36
31
def subst_dates(string):
39
34
'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
42
class DiffBase(tests.TestCaseWithTransport):
37
class DiffBase(ExternalBase):
43
38
"""Base class with common setup method"""
45
40
def make_example_branch(self):
61
56
self.build_tree_contents([('hello', 'hello world!')])
62
57
tree.commit(message='fixing hello')
63
58
output = self.run_bzr('diff -r 2..3', retcode=1)[0]
64
self.assertTrue('\n+hello world!' in output)
59
self.assert_('\n+hello world!' in output)
65
60
output = self.run_bzr('diff -c 3', retcode=1)[0]
66
self.assertTrue('\n+hello world!' in output)
61
self.assert_('\n+hello world!' in output)
67
62
output = self.run_bzr('diff -r last:3..last:1', retcode=1)[0]
68
self.assertTrue('\n+baz' in output)
63
self.assert_('\n+baz' in output)
69
64
output = self.run_bzr('diff -c last:2', retcode=1)[0]
70
self.assertTrue('\n+baz' in output)
65
self.assert_('\n+baz' in output)
71
66
self.build_tree(['moo'])
78
73
self.make_example_branch()
79
74
self.build_tree_contents([('hello', 'hello world!\n')])
80
75
out, err = self.run_bzr('diff --prefix old/:new/', retcode=1)
81
self.assertEqual(err, '')
76
self.assertEquals(err, '')
82
77
self.assertEqualDiff(subst_dates(out), '''\
83
78
=== modified file 'hello'
84
79
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
100
95
self.make_example_branch()
101
96
self.build_tree_contents([('hello', 'hello world!\n')])
102
97
out, err = self.run_bzr('diff -p1', retcode=1)
103
self.assertEqual(err, '')
98
self.assertEquals(err, '')
104
99
self.assertEqualDiff(subst_dates(out), '''\
105
100
=== modified file 'hello'
106
101
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
116
111
self.make_example_branch()
117
112
self.build_tree_contents([('hello', 'hello world!\n')])
118
113
out, err = self.run_bzr('diff -p0', retcode=1)
119
self.assertEqual(err, '')
114
self.assertEquals(err, '')
120
115
self.assertEqualDiff(subst_dates(out), '''\
121
116
=== modified file 'hello'
122
117
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
131
126
# Get an error from a file that does not exist at all
133
128
self.make_example_branch()
134
out, err = self.run_bzr('diff does-not-exist', retcode=3,
135
error_regexes=('not versioned.*does-not-exist',))
129
out, err = self.run_bzr('diff does-not-exist', retcode=3)
130
self.assertContainsRe(err, 'not versioned.*does-not-exist')
137
132
def test_diff_illegal_revision_specifiers(self):
138
out, err = self.run_bzr('diff -r 1..23..123', retcode=3,
139
error_regexes=('one or two revision specifiers',))
141
def test_diff_using_and_format(self):
142
out, err = self.run_bzr('diff --format=default --using=mydi', retcode=3,
143
error_regexes=('are mutually exclusive',))
145
def test_diff_nonexistent_revision(self):
146
out, err = self.run_bzr('diff -r 123', retcode=3,
147
error_regexes=("Requested revision: '123' does not "
148
"exist in branch:",))
150
def test_diff_nonexistent_dotted_revision(self):
151
out, err = self.run_bzr('diff -r 1.1', retcode=3)
152
self.assertContainsRe(err,
153
"Requested revision: '1.1' does not exist in branch:")
155
def test_diff_nonexistent_dotted_revision_change(self):
156
out, err = self.run_bzr('diff -c 1.1', retcode=3)
157
self.assertContainsRe(err,
158
"Requested revision: '1.1' does not exist in branch:")
133
out, err = self.run_bzr('diff -r 1..23..123', retcode=3)
134
self.assertContainsRe(err, 'one or two revision specifiers')
160
136
def test_diff_unversioned(self):
161
137
# Get an error when diffing a non-versioned file.
182
158
def check_b2_vs_b1(self, cmd):
183
159
# Compare branch2 vs branch1 using cmd and check the result
184
160
out, err = self.run_bzr(cmd, retcode=1)
185
self.assertEqual('', err)
186
self.assertEqual("=== modified file 'file'\n"
161
self.assertEquals('', err)
162
self.assertEquals("=== modified file 'file'\n"
187
163
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
188
164
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
189
165
"@@ -1,1 +1,1 @@\n"
194
170
def check_b1_vs_b2(self, cmd):
195
171
# Compare branch1 vs branch2 using cmd and check the result
196
172
out, err = self.run_bzr(cmd, retcode=1)
197
self.assertEqual('', err)
173
self.assertEquals('', err)
198
174
self.assertEqualDiff("=== modified file 'file'\n"
199
175
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
200
176
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
206
182
def check_no_diffs(self, cmd):
207
183
# Check that running cmd returns an empty diff
208
184
out, err = self.run_bzr(cmd, retcode=0)
209
self.assertEqual('', err)
210
self.assertEqual('', out)
185
self.assertEquals('', err)
186
self.assertEquals('', out)
212
188
def test_diff_branches(self):
213
189
self.example_branches()
214
# should open branch1 and diff against branch2,
190
# should open branch1 and diff against branch2,
215
191
self.check_b2_vs_b1('diff -r branch:branch2 branch1')
216
192
# Compare two working trees using various syntax forms
217
193
self.check_b2_vs_b1('diff --old branch2 --new branch1')
256
232
out, err = self.run_bzr('diff -r revno:1:branch2..revno:1:branch1',
258
self.assertEqual('', err)
259
self.assertEqual('', out)
234
self.assertEquals('', err)
235
self.assertEquals('', out)
260
236
out, err = self.run_bzr('diff -r revno:2:branch2..revno:1:branch1',
262
self.assertEqual('', err)
238
self.assertEquals('', err)
263
239
self.assertEqualDiff("=== modified file 'file'\n"
264
240
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
265
241
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
287
263
self.example_branch2()
288
264
self.build_tree_contents([('branch1/file1', 'new line')])
289
265
os.mkdir('branch1/dir1')
290
output = self.run_bzr('diff -r 1..', retcode=1,
291
working_dir='branch1/dir1')
266
os.chdir('branch1/dir1')
267
output = self.run_bzr('diff -r 1..', retcode=1)
292
268
self.assertContainsRe(output[0], '\n\\-original line\n\\+new line\n')
294
270
def test_diff_across_rename(self):
307
283
output = self.run_bzr('diff -r 1.. branch1', retcode=1)
308
284
self.assertContainsRe(output[0], '\n\\-original line\n\\+repo line\n')
310
def test_custom_format(self):
311
class BooDiffTree(DiffTree):
313
def show_diff(self, specific_files, extra_trees=None):
314
self.to_file.write("BOO!\n")
315
return super(BooDiffTree, self).show_diff(specific_files,
318
diff_format_registry.register("boo", BooDiffTree, "Scary diff format")
319
self.addCleanup(diff_format_registry.remove, "boo")
320
self.make_example_branch()
321
self.build_tree_contents([('hello', 'hello world!\n')])
322
output = self.run_bzr('diff --format=boo', retcode=1)
323
self.assertTrue("BOO!" in output[0])
324
output = self.run_bzr('diff -Fboo', retcode=1)
325
self.assertTrue("BOO!" in output[0])
328
287
class TestCheckoutDiff(TestDiff):
343
302
def example_branches(self):
344
branch1_tree, branch2_tree = super(TestCheckoutDiff,
345
self).example_branches()
303
branch1_tree, branch2_tree = super(TestCheckoutDiff, self).example_branches()
346
304
os.mkdir('checkouts')
347
305
branch1_tree = branch1_tree.branch.create_checkout('checkouts/branch1')
348
306
branch2_tree = branch2_tree.branch.create_checkout('checkouts/branch2')
383
341
def test_external_diff(self):
384
342
"""Test that we can spawn an external diff process"""
385
self.disable_missing_extensions_warning()
386
343
# We have to use run_bzr_subprocess, because we need to
387
344
# test writing directly to stdout, (there was a bug in
388
345
# subprocess.py that we had to workaround).
389
346
# However, if 'diff' may not be available
390
347
self.make_example_branch()
391
out, err = self.run_bzr_subprocess(
392
'diff -Oprogress_bar=none -r 1 --diff-options -ub',
393
universal_newlines=True,
348
orig_progress = os.environ.get('BZR_PROGRESS_BAR')
350
os.environ['BZR_PROGRESS_BAR'] = 'none'
351
out, err = self.run_bzr_subprocess('diff -r 1 --diff-options -ub',
352
universal_newlines=True,
355
if orig_progress is None:
356
del os.environ['BZR_PROGRESS_BAR']
358
os.environ['BZR_PROGRESS_BAR'] = orig_progress
395
360
if 'Diff is not installed on this machine' in err:
396
raise tests.TestSkipped("No external 'diff' is available")
361
raise TestSkipped("No external 'diff' is available")
397
362
self.assertEqual('', err)
398
363
# We have to skip the stuff in the middle, because it depends
403
368
self.assertEndsWith(out, "\n@@ -0,0 +1 @@\n"
406
def test_external_diff_options_and_using(self):
407
"""Test that the options are passed correctly to an external diff process"""
408
self.requireFeature(features.diff_feature)
409
self.make_example_branch()
410
self.build_tree_contents([('hello', 'Foo\n')])
411
out, err = self.run_bzr('diff --diff-options -i --using diff',
413
self.assertEqual("=== modified file 'hello'\n", out)
414
self.assertEqual('', err)
417
372
class TestDiffOutput(DiffBase):
421
376
self.make_example_branch()
422
377
self.build_tree_contents([('hello', 'hello world!\n')])
423
378
output = self.run_bzr_subprocess('diff', retcode=1)[0]
424
self.assertTrue('\n+hello world!\n' in output)
379
self.assert_('\n+hello world!\n' in output)