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.diff import internal_diff, external_diff, show_diff_trees
24
from bzrlib.errors import BinaryFile, NoDiff
25
import bzrlib.osutils as osutils
26
import bzrlib.patiencediff
27
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
28
TestCaseInTempDir, TestSkipped)
31
class _UnicodeFilename(Feature):
32
"""Does the filesystem support Unicode filenames?"""
37
except UnicodeEncodeError:
39
except (IOError, OSError):
40
# The filesystem allows the Unicode filename but the file doesn't
44
# The filesystem allows the Unicode filename and the file exists,
48
UnicodeFilename = _UnicodeFilename()
51
class TestUnicodeFilename(TestCase):
53
def test_probe_passes(self):
54
"""UnicodeFilename._probe passes."""
55
# We can't test much more than that because the behaviour depends
57
UnicodeFilename._probe()
60
def udiff_lines(old, new, allow_binary=False):
62
internal_diff('old', old, 'new', new, output, allow_binary)
64
return output.readlines()
67
def external_udiff_lines(old, new, use_stringio=False):
69
# StringIO has no fileno, so it tests a different codepath
72
output = TemporaryFile()
74
external_diff('old', old, 'new', new, output, diff_opts=['-u'])
76
raise TestSkipped('external "diff" not present to test')
78
lines = output.readlines()
83
class TestDiff(TestCase):
85
def test_add_nl(self):
86
"""diff generates a valid diff for patches that add a newline"""
87
lines = udiff_lines(['boo'], ['boo\n'])
88
self.check_patch(lines)
89
self.assertEquals(lines[4], '\\ No newline at end of file\n')
90
## "expected no-nl, got %r" % lines[4]
92
def test_add_nl_2(self):
93
"""diff generates a valid diff for patches that change last line and
96
lines = udiff_lines(['boo'], ['goo\n'])
97
self.check_patch(lines)
98
self.assertEquals(lines[4], '\\ No newline at end of file\n')
99
## "expected no-nl, got %r" % lines[4]
101
def test_remove_nl(self):
102
"""diff generates a valid diff for patches that change last line and
105
lines = udiff_lines(['boo\n'], ['boo'])
106
self.check_patch(lines)
107
self.assertEquals(lines[5], '\\ No newline at end of file\n')
108
## "expected no-nl, got %r" % lines[5]
110
def check_patch(self, lines):
111
self.assert_(len(lines) > 1)
112
## "Not enough lines for a file header for patch:\n%s" % "".join(lines)
113
self.assert_(lines[0].startswith ('---'))
114
## 'No orig line for patch:\n%s' % "".join(lines)
115
self.assert_(lines[1].startswith ('+++'))
116
## 'No mod line for patch:\n%s' % "".join(lines)
117
self.assert_(len(lines) > 2)
118
## "No hunks for patch:\n%s" % "".join(lines)
119
self.assert_(lines[2].startswith('@@'))
120
## "No hunk header for patch:\n%s" % "".join(lines)
121
self.assert_('@@' in lines[2][2:])
122
## "Unterminated hunk header for patch:\n%s" % "".join(lines)
124
def test_binary_lines(self):
125
self.assertRaises(BinaryFile, udiff_lines, [1023 * 'a' + '\x00'], [])
126
self.assertRaises(BinaryFile, udiff_lines, [], [1023 * 'a' + '\x00'])
127
udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
128
udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
130
def test_external_diff(self):
131
lines = external_udiff_lines(['boo\n'], ['goo\n'])
132
self.check_patch(lines)
133
self.assertEqual('\n', lines[-1])
135
def test_external_diff_no_fileno(self):
136
# Make sure that we can handle not having a fileno, even
137
# if the diff is large
138
lines = external_udiff_lines(['boo\n']*10000,
141
self.check_patch(lines)
143
def test_external_diff_binary_lang_c(self):
145
for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
146
old_env[lang] = osutils.set_or_unset_env(lang, 'C')
148
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
149
# Older versions of diffutils say "Binary files", newer
150
# versions just say "Files".
151
self.assertContainsRe(lines[0],
152
'(Binary f|F)iles old and new differ\n')
153
self.assertEquals(lines[1:], ['\n'])
155
for lang, old_val in old_env.iteritems():
156
osutils.set_or_unset_env(lang, old_val)
158
def test_no_external_diff(self):
159
"""Check that NoDiff is raised when diff is not available"""
160
# Use os.environ['PATH'] to make sure no 'diff' command is available
161
orig_path = os.environ['PATH']
163
os.environ['PATH'] = ''
164
self.assertRaises(NoDiff, external_diff,
165
'old', ['boo\n'], 'new', ['goo\n'],
166
StringIO(), diff_opts=['-u'])
168
os.environ['PATH'] = orig_path
170
def test_internal_diff_default(self):
171
# Default internal diff encoding is utf8
173
internal_diff(u'old_\xb5', ['old_text\n'],
174
u'new_\xe5', ['new_text\n'], output)
175
lines = output.getvalue().splitlines(True)
176
self.check_patch(lines)
177
self.assertEquals(['--- old_\xc2\xb5\n',
178
'+++ new_\xc3\xa5\n',
186
def test_internal_diff_utf8(self):
188
internal_diff(u'old_\xb5', ['old_text\n'],
189
u'new_\xe5', ['new_text\n'], output,
190
path_encoding='utf8')
191
lines = output.getvalue().splitlines(True)
192
self.check_patch(lines)
193
self.assertEquals(['--- old_\xc2\xb5\n',
194
'+++ new_\xc3\xa5\n',
202
def test_internal_diff_iso_8859_1(self):
204
internal_diff(u'old_\xb5', ['old_text\n'],
205
u'new_\xe5', ['new_text\n'], output,
206
path_encoding='iso-8859-1')
207
lines = output.getvalue().splitlines(True)
208
self.check_patch(lines)
209
self.assertEquals(['--- old_\xb5\n',
218
def test_internal_diff_returns_bytes(self):
220
output = StringIO.StringIO()
221
internal_diff(u'old_\xb5', ['old_text\n'],
222
u'new_\xe5', ['new_text\n'], output)
223
self.failUnless(isinstance(output.getvalue(), str),
224
'internal_diff should return bytestrings')
227
class TestDiffFiles(TestCaseInTempDir):
229
def test_external_diff_binary(self):
230
"""The output when using external diff should use diff's i18n error"""
231
# Make sure external_diff doesn't fail in the current LANG
232
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
234
cmd = ['diff', '-u', '--binary', 'old', 'new']
235
open('old', 'wb').write('\x00foobar\n')
236
open('new', 'wb').write('foo\x00bar\n')
237
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
238
stdin=subprocess.PIPE)
239
out, err = pipe.communicate()
240
# Diff returns '2' on Binary files.
241
self.assertEqual(2, pipe.returncode)
242
# We should output whatever diff tells us, plus a trailing newline
243
self.assertEqual(out.splitlines(True) + ['\n'], lines)
246
class TestShowDiffTreesHelper(TestCaseWithTransport):
247
"""Has a helper for running show_diff_trees"""
249
def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
251
if working_tree is not None:
252
extra_trees = (working_tree,)
255
show_diff_trees(tree1, tree2, output, specific_files=specific_files,
256
extra_trees=extra_trees, old_label='old/',
258
return output.getvalue()
261
class TestDiffDates(TestShowDiffTreesHelper):
264
super(TestDiffDates, self).setUp()
265
self.wt = self.make_branch_and_tree('.')
266
self.b = self.wt.branch
267
self.build_tree_contents([
268
('file1', 'file1 contents at rev 1\n'),
269
('file2', 'file2 contents at rev 1\n')
271
self.wt.add(['file1', 'file2'])
273
message='Revision 1',
274
timestamp=1143849600, # 2006-04-01 00:00:00 UTC
277
self.build_tree_contents([('file1', 'file1 contents at rev 2\n')])
279
message='Revision 2',
280
timestamp=1143936000, # 2006-04-02 00:00:00 UTC
283
self.build_tree_contents([('file2', 'file2 contents at rev 3\n')])
285
message='Revision 3',
286
timestamp=1144022400, # 2006-04-03 00:00:00 UTC
289
self.wt.remove(['file2'])
291
message='Revision 4',
292
timestamp=1144108800, # 2006-04-04 00:00:00 UTC
295
self.build_tree_contents([
296
('file1', 'file1 contents in working tree\n')
298
# set the date stamps for files in the working tree to known values
299
os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
301
def test_diff_rev_tree_working_tree(self):
302
output = self.get_diff(self.wt.basis_tree(), self.wt)
303
# note that the date for old/file1 is from rev 2 rather than from
304
# the basis revision (rev 4)
305
self.assertEqualDiff(output, '''\
306
=== modified file 'file1'
307
--- old/file1\t2006-04-02 00:00:00 +0000
308
+++ new/file1\t2006-04-05 00:00:00 +0000
310
-file1 contents at rev 2
311
+file1 contents in working tree
315
def test_diff_rev_tree_rev_tree(self):
316
tree1 = self.b.repository.revision_tree('rev-2')
317
tree2 = self.b.repository.revision_tree('rev-3')
318
output = self.get_diff(tree1, tree2)
319
self.assertEqualDiff(output, '''\
320
=== modified file 'file2'
321
--- old/file2\t2006-04-01 00:00:00 +0000
322
+++ new/file2\t2006-04-03 00:00:00 +0000
324
-file2 contents at rev 1
325
+file2 contents at rev 3
329
def test_diff_add_files(self):
330
tree1 = self.b.repository.revision_tree(None)
331
tree2 = self.b.repository.revision_tree('rev-1')
332
output = self.get_diff(tree1, tree2)
333
# the files have the epoch time stamp for the tree in which
335
self.assertEqualDiff(output, '''\
336
=== added file 'file1'
337
--- old/file1\t1970-01-01 00:00:00 +0000
338
+++ new/file1\t2006-04-01 00:00:00 +0000
340
+file1 contents at rev 1
342
=== added file 'file2'
343
--- old/file2\t1970-01-01 00:00:00 +0000
344
+++ new/file2\t2006-04-01 00:00:00 +0000
346
+file2 contents at rev 1
350
def test_diff_remove_files(self):
351
tree1 = self.b.repository.revision_tree('rev-3')
352
tree2 = self.b.repository.revision_tree('rev-4')
353
output = self.get_diff(tree1, tree2)
354
# the file has the epoch time stamp for the tree in which
356
self.assertEqualDiff(output, '''\
357
=== removed file 'file2'
358
--- old/file2\t2006-04-03 00:00:00 +0000
359
+++ new/file2\t1970-01-01 00:00:00 +0000
361
-file2 contents at rev 3
365
def test_show_diff_specified(self):
366
"""A working tree filename can be used to identify a file"""
367
self.wt.rename_one('file1', 'file1b')
368
old_tree = self.b.repository.revision_tree('rev-1')
369
new_tree = self.b.repository.revision_tree('rev-4')
370
out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
371
working_tree=self.wt)
372
self.assertContainsRe(out, 'file1\t')
374
def test_recursive_diff(self):
375
"""Children of directories are matched"""
378
self.wt.add(['dir1', 'dir2'])
379
self.wt.rename_one('file1', 'dir1/file1')
380
old_tree = self.b.repository.revision_tree('rev-1')
381
new_tree = self.b.repository.revision_tree('rev-4')
382
out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
383
working_tree=self.wt)
384
self.assertContainsRe(out, 'file1\t')
385
out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
386
working_tree=self.wt)
387
self.assertNotContainsRe(out, 'file1\t')
391
class TestShowDiffTrees(TestShowDiffTreesHelper):
392
"""Direct tests for show_diff_trees"""
394
def test_modified_file(self):
395
"""Test when a file is modified."""
396
tree = self.make_branch_and_tree('tree')
397
self.build_tree_contents([('tree/file', 'contents\n')])
398
tree.add(['file'], ['file-id'])
399
tree.commit('one', rev_id='rev-1')
401
self.build_tree_contents([('tree/file', 'new contents\n')])
402
diff = self.get_diff(tree.basis_tree(), tree)
403
self.assertContainsRe(diff, "=== modified file 'file'\n")
404
self.assertContainsRe(diff, '--- old/file\t')
405
self.assertContainsRe(diff, '\\+\\+\\+ new/file\t')
406
self.assertContainsRe(diff, '-contents\n'
409
def test_modified_file_in_renamed_dir(self):
410
"""Test when a file is modified in a renamed directory."""
411
tree = self.make_branch_and_tree('tree')
412
self.build_tree(['tree/dir/'])
413
self.build_tree_contents([('tree/dir/file', 'contents\n')])
414
tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
415
tree.commit('one', rev_id='rev-1')
417
tree.rename_one('dir', 'other')
418
self.build_tree_contents([('tree/other/file', 'new contents\n')])
419
diff = self.get_diff(tree.basis_tree(), tree)
420
self.assertContainsRe(diff, "=== renamed directory 'dir' => 'other'\n")
421
self.assertContainsRe(diff, "=== modified file 'other/file'\n")
422
# XXX: This is technically incorrect, because it used to be at another
423
# location. What to do?
424
self.assertContainsRe(diff, '--- old/dir/file\t')
425
self.assertContainsRe(diff, '\\+\\+\\+ new/other/file\t')
426
self.assertContainsRe(diff, '-contents\n'
429
def test_renamed_directory(self):
430
"""Test when only a directory is only renamed."""
431
tree = self.make_branch_and_tree('tree')
432
self.build_tree(['tree/dir/'])
433
self.build_tree_contents([('tree/dir/file', 'contents\n')])
434
tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
435
tree.commit('one', rev_id='rev-1')
437
tree.rename_one('dir', 'newdir')
438
diff = self.get_diff(tree.basis_tree(), tree)
439
# Renaming a directory should be a single "you renamed this dir" even
440
# when there are files inside.
441
self.assertEqual("=== renamed directory 'dir' => 'newdir'\n", diff)
443
def test_renamed_file(self):
444
"""Test when a file is only renamed."""
445
tree = self.make_branch_and_tree('tree')
446
self.build_tree_contents([('tree/file', 'contents\n')])
447
tree.add(['file'], ['file-id'])
448
tree.commit('one', rev_id='rev-1')
450
tree.rename_one('file', 'newname')
451
diff = self.get_diff(tree.basis_tree(), tree)
452
self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
453
# We shouldn't have a --- or +++ line, because there is no content
455
self.assertNotContainsRe(diff, '---')
457
def test_renamed_and_modified_file(self):
458
"""Test when a file is only renamed."""
459
tree = self.make_branch_and_tree('tree')
460
self.build_tree_contents([('tree/file', 'contents\n')])
461
tree.add(['file'], ['file-id'])
462
tree.commit('one', rev_id='rev-1')
464
tree.rename_one('file', 'newname')
465
self.build_tree_contents([('tree/newname', 'new contents\n')])
466
diff = self.get_diff(tree.basis_tree(), tree)
467
self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
468
self.assertContainsRe(diff, '--- old/file\t')
469
self.assertContainsRe(diff, '\\+\\+\\+ new/newname\t')
470
self.assertContainsRe(diff, '-contents\n'
473
def test_binary_unicode_filenames(self):
474
"""Test that contents of files are *not* encoded in UTF-8 when there
475
is a binary file in the diff.
477
# See https://bugs.launchpad.net/bugs/110092.
478
self.requireFeature(UnicodeFilename)
480
# This bug isn't triggered with cStringIO.
481
from StringIO import StringIO
482
tree = self.make_branch_and_tree('tree')
483
alpha, omega = u'\u03b1', u'\u03c9'
484
alpha_utf8, omega_utf8 = alpha.encode('utf8'), omega.encode('utf8')
485
self.build_tree_contents(
486
[('tree/' + alpha, chr(0)),
488
('The %s and the %s\n' % (alpha_utf8, omega_utf8)))])
489
tree.add([alpha], ['file-id'])
490
tree.add([omega], ['file-id-2'])
491
diff_content = StringIO()
492
show_diff_trees(tree.basis_tree(), tree, diff_content)
493
diff = diff_content.getvalue()
494
self.assertContainsRe(diff, r"=== added file '%s'" % alpha_utf8)
495
self.assertContainsRe(
496
diff, "Binary files a/%s.*and b/%s.* differ\n" % (alpha_utf8, alpha_utf8))
497
self.assertContainsRe(diff, r"=== added file '%s'" % omega_utf8)
498
self.assertContainsRe(diff, r"--- a/%s" % (omega_utf8,))
499
self.assertContainsRe(diff, r"\+\+\+ b/%s" % (omega_utf8,))
502
class TestPatienceDiffLib(TestCase):
504
def test_unique_lcs(self):
505
unique_lcs = bzrlib.patiencediff.unique_lcs
506
self.assertEquals(unique_lcs('', ''), [])
507
self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
508
self.assertEquals(unique_lcs('a', 'b'), [])
509
self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
510
self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
511
self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
512
self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
514
self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
516
def test_recurse_matches(self):
517
def test_one(a, b, matches):
519
bzrlib.patiencediff.recurse_matches(a, b, 0, 0, len(a), len(b),
521
self.assertEquals(test_matches, matches)
523
test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
524
[(0, 0), (2, 2), (4, 4)])
525
test_one(['a', 'c', 'b', 'a', 'c'], ['a', 'b', 'c'],
526
[(0, 0), (2, 1), (4, 2)])
528
# recurse_matches doesn't match non-unique
529
# lines surrounded by bogus text.
530
# The update has been done in patiencediff.SequenceMatcher instead
532
# This is what it could be
533
#test_one('aBccDe', 'abccde', [(0,0), (2,2), (3,3), (5,5)])
535
# This is what it currently gives:
536
test_one('aBccDe', 'abccde', [(0,0), (5,5)])
538
def test_matching_blocks(self):
539
def chk_blocks(a, b, expected_blocks):
540
# difflib always adds a signature of the total
541
# length, with no matching entries at the end
542
s = bzrlib.patiencediff.PatienceSequenceMatcher(None, a, b)
543
blocks = s.get_matching_blocks()
544
self.assertEquals((len(a), len(b), 0), blocks[-1])
545
self.assertEquals(expected_blocks, blocks[:-1])
547
# Some basic matching tests
548
chk_blocks('', '', [])
549
chk_blocks([], [], [])
550
chk_blocks('abcd', 'abcd', [(0, 0, 4)])
551
chk_blocks('abcd', 'abce', [(0, 0, 3)])
552
chk_blocks('eabc', 'abce', [(1, 0, 3)])
553
chk_blocks('eabce', 'abce', [(1, 0, 4)])
554
chk_blocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
555
chk_blocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
556
chk_blocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
557
# This may check too much, but it checks to see that
558
# a copied block stays attached to the previous section,
560
# difflib would tend to grab the trailing longest match
561
# which would make the diff not look right
562
chk_blocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
563
[(0, 0, 6), (6, 11, 10)])
565
# make sure it supports passing in lists
569
'how are you today?\n'],
571
'how are you today?\n'],
572
[(0, 0, 1), (2, 1, 1)])
574
# non unique lines surrounded by non-matching lines
576
chk_blocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
578
# But they only need to be locally unique
579
chk_blocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
581
# non unique blocks won't be matched
582
chk_blocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
584
# but locally unique ones will
585
chk_blocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
586
(5,4,1), (7,5,2), (10,8,1)])
588
chk_blocks('abbabbXd', 'cabbabxd', [(7,7,1)])
589
chk_blocks('abbabbbb', 'cabbabbc', [])
590
chk_blocks('bbbbbbbb', 'cbbbbbbc', [])
592
def test_opcodes(self):
593
def chk_ops(a, b, expected_codes):
594
s = bzrlib.patiencediff.PatienceSequenceMatcher(None, a, b)
595
self.assertEquals(expected_codes, s.get_opcodes())
599
chk_ops('abcd', 'abcd', [('equal', 0,4, 0,4)])
600
chk_ops('abcd', 'abce', [('equal', 0,3, 0,3),
601
('replace', 3,4, 3,4)
603
chk_ops('eabc', 'abce', [('delete', 0,1, 0,0),
607
chk_ops('eabce', 'abce', [('delete', 0,1, 0,0),
610
chk_ops('abcde', 'abXde', [('equal', 0,2, 0,2),
611
('replace', 2,3, 2,3),
614
chk_ops('abcde', 'abXYZde', [('equal', 0,2, 0,2),
615
('replace', 2,3, 2,5),
618
chk_ops('abde', 'abXYZde', [('equal', 0,2, 0,2),
619
('insert', 2,2, 2,5),
622
chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
623
[('equal', 0,6, 0,6),
624
('insert', 6,6, 6,11),
625
('equal', 6,16, 11,21)
630
, 'how are you today?\n'],
632
, 'how are you today?\n'],
633
[('equal', 0,1, 0,1),
634
('delete', 1,2, 1,1),
637
chk_ops('aBccDe', 'abccde',
638
[('equal', 0,1, 0,1),
639
('replace', 1,5, 1,5),
642
chk_ops('aBcDec', 'abcdec',
643
[('equal', 0,1, 0,1),
644
('replace', 1,2, 1,2),
646
('replace', 3,4, 3,4),
649
chk_ops('aBcdEcdFg', 'abcdecdfg',
650
[('equal', 0,1, 0,1),
651
('replace', 1,8, 1,8),
654
chk_ops('aBcdEeXcdFg', 'abcdecdfg',
655
[('equal', 0,1, 0,1),
656
('replace', 1,2, 1,2),
658
('delete', 4,5, 4,4),
660
('delete', 6,7, 5,5),
662
('replace', 9,10, 7,8),
663
('equal', 10,11, 8,9)
666
def test_multiple_ranges(self):
667
# There was an earlier bug where we used a bad set of ranges,
668
# this triggers that specific bug, to make sure it doesn't regress
669
def chk_blocks(a, b, expected_blocks):
670
# difflib always adds a signature of the total
671
# length, with no matching entries at the end
672
s = bzrlib.patiencediff.PatienceSequenceMatcher(None, a, b)
673
blocks = s.get_matching_blocks()
675
self.assertEquals(x, (len(a), len(b), 0))
676
self.assertEquals(expected_blocks, blocks)
678
chk_blocks('abcdefghijklmnop'
679
, 'abcXghiYZQRSTUVWXYZijklmnop'
680
, [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
682
chk_blocks('ABCd efghIjk L'
683
, 'AxyzBCn mo pqrstuvwI1 2 L'
684
, [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
686
# These are rot13 code snippets.
688
trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
690
gnxrf_netf = ['svyr*']
691
gnxrf_bcgvbaf = ['ab-erphefr']
693
qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr):
694
sebz omeyvo.nqq vzcbeg fzneg_nqq, nqq_ercbegre_cevag, nqq_ercbegre_ahyy
696
ercbegre = nqq_ercbegre_ahyy
698
ercbegre = nqq_ercbegre_cevag
699
fzneg_nqq(svyr_yvfg, abg ab_erphefr, ercbegre)
702
pynff pzq_zxqve(Pbzznaq):
703
'''.splitlines(True), '''\
704
trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
706
--qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl
709
gnxrf_netf = ['svyr*']
710
gnxrf_bcgvbaf = ['ab-erphefr', 'qel-eha']
712
qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr, qel_eha=Snyfr):
717
# Guvf vf cbvagyrff, ohg V'q engure abg envfr na reebe
718
npgvba = omeyvo.nqq.nqq_npgvba_ahyy
720
npgvba = omeyvo.nqq.nqq_npgvba_cevag
722
npgvba = omeyvo.nqq.nqq_npgvba_nqq
724
npgvba = omeyvo.nqq.nqq_npgvba_nqq_naq_cevag
726
omeyvo.nqq.fzneg_nqq(svyr_yvfg, abg ab_erphefr, npgvba)
729
pynff pzq_zxqve(Pbzznaq):
731
, [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
733
def test_patience_unified_diff(self):
734
txt_a = ['hello there\n',
736
'how are you today?\n']
737
txt_b = ['hello there\n',
738
'how are you today?\n']
739
unified_diff = bzrlib.patiencediff.unified_diff
740
psm = bzrlib.patiencediff.PatienceSequenceMatcher
741
self.assertEquals([ '--- \n',
746
' how are you today?\n'
748
, list(unified_diff(txt_a, txt_b,
749
sequencematcher=psm)))
750
txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
751
txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
752
# This is the result with LongestCommonSubstring matching
753
self.assertEquals(['--- \n',
755
'@@ -1,6 +1,11 @@\n',
767
, list(unified_diff(txt_a, txt_b)))
768
# And the patience diff
769
self.assertEquals(['--- \n',
771
'@@ -4,6 +4,11 @@\n',
784
, list(unified_diff(txt_a, txt_b,
785
sequencematcher=psm)))
788
class TestPatienceDiffLibFiles(TestCaseInTempDir):
790
def test_patience_unified_diff_files(self):
791
txt_a = ['hello there\n',
793
'how are you today?\n']
794
txt_b = ['hello there\n',
795
'how are you today?\n']
796
open('a1', 'wb').writelines(txt_a)
797
open('b1', 'wb').writelines(txt_b)
799
unified_diff_files = bzrlib.patiencediff.unified_diff_files
800
psm = bzrlib.patiencediff.PatienceSequenceMatcher
801
self.assertEquals(['--- a1 \n',
806
' how are you today?\n',
808
, list(unified_diff_files('a1', 'b1',
809
sequencematcher=psm)))
811
txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
812
txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
813
open('a2', 'wb').writelines(txt_a)
814
open('b2', 'wb').writelines(txt_b)
816
# This is the result with LongestCommonSubstring matching
817
self.assertEquals(['--- a2 \n',
819
'@@ -1,6 +1,11 @@\n',
831
, list(unified_diff_files('a2', 'b2')))
833
# And the patience diff
834
self.assertEquals(['--- a2 \n',
836
'@@ -4,6 +4,11 @@\n',
849
, list(unified_diff_files('a2', 'b2',
850
sequencematcher=psm)))