395
def test_show_diff_specified(self):
396
"""A working tree filename can be used to identify a file"""
397
self.wt.rename_one('file1', 'file1b')
398
old_tree = self.b.repository.revision_tree('rev-1')
399
new_tree = self.b.repository.revision_tree('rev-4')
400
out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
401
working_tree=self.wt)
402
self.assertContainsRe(out, 'file1\t')
404
def test_recursive_diff(self):
405
"""Children of directories are matched"""
408
self.wt.add(['dir1', 'dir2'])
409
self.wt.rename_one('file1', 'dir1/file1')
410
old_tree = self.b.repository.revision_tree('rev-1')
411
new_tree = self.b.repository.revision_tree('rev-4')
412
out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
413
working_tree=self.wt)
414
self.assertContainsRe(out, 'file1\t')
415
out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
416
working_tree=self.wt)
417
self.assertNotContainsRe(out, 'file1\t')
421
class TestShowDiffTrees(TestShowDiffTreesHelper):
422
"""Direct tests for show_diff_trees"""
424
def test_modified_file(self):
425
"""Test when a file is modified."""
426
tree = self.make_branch_and_tree('tree')
427
self.build_tree_contents([('tree/file', 'contents\n')])
428
tree.add(['file'], ['file-id'])
429
tree.commit('one', rev_id='rev-1')
431
self.build_tree_contents([('tree/file', 'new contents\n')])
432
diff = self.get_diff(tree.basis_tree(), tree)
433
self.assertContainsRe(diff, "=== modified file 'file'\n")
434
self.assertContainsRe(diff, '--- old/file\t')
435
self.assertContainsRe(diff, '\\+\\+\\+ new/file\t')
436
self.assertContainsRe(diff, '-contents\n'
439
def test_modified_file_in_renamed_dir(self):
440
"""Test when a file is modified in a renamed directory."""
441
tree = self.make_branch_and_tree('tree')
442
self.build_tree(['tree/dir/'])
443
self.build_tree_contents([('tree/dir/file', 'contents\n')])
444
tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
445
tree.commit('one', rev_id='rev-1')
447
tree.rename_one('dir', 'other')
448
self.build_tree_contents([('tree/other/file', 'new contents\n')])
449
diff = self.get_diff(tree.basis_tree(), tree)
450
self.assertContainsRe(diff, "=== renamed directory 'dir' => 'other'\n")
451
self.assertContainsRe(diff, "=== modified file 'other/file'\n")
452
# XXX: This is technically incorrect, because it used to be at another
453
# location. What to do?
454
self.assertContainsRe(diff, '--- old/dir/file\t')
455
self.assertContainsRe(diff, '\\+\\+\\+ new/other/file\t')
456
self.assertContainsRe(diff, '-contents\n'
459
def test_renamed_directory(self):
460
"""Test when only a directory is only renamed."""
461
tree = self.make_branch_and_tree('tree')
462
self.build_tree(['tree/dir/'])
463
self.build_tree_contents([('tree/dir/file', 'contents\n')])
464
tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
465
tree.commit('one', rev_id='rev-1')
467
tree.rename_one('dir', 'newdir')
468
diff = self.get_diff(tree.basis_tree(), tree)
469
# Renaming a directory should be a single "you renamed this dir" even
470
# when there are files inside.
471
self.assertEqual("=== renamed directory 'dir' => 'newdir'\n", diff)
473
def test_renamed_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
diff = self.get_diff(tree.basis_tree(), tree)
482
self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
483
# We shouldn't have a --- or +++ line, because there is no content
485
self.assertNotContainsRe(diff, '---')
487
def test_renamed_and_modified_file(self):
488
"""Test when a file is only renamed."""
489
tree = self.make_branch_and_tree('tree')
490
self.build_tree_contents([('tree/file', 'contents\n')])
491
tree.add(['file'], ['file-id'])
492
tree.commit('one', rev_id='rev-1')
494
tree.rename_one('file', 'newname')
495
self.build_tree_contents([('tree/newname', 'new contents\n')])
496
diff = self.get_diff(tree.basis_tree(), tree)
497
self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
498
self.assertContainsRe(diff, '--- old/file\t')
499
self.assertContainsRe(diff, '\\+\\+\\+ new/newname\t')
500
self.assertContainsRe(diff, '-contents\n'
504
def test_internal_diff_exec_property(self):
505
tree = self.make_branch_and_tree('tree')
507
tt = transform.TreeTransform(tree)
508
tt.new_file('a', tt.root, 'contents\n', 'a-id', True)
509
tt.new_file('b', tt.root, 'contents\n', 'b-id', False)
510
tt.new_file('c', tt.root, 'contents\n', 'c-id', True)
511
tt.new_file('d', tt.root, 'contents\n', 'd-id', False)
512
tt.new_file('e', tt.root, 'contents\n', 'control-e-id', True)
513
tt.new_file('f', tt.root, 'contents\n', 'control-f-id', False)
515
tree.commit('one', rev_id='rev-1')
517
tt = transform.TreeTransform(tree)
518
tt.set_executability(False, tt.trans_id_file_id('a-id'))
519
tt.set_executability(True, tt.trans_id_file_id('b-id'))
520
tt.set_executability(False, tt.trans_id_file_id('c-id'))
521
tt.set_executability(True, tt.trans_id_file_id('d-id'))
523
tree.rename_one('c', 'new-c')
524
tree.rename_one('d', 'new-d')
526
diff = self.get_diff(tree.basis_tree(), tree)
528
self.assertContainsRe(diff, r"file 'a'.*\(properties changed:.*\+x to -x.*\)")
529
self.assertContainsRe(diff, r"file 'b'.*\(properties changed:.*-x to \+x.*\)")
530
self.assertContainsRe(diff, r"file 'c'.*\(properties changed:.*\+x to -x.*\)")
531
self.assertContainsRe(diff, r"file 'd'.*\(properties changed:.*-x to \+x.*\)")
532
self.assertNotContainsRe(diff, r"file 'e'")
533
self.assertNotContainsRe(diff, r"file 'f'")
536
def test_binary_unicode_filenames(self):
537
"""Test that contents of files are *not* encoded in UTF-8 when there
538
is a binary file in the diff.
540
# See https://bugs.launchpad.net/bugs/110092.
541
self.requireFeature(tests.UnicodeFilenameFeature)
543
# This bug isn't triggered with cStringIO.
544
from StringIO import StringIO
545
tree = self.make_branch_and_tree('tree')
546
alpha, omega = u'\u03b1', u'\u03c9'
547
alpha_utf8, omega_utf8 = alpha.encode('utf8'), omega.encode('utf8')
548
self.build_tree_contents(
549
[('tree/' + alpha, chr(0)),
551
('The %s and the %s\n' % (alpha_utf8, omega_utf8)))])
552
tree.add([alpha], ['file-id'])
553
tree.add([omega], ['file-id-2'])
554
diff_content = StringIO()
555
show_diff_trees(tree.basis_tree(), tree, diff_content)
556
diff = diff_content.getvalue()
557
self.assertContainsRe(diff, r"=== added file '%s'" % alpha_utf8)
558
self.assertContainsRe(
559
diff, "Binary files a/%s.*and b/%s.* differ\n" % (alpha_utf8, alpha_utf8))
560
self.assertContainsRe(diff, r"=== added file '%s'" % omega_utf8)
561
self.assertContainsRe(diff, r"--- a/%s" % (omega_utf8,))
562
self.assertContainsRe(diff, r"\+\+\+ b/%s" % (omega_utf8,))
564
def test_unicode_filename(self):
565
"""Test when the filename are unicode."""
566
self.requireFeature(tests.UnicodeFilenameFeature)
568
alpha, omega = u'\u03b1', u'\u03c9'
569
autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
571
tree = self.make_branch_and_tree('tree')
572
self.build_tree_contents([('tree/ren_'+alpha, 'contents\n')])
573
tree.add(['ren_'+alpha], ['file-id-2'])
574
self.build_tree_contents([('tree/del_'+alpha, 'contents\n')])
575
tree.add(['del_'+alpha], ['file-id-3'])
576
self.build_tree_contents([('tree/mod_'+alpha, 'contents\n')])
577
tree.add(['mod_'+alpha], ['file-id-4'])
579
tree.commit('one', rev_id='rev-1')
581
tree.rename_one('ren_'+alpha, 'ren_'+omega)
582
tree.remove('del_'+alpha)
583
self.build_tree_contents([('tree/add_'+alpha, 'contents\n')])
584
tree.add(['add_'+alpha], ['file-id'])
585
self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
587
diff = self.get_diff(tree.basis_tree(), tree)
588
self.assertContainsRe(diff,
589
"=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
590
self.assertContainsRe(diff, "=== added file 'add_%s'"%autf8)
591
self.assertContainsRe(diff, "=== modified file 'mod_%s'"%autf8)
592
self.assertContainsRe(diff, "=== removed file 'del_%s'"%autf8)
595
class DiffWasIs(DiffPath):
597
def diff(self, file_id, old_path, new_path, old_kind, new_kind):
598
self.to_file.write('was: ')
599
self.to_file.write(self.old_tree.get_file(file_id).read())
600
self.to_file.write('is: ')
601
self.to_file.write(self.new_tree.get_file(file_id).read())
605
class TestDiffTree(TestCaseWithTransport):
608
TestCaseWithTransport.setUp(self)
609
self.old_tree = self.make_branch_and_tree('old-tree')
610
self.old_tree.lock_write()
611
self.addCleanup(self.old_tree.unlock)
612
self.new_tree = self.make_branch_and_tree('new-tree')
613
self.new_tree.lock_write()
614
self.addCleanup(self.new_tree.unlock)
615
self.differ = DiffTree(self.old_tree, self.new_tree, StringIO())
617
def test_diff_text(self):
618
self.build_tree_contents([('old-tree/olddir/',),
619
('old-tree/olddir/oldfile', 'old\n')])
620
self.old_tree.add('olddir')
621
self.old_tree.add('olddir/oldfile', 'file-id')
622
self.build_tree_contents([('new-tree/newdir/',),
623
('new-tree/newdir/newfile', 'new\n')])
624
self.new_tree.add('newdir')
625
self.new_tree.add('newdir/newfile', 'file-id')
626
differ = DiffText(self.old_tree, self.new_tree, StringIO())
627
differ.diff_text('file-id', None, 'old label', 'new label')
629
'--- old label\n+++ new label\n@@ -1,1 +0,0 @@\n-old\n\n',
630
differ.to_file.getvalue())
631
differ.to_file.seek(0)
632
differ.diff_text(None, 'file-id', 'old label', 'new label')
634
'--- old label\n+++ new label\n@@ -0,0 +1,1 @@\n+new\n\n',
635
differ.to_file.getvalue())
636
differ.to_file.seek(0)
637
differ.diff_text('file-id', 'file-id', 'old label', 'new label')
639
'--- old label\n+++ new label\n@@ -1,1 +1,1 @@\n-old\n+new\n\n',
640
differ.to_file.getvalue())
642
def test_diff_deletion(self):
643
self.build_tree_contents([('old-tree/file', 'contents'),
644
('new-tree/file', 'contents')])
645
self.old_tree.add('file', 'file-id')
646
self.new_tree.add('file', 'file-id')
647
os.unlink('new-tree/file')
648
self.differ.show_diff(None)
649
self.assertContainsRe(self.differ.to_file.getvalue(), '-contents')
651
def test_diff_creation(self):
652
self.build_tree_contents([('old-tree/file', 'contents'),
653
('new-tree/file', 'contents')])
654
self.old_tree.add('file', 'file-id')
655
self.new_tree.add('file', 'file-id')
656
os.unlink('old-tree/file')
657
self.differ.show_diff(None)
658
self.assertContainsRe(self.differ.to_file.getvalue(), '\+contents')
660
def test_diff_symlink(self):
661
differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
662
differ.diff_symlink('old target', None)
663
self.assertEqual("=== target was 'old target'\n",
664
differ.to_file.getvalue())
666
differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
667
differ.diff_symlink(None, 'new target')
668
self.assertEqual("=== target is 'new target'\n",
669
differ.to_file.getvalue())
671
differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
672
differ.diff_symlink('old target', 'new target')
673
self.assertEqual("=== target changed 'old target' => 'new target'\n",
674
differ.to_file.getvalue())
677
self.build_tree_contents([('old-tree/olddir/',),
678
('old-tree/olddir/oldfile', 'old\n')])
679
self.old_tree.add('olddir')
680
self.old_tree.add('olddir/oldfile', 'file-id')
681
self.build_tree_contents([('new-tree/newdir/',),
682
('new-tree/newdir/newfile', 'new\n')])
683
self.new_tree.add('newdir')
684
self.new_tree.add('newdir/newfile', 'file-id')
685
self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
686
self.assertContainsRe(
687
self.differ.to_file.getvalue(),
688
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
689
' \@\@\n-old\n\+new\n\n')
691
def test_diff_kind_change(self):
692
self.requireFeature(tests.SymlinkFeature)
693
self.build_tree_contents([('old-tree/olddir/',),
694
('old-tree/olddir/oldfile', 'old\n')])
695
self.old_tree.add('olddir')
696
self.old_tree.add('olddir/oldfile', 'file-id')
697
self.build_tree(['new-tree/newdir/'])
698
os.symlink('new', 'new-tree/newdir/newfile')
699
self.new_tree.add('newdir')
700
self.new_tree.add('newdir/newfile', 'file-id')
701
self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
702
self.assertContainsRe(
703
self.differ.to_file.getvalue(),
704
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+0,0'
706
self.assertContainsRe(self.differ.to_file.getvalue(),
707
"=== target is 'new'\n")
709
def test_diff_directory(self):
710
self.build_tree(['new-tree/new-dir/'])
711
self.new_tree.add('new-dir', 'new-dir-id')
712
self.differ.diff('new-dir-id', None, 'new-dir')
713
self.assertEqual(self.differ.to_file.getvalue(), '')
715
def create_old_new(self):
716
self.build_tree_contents([('old-tree/olddir/',),
717
('old-tree/olddir/oldfile', 'old\n')])
718
self.old_tree.add('olddir')
719
self.old_tree.add('olddir/oldfile', 'file-id')
720
self.build_tree_contents([('new-tree/newdir/',),
721
('new-tree/newdir/newfile', 'new\n')])
722
self.new_tree.add('newdir')
723
self.new_tree.add('newdir/newfile', 'file-id')
725
def test_register_diff(self):
726
self.create_old_new()
727
old_diff_factories = DiffTree.diff_factories
728
DiffTree.diff_factories=old_diff_factories[:]
729
DiffTree.diff_factories.insert(0, DiffWasIs.from_diff_tree)
731
differ = DiffTree(self.old_tree, self.new_tree, StringIO())
733
DiffTree.diff_factories = old_diff_factories
734
differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
735
self.assertNotContainsRe(
736
differ.to_file.getvalue(),
737
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
738
' \@\@\n-old\n\+new\n\n')
739
self.assertContainsRe(differ.to_file.getvalue(),
740
'was: old\nis: new\n')
742
def test_extra_factories(self):
743
self.create_old_new()
744
differ = DiffTree(self.old_tree, self.new_tree, StringIO(),
745
extra_factories=[DiffWasIs.from_diff_tree])
746
differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
747
self.assertNotContainsRe(
748
differ.to_file.getvalue(),
749
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
750
' \@\@\n-old\n\+new\n\n')
751
self.assertContainsRe(differ.to_file.getvalue(),
752
'was: old\nis: new\n')
754
def test_alphabetical_order(self):
755
self.build_tree(['new-tree/a-file'])
756
self.new_tree.add('a-file')
757
self.build_tree(['old-tree/b-file'])
758
self.old_tree.add('b-file')
759
self.differ.show_diff(None)
760
self.assertContainsRe(self.differ.to_file.getvalue(),
761
'.*a-file(.|\n)*b-file')
249
764
class TestPatienceDiffLib(TestCase):
767
super(TestPatienceDiffLib, self).setUp()
768
self._unique_lcs = bzrlib._patiencediff_py.unique_lcs_py
769
self._recurse_matches = bzrlib._patiencediff_py.recurse_matches_py
770
self._PatienceSequenceMatcher = \
771
bzrlib._patiencediff_py.PatienceSequenceMatcher_py
773
def test_diff_unicode_string(self):
774
a = ''.join([unichr(i) for i in range(4000, 4500, 3)])
775
b = ''.join([unichr(i) for i in range(4300, 4800, 2)])
776
sm = self._PatienceSequenceMatcher(None, a, b)
777
mb = sm.get_matching_blocks()
778
self.assertEquals(35, len(mb))
251
780
def test_unique_lcs(self):
252
unique_lcs = bzrlib.patiencediff.unique_lcs
781
unique_lcs = self._unique_lcs
253
782
self.assertEquals(unique_lcs('', ''), [])
783
self.assertEquals(unique_lcs('', 'a'), [])
784
self.assertEquals(unique_lcs('a', ''), [])
254
785
self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
255
786
self.assertEquals(unique_lcs('a', 'b'), [])
256
787
self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
282
818
# This is what it currently gives:
283
819
test_one('aBccDe', 'abccde', [(0,0), (5,5)])
821
def assertDiffBlocks(self, a, b, expected_blocks):
822
"""Check that the sequence matcher returns the correct blocks.
824
:param a: A sequence to match
825
:param b: Another sequence to match
826
:param expected_blocks: The expected output, not including the final
827
matching block (len(a), len(b), 0)
829
matcher = self._PatienceSequenceMatcher(None, a, b)
830
blocks = matcher.get_matching_blocks()
832
self.assertEqual((len(a), len(b), 0), last)
833
self.assertEqual(expected_blocks, blocks)
285
835
def test_matching_blocks(self):
286
def chk_blocks(a, b, expected_blocks):
287
# difflib always adds a signature of the total
288
# length, with no matching entries at the end
289
s = bzrlib.patiencediff.PatienceSequenceMatcher(None, a, b)
290
blocks = s.get_matching_blocks()
291
self.assertEquals((len(a), len(b), 0), blocks[-1])
292
self.assertEquals(expected_blocks, blocks[:-1])
294
836
# Some basic matching tests
295
chk_blocks('', '', [])
296
chk_blocks([], [], [])
297
chk_blocks('abcd', 'abcd', [(0, 0, 4)])
298
chk_blocks('abcd', 'abce', [(0, 0, 3)])
299
chk_blocks('eabc', 'abce', [(1, 0, 3)])
300
chk_blocks('eabce', 'abce', [(1, 0, 4)])
301
chk_blocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
302
chk_blocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
303
chk_blocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
304
# This may check too much, but it checks to see that
837
self.assertDiffBlocks('', '', [])
838
self.assertDiffBlocks([], [], [])
839
self.assertDiffBlocks('abc', '', [])
840
self.assertDiffBlocks('', 'abc', [])
841
self.assertDiffBlocks('abcd', 'abcd', [(0, 0, 4)])
842
self.assertDiffBlocks('abcd', 'abce', [(0, 0, 3)])
843
self.assertDiffBlocks('eabc', 'abce', [(1, 0, 3)])
844
self.assertDiffBlocks('eabce', 'abce', [(1, 0, 4)])
845
self.assertDiffBlocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
846
self.assertDiffBlocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
847
self.assertDiffBlocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
848
# This may check too much, but it checks to see that
305
849
# a copied block stays attached to the previous section,
306
850
# not the later one.
307
851
# difflib would tend to grab the trailing longest match
308
852
# which would make the diff not look right
309
chk_blocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
310
[(0, 0, 6), (6, 11, 10)])
853
self.assertDiffBlocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
854
[(0, 0, 6), (6, 11, 10)])
312
856
# make sure it supports passing in lists
857
self.assertDiffBlocks(
314
858
['hello there\n',
316
860
'how are you today?\n'],
321
865
# non unique lines surrounded by non-matching lines
323
chk_blocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
867
self.assertDiffBlocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
325
869
# But they only need to be locally unique
326
chk_blocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
870
self.assertDiffBlocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
328
872
# non unique blocks won't be matched
329
chk_blocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
873
self.assertDiffBlocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
331
875
# but locally unique ones will
332
chk_blocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
876
self.assertDiffBlocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
333
877
(5,4,1), (7,5,2), (10,8,1)])
335
chk_blocks('abbabbXd', 'cabbabxd', [(7,7,1)])
336
chk_blocks('abbabbbb', 'cabbabbc', [])
337
chk_blocks('bbbbbbbb', 'cbbbbbbc', [])
879
self.assertDiffBlocks('abbabbXd', 'cabbabxd', [(7,7,1)])
880
self.assertDiffBlocks('abbabbbb', 'cabbabbc', [])
881
self.assertDiffBlocks('bbbbbbbb', 'cbbbbbbc', [])
883
def test_matching_blocks_tuples(self):
884
# Some basic matching tests
885
self.assertDiffBlocks([], [], [])
886
self.assertDiffBlocks([('a',), ('b',), ('c,')], [], [])
887
self.assertDiffBlocks([], [('a',), ('b',), ('c,')], [])
888
self.assertDiffBlocks([('a',), ('b',), ('c,')],
889
[('a',), ('b',), ('c,')],
891
self.assertDiffBlocks([('a',), ('b',), ('c,')],
892
[('a',), ('b',), ('d,')],
894
self.assertDiffBlocks([('d',), ('b',), ('c,')],
895
[('a',), ('b',), ('c,')],
897
self.assertDiffBlocks([('d',), ('a',), ('b',), ('c,')],
898
[('a',), ('b',), ('c,')],
900
self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
901
[('a', 'b'), ('c', 'X'), ('e', 'f')],
902
[(0, 0, 1), (2, 2, 1)])
903
self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
904
[('a', 'b'), ('c', 'dX'), ('e', 'f')],
905
[(0, 0, 1), (2, 2, 1)])
339
907
def test_opcodes(self):
340
908
def chk_ops(a, b, expected_codes):
341
s = bzrlib.patiencediff.PatienceSequenceMatcher(None, a, b)
909
s = self._PatienceSequenceMatcher(None, a, b)
342
910
self.assertEquals(expected_codes, s.get_opcodes())
344
912
chk_ops('', '', [])
345
913
chk_ops([], [], [])
914
chk_ops('abc', '', [('delete', 0,3, 0,0)])
915
chk_ops('', 'abc', [('insert', 0,0, 0,3)])
346
916
chk_ops('abcd', 'abcd', [('equal', 0,4, 0,4)])
347
917
chk_ops('abcd', 'abce', [('equal', 0,3, 0,3),
348
918
('replace', 3,4, 3,4)
596
1225
, list(unified_diff_files('a2', 'b2',
597
1226
sequencematcher=psm)))
1229
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1231
_test_needs_features = [CompiledPatienceDiffFeature]
1234
super(TestPatienceDiffLibFiles_c, self).setUp()
1235
import bzrlib._patiencediff_c
1236
self._PatienceSequenceMatcher = \
1237
bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1240
class TestUsingCompiledIfAvailable(TestCase):
1242
def test_PatienceSequenceMatcher(self):
1243
if CompiledPatienceDiffFeature.available():
1244
from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1245
self.assertIs(PatienceSequenceMatcher_c,
1246
bzrlib.patiencediff.PatienceSequenceMatcher)
1248
from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
1249
self.assertIs(PatienceSequenceMatcher_py,
1250
bzrlib.patiencediff.PatienceSequenceMatcher)
1252
def test_unique_lcs(self):
1253
if CompiledPatienceDiffFeature.available():
1254
from bzrlib._patiencediff_c import unique_lcs_c
1255
self.assertIs(unique_lcs_c,
1256
bzrlib.patiencediff.unique_lcs)
1258
from bzrlib._patiencediff_py import unique_lcs_py
1259
self.assertIs(unique_lcs_py,
1260
bzrlib.patiencediff.unique_lcs)
1262
def test_recurse_matches(self):
1263
if CompiledPatienceDiffFeature.available():
1264
from bzrlib._patiencediff_c import recurse_matches_c
1265
self.assertIs(recurse_matches_c,
1266
bzrlib.patiencediff.recurse_matches)
1268
from bzrlib._patiencediff_py import recurse_matches_py
1269
self.assertIs(recurse_matches_py,
1270
bzrlib.patiencediff.recurse_matches)
1273
class TestDiffFromTool(TestCaseWithTransport):
1275
def test_from_string(self):
1276
diff_obj = DiffFromTool.from_string('diff', None, None, None)
1277
self.addCleanup(diff_obj.finish)
1278
self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
1279
diff_obj.command_template)
1281
def test_from_string_u5(self):
1282
diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1283
self.addCleanup(diff_obj.finish)
1284
self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
1285
diff_obj.command_template)
1286
self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1287
diff_obj._get_command('old-path', 'new-path'))
1289
def test_execute(self):
1291
diff_obj = DiffFromTool(['python', '-c',
1292
'print "%(old_path)s %(new_path)s"'],
1294
self.addCleanup(diff_obj.finish)
1295
diff_obj._execute('old', 'new')
1296
self.assertEqual(output.getvalue().rstrip(), 'old new')
1298
def test_excute_missing(self):
1299
diff_obj = DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1301
self.addCleanup(diff_obj.finish)
1302
e = self.assertRaises(ExecutableMissing, diff_obj._execute, 'old',
1304
self.assertEqual('a-tool-which-is-unlikely-to-exist could not be found'
1305
' on this machine', str(e))
1307
def test_prepare_files_creates_paths_readable_by_windows_tool(self):
1308
self.requireFeature(AttribFeature)
1310
tree = self.make_branch_and_tree('tree')
1311
self.build_tree_contents([('tree/file', 'content')])
1312
tree.add('file', 'file-id')
1313
tree.commit('old tree')
1315
self.addCleanup(tree.unlock)
1316
diff_obj = DiffFromTool(['python', '-c',
1317
'print "%(old_path)s %(new_path)s"'],
1319
diff_obj._prepare_files('file-id', 'file', 'file')
1320
self.assertReadableByAttrib(diff_obj._root, 'old\\file', r'old\\file')
1321
self.assertReadableByAttrib(diff_obj._root, 'new\\file', r'new\\file')
1323
def assertReadableByAttrib(self, cwd, relpath, regex):
1324
proc = subprocess.Popen(['attrib', relpath],
1325
stdout=subprocess.PIPE,
1328
result = proc.stdout.read()
1329
self.assertContainsRe(result, regex)
1331
def test_prepare_files(self):
1333
tree = self.make_branch_and_tree('tree')
1334
self.build_tree_contents([('tree/oldname', 'oldcontent')])
1335
self.build_tree_contents([('tree/oldname2', 'oldcontent2')])
1336
tree.add('oldname', 'file-id')
1337
tree.add('oldname2', 'file2-id')
1338
tree.commit('old tree', timestamp=0)
1339
tree.rename_one('oldname', 'newname')
1340
tree.rename_one('oldname2', 'newname2')
1341
self.build_tree_contents([('tree/newname', 'newcontent')])
1342
self.build_tree_contents([('tree/newname2', 'newcontent2')])
1343
old_tree = tree.basis_tree()
1344
old_tree.lock_read()
1345
self.addCleanup(old_tree.unlock)
1347
self.addCleanup(tree.unlock)
1348
diff_obj = DiffFromTool(['python', '-c',
1349
'print "%(old_path)s %(new_path)s"'],
1350
old_tree, tree, output)
1351
self.addCleanup(diff_obj.finish)
1352
self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1353
old_path, new_path = diff_obj._prepare_files('file-id', 'oldname',
1355
self.assertContainsRe(old_path, 'old/oldname$')
1356
self.assertEqual(0, os.stat(old_path).st_mtime)
1357
self.assertContainsRe(new_path, 'new/newname$')
1358
self.assertFileEqual('oldcontent', old_path)
1359
self.assertFileEqual('newcontent', new_path)
1360
if osutils.host_os_dereferences_symlinks():
1361
self.assertTrue(os.path.samefile('tree/newname', new_path))
1362
# make sure we can create files with the same parent directories
1363
diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')