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
from cStringIO import StringIO
21
from tempfile import TemporaryFile
23
from bzrlib import tests
24
from bzrlib.diff import (
34
from bzrlib.errors import BinaryFile, NoDiff
35
import bzrlib.osutils as osutils
36
import bzrlib.patiencediff
37
import bzrlib._patiencediff_py
38
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
39
TestCaseInTempDir, TestSkipped)
42
class _CompiledPatienceDiffFeature(Feature):
46
import bzrlib._patiencediff_c
51
def feature_name(self):
52
return 'bzrlib._patiencediff_c'
54
CompiledPatienceDiffFeature = _CompiledPatienceDiffFeature()
57
class _UnicodeFilename(Feature):
58
"""Does the filesystem support Unicode filenames?"""
63
except UnicodeEncodeError:
65
except (IOError, OSError):
66
# The filesystem allows the Unicode filename but the file doesn't
70
# The filesystem allows the Unicode filename and the file exists,
74
UnicodeFilename = _UnicodeFilename()
77
class TestUnicodeFilename(TestCase):
79
def test_probe_passes(self):
80
"""UnicodeFilename._probe passes."""
81
# We can't test much more than that because the behaviour depends
83
UnicodeFilename._probe()
86
def udiff_lines(old, new, allow_binary=False):
88
internal_diff('old', old, 'new', new, output, allow_binary)
90
return output.readlines()
93
def external_udiff_lines(old, new, use_stringio=False):
95
# StringIO has no fileno, so it tests a different codepath
98
output = TemporaryFile()
100
external_diff('old', old, 'new', new, output, diff_opts=['-u'])
102
raise TestSkipped('external "diff" not present to test')
104
lines = output.readlines()
109
class TestDiff(TestCase):
111
def test_add_nl(self):
112
"""diff generates a valid diff for patches that add a newline"""
113
lines = udiff_lines(['boo'], ['boo\n'])
114
self.check_patch(lines)
115
self.assertEquals(lines[4], '\\ No newline at end of file\n')
116
## "expected no-nl, got %r" % lines[4]
118
def test_add_nl_2(self):
119
"""diff generates a valid diff for patches that change last line and
122
lines = udiff_lines(['boo'], ['goo\n'])
123
self.check_patch(lines)
124
self.assertEquals(lines[4], '\\ No newline at end of file\n')
125
## "expected no-nl, got %r" % lines[4]
127
def test_remove_nl(self):
128
"""diff generates a valid diff for patches that change last line and
131
lines = udiff_lines(['boo\n'], ['boo'])
132
self.check_patch(lines)
133
self.assertEquals(lines[5], '\\ No newline at end of file\n')
134
## "expected no-nl, got %r" % lines[5]
136
def check_patch(self, lines):
137
self.assert_(len(lines) > 1)
138
## "Not enough lines for a file header for patch:\n%s" % "".join(lines)
139
self.assert_(lines[0].startswith ('---'))
140
## 'No orig line for patch:\n%s' % "".join(lines)
141
self.assert_(lines[1].startswith ('+++'))
142
## 'No mod line for patch:\n%s' % "".join(lines)
143
self.assert_(len(lines) > 2)
144
## "No hunks for patch:\n%s" % "".join(lines)
145
self.assert_(lines[2].startswith('@@'))
146
## "No hunk header for patch:\n%s" % "".join(lines)
147
self.assert_('@@' in lines[2][2:])
148
## "Unterminated hunk header for patch:\n%s" % "".join(lines)
150
def test_binary_lines(self):
151
self.assertRaises(BinaryFile, udiff_lines, [1023 * 'a' + '\x00'], [])
152
self.assertRaises(BinaryFile, udiff_lines, [], [1023 * 'a' + '\x00'])
153
udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
154
udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
156
def test_external_diff(self):
157
lines = external_udiff_lines(['boo\n'], ['goo\n'])
158
self.check_patch(lines)
159
self.assertEqual('\n', lines[-1])
161
def test_external_diff_no_fileno(self):
162
# Make sure that we can handle not having a fileno, even
163
# if the diff is large
164
lines = external_udiff_lines(['boo\n']*10000,
167
self.check_patch(lines)
169
def test_external_diff_binary_lang_c(self):
171
for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
172
old_env[lang] = osutils.set_or_unset_env(lang, 'C')
174
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
175
# Older versions of diffutils say "Binary files", newer
176
# versions just say "Files".
177
self.assertContainsRe(lines[0],
178
'(Binary f|F)iles old and new differ\n')
179
self.assertEquals(lines[1:], ['\n'])
181
for lang, old_val in old_env.iteritems():
182
osutils.set_or_unset_env(lang, old_val)
184
def test_no_external_diff(self):
185
"""Check that NoDiff is raised when diff is not available"""
186
# Use os.environ['PATH'] to make sure no 'diff' command is available
187
orig_path = os.environ['PATH']
189
os.environ['PATH'] = ''
190
self.assertRaises(NoDiff, external_diff,
191
'old', ['boo\n'], 'new', ['goo\n'],
192
StringIO(), diff_opts=['-u'])
194
os.environ['PATH'] = orig_path
196
def test_internal_diff_default(self):
197
# Default internal diff encoding is utf8
199
internal_diff(u'old_\xb5', ['old_text\n'],
200
u'new_\xe5', ['new_text\n'], output)
201
lines = output.getvalue().splitlines(True)
202
self.check_patch(lines)
203
self.assertEquals(['--- old_\xc2\xb5\n',
204
'+++ new_\xc3\xa5\n',
212
def test_internal_diff_utf8(self):
214
internal_diff(u'old_\xb5', ['old_text\n'],
215
u'new_\xe5', ['new_text\n'], output,
216
path_encoding='utf8')
217
lines = output.getvalue().splitlines(True)
218
self.check_patch(lines)
219
self.assertEquals(['--- old_\xc2\xb5\n',
220
'+++ new_\xc3\xa5\n',
228
def test_internal_diff_iso_8859_1(self):
230
internal_diff(u'old_\xb5', ['old_text\n'],
231
u'new_\xe5', ['new_text\n'], output,
232
path_encoding='iso-8859-1')
233
lines = output.getvalue().splitlines(True)
234
self.check_patch(lines)
235
self.assertEquals(['--- old_\xb5\n',
244
def test_internal_diff_no_content(self):
246
internal_diff(u'old', [], u'new', [], output)
247
self.assertEqual('', output.getvalue())
249
def test_internal_diff_no_changes(self):
251
internal_diff(u'old', ['text\n', 'contents\n'],
252
u'new', ['text\n', 'contents\n'],
254
self.assertEqual('', output.getvalue())
256
def test_internal_diff_returns_bytes(self):
258
output = StringIO.StringIO()
259
internal_diff(u'old_\xb5', ['old_text\n'],
260
u'new_\xe5', ['new_text\n'], output)
261
self.failUnless(isinstance(output.getvalue(), str),
262
'internal_diff should return bytestrings')
265
class TestDiffFiles(TestCaseInTempDir):
267
def test_external_diff_binary(self):
268
"""The output when using external diff should use diff's i18n error"""
269
# Make sure external_diff doesn't fail in the current LANG
270
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
272
cmd = ['diff', '-u', '--binary', 'old', 'new']
273
open('old', 'wb').write('\x00foobar\n')
274
open('new', 'wb').write('foo\x00bar\n')
275
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
276
stdin=subprocess.PIPE)
277
out, err = pipe.communicate()
278
# Diff returns '2' on Binary files.
279
self.assertEqual(2, pipe.returncode)
280
# We should output whatever diff tells us, plus a trailing newline
281
self.assertEqual(out.splitlines(True) + ['\n'], lines)
284
class TestShowDiffTreesHelper(TestCaseWithTransport):
285
"""Has a helper for running show_diff_trees"""
287
def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
289
if working_tree is not None:
290
extra_trees = (working_tree,)
293
show_diff_trees(tree1, tree2, output, specific_files=specific_files,
294
extra_trees=extra_trees, old_label='old/',
296
return output.getvalue()
299
class TestDiffDates(TestShowDiffTreesHelper):
302
super(TestDiffDates, self).setUp()
303
self.wt = self.make_branch_and_tree('.')
304
self.b = self.wt.branch
305
self.build_tree_contents([
306
('file1', 'file1 contents at rev 1\n'),
307
('file2', 'file2 contents at rev 1\n')
309
self.wt.add(['file1', 'file2'])
311
message='Revision 1',
312
timestamp=1143849600, # 2006-04-01 00:00:00 UTC
315
self.build_tree_contents([('file1', 'file1 contents at rev 2\n')])
317
message='Revision 2',
318
timestamp=1143936000, # 2006-04-02 00:00:00 UTC
321
self.build_tree_contents([('file2', 'file2 contents at rev 3\n')])
323
message='Revision 3',
324
timestamp=1144022400, # 2006-04-03 00:00:00 UTC
327
self.wt.remove(['file2'])
329
message='Revision 4',
330
timestamp=1144108800, # 2006-04-04 00:00:00 UTC
333
self.build_tree_contents([
334
('file1', 'file1 contents in working tree\n')
336
# set the date stamps for files in the working tree to known values
337
os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
339
def test_diff_rev_tree_working_tree(self):
340
output = self.get_diff(self.wt.basis_tree(), self.wt)
341
# note that the date for old/file1 is from rev 2 rather than from
342
# the basis revision (rev 4)
343
self.assertEqualDiff(output, '''\
344
=== modified file 'file1'
345
--- old/file1\t2006-04-02 00:00:00 +0000
346
+++ new/file1\t2006-04-05 00:00:00 +0000
348
-file1 contents at rev 2
349
+file1 contents in working tree
353
def test_diff_rev_tree_rev_tree(self):
354
tree1 = self.b.repository.revision_tree('rev-2')
355
tree2 = self.b.repository.revision_tree('rev-3')
356
output = self.get_diff(tree1, tree2)
357
self.assertEqualDiff(output, '''\
358
=== modified file 'file2'
359
--- old/file2\t2006-04-01 00:00:00 +0000
360
+++ new/file2\t2006-04-03 00:00:00 +0000
362
-file2 contents at rev 1
363
+file2 contents at rev 3
367
def test_diff_add_files(self):
368
tree1 = self.b.repository.revision_tree(None)
369
tree2 = self.b.repository.revision_tree('rev-1')
370
output = self.get_diff(tree1, tree2)
371
# the files have the epoch time stamp for the tree in which
373
self.assertEqualDiff(output, '''\
374
=== added file 'file1'
375
--- old/file1\t1970-01-01 00:00:00 +0000
376
+++ new/file1\t2006-04-01 00:00:00 +0000
378
+file1 contents at rev 1
380
=== added file 'file2'
381
--- old/file2\t1970-01-01 00:00:00 +0000
382
+++ new/file2\t2006-04-01 00:00:00 +0000
384
+file2 contents at rev 1
388
def test_diff_remove_files(self):
389
tree1 = self.b.repository.revision_tree('rev-3')
390
tree2 = self.b.repository.revision_tree('rev-4')
391
output = self.get_diff(tree1, tree2)
392
# the file has the epoch time stamp for the tree in which
394
self.assertEqualDiff(output, '''\
395
=== removed file 'file2'
396
--- old/file2\t2006-04-03 00:00:00 +0000
397
+++ new/file2\t1970-01-01 00:00:00 +0000
399
-file2 contents at rev 3
403
def test_show_diff_specified(self):
404
"""A working tree filename can be used to identify a file"""
405
self.wt.rename_one('file1', 'file1b')
406
old_tree = self.b.repository.revision_tree('rev-1')
407
new_tree = self.b.repository.revision_tree('rev-4')
408
out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
409
working_tree=self.wt)
410
self.assertContainsRe(out, 'file1\t')
412
def test_recursive_diff(self):
413
"""Children of directories are matched"""
416
self.wt.add(['dir1', 'dir2'])
417
self.wt.rename_one('file1', 'dir1/file1')
418
old_tree = self.b.repository.revision_tree('rev-1')
419
new_tree = self.b.repository.revision_tree('rev-4')
420
out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
421
working_tree=self.wt)
422
self.assertContainsRe(out, 'file1\t')
423
out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
424
working_tree=self.wt)
425
self.assertNotContainsRe(out, 'file1\t')
429
class TestShowDiffTrees(TestShowDiffTreesHelper):
430
"""Direct tests for show_diff_trees"""
432
def test_modified_file(self):
433
"""Test when a file is modified."""
434
tree = self.make_branch_and_tree('tree')
435
self.build_tree_contents([('tree/file', 'contents\n')])
436
tree.add(['file'], ['file-id'])
437
tree.commit('one', rev_id='rev-1')
439
self.build_tree_contents([('tree/file', 'new contents\n')])
440
diff = self.get_diff(tree.basis_tree(), tree)
441
self.assertContainsRe(diff, "=== modified file 'file'\n")
442
self.assertContainsRe(diff, '--- old/file\t')
443
self.assertContainsRe(diff, '\\+\\+\\+ new/file\t')
444
self.assertContainsRe(diff, '-contents\n'
447
def test_modified_file_in_renamed_dir(self):
448
"""Test when a file is modified in a renamed directory."""
449
tree = self.make_branch_and_tree('tree')
450
self.build_tree(['tree/dir/'])
451
self.build_tree_contents([('tree/dir/file', 'contents\n')])
452
tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
453
tree.commit('one', rev_id='rev-1')
455
tree.rename_one('dir', 'other')
456
self.build_tree_contents([('tree/other/file', 'new contents\n')])
457
diff = self.get_diff(tree.basis_tree(), tree)
458
self.assertContainsRe(diff, "=== renamed directory 'dir' => 'other'\n")
459
self.assertContainsRe(diff, "=== modified file 'other/file'\n")
460
# XXX: This is technically incorrect, because it used to be at another
461
# location. What to do?
462
self.assertContainsRe(diff, '--- old/dir/file\t')
463
self.assertContainsRe(diff, '\\+\\+\\+ new/other/file\t')
464
self.assertContainsRe(diff, '-contents\n'
467
def test_renamed_directory(self):
468
"""Test when only a directory is only renamed."""
469
tree = self.make_branch_and_tree('tree')
470
self.build_tree(['tree/dir/'])
471
self.build_tree_contents([('tree/dir/file', 'contents\n')])
472
tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
473
tree.commit('one', rev_id='rev-1')
475
tree.rename_one('dir', 'newdir')
476
diff = self.get_diff(tree.basis_tree(), tree)
477
# Renaming a directory should be a single "you renamed this dir" even
478
# when there are files inside.
479
self.assertEqual("=== renamed directory 'dir' => 'newdir'\n", diff)
481
def test_renamed_file(self):
482
"""Test when a file is only renamed."""
483
tree = self.make_branch_and_tree('tree')
484
self.build_tree_contents([('tree/file', 'contents\n')])
485
tree.add(['file'], ['file-id'])
486
tree.commit('one', rev_id='rev-1')
488
tree.rename_one('file', 'newname')
489
diff = self.get_diff(tree.basis_tree(), tree)
490
self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
491
# We shouldn't have a --- or +++ line, because there is no content
493
self.assertNotContainsRe(diff, '---')
495
def test_renamed_and_modified_file(self):
496
"""Test when a file is only renamed."""
497
tree = self.make_branch_and_tree('tree')
498
self.build_tree_contents([('tree/file', 'contents\n')])
499
tree.add(['file'], ['file-id'])
500
tree.commit('one', rev_id='rev-1')
502
tree.rename_one('file', 'newname')
503
self.build_tree_contents([('tree/newname', 'new contents\n')])
504
diff = self.get_diff(tree.basis_tree(), tree)
505
self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
506
self.assertContainsRe(diff, '--- old/file\t')
507
self.assertContainsRe(diff, '\\+\\+\\+ new/newname\t')
508
self.assertContainsRe(diff, '-contents\n'
511
def test_binary_unicode_filenames(self):
512
"""Test that contents of files are *not* encoded in UTF-8 when there
513
is a binary file in the diff.
515
# See https://bugs.launchpad.net/bugs/110092.
516
self.requireFeature(UnicodeFilename)
518
# This bug isn't triggered with cStringIO.
519
from StringIO import StringIO
520
tree = self.make_branch_and_tree('tree')
521
alpha, omega = u'\u03b1', u'\u03c9'
522
alpha_utf8, omega_utf8 = alpha.encode('utf8'), omega.encode('utf8')
523
self.build_tree_contents(
524
[('tree/' + alpha, chr(0)),
526
('The %s and the %s\n' % (alpha_utf8, omega_utf8)))])
527
tree.add([alpha], ['file-id'])
528
tree.add([omega], ['file-id-2'])
529
diff_content = StringIO()
530
show_diff_trees(tree.basis_tree(), tree, diff_content)
531
diff = diff_content.getvalue()
532
self.assertContainsRe(diff, r"=== added file '%s'" % alpha_utf8)
533
self.assertContainsRe(
534
diff, "Binary files a/%s.*and b/%s.* differ\n" % (alpha_utf8, alpha_utf8))
535
self.assertContainsRe(diff, r"=== added file '%s'" % omega_utf8)
536
self.assertContainsRe(diff, r"--- a/%s" % (omega_utf8,))
537
self.assertContainsRe(diff, r"\+\+\+ b/%s" % (omega_utf8,))
539
def test_unicode_filename(self):
540
"""Test when the filename are unicode."""
541
self.requireFeature(UnicodeFilename)
543
alpha, omega = u'\u03b1', u'\u03c9'
544
autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
546
tree = self.make_branch_and_tree('tree')
547
self.build_tree_contents([('tree/ren_'+alpha, 'contents\n')])
548
tree.add(['ren_'+alpha], ['file-id-2'])
549
self.build_tree_contents([('tree/del_'+alpha, 'contents\n')])
550
tree.add(['del_'+alpha], ['file-id-3'])
551
self.build_tree_contents([('tree/mod_'+alpha, 'contents\n')])
552
tree.add(['mod_'+alpha], ['file-id-4'])
554
tree.commit('one', rev_id='rev-1')
556
tree.rename_one('ren_'+alpha, 'ren_'+omega)
557
tree.remove('del_'+alpha)
558
self.build_tree_contents([('tree/add_'+alpha, 'contents\n')])
559
tree.add(['add_'+alpha], ['file-id'])
560
self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
562
diff = self.get_diff(tree.basis_tree(), tree)
563
self.assertContainsRe(diff,
564
"=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
565
self.assertContainsRe(diff, "=== added file 'add_%s'"%autf8)
566
self.assertContainsRe(diff, "=== modified file 'mod_%s'"%autf8)
567
self.assertContainsRe(diff, "=== removed file 'del_%s'"%autf8)
570
class DiffWasIs(DiffPath):
572
def diff(self, file_id, old_path, new_path, old_kind, new_kind):
573
self.to_file.write('was: ')
574
self.to_file.write(self.old_tree.get_file(file_id).read())
575
self.to_file.write('is: ')
576
self.to_file.write(self.new_tree.get_file(file_id).read())
580
class TestDiffTree(TestCaseWithTransport):
583
TestCaseWithTransport.setUp(self)
584
self.old_tree = self.make_branch_and_tree('old-tree')
585
self.old_tree.lock_write()
586
self.addCleanup(self.old_tree.unlock)
587
self.new_tree = self.make_branch_and_tree('new-tree')
588
self.new_tree.lock_write()
589
self.addCleanup(self.new_tree.unlock)
590
self.differ = DiffTree(self.old_tree, self.new_tree, StringIO())
592
def test_diff_text(self):
593
self.build_tree_contents([('old-tree/olddir/',),
594
('old-tree/olddir/oldfile', 'old\n')])
595
self.old_tree.add('olddir')
596
self.old_tree.add('olddir/oldfile', 'file-id')
597
self.build_tree_contents([('new-tree/newdir/',),
598
('new-tree/newdir/newfile', 'new\n')])
599
self.new_tree.add('newdir')
600
self.new_tree.add('newdir/newfile', 'file-id')
601
differ = DiffText(self.old_tree, self.new_tree, StringIO())
602
differ.diff_text('file-id', None, 'old label', 'new label')
604
'--- old label\n+++ new label\n@@ -1,1 +0,0 @@\n-old\n\n',
605
differ.to_file.getvalue())
606
differ.to_file.seek(0)
607
differ.diff_text(None, 'file-id', 'old label', 'new label')
609
'--- old label\n+++ new label\n@@ -0,0 +1,1 @@\n+new\n\n',
610
differ.to_file.getvalue())
611
differ.to_file.seek(0)
612
differ.diff_text('file-id', 'file-id', 'old label', 'new label')
614
'--- old label\n+++ new label\n@@ -1,1 +1,1 @@\n-old\n+new\n\n',
615
differ.to_file.getvalue())
617
def test_diff_deletion(self):
618
self.build_tree_contents([('old-tree/file', 'contents'),
619
('new-tree/file', 'contents')])
620
self.old_tree.add('file', 'file-id')
621
self.new_tree.add('file', 'file-id')
622
os.unlink('new-tree/file')
623
self.differ.show_diff(None)
624
self.assertContainsRe(self.differ.to_file.getvalue(), '-contents')
626
def test_diff_creation(self):
627
self.build_tree_contents([('old-tree/file', 'contents'),
628
('new-tree/file', 'contents')])
629
self.old_tree.add('file', 'file-id')
630
self.new_tree.add('file', 'file-id')
631
os.unlink('old-tree/file')
632
self.differ.show_diff(None)
633
self.assertContainsRe(self.differ.to_file.getvalue(), '\+contents')
635
def test_diff_symlink(self):
636
differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
637
differ.diff_symlink('old target', None)
638
self.assertEqual("=== target was 'old target'\n",
639
differ.to_file.getvalue())
641
differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
642
differ.diff_symlink(None, 'new target')
643
self.assertEqual("=== target is 'new target'\n",
644
differ.to_file.getvalue())
646
differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
647
differ.diff_symlink('old target', 'new target')
648
self.assertEqual("=== target changed 'old target' => 'new target'\n",
649
differ.to_file.getvalue())
652
self.build_tree_contents([('old-tree/olddir/',),
653
('old-tree/olddir/oldfile', 'old\n')])
654
self.old_tree.add('olddir')
655
self.old_tree.add('olddir/oldfile', 'file-id')
656
self.build_tree_contents([('new-tree/newdir/',),
657
('new-tree/newdir/newfile', 'new\n')])
658
self.new_tree.add('newdir')
659
self.new_tree.add('newdir/newfile', 'file-id')
660
self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
661
self.assertContainsRe(
662
self.differ.to_file.getvalue(),
663
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
664
' \@\@\n-old\n\+new\n\n')
666
def test_diff_kind_change(self):
667
self.requireFeature(tests.SymlinkFeature)
668
self.build_tree_contents([('old-tree/olddir/',),
669
('old-tree/olddir/oldfile', 'old\n')])
670
self.old_tree.add('olddir')
671
self.old_tree.add('olddir/oldfile', 'file-id')
672
self.build_tree(['new-tree/newdir/'])
673
os.symlink('new', 'new-tree/newdir/newfile')
674
self.new_tree.add('newdir')
675
self.new_tree.add('newdir/newfile', 'file-id')
676
self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
677
self.assertContainsRe(
678
self.differ.to_file.getvalue(),
679
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+0,0'
681
self.assertContainsRe(self.differ.to_file.getvalue(),
682
"=== target is 'new'\n")
684
def test_diff_directory(self):
685
self.build_tree(['new-tree/new-dir/'])
686
self.new_tree.add('new-dir', 'new-dir-id')
687
self.differ.diff('new-dir-id', None, 'new-dir')
688
self.assertEqual(self.differ.to_file.getvalue(), '')
690
def create_old_new(self):
691
self.build_tree_contents([('old-tree/olddir/',),
692
('old-tree/olddir/oldfile', 'old\n')])
693
self.old_tree.add('olddir')
694
self.old_tree.add('olddir/oldfile', 'file-id')
695
self.build_tree_contents([('new-tree/newdir/',),
696
('new-tree/newdir/newfile', 'new\n')])
697
self.new_tree.add('newdir')
698
self.new_tree.add('newdir/newfile', 'file-id')
700
def test_register_diff(self):
701
self.create_old_new()
702
old_diff_factories = DiffTree.diff_factories
703
DiffTree.diff_factories=old_diff_factories[:]
704
DiffTree.diff_factories.insert(0, DiffWasIs.from_diff_tree)
706
differ = DiffTree(self.old_tree, self.new_tree, StringIO())
708
DiffTree.diff_factories = old_diff_factories
709
differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
710
self.assertNotContainsRe(
711
differ.to_file.getvalue(),
712
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
713
' \@\@\n-old\n\+new\n\n')
714
self.assertContainsRe(differ.to_file.getvalue(),
715
'was: old\nis: new\n')
717
def test_extra_factories(self):
718
self.create_old_new()
719
differ = DiffTree(self.old_tree, self.new_tree, StringIO(),
720
extra_factories=[DiffWasIs.from_diff_tree])
721
differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
722
self.assertNotContainsRe(
723
differ.to_file.getvalue(),
724
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
725
' \@\@\n-old\n\+new\n\n')
726
self.assertContainsRe(differ.to_file.getvalue(),
727
'was: old\nis: new\n')
729
def test_alphabetical_order(self):
730
self.build_tree(['new-tree/a-file'])
731
self.new_tree.add('a-file')
732
self.build_tree(['old-tree/b-file'])
733
self.old_tree.add('b-file')
734
self.differ.show_diff(None)
735
self.assertContainsRe(self.differ.to_file.getvalue(),
736
'.*a-file(.|\n)*b-file')
739
class TestPatienceDiffLib(TestCase):
742
super(TestPatienceDiffLib, self).setUp()
743
self._unique_lcs = bzrlib._patiencediff_py.unique_lcs_py
744
self._recurse_matches = bzrlib._patiencediff_py.recurse_matches_py
745
self._PatienceSequenceMatcher = \
746
bzrlib._patiencediff_py.PatienceSequenceMatcher_py
748
def test_unique_lcs(self):
749
unique_lcs = self._unique_lcs
750
self.assertEquals(unique_lcs('', ''), [])
751
self.assertEquals(unique_lcs('', 'a'), [])
752
self.assertEquals(unique_lcs('a', ''), [])
753
self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
754
self.assertEquals(unique_lcs('a', 'b'), [])
755
self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
756
self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
757
self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
758
self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
760
self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
762
def test_recurse_matches(self):
763
def test_one(a, b, matches):
765
self._recurse_matches(
766
a, b, 0, 0, len(a), len(b), test_matches, 10)
767
self.assertEquals(test_matches, matches)
769
test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
770
[(0, 0), (2, 2), (4, 4)])
771
test_one(['a', 'c', 'b', 'a', 'c'], ['a', 'b', 'c'],
772
[(0, 0), (2, 1), (4, 2)])
773
# Even though 'bc' is not unique globally, and is surrounded by
774
# non-matching lines, we should still match, because they are locally
776
test_one('abcdbce', 'afbcgdbce', [(0,0), (1, 2), (2, 3), (3, 5),
777
(4, 6), (5, 7), (6, 8)])
779
# recurse_matches doesn't match non-unique
780
# lines surrounded by bogus text.
781
# The update has been done in patiencediff.SequenceMatcher instead
783
# This is what it could be
784
#test_one('aBccDe', 'abccde', [(0,0), (2,2), (3,3), (5,5)])
786
# This is what it currently gives:
787
test_one('aBccDe', 'abccde', [(0,0), (5,5)])
789
def assertDiffBlocks(self, a, b, expected_blocks):
790
"""Check that the sequence matcher returns the correct blocks.
792
:param a: A sequence to match
793
:param b: Another sequence to match
794
:param expected_blocks: The expected output, not including the final
795
matching block (len(a), len(b), 0)
797
matcher = self._PatienceSequenceMatcher(None, a, b)
798
blocks = matcher.get_matching_blocks()
800
self.assertEqual((len(a), len(b), 0), last)
801
self.assertEqual(expected_blocks, blocks)
803
def test_matching_blocks(self):
804
# Some basic matching tests
805
self.assertDiffBlocks('', '', [])
806
self.assertDiffBlocks([], [], [])
807
self.assertDiffBlocks('abc', '', [])
808
self.assertDiffBlocks('', 'abc', [])
809
self.assertDiffBlocks('abcd', 'abcd', [(0, 0, 4)])
810
self.assertDiffBlocks('abcd', 'abce', [(0, 0, 3)])
811
self.assertDiffBlocks('eabc', 'abce', [(1, 0, 3)])
812
self.assertDiffBlocks('eabce', 'abce', [(1, 0, 4)])
813
self.assertDiffBlocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
814
self.assertDiffBlocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
815
self.assertDiffBlocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
816
# This may check too much, but it checks to see that
817
# a copied block stays attached to the previous section,
819
# difflib would tend to grab the trailing longest match
820
# which would make the diff not look right
821
self.assertDiffBlocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
822
[(0, 0, 6), (6, 11, 10)])
824
# make sure it supports passing in lists
825
self.assertDiffBlocks(
828
'how are you today?\n'],
830
'how are you today?\n'],
831
[(0, 0, 1), (2, 1, 1)])
833
# non unique lines surrounded by non-matching lines
835
self.assertDiffBlocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
837
# But they only need to be locally unique
838
self.assertDiffBlocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
840
# non unique blocks won't be matched
841
self.assertDiffBlocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
843
# but locally unique ones will
844
self.assertDiffBlocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
845
(5,4,1), (7,5,2), (10,8,1)])
847
self.assertDiffBlocks('abbabbXd', 'cabbabxd', [(7,7,1)])
848
self.assertDiffBlocks('abbabbbb', 'cabbabbc', [])
849
self.assertDiffBlocks('bbbbbbbb', 'cbbbbbbc', [])
851
def test_matching_blocks_tuples(self):
852
# Some basic matching tests
853
self.assertDiffBlocks([], [], [])
854
self.assertDiffBlocks([('a',), ('b',), ('c,')], [], [])
855
self.assertDiffBlocks([], [('a',), ('b',), ('c,')], [])
856
self.assertDiffBlocks([('a',), ('b',), ('c,')],
857
[('a',), ('b',), ('c,')],
859
self.assertDiffBlocks([('a',), ('b',), ('c,')],
860
[('a',), ('b',), ('d,')],
862
self.assertDiffBlocks([('d',), ('b',), ('c,')],
863
[('a',), ('b',), ('c,')],
865
self.assertDiffBlocks([('d',), ('a',), ('b',), ('c,')],
866
[('a',), ('b',), ('c,')],
868
self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
869
[('a', 'b'), ('c', 'X'), ('e', 'f')],
870
[(0, 0, 1), (2, 2, 1)])
871
self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
872
[('a', 'b'), ('c', 'dX'), ('e', 'f')],
873
[(0, 0, 1), (2, 2, 1)])
875
def test_opcodes(self):
876
def chk_ops(a, b, expected_codes):
877
s = self._PatienceSequenceMatcher(None, a, b)
878
self.assertEquals(expected_codes, s.get_opcodes())
882
chk_ops('abc', '', [('delete', 0,3, 0,0)])
883
chk_ops('', 'abc', [('insert', 0,0, 0,3)])
884
chk_ops('abcd', 'abcd', [('equal', 0,4, 0,4)])
885
chk_ops('abcd', 'abce', [('equal', 0,3, 0,3),
886
('replace', 3,4, 3,4)
888
chk_ops('eabc', 'abce', [('delete', 0,1, 0,0),
892
chk_ops('eabce', 'abce', [('delete', 0,1, 0,0),
895
chk_ops('abcde', 'abXde', [('equal', 0,2, 0,2),
896
('replace', 2,3, 2,3),
899
chk_ops('abcde', 'abXYZde', [('equal', 0,2, 0,2),
900
('replace', 2,3, 2,5),
903
chk_ops('abde', 'abXYZde', [('equal', 0,2, 0,2),
904
('insert', 2,2, 2,5),
907
chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
908
[('equal', 0,6, 0,6),
909
('insert', 6,6, 6,11),
910
('equal', 6,16, 11,21)
915
, 'how are you today?\n'],
917
, 'how are you today?\n'],
918
[('equal', 0,1, 0,1),
919
('delete', 1,2, 1,1),
922
chk_ops('aBccDe', 'abccde',
923
[('equal', 0,1, 0,1),
924
('replace', 1,5, 1,5),
927
chk_ops('aBcDec', 'abcdec',
928
[('equal', 0,1, 0,1),
929
('replace', 1,2, 1,2),
931
('replace', 3,4, 3,4),
934
chk_ops('aBcdEcdFg', 'abcdecdfg',
935
[('equal', 0,1, 0,1),
936
('replace', 1,8, 1,8),
939
chk_ops('aBcdEeXcdFg', 'abcdecdfg',
940
[('equal', 0,1, 0,1),
941
('replace', 1,2, 1,2),
943
('delete', 4,5, 4,4),
945
('delete', 6,7, 5,5),
947
('replace', 9,10, 7,8),
948
('equal', 10,11, 8,9)
951
def test_grouped_opcodes(self):
952
def chk_ops(a, b, expected_codes, n=3):
953
s = self._PatienceSequenceMatcher(None, a, b)
954
self.assertEquals(expected_codes, list(s.get_grouped_opcodes(n)))
958
chk_ops('abc', '', [[('delete', 0,3, 0,0)]])
959
chk_ops('', 'abc', [[('insert', 0,0, 0,3)]])
960
chk_ops('abcd', 'abcd', [])
961
chk_ops('abcd', 'abce', [[('equal', 0,3, 0,3),
962
('replace', 3,4, 3,4)
964
chk_ops('eabc', 'abce', [[('delete', 0,1, 0,0),
968
chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
969
[[('equal', 3,6, 3,6),
970
('insert', 6,6, 6,11),
971
('equal', 6,9, 11,14)
973
chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
974
[[('equal', 2,6, 2,6),
975
('insert', 6,6, 6,11),
976
('equal', 6,10, 11,15)
978
chk_ops('Xabcdef', 'abcdef',
979
[[('delete', 0,1, 0,0),
982
chk_ops('abcdef', 'abcdefX',
983
[[('equal', 3,6, 3,6),
988
def test_multiple_ranges(self):
989
# There was an earlier bug where we used a bad set of ranges,
990
# this triggers that specific bug, to make sure it doesn't regress
991
self.assertDiffBlocks('abcdefghijklmnop',
992
'abcXghiYZQRSTUVWXYZijklmnop',
993
[(0, 0, 3), (6, 4, 3), (9, 20, 7)])
995
self.assertDiffBlocks('ABCd efghIjk L',
996
'AxyzBCn mo pqrstuvwI1 2 L',
997
[(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
999
# These are rot13 code snippets.
1000
self.assertDiffBlocks('''\
1001
trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
1003
gnxrf_netf = ['svyr*']
1004
gnxrf_bcgvbaf = ['ab-erphefr']
1006
qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr):
1007
sebz omeyvo.nqq vzcbeg fzneg_nqq, nqq_ercbegre_cevag, nqq_ercbegre_ahyy
1009
ercbegre = nqq_ercbegre_ahyy
1011
ercbegre = nqq_ercbegre_cevag
1012
fzneg_nqq(svyr_yvfg, abg ab_erphefr, ercbegre)
1015
pynff pzq_zxqve(Pbzznaq):
1016
'''.splitlines(True), '''\
1017
trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
1019
--qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl
1022
gnxrf_netf = ['svyr*']
1023
gnxrf_bcgvbaf = ['ab-erphefr', 'qel-eha']
1025
qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr, qel_eha=Snyfr):
1030
# Guvf vf cbvagyrff, ohg V'q engure abg envfr na reebe
1031
npgvba = omeyvo.nqq.nqq_npgvba_ahyy
1033
npgvba = omeyvo.nqq.nqq_npgvba_cevag
1035
npgvba = omeyvo.nqq.nqq_npgvba_nqq
1037
npgvba = omeyvo.nqq.nqq_npgvba_nqq_naq_cevag
1039
omeyvo.nqq.fzneg_nqq(svyr_yvfg, abg ab_erphefr, npgvba)
1042
pynff pzq_zxqve(Pbzznaq):
1043
'''.splitlines(True)
1044
, [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
1046
def test_patience_unified_diff(self):
1047
txt_a = ['hello there\n',
1049
'how are you today?\n']
1050
txt_b = ['hello there\n',
1051
'how are you today?\n']
1052
unified_diff = bzrlib.patiencediff.unified_diff
1053
psm = self._PatienceSequenceMatcher
1054
self.assertEquals([ '--- \n',
1056
'@@ -1,3 +1,2 @@\n',
1059
' how are you today?\n'
1061
, list(unified_diff(txt_a, txt_b,
1062
sequencematcher=psm)))
1063
txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1064
txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1065
# This is the result with LongestCommonSubstring matching
1066
self.assertEquals(['--- \n',
1068
'@@ -1,6 +1,11 @@\n',
1080
, list(unified_diff(txt_a, txt_b)))
1081
# And the patience diff
1082
self.assertEquals(['--- \n',
1084
'@@ -4,6 +4,11 @@\n',
1097
, list(unified_diff(txt_a, txt_b,
1098
sequencematcher=psm)))
1101
class TestPatienceDiffLib_c(TestPatienceDiffLib):
1103
_test_needs_features = [CompiledPatienceDiffFeature]
1106
super(TestPatienceDiffLib_c, self).setUp()
1107
import bzrlib._patiencediff_c
1108
self._unique_lcs = bzrlib._patiencediff_c.unique_lcs_c
1109
self._recurse_matches = bzrlib._patiencediff_c.recurse_matches_c
1110
self._PatienceSequenceMatcher = \
1111
bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1113
def test_unhashable(self):
1114
"""We should get a proper exception here."""
1115
# We need to be able to hash items in the sequence, lists are
1116
# unhashable, and thus cannot be diffed
1117
e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1119
e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1120
None, ['valid', []], [])
1121
e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1122
None, ['valid'], [[]])
1123
e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1124
None, ['valid'], ['valid', []])
1127
class TestPatienceDiffLibFiles(TestCaseInTempDir):
1130
super(TestPatienceDiffLibFiles, self).setUp()
1131
self._PatienceSequenceMatcher = \
1132
bzrlib._patiencediff_py.PatienceSequenceMatcher_py
1134
def test_patience_unified_diff_files(self):
1135
txt_a = ['hello there\n',
1137
'how are you today?\n']
1138
txt_b = ['hello there\n',
1139
'how are you today?\n']
1140
open('a1', 'wb').writelines(txt_a)
1141
open('b1', 'wb').writelines(txt_b)
1143
unified_diff_files = bzrlib.patiencediff.unified_diff_files
1144
psm = self._PatienceSequenceMatcher
1145
self.assertEquals(['--- a1 \n',
1147
'@@ -1,3 +1,2 @@\n',
1150
' how are you today?\n',
1152
, list(unified_diff_files('a1', 'b1',
1153
sequencematcher=psm)))
1155
txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1156
txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1157
open('a2', 'wb').writelines(txt_a)
1158
open('b2', 'wb').writelines(txt_b)
1160
# This is the result with LongestCommonSubstring matching
1161
self.assertEquals(['--- a2 \n',
1163
'@@ -1,6 +1,11 @@\n',
1175
, list(unified_diff_files('a2', 'b2')))
1177
# And the patience diff
1178
self.assertEquals(['--- a2 \n',
1180
'@@ -4,6 +4,11 @@\n',
1193
, list(unified_diff_files('a2', 'b2',
1194
sequencematcher=psm)))
1197
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1199
_test_needs_features = [CompiledPatienceDiffFeature]
1202
super(TestPatienceDiffLibFiles_c, self).setUp()
1203
import bzrlib._patiencediff_c
1204
self._PatienceSequenceMatcher = \
1205
bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1208
class TestUsingCompiledIfAvailable(TestCase):
1210
def test_PatienceSequenceMatcher(self):
1211
if CompiledPatienceDiffFeature.available():
1212
from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1213
self.assertIs(PatienceSequenceMatcher_c,
1214
bzrlib.patiencediff.PatienceSequenceMatcher)
1216
from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
1217
self.assertIs(PatienceSequenceMatcher_py,
1218
bzrlib.patiencediff.PatienceSequenceMatcher)
1220
def test_unique_lcs(self):
1221
if CompiledPatienceDiffFeature.available():
1222
from bzrlib._patiencediff_c import unique_lcs_c
1223
self.assertIs(unique_lcs_c,
1224
bzrlib.patiencediff.unique_lcs)
1226
from bzrlib._patiencediff_py import unique_lcs_py
1227
self.assertIs(unique_lcs_py,
1228
bzrlib.patiencediff.unique_lcs)
1230
def test_recurse_matches(self):
1231
if CompiledPatienceDiffFeature.available():
1232
from bzrlib._patiencediff_c import recurse_matches_c
1233
self.assertIs(recurse_matches_c,
1234
bzrlib.patiencediff.recurse_matches)
1236
from bzrlib._patiencediff_py import recurse_matches_py
1237
self.assertIs(recurse_matches_py,
1238
bzrlib.patiencediff.recurse_matches)
1241
class TestDiffFromTool(TestCaseWithTransport):
1243
def test_from_string(self):
1244
diff_obj = DiffFromTool.from_string('diff', None, None, None)
1245
self.addCleanup(diff_obj.finish)
1246
self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
1247
diff_obj.command_template)
1248
diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1249
self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
1250
diff_obj.command_template)
1251
self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1252
diff_obj._get_command('old-path', 'new-path'))
1254
def test_execute(self):
1256
diff_obj = DiffFromTool(['python', '-c',
1257
'print "%(old_path)s %(new_path)s"'],
1259
self.addCleanup(diff_obj.finish)
1260
diff_obj._execute('old', 'new')
1261
self.assertEqual(output.getvalue().rstrip(), 'old new')
1263
def test_prepare_files(self):
1265
tree = self.make_branch_and_tree('tree')
1266
self.build_tree_contents([('tree/file', 'oldcontent')])
1267
tree.add('file', 'file-id')
1268
tree.commit('old tree')
1269
self.build_tree_contents([('tree/file', 'newcontent')])
1270
old_tree = tree.basis_tree()
1271
old_tree.lock_read()
1272
self.addCleanup(old_tree.unlock)
1273
diff_obj = DiffFromTool(['python', '-c',
1274
'print "%(old_path)s %(new_path)s"'],
1275
old_tree, tree, output)
1276
self.addCleanup(diff_obj.finish)
1277
self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1278
old_path, new_path = diff_obj._prepare_files('file-id', 'oldname',
1280
self.assertContainsRe(old_path, 'old/oldname$')
1281
self.assertContainsRe(new_path, 'new/newname$')
1282
self.assertFileEqual('oldcontent', old_path)
1283
self.assertFileEqual('newcontent', new_path)
1284
# make sure we can create files with the same parent directories
1285
diff_obj._prepare_files('file-id', 'oldname2', 'newname2')