59
59
def test_diff(self):
60
60
tree = self.make_example_branch()
61
self.build_tree_contents([('hello', 'hello world!')])
61
self.build_tree_contents([('hello', b'hello world!')])
62
62
tree.commit(message='fixing hello')
63
63
output = self.run_bzr('diff -r 2..3', retcode=1)[0]
64
self.assert_('\n+hello world!' in output)
64
self.assertTrue('\n+hello world!' in output)
65
65
output = self.run_bzr('diff -c 3', retcode=1)[0]
66
self.assert_('\n+hello world!' in output)
66
self.assertTrue('\n+hello world!' in output)
67
67
output = self.run_bzr('diff -r last:3..last:1', retcode=1)[0]
68
self.assert_('\n+baz' in output)
68
self.assertTrue('\n+baz' in output)
69
69
output = self.run_bzr('diff -c last:2', retcode=1)[0]
70
self.assert_('\n+baz' in output)
70
self.assertTrue('\n+baz' in output)
71
71
self.build_tree(['moo'])
76
76
def test_diff_prefix(self):
77
77
"""diff --prefix appends to filenames in output"""
78
78
self.make_example_branch()
79
self.build_tree_contents([('hello', 'hello world!\n')])
79
self.build_tree_contents([('hello', b'hello world!\n')])
80
80
out, err = self.run_bzr('diff --prefix old/:new/', retcode=1)
81
self.assertEquals(err, '')
81
self.assertEqual(err, '')
82
82
self.assertEqualDiff(subst_dates(out), '''\
83
83
=== modified file 'hello'
84
84
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
93
93
# There was an error in error reporting for this option
94
94
out, err = self.run_bzr('diff --prefix old/', retcode=3)
95
95
self.assertContainsRe(err,
96
'--prefix expects two values separated by a colon')
96
'--prefix expects two values separated by a colon')
98
98
def test_diff_p1(self):
99
99
"""diff -p1 produces lkml-style diffs"""
100
100
self.make_example_branch()
101
self.build_tree_contents([('hello', 'hello world!\n')])
101
self.build_tree_contents([('hello', b'hello world!\n')])
102
102
out, err = self.run_bzr('diff -p1', retcode=1)
103
self.assertEquals(err, '')
103
self.assertEqual(err, '')
104
104
self.assertEqualDiff(subst_dates(out), '''\
105
105
=== modified file 'hello'
106
106
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
114
114
def test_diff_p0(self):
115
115
"""diff -p0 produces diffs with no prefix"""
116
116
self.make_example_branch()
117
self.build_tree_contents([('hello', 'hello world!\n')])
117
self.build_tree_contents([('hello', b'hello world!\n')])
118
118
out, err = self.run_bzr('diff -p0', retcode=1)
119
self.assertEquals(err, '')
119
self.assertEqual(err, '')
120
120
self.assertEqualDiff(subst_dates(out), '''\
121
121
=== modified file 'hello'
122
122
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
133
133
self.make_example_branch()
134
134
out, err = self.run_bzr('diff does-not-exist', retcode=3,
135
error_regexes=('not versioned.*does-not-exist',))
135
error_regexes=('not versioned.*does-not-exist',))
137
137
def test_diff_illegal_revision_specifiers(self):
138
138
out, err = self.run_bzr('diff -r 1..23..123', retcode=3,
139
error_regexes=('one or two revision specifiers',))
139
error_regexes=('one or two revision specifiers',))
141
141
def test_diff_using_and_format(self):
142
142
out, err = self.run_bzr('diff --format=default --using=mydi', retcode=3,
143
error_regexes=('are mutually exclusive',))
143
error_regexes=('are mutually exclusive',))
145
145
def test_diff_nonexistent_revision(self):
146
146
out, err = self.run_bzr('diff -r 123', retcode=3,
147
error_regexes=("Requested revision: '123' does not "
148
"exist in branch:",))
147
error_regexes=("Requested revision: '123' does not "
148
"exist in branch:",))
150
150
def test_diff_nonexistent_dotted_revision(self):
151
151
out, err = self.run_bzr('diff -r 1.1', retcode=3)
152
152
self.assertContainsRe(err,
153
"Requested revision: '1.1' does not exist in branch:")
153
"Requested revision: '1.1' does not exist in branch:")
155
155
def test_diff_nonexistent_dotted_revision_change(self):
156
156
out, err = self.run_bzr('diff -c 1.1', retcode=3)
157
157
self.assertContainsRe(err,
158
"Requested revision: '1.1' does not exist in branch:")
158
"Requested revision: '1.1' does not exist in branch:")
160
160
def test_diff_unversioned(self):
161
161
# Get an error when diffing a non-versioned file.
174
174
branch1_tree.add('file')
175
175
branch1_tree.add('file2')
176
176
branch1_tree.commit(message='add file and file2')
177
branch2_tree = branch1_tree.bzrdir.sprout('branch2').open_workingtree()
178
self.build_tree_contents([('branch2/file', 'new content\n')])
177
branch2_tree = branch1_tree.controldir.sprout(
178
'branch2').open_workingtree()
179
self.build_tree_contents([('branch2/file', b'new content\n')])
179
180
branch2_tree.commit(message='update file')
180
181
return branch1_tree, branch2_tree
182
183
def check_b2_vs_b1(self, cmd):
183
184
# Compare branch2 vs branch1 using cmd and check the result
184
185
out, err = self.run_bzr(cmd, retcode=1)
185
self.assertEquals('', err)
186
self.assertEquals("=== modified file 'file'\n"
187
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
188
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
191
"+contents of branch1/file\n"
192
"\n", subst_dates(out))
186
self.assertEqual('', err)
187
self.assertEqual("=== modified file 'file'\n"
188
"--- old/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
189
"+++ new/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
192
"+contents of branch1/file\n"
193
"\n", subst_dates(out))
194
195
def check_b1_vs_b2(self, cmd):
195
196
# Compare branch1 vs branch2 using cmd and check the result
196
197
out, err = self.run_bzr(cmd, retcode=1)
197
self.assertEquals('', err)
198
self.assertEqual('', err)
198
199
self.assertEqualDiff("=== modified file 'file'\n"
199
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
200
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
202
"-contents of branch1/file\n"
204
"\n", subst_dates(out))
200
"--- old/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
201
"+++ new/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
203
"-contents of branch1/file\n"
205
"\n", subst_dates(out))
206
207
def check_no_diffs(self, cmd):
207
208
# Check that running cmd returns an empty diff
208
209
out, err = self.run_bzr(cmd, retcode=0)
209
self.assertEquals('', err)
210
self.assertEquals('', out)
210
self.assertEqual('', err)
211
self.assertEqual('', out)
212
213
def test_diff_branches(self):
213
214
self.example_branches()
250
251
def test_diff_revno_branches(self):
251
252
self.example_branches()
252
253
branch2_tree = workingtree.WorkingTree.open_containing('branch2')[0]
253
self.build_tree_contents([('branch2/file', 'even newer content')])
254
self.build_tree_contents([('branch2/file', b'even newer content')])
254
255
branch2_tree.commit(message='update file once more')
256
257
out, err = self.run_bzr('diff -r revno:1:branch2..revno:1:branch1',
258
self.assertEquals('', err)
259
self.assertEquals('', out)
259
self.assertEqual('', err)
260
self.assertEqual('', out)
260
261
out, err = self.run_bzr('diff -r revno:2:branch2..revno:1:branch1',
262
self.assertEquals('', err)
263
self.assertEqual('', err)
263
264
self.assertEqualDiff("=== modified file 'file'\n"
264
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
265
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
268
"+contents of branch1/file\n"
269
"\n", subst_dates(out))
265
"--- old/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
266
"+++ new/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
269
"+contents of branch1/file\n"
270
"\n", subst_dates(out))
272
def test_diff_color_always(self):
273
from ...terminal import colorstring
274
from ... import colordiff
275
self.overrideAttr(colordiff, 'GLOBAL_COLORDIFFRC', None)
276
self.example_branches()
277
branch2_tree = workingtree.WorkingTree.open_containing('branch2')[0]
278
self.build_tree_contents([('branch2/file', b'even newer content')])
279
branch2_tree.commit(message='update file once more')
281
out, err = self.run_bzr('diff --color=always -r revno:2:branch2..revno:1:branch1',
283
self.assertEqual('', err)
284
self.assertEqualDiff((
285
colorstring(b"=== modified file 'file'\n", 'darkyellow') +
286
colorstring(b"--- old/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n", 'darkred') +
287
colorstring(b"+++ new/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n", 'darkblue') +
288
colorstring(b"@@ -1 +1 @@\n", 'darkgreen') +
289
colorstring(b"-new content\n", 'darkred') +
290
colorstring(b"+contents of branch1/file\n", 'darkblue') +
291
colorstring(b"\n", 'darkwhite')).decode(),
271
294
def example_branch2(self):
272
295
branch1_tree = self.make_branch_and_tree('branch1')
273
self.build_tree_contents([('branch1/file1', 'original line\n')])
296
self.build_tree_contents([('branch1/file1', b'original line\n')])
274
297
branch1_tree.add('file1')
275
298
branch1_tree.commit(message='first commit')
276
self.build_tree_contents([('branch1/file1', 'repo line\n')])
299
self.build_tree_contents([('branch1/file1', b'repo line\n')])
277
300
branch1_tree.commit(message='second commit')
278
301
return branch1_tree
280
303
def test_diff_to_working_tree(self):
281
304
self.example_branch2()
282
self.build_tree_contents([('branch1/file1', 'new line')])
305
self.build_tree_contents([('branch1/file1', b'new line')])
283
306
output = self.run_bzr('diff -r 1.. branch1', retcode=1)
284
307
self.assertContainsRe(output[0], '\n\\-original line\n\\+new line\n')
286
309
def test_diff_to_working_tree_in_subdir(self):
287
310
self.example_branch2()
288
self.build_tree_contents([('branch1/file1', 'new line')])
311
self.build_tree_contents([('branch1/file1', b'new line')])
289
312
os.mkdir('branch1/dir1')
290
313
output = self.run_bzr('diff -r 1..', retcode=1,
291
314
working_dir='branch1/dir1')
313
336
def show_diff(self, specific_files, extra_trees=None):
314
337
self.to_file.write("BOO!\n")
315
338
return super(BooDiffTree, self).show_diff(specific_files,
318
341
diff_format_registry.register("boo", BooDiffTree, "Scary diff format")
319
342
self.addCleanup(diff_format_registry.remove, "boo")
320
343
self.make_example_branch()
321
self.build_tree_contents([('hello', 'hello world!\n')])
344
self.build_tree_contents([('hello', b'hello world!\n')])
322
345
output = self.run_bzr('diff --format=boo', retcode=1)
323
346
self.assertTrue("BOO!" in output[0])
324
347
output = self.run_bzr('diff -Fboo', retcode=1)
325
348
self.assertTrue("BOO!" in output[0])
350
def test_binary_diff_remove(self):
351
tree = self.make_branch_and_tree('.')
352
self.build_tree_contents([('a', b'\x00' * 20)])
354
tree.commit('add binary file')
356
output = self.run_bzr('diff', retcode=1)
358
"=== removed file 'a'\nBinary files old/a and new/a differ\n",
361
def test_moved_away(self):
363
tree = self.make_branch_and_tree('.')
364
self.build_tree_contents([('a', 'asdf\n')])
367
tree.rename_one('a', 'b')
368
self.build_tree_contents([('a', 'qwer\n')])
370
output, error = self.run_bzr('diff -p0', retcode=1)
371
self.assertEqualDiff("""\
373
--- a\tYYYY-MM-DD HH:MM:SS +ZZZZ
374
+++ a\tYYYY-MM-DD HH:MM:SS +ZZZZ
378
=== renamed file 'a' => 'b'
379
""", subst_dates(output))
328
382
class TestCheckoutDiff(TestDiff):
361
415
def test_diff_label_added(self):
362
416
tree = super(TestDiffLabels, self).make_example_branch()
363
self.build_tree_contents([('barbar', 'barbar')])
417
self.build_tree_contents([('barbar', b'barbar')])
364
418
tree.add('barbar')
365
419
diff = self.run_bzr('diff', retcode=1)
366
420
self.assertTrue("=== added file 'barbar'" in diff[0])
368
422
def test_diff_label_modified(self):
369
423
super(TestDiffLabels, self).make_example_branch()
370
self.build_tree_contents([('hello', 'barbar')])
424
self.build_tree_contents([('hello', b'barbar')])
371
425
diff = self.run_bzr('diff', retcode=1)
372
426
self.assertTrue("=== modified file 'hello'" in diff[0])
392
446
'diff -Oprogress_bar=none -r 1 --diff-options -ub',
393
447
universal_newlines=True,
395
if 'Diff is not installed on this machine' in err:
449
if b'Diff is not installed on this machine' in err:
396
450
raise tests.TestSkipped("No external 'diff' is available")
397
self.assertEqual('', err)
451
self.assertEqual(b'', err)
398
452
# We have to skip the stuff in the middle, because it depends
400
self.assertStartsWith(out, "=== added file 'goodbye'\n"
401
"--- goodbye\t1970-01-01 00:00:00 +0000\n"
403
self.assertEndsWith(out, "\n@@ -0,0 +1 @@\n"
454
self.assertStartsWith(
456
b"=== added file 'goodbye'\n"
457
b"--- old/goodbye\t1970-01-01 00:00:00 +0000\n"
458
b"+++ new/goodbye\t")
459
self.assertEndsWith(out, b"\n@@ -0,0 +1 @@\n"
406
462
def test_external_diff_options_and_using(self):
407
463
"""Test that the options are passed correctly to an external diff process"""
408
464
self.requireFeature(features.diff_feature)
409
465
self.make_example_branch()
410
self.build_tree_contents([('hello', 'Foo\n')])
466
self.build_tree_contents([('hello', b'Foo\n')])
411
467
out, err = self.run_bzr('diff --diff-options -i --using diff',
413
self.assertEquals("=== modified file 'hello'\n", out)
414
self.assertEquals('', err)
469
self.assertEqual("=== modified file 'hello'\n", out)
470
self.assertEqual('', err)
417
473
class TestDiffOutput(DiffBase):