23
23
from bzrlib.diff import internal_diff, external_diff, show_diff_trees
24
24
from bzrlib.errors import BinaryFile, NoDiff
25
import bzrlib.osutils as osutils
26
25
import bzrlib.patiencediff
27
import bzrlib._patiencediff_py
28
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
26
from bzrlib.tests import (TestCase, TestCaseWithTransport,
29
27
TestCaseInTempDir, TestSkipped)
32
class _CompiledPatienceDiffFeature(Feature):
36
import bzrlib._patiencediff_c
41
def feature_name(self):
42
return 'bzrlib._patiencediff_c'
44
CompiledPatienceDiffFeature = _CompiledPatienceDiffFeature()
47
class _UnicodeFilename(Feature):
48
"""Does the filesystem support Unicode filenames?"""
53
except UnicodeEncodeError:
55
except (IOError, OSError):
56
# The filesystem allows the Unicode filename but the file doesn't
60
# The filesystem allows the Unicode filename and the file exists,
64
UnicodeFilename = _UnicodeFilename()
67
class TestUnicodeFilename(TestCase):
69
def test_probe_passes(self):
70
"""UnicodeFilename._probe passes."""
71
# We can't test much more than that because the behaviour depends
73
UnicodeFilename._probe()
76
30
def udiff_lines(old, new, allow_binary=False):
77
31
output = StringIO()
78
32
internal_diff('old', old, 'new', new, output, allow_binary)
403
350
self.assertNotContainsRe(out, 'file1\t')
407
class TestShowDiffTrees(TestShowDiffTreesHelper):
408
"""Direct tests for show_diff_trees"""
410
def test_modified_file(self):
411
"""Test when a file is modified."""
412
tree = self.make_branch_and_tree('tree')
413
self.build_tree_contents([('tree/file', 'contents\n')])
414
tree.add(['file'], ['file-id'])
415
tree.commit('one', rev_id='rev-1')
417
self.build_tree_contents([('tree/file', 'new contents\n')])
418
diff = self.get_diff(tree.basis_tree(), tree)
419
self.assertContainsRe(diff, "=== modified file 'file'\n")
420
self.assertContainsRe(diff, '--- old/file\t')
421
self.assertContainsRe(diff, '\\+\\+\\+ new/file\t')
422
self.assertContainsRe(diff, '-contents\n'
425
def test_modified_file_in_renamed_dir(self):
426
"""Test when a file is modified in a renamed directory."""
427
tree = self.make_branch_and_tree('tree')
428
self.build_tree(['tree/dir/'])
429
self.build_tree_contents([('tree/dir/file', 'contents\n')])
430
tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
431
tree.commit('one', rev_id='rev-1')
433
tree.rename_one('dir', 'other')
434
self.build_tree_contents([('tree/other/file', 'new contents\n')])
435
diff = self.get_diff(tree.basis_tree(), tree)
436
self.assertContainsRe(diff, "=== renamed directory 'dir' => 'other'\n")
437
self.assertContainsRe(diff, "=== modified file 'other/file'\n")
438
# XXX: This is technically incorrect, because it used to be at another
439
# location. What to do?
440
self.assertContainsRe(diff, '--- old/dir/file\t')
441
self.assertContainsRe(diff, '\\+\\+\\+ new/other/file\t')
442
self.assertContainsRe(diff, '-contents\n'
445
def test_renamed_directory(self):
446
"""Test when only a directory is only renamed."""
447
tree = self.make_branch_and_tree('tree')
448
self.build_tree(['tree/dir/'])
449
self.build_tree_contents([('tree/dir/file', 'contents\n')])
450
tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
451
tree.commit('one', rev_id='rev-1')
453
tree.rename_one('dir', 'newdir')
454
diff = self.get_diff(tree.basis_tree(), tree)
455
# Renaming a directory should be a single "you renamed this dir" even
456
# when there are files inside.
457
self.assertEqual("=== renamed directory 'dir' => 'newdir'\n", diff)
459
def test_renamed_file(self):
460
"""Test when a file is only renamed."""
461
tree = self.make_branch_and_tree('tree')
462
self.build_tree_contents([('tree/file', 'contents\n')])
463
tree.add(['file'], ['file-id'])
464
tree.commit('one', rev_id='rev-1')
466
tree.rename_one('file', 'newname')
467
diff = self.get_diff(tree.basis_tree(), tree)
468
self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
469
# We shouldn't have a --- or +++ line, because there is no content
471
self.assertNotContainsRe(diff, '---')
473
def test_renamed_and_modified_file(self):
474
"""Test when a file is only renamed."""
475
tree = self.make_branch_and_tree('tree')
476
self.build_tree_contents([('tree/file', 'contents\n')])
477
tree.add(['file'], ['file-id'])
478
tree.commit('one', rev_id='rev-1')
480
tree.rename_one('file', 'newname')
481
self.build_tree_contents([('tree/newname', 'new contents\n')])
482
diff = self.get_diff(tree.basis_tree(), tree)
483
self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
484
self.assertContainsRe(diff, '--- old/file\t')
485
self.assertContainsRe(diff, '\\+\\+\\+ new/newname\t')
486
self.assertContainsRe(diff, '-contents\n'
489
def test_binary_unicode_filenames(self):
490
"""Test that contents of files are *not* encoded in UTF-8 when there
491
is a binary file in the diff.
493
# See https://bugs.launchpad.net/bugs/110092.
494
self.requireFeature(UnicodeFilename)
496
# This bug isn't triggered with cStringIO.
497
from StringIO import StringIO
498
tree = self.make_branch_and_tree('tree')
499
alpha, omega = u'\u03b1', u'\u03c9'
500
alpha_utf8, omega_utf8 = alpha.encode('utf8'), omega.encode('utf8')
501
self.build_tree_contents(
502
[('tree/' + alpha, chr(0)),
504
('The %s and the %s\n' % (alpha_utf8, omega_utf8)))])
505
tree.add([alpha], ['file-id'])
506
tree.add([omega], ['file-id-2'])
507
diff_content = StringIO()
508
show_diff_trees(tree.basis_tree(), tree, diff_content)
509
diff = diff_content.getvalue()
510
self.assertContainsRe(diff, r"=== added file '%s'" % alpha_utf8)
511
self.assertContainsRe(
512
diff, "Binary files a/%s.*and b/%s.* differ\n" % (alpha_utf8, alpha_utf8))
513
self.assertContainsRe(diff, r"=== added file '%s'" % omega_utf8)
514
self.assertContainsRe(diff, r"--- a/%s" % (omega_utf8,))
515
self.assertContainsRe(diff, r"\+\+\+ b/%s" % (omega_utf8,))
517
def test_unicode_filename(self):
518
"""Test when the filename are unicode."""
519
self.requireFeature(UnicodeFilename)
521
alpha, omega = u'\u03b1', u'\u03c9'
522
autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
524
tree = self.make_branch_and_tree('tree')
525
self.build_tree_contents([('tree/ren_'+alpha, 'contents\n')])
526
tree.add(['ren_'+alpha], ['file-id-2'])
527
self.build_tree_contents([('tree/del_'+alpha, 'contents\n')])
528
tree.add(['del_'+alpha], ['file-id-3'])
529
self.build_tree_contents([('tree/mod_'+alpha, 'contents\n')])
530
tree.add(['mod_'+alpha], ['file-id-4'])
532
tree.commit('one', rev_id='rev-1')
534
tree.rename_one('ren_'+alpha, 'ren_'+omega)
535
tree.remove('del_'+alpha)
536
self.build_tree_contents([('tree/add_'+alpha, 'contents\n')])
537
tree.add(['add_'+alpha], ['file-id'])
538
self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
540
diff = self.get_diff(tree.basis_tree(), tree)
541
self.assertContainsRe(diff,
542
"=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
543
self.assertContainsRe(diff, "=== added file 'add_%s'"%autf8)
544
self.assertContainsRe(diff, "=== modified file 'mod_%s'"%autf8)
545
self.assertContainsRe(diff, "=== removed file 'del_%s'"%autf8)
547
353
class TestPatienceDiffLib(TestCase):
550
super(TestPatienceDiffLib, self).setUp()
551
self._unique_lcs = bzrlib._patiencediff_py.unique_lcs_py
552
self._recurse_matches = bzrlib._patiencediff_py.recurse_matches_py
553
self._PatienceSequenceMatcher = \
554
bzrlib._patiencediff_py.PatienceSequenceMatcher_py
556
355
def test_unique_lcs(self):
557
unique_lcs = self._unique_lcs
356
unique_lcs = bzrlib.patiencediff.unique_lcs
558
357
self.assertEquals(unique_lcs('', ''), [])
559
self.assertEquals(unique_lcs('', 'a'), [])
560
self.assertEquals(unique_lcs('a', ''), [])
561
358
self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
562
359
self.assertEquals(unique_lcs('a', 'b'), [])
563
360
self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
570
367
def test_recurse_matches(self):
571
368
def test_one(a, b, matches):
572
369
test_matches = []
573
self._recurse_matches(
574
a, b, 0, 0, len(a), len(b), test_matches, 10)
370
bzrlib.patiencediff.recurse_matches(a, b, 0, 0, len(a), len(b),
575
372
self.assertEquals(test_matches, matches)
577
374
test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
578
375
[(0, 0), (2, 2), (4, 4)])
579
376
test_one(['a', 'c', 'b', 'a', 'c'], ['a', 'b', 'c'],
580
377
[(0, 0), (2, 1), (4, 2)])
581
# Even though 'bc' is not unique globally, and is surrounded by
582
# non-matching lines, we should still match, because they are locally
584
test_one('abcdbce', 'afbcgdbce', [(0,0), (1, 2), (2, 3), (3, 5),
585
(4, 6), (5, 7), (6, 8)])
587
379
# recurse_matches doesn't match non-unique
588
380
# lines surrounded by bogus text.
726
514
('equal', 10,11, 8,9)
729
def test_grouped_opcodes(self):
730
def chk_ops(a, b, expected_codes, n=3):
731
s = self._PatienceSequenceMatcher(None, a, b)
732
self.assertEquals(expected_codes, list(s.get_grouped_opcodes(n)))
736
chk_ops('abc', '', [[('delete', 0,3, 0,0)]])
737
chk_ops('', 'abc', [[('insert', 0,0, 0,3)]])
738
chk_ops('abcd', 'abcd', [])
739
chk_ops('abcd', 'abce', [[('equal', 0,3, 0,3),
740
('replace', 3,4, 3,4)
742
chk_ops('eabc', 'abce', [[('delete', 0,1, 0,0),
746
chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
747
[[('equal', 3,6, 3,6),
748
('insert', 6,6, 6,11),
749
('equal', 6,9, 11,14)
751
chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
752
[[('equal', 2,6, 2,6),
753
('insert', 6,6, 6,11),
754
('equal', 6,10, 11,15)
756
chk_ops('Xabcdef', 'abcdef',
757
[[('delete', 0,1, 0,0),
760
chk_ops('abcdef', 'abcdefX',
761
[[('equal', 3,6, 3,6),
766
517
def test_multiple_ranges(self):
767
518
# There was an earlier bug where we used a bad set of ranges,
768
519
# this triggers that specific bug, to make sure it doesn't regress
769
520
def chk_blocks(a, b, expected_blocks):
770
521
# difflib always adds a signature of the total
771
522
# length, with no matching entries at the end
772
s = self._PatienceSequenceMatcher(None, a, b)
523
s = bzrlib.patiencediff.PatienceSequenceMatcher(None, a, b)
773
524
blocks = s.get_matching_blocks()
775
526
self.assertEquals(x, (len(a), len(b), 0))
967
700
, list(unified_diff_files('a2', 'b2',
968
701
sequencematcher=psm)))
971
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
973
_test_needs_features = [CompiledPatienceDiffFeature]
976
super(TestPatienceDiffLibFiles_c, self).setUp()
977
import bzrlib._patiencediff_c
978
self._PatienceSequenceMatcher = \
979
bzrlib._patiencediff_c.PatienceSequenceMatcher_c
982
class TestUsingCompiledIfAvailable(TestCase):
984
def test_PatienceSequenceMatcher(self):
985
if CompiledPatienceDiffFeature.available():
986
from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
987
self.assertIs(PatienceSequenceMatcher_c,
988
bzrlib.patiencediff.PatienceSequenceMatcher)
990
from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
991
self.assertIs(PatienceSequenceMatcher_py,
992
bzrlib.patiencediff.PatienceSequenceMatcher)
994
def test_unique_lcs(self):
995
if CompiledPatienceDiffFeature.available():
996
from bzrlib._patiencediff_c import unique_lcs_c
997
self.assertIs(unique_lcs_c,
998
bzrlib.patiencediff.unique_lcs)
1000
from bzrlib._patiencediff_py import unique_lcs_py
1001
self.assertIs(unique_lcs_py,
1002
bzrlib.patiencediff.unique_lcs)
1004
def test_recurse_matches(self):
1005
if CompiledPatienceDiffFeature.available():
1006
from bzrlib._patiencediff_c import recurse_matches_c
1007
self.assertIs(recurse_matches_c,
1008
bzrlib.patiencediff.recurse_matches)
1010
from bzrlib._patiencediff_py import recurse_matches_py
1011
self.assertIs(recurse_matches_py,
1012
bzrlib.patiencediff.recurse_matches)