555
498
self.assertContainsRe(diff, r"--- a/%s" % (omega_utf8,))
556
499
self.assertContainsRe(diff, r"\+\+\+ b/%s" % (omega_utf8,))
558
def test_unicode_filename(self):
559
"""Test when the filename are unicode."""
560
self.requireFeature(tests.UnicodeFilenameFeature)
562
alpha, omega = u'\u03b1', u'\u03c9'
563
autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
565
tree = self.make_branch_and_tree('tree')
566
self.build_tree_contents([('tree/ren_'+alpha, 'contents\n')])
567
tree.add(['ren_'+alpha], ['file-id-2'])
568
self.build_tree_contents([('tree/del_'+alpha, 'contents\n')])
569
tree.add(['del_'+alpha], ['file-id-3'])
570
self.build_tree_contents([('tree/mod_'+alpha, 'contents\n')])
571
tree.add(['mod_'+alpha], ['file-id-4'])
573
tree.commit('one', rev_id='rev-1')
575
tree.rename_one('ren_'+alpha, 'ren_'+omega)
576
tree.remove('del_'+alpha)
577
self.build_tree_contents([('tree/add_'+alpha, 'contents\n')])
578
tree.add(['add_'+alpha], ['file-id'])
579
self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
581
diff = self.get_diff(tree.basis_tree(), tree)
582
self.assertContainsRe(diff,
583
"=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
584
self.assertContainsRe(diff, "=== added file 'add_%s'"%autf8)
585
self.assertContainsRe(diff, "=== modified file 'mod_%s'"%autf8)
586
self.assertContainsRe(diff, "=== removed file 'del_%s'"%autf8)
589
class DiffWasIs(DiffPath):
591
def diff(self, file_id, old_path, new_path, old_kind, new_kind):
592
self.to_file.write('was: ')
593
self.to_file.write(self.old_tree.get_file(file_id).read())
594
self.to_file.write('is: ')
595
self.to_file.write(self.new_tree.get_file(file_id).read())
599
class TestDiffTree(TestCaseWithTransport):
602
TestCaseWithTransport.setUp(self)
603
self.old_tree = self.make_branch_and_tree('old-tree')
604
self.old_tree.lock_write()
605
self.addCleanup(self.old_tree.unlock)
606
self.new_tree = self.make_branch_and_tree('new-tree')
607
self.new_tree.lock_write()
608
self.addCleanup(self.new_tree.unlock)
609
self.differ = DiffTree(self.old_tree, self.new_tree, StringIO())
611
def test_diff_text(self):
612
self.build_tree_contents([('old-tree/olddir/',),
613
('old-tree/olddir/oldfile', 'old\n')])
614
self.old_tree.add('olddir')
615
self.old_tree.add('olddir/oldfile', 'file-id')
616
self.build_tree_contents([('new-tree/newdir/',),
617
('new-tree/newdir/newfile', 'new\n')])
618
self.new_tree.add('newdir')
619
self.new_tree.add('newdir/newfile', 'file-id')
620
differ = DiffText(self.old_tree, self.new_tree, StringIO())
621
differ.diff_text('file-id', None, 'old label', 'new label')
623
'--- old label\n+++ new label\n@@ -1,1 +0,0 @@\n-old\n\n',
624
differ.to_file.getvalue())
625
differ.to_file.seek(0)
626
differ.diff_text(None, 'file-id', 'old label', 'new label')
628
'--- old label\n+++ new label\n@@ -0,0 +1,1 @@\n+new\n\n',
629
differ.to_file.getvalue())
630
differ.to_file.seek(0)
631
differ.diff_text('file-id', 'file-id', 'old label', 'new label')
633
'--- old label\n+++ new label\n@@ -1,1 +1,1 @@\n-old\n+new\n\n',
634
differ.to_file.getvalue())
636
def test_diff_deletion(self):
637
self.build_tree_contents([('old-tree/file', 'contents'),
638
('new-tree/file', 'contents')])
639
self.old_tree.add('file', 'file-id')
640
self.new_tree.add('file', 'file-id')
641
os.unlink('new-tree/file')
642
self.differ.show_diff(None)
643
self.assertContainsRe(self.differ.to_file.getvalue(), '-contents')
645
def test_diff_creation(self):
646
self.build_tree_contents([('old-tree/file', 'contents'),
647
('new-tree/file', 'contents')])
648
self.old_tree.add('file', 'file-id')
649
self.new_tree.add('file', 'file-id')
650
os.unlink('old-tree/file')
651
self.differ.show_diff(None)
652
self.assertContainsRe(self.differ.to_file.getvalue(), '\+contents')
654
def test_diff_symlink(self):
655
differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
656
differ.diff_symlink('old target', None)
657
self.assertEqual("=== target was 'old target'\n",
658
differ.to_file.getvalue())
660
differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
661
differ.diff_symlink(None, 'new target')
662
self.assertEqual("=== target is 'new target'\n",
663
differ.to_file.getvalue())
665
differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
666
differ.diff_symlink('old target', 'new target')
667
self.assertEqual("=== target changed 'old target' => 'new target'\n",
668
differ.to_file.getvalue())
671
self.build_tree_contents([('old-tree/olddir/',),
672
('old-tree/olddir/oldfile', 'old\n')])
673
self.old_tree.add('olddir')
674
self.old_tree.add('olddir/oldfile', 'file-id')
675
self.build_tree_contents([('new-tree/newdir/',),
676
('new-tree/newdir/newfile', 'new\n')])
677
self.new_tree.add('newdir')
678
self.new_tree.add('newdir/newfile', 'file-id')
679
self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
680
self.assertContainsRe(
681
self.differ.to_file.getvalue(),
682
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
683
' \@\@\n-old\n\+new\n\n')
685
def test_diff_kind_change(self):
686
self.requireFeature(tests.SymlinkFeature)
687
self.build_tree_contents([('old-tree/olddir/',),
688
('old-tree/olddir/oldfile', 'old\n')])
689
self.old_tree.add('olddir')
690
self.old_tree.add('olddir/oldfile', 'file-id')
691
self.build_tree(['new-tree/newdir/'])
692
os.symlink('new', 'new-tree/newdir/newfile')
693
self.new_tree.add('newdir')
694
self.new_tree.add('newdir/newfile', 'file-id')
695
self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
696
self.assertContainsRe(
697
self.differ.to_file.getvalue(),
698
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+0,0'
700
self.assertContainsRe(self.differ.to_file.getvalue(),
701
"=== target is u'new'\n")
703
def test_diff_directory(self):
704
self.build_tree(['new-tree/new-dir/'])
705
self.new_tree.add('new-dir', 'new-dir-id')
706
self.differ.diff('new-dir-id', None, 'new-dir')
707
self.assertEqual(self.differ.to_file.getvalue(), '')
709
def create_old_new(self):
710
self.build_tree_contents([('old-tree/olddir/',),
711
('old-tree/olddir/oldfile', 'old\n')])
712
self.old_tree.add('olddir')
713
self.old_tree.add('olddir/oldfile', 'file-id')
714
self.build_tree_contents([('new-tree/newdir/',),
715
('new-tree/newdir/newfile', 'new\n')])
716
self.new_tree.add('newdir')
717
self.new_tree.add('newdir/newfile', 'file-id')
719
def test_register_diff(self):
720
self.create_old_new()
721
old_diff_factories = DiffTree.diff_factories
722
DiffTree.diff_factories=old_diff_factories[:]
723
DiffTree.diff_factories.insert(0, DiffWasIs.from_diff_tree)
725
differ = DiffTree(self.old_tree, self.new_tree, StringIO())
727
DiffTree.diff_factories = old_diff_factories
728
differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
729
self.assertNotContainsRe(
730
differ.to_file.getvalue(),
731
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
732
' \@\@\n-old\n\+new\n\n')
733
self.assertContainsRe(differ.to_file.getvalue(),
734
'was: old\nis: new\n')
736
def test_extra_factories(self):
737
self.create_old_new()
738
differ = DiffTree(self.old_tree, self.new_tree, StringIO(),
739
extra_factories=[DiffWasIs.from_diff_tree])
740
differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
741
self.assertNotContainsRe(
742
differ.to_file.getvalue(),
743
r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
744
' \@\@\n-old\n\+new\n\n')
745
self.assertContainsRe(differ.to_file.getvalue(),
746
'was: old\nis: new\n')
748
def test_alphabetical_order(self):
749
self.build_tree(['new-tree/a-file'])
750
self.new_tree.add('a-file')
751
self.build_tree(['old-tree/b-file'])
752
self.old_tree.add('b-file')
753
self.differ.show_diff(None)
754
self.assertContainsRe(self.differ.to_file.getvalue(),
755
'.*a-file(.|\n)*b-file')
758
502
class TestPatienceDiffLib(TestCase):
761
super(TestPatienceDiffLib, self).setUp()
762
self._unique_lcs = bzrlib._patiencediff_py.unique_lcs_py
763
self._recurse_matches = bzrlib._patiencediff_py.recurse_matches_py
764
self._PatienceSequenceMatcher = \
765
bzrlib._patiencediff_py.PatienceSequenceMatcher_py
767
def test_diff_unicode_string(self):
768
a = ''.join([unichr(i) for i in range(4000, 4500, 3)])
769
b = ''.join([unichr(i) for i in range(4300, 4800, 2)])
770
sm = self._PatienceSequenceMatcher(None, a, b)
771
mb = sm.get_matching_blocks()
772
self.assertEquals(35, len(mb))
774
504
def test_unique_lcs(self):
775
unique_lcs = self._unique_lcs
505
unique_lcs = bzrlib.patiencediff.unique_lcs
776
506
self.assertEquals(unique_lcs('', ''), [])
777
self.assertEquals(unique_lcs('', 'a'), [])
778
self.assertEquals(unique_lcs('a', ''), [])
779
507
self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
780
508
self.assertEquals(unique_lcs('a', 'b'), [])
781
509
self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
782
510
self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
783
511
self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
784
self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
512
self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
786
514
self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
788
516
def test_recurse_matches(self):
789
517
def test_one(a, b, matches):
790
518
test_matches = []
791
self._recurse_matches(
792
a, b, 0, 0, len(a), len(b), test_matches, 10)
519
bzrlib.patiencediff.recurse_matches(a, b, 0, 0, len(a), len(b),
793
521
self.assertEquals(test_matches, matches)
795
523
test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
796
524
[(0, 0), (2, 2), (4, 4)])
797
525
test_one(['a', 'c', 'b', 'a', 'c'], ['a', 'b', 'c'],
798
526
[(0, 0), (2, 1), (4, 2)])
799
# Even though 'bc' is not unique globally, and is surrounded by
800
# non-matching lines, we should still match, because they are locally
802
test_one('abcdbce', 'afbcgdbce', [(0,0), (1, 2), (2, 3), (3, 5),
803
(4, 6), (5, 7), (6, 8)])
805
# recurse_matches doesn't match non-unique
528
# recurse_matches doesn't match non-unique
806
529
# lines surrounded by bogus text.
807
530
# The update has been done in patiencediff.SequenceMatcher instead
812
535
# This is what it currently gives:
813
536
test_one('aBccDe', 'abccde', [(0,0), (5,5)])
815
def assertDiffBlocks(self, a, b, expected_blocks):
816
"""Check that the sequence matcher returns the correct blocks.
818
:param a: A sequence to match
819
:param b: Another sequence to match
820
:param expected_blocks: The expected output, not including the final
821
matching block (len(a), len(b), 0)
823
matcher = self._PatienceSequenceMatcher(None, a, b)
824
blocks = matcher.get_matching_blocks()
826
self.assertEqual((len(a), len(b), 0), last)
827
self.assertEqual(expected_blocks, blocks)
829
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])
830
547
# Some basic matching tests
831
self.assertDiffBlocks('', '', [])
832
self.assertDiffBlocks([], [], [])
833
self.assertDiffBlocks('abc', '', [])
834
self.assertDiffBlocks('', 'abc', [])
835
self.assertDiffBlocks('abcd', 'abcd', [(0, 0, 4)])
836
self.assertDiffBlocks('abcd', 'abce', [(0, 0, 3)])
837
self.assertDiffBlocks('eabc', 'abce', [(1, 0, 3)])
838
self.assertDiffBlocks('eabce', 'abce', [(1, 0, 4)])
839
self.assertDiffBlocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
840
self.assertDiffBlocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
841
self.assertDiffBlocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
842
# This may check too much, but it checks to see that
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
843
558
# a copied block stays attached to the previous section,
844
559
# not the later one.
845
560
# difflib would tend to grab the trailing longest match
846
561
# which would make the diff not look right
847
self.assertDiffBlocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
848
[(0, 0, 6), (6, 11, 10)])
562
chk_blocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
563
[(0, 0, 6), (6, 11, 10)])
850
565
# make sure it supports passing in lists
851
self.assertDiffBlocks(
852
567
['hello there\n',
854
569
'how are you today?\n'],
859
574
# non unique lines surrounded by non-matching lines
861
self.assertDiffBlocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
576
chk_blocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
863
578
# But they only need to be locally unique
864
self.assertDiffBlocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
579
chk_blocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
866
581
# non unique blocks won't be matched
867
self.assertDiffBlocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
582
chk_blocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
869
584
# but locally unique ones will
870
self.assertDiffBlocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
585
chk_blocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
871
586
(5,4,1), (7,5,2), (10,8,1)])
873
self.assertDiffBlocks('abbabbXd', 'cabbabxd', [(7,7,1)])
874
self.assertDiffBlocks('abbabbbb', 'cabbabbc', [])
875
self.assertDiffBlocks('bbbbbbbb', 'cbbbbbbc', [])
877
def test_matching_blocks_tuples(self):
878
# Some basic matching tests
879
self.assertDiffBlocks([], [], [])
880
self.assertDiffBlocks([('a',), ('b',), ('c,')], [], [])
881
self.assertDiffBlocks([], [('a',), ('b',), ('c,')], [])
882
self.assertDiffBlocks([('a',), ('b',), ('c,')],
883
[('a',), ('b',), ('c,')],
885
self.assertDiffBlocks([('a',), ('b',), ('c,')],
886
[('a',), ('b',), ('d,')],
888
self.assertDiffBlocks([('d',), ('b',), ('c,')],
889
[('a',), ('b',), ('c,')],
891
self.assertDiffBlocks([('d',), ('a',), ('b',), ('c,')],
892
[('a',), ('b',), ('c,')],
894
self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
895
[('a', 'b'), ('c', 'X'), ('e', 'f')],
896
[(0, 0, 1), (2, 2, 1)])
897
self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
898
[('a', 'b'), ('c', 'dX'), ('e', 'f')],
899
[(0, 0, 1), (2, 2, 1)])
588
chk_blocks('abbabbXd', 'cabbabxd', [(7,7,1)])
589
chk_blocks('abbabbbb', 'cabbabbc', [])
590
chk_blocks('bbbbbbbb', 'cbbbbbbc', [])
901
592
def test_opcodes(self):
902
593
def chk_ops(a, b, expected_codes):
903
s = self._PatienceSequenceMatcher(None, a, b)
594
s = bzrlib.patiencediff.PatienceSequenceMatcher(None, a, b)
904
595
self.assertEquals(expected_codes, s.get_opcodes())
906
597
chk_ops('', '', [])
907
598
chk_ops([], [], [])
908
chk_ops('abc', '', [('delete', 0,3, 0,0)])
909
chk_ops('', 'abc', [('insert', 0,0, 0,3)])
910
599
chk_ops('abcd', 'abcd', [('equal', 0,4, 0,4)])
911
600
chk_ops('abcd', 'abce', [('equal', 0,3, 0,3),
912
601
('replace', 3,4, 3,4)
974
663
('equal', 10,11, 8,9)
977
def test_grouped_opcodes(self):
978
def chk_ops(a, b, expected_codes, n=3):
979
s = self._PatienceSequenceMatcher(None, a, b)
980
self.assertEquals(expected_codes, list(s.get_grouped_opcodes(n)))
984
chk_ops('abc', '', [[('delete', 0,3, 0,0)]])
985
chk_ops('', 'abc', [[('insert', 0,0, 0,3)]])
986
chk_ops('abcd', 'abcd', [])
987
chk_ops('abcd', 'abce', [[('equal', 0,3, 0,3),
988
('replace', 3,4, 3,4)
990
chk_ops('eabc', 'abce', [[('delete', 0,1, 0,0),
994
chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
995
[[('equal', 3,6, 3,6),
996
('insert', 6,6, 6,11),
997
('equal', 6,9, 11,14)
999
chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
1000
[[('equal', 2,6, 2,6),
1001
('insert', 6,6, 6,11),
1002
('equal', 6,10, 11,15)
1004
chk_ops('Xabcdef', 'abcdef',
1005
[[('delete', 0,1, 0,0),
1008
chk_ops('abcdef', 'abcdefX',
1009
[[('equal', 3,6, 3,6),
1010
('insert', 6,6, 6,7)
1014
666
def test_multiple_ranges(self):
1015
667
# There was an earlier bug where we used a bad set of ranges,
1016
668
# this triggers that specific bug, to make sure it doesn't regress
1017
self.assertDiffBlocks('abcdefghijklmnop',
1018
'abcXghiYZQRSTUVWXYZijklmnop',
1019
[(0, 0, 3), (6, 4, 3), (9, 20, 7)])
1021
self.assertDiffBlocks('ABCd efghIjk L',
1022
'AxyzBCn mo pqrstuvwI1 2 L',
1023
[(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
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)])
1025
686
# These are rot13 code snippets.
1026
self.assertDiffBlocks('''\
1027
688
trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
1029
690
gnxrf_netf = ['svyr*']
1030
691
gnxrf_bcgvbaf = ['ab-erphefr']
1032
693
qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr):
1033
694
sebz omeyvo.nqq vzcbeg fzneg_nqq, nqq_ercbegre_cevag, nqq_ercbegre_ahyy
1240
849
, list(unified_diff_files('a2', 'b2',
1241
850
sequencematcher=psm)))
1244
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1246
_test_needs_features = [compiled_patiencediff_feature]
1249
super(TestPatienceDiffLibFiles_c, self).setUp()
1250
import bzrlib._patiencediff_c
1251
self._PatienceSequenceMatcher = \
1252
bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1255
class TestUsingCompiledIfAvailable(TestCase):
1257
def test_PatienceSequenceMatcher(self):
1258
if compiled_patiencediff_feature.available():
1259
from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1260
self.assertIs(PatienceSequenceMatcher_c,
1261
bzrlib.patiencediff.PatienceSequenceMatcher)
1263
from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
1264
self.assertIs(PatienceSequenceMatcher_py,
1265
bzrlib.patiencediff.PatienceSequenceMatcher)
1267
def test_unique_lcs(self):
1268
if compiled_patiencediff_feature.available():
1269
from bzrlib._patiencediff_c import unique_lcs_c
1270
self.assertIs(unique_lcs_c,
1271
bzrlib.patiencediff.unique_lcs)
1273
from bzrlib._patiencediff_py import unique_lcs_py
1274
self.assertIs(unique_lcs_py,
1275
bzrlib.patiencediff.unique_lcs)
1277
def test_recurse_matches(self):
1278
if compiled_patiencediff_feature.available():
1279
from bzrlib._patiencediff_c import recurse_matches_c
1280
self.assertIs(recurse_matches_c,
1281
bzrlib.patiencediff.recurse_matches)
1283
from bzrlib._patiencediff_py import recurse_matches_py
1284
self.assertIs(recurse_matches_py,
1285
bzrlib.patiencediff.recurse_matches)
1288
class TestDiffFromTool(TestCaseWithTransport):
1290
def test_from_string(self):
1291
diff_obj = DiffFromTool.from_string('diff', None, None, None)
1292
self.addCleanup(diff_obj.finish)
1293
self.assertEqual(['diff', '@old_path', '@new_path'],
1294
diff_obj.command_template)
1296
def test_from_string_u5(self):
1297
diff_obj = DiffFromTool.from_string('diff "-u 5"', None, None, None)
1298
self.addCleanup(diff_obj.finish)
1299
self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
1300
diff_obj.command_template)
1301
self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1302
diff_obj._get_command('old-path', 'new-path'))
1304
def test_from_string_path_with_backslashes(self):
1305
self.requireFeature(BackslashDirSeparatorFeature)
1306
tool = 'C:\\Tools\\Diff.exe'
1307
diff_obj = DiffFromTool.from_string(tool, None, None, None)
1308
self.addCleanup(diff_obj.finish)
1309
self.assertEqual(['C:\\Tools\\Diff.exe', '@old_path', '@new_path'],
1310
diff_obj.command_template)
1311
self.assertEqual(['C:\\Tools\\Diff.exe', 'old-path', 'new-path'],
1312
diff_obj._get_command('old-path', 'new-path'))
1314
def test_execute(self):
1316
diff_obj = DiffFromTool(['python', '-c',
1317
'print "@old_path @new_path"'],
1319
self.addCleanup(diff_obj.finish)
1320
diff_obj._execute('old', 'new')
1321
self.assertEqual(output.getvalue().rstrip(), 'old new')
1323
def test_excute_missing(self):
1324
diff_obj = DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1326
self.addCleanup(diff_obj.finish)
1327
e = self.assertRaises(ExecutableMissing, diff_obj._execute, 'old',
1329
self.assertEqual('a-tool-which-is-unlikely-to-exist could not be found'
1330
' on this machine', str(e))
1332
def test_prepare_files_creates_paths_readable_by_windows_tool(self):
1333
self.requireFeature(AttribFeature)
1335
tree = self.make_branch_and_tree('tree')
1336
self.build_tree_contents([('tree/file', 'content')])
1337
tree.add('file', 'file-id')
1338
tree.commit('old tree')
1340
self.addCleanup(tree.unlock)
1341
basis_tree = tree.basis_tree()
1342
basis_tree.lock_read()
1343
self.addCleanup(basis_tree.unlock)
1344
diff_obj = DiffFromTool(['python', '-c',
1345
'print "@old_path @new_path"'],
1346
basis_tree, tree, output)
1347
diff_obj._prepare_files('file-id', 'file', 'file')
1348
# The old content should be readonly
1349
self.assertReadableByAttrib(diff_obj._root, 'old\\file',
1351
# The new content should use the tree object, not a 'new' file anymore
1352
self.assertEndsWith(tree.basedir, 'work/tree')
1353
self.assertReadableByAttrib(tree.basedir, 'file', r'work\\tree\\file$')
1355
def assertReadableByAttrib(self, cwd, relpath, regex):
1356
proc = subprocess.Popen(['attrib', relpath],
1357
stdout=subprocess.PIPE,
1359
(result, err) = proc.communicate()
1360
self.assertContainsRe(result.replace('\r\n', '\n'), regex)
1362
def test_prepare_files(self):
1364
tree = self.make_branch_and_tree('tree')
1365
self.build_tree_contents([('tree/oldname', 'oldcontent')])
1366
self.build_tree_contents([('tree/oldname2', 'oldcontent2')])
1367
tree.add('oldname', 'file-id')
1368
tree.add('oldname2', 'file2-id')
1369
tree.commit('old tree', timestamp=0)
1370
tree.rename_one('oldname', 'newname')
1371
tree.rename_one('oldname2', 'newname2')
1372
self.build_tree_contents([('tree/newname', 'newcontent')])
1373
self.build_tree_contents([('tree/newname2', 'newcontent2')])
1374
old_tree = tree.basis_tree()
1375
old_tree.lock_read()
1376
self.addCleanup(old_tree.unlock)
1378
self.addCleanup(tree.unlock)
1379
diff_obj = DiffFromTool(['python', '-c',
1380
'print "@old_path @new_path"'],
1381
old_tree, tree, output)
1382
self.addCleanup(diff_obj.finish)
1383
self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1384
old_path, new_path = diff_obj._prepare_files('file-id', 'oldname',
1386
self.assertContainsRe(old_path, 'old/oldname$')
1387
self.assertEqual(0, os.stat(old_path).st_mtime)
1388
self.assertContainsRe(new_path, 'tree/newname$')
1389
self.assertFileEqual('oldcontent', old_path)
1390
self.assertFileEqual('newcontent', new_path)
1391
if osutils.host_os_dereferences_symlinks():
1392
self.assertTrue(os.path.samefile('tree/newname', new_path))
1393
# make sure we can create files with the same parent directories
1394
diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1397
class TestGetTreesAndBranchesToDiff(TestCaseWithTransport):
1399
def test_basic(self):
1400
tree = self.make_branch_and_tree('tree')
1401
(old_tree, new_tree,
1402
old_branch, new_branch,
1403
specific_files, extra_trees) = \
1404
get_trees_and_branches_to_diff(['tree'], None, None, None)
1406
self.assertIsInstance(old_tree, RevisionTree)
1407
#print dir (old_tree)
1408
self.assertEqual(_mod_revision.NULL_REVISION, old_tree.get_revision_id())
1409
self.assertEqual(tree.basedir, new_tree.basedir)
1410
self.assertEqual(tree.branch.base, old_branch.base)
1411
self.assertEqual(tree.branch.base, new_branch.base)
1412
self.assertIs(None, specific_files)
1413
self.assertIs(None, extra_trees)
1415
def test_with_rev_specs(self):
1416
tree = self.make_branch_and_tree('tree')
1417
self.build_tree_contents([('tree/file', 'oldcontent')])
1418
tree.add('file', 'file-id')
1419
tree.commit('old tree', timestamp=0, rev_id="old-id")
1420
self.build_tree_contents([('tree/file', 'newcontent')])
1421
tree.commit('new tree', timestamp=0, rev_id="new-id")
1423
revisions = [RevisionSpec.from_string('1'),
1424
RevisionSpec.from_string('2')]
1425
(old_tree, new_tree,
1426
old_branch, new_branch,
1427
specific_files, extra_trees) = \
1428
get_trees_and_branches_to_diff(['tree'], revisions, None, None)
1430
self.assertIsInstance(old_tree, RevisionTree)
1431
self.assertEqual("old-id", old_tree.get_revision_id())
1432
self.assertIsInstance(new_tree, RevisionTree)
1433
self.assertEqual("new-id", new_tree.get_revision_id())
1434
self.assertEqual(tree.branch.base, old_branch.base)
1435
self.assertEqual(tree.branch.base, new_branch.base)
1436
self.assertIs(None, specific_files)
1437
self.assertEqual(tree.basedir, extra_trees[0].basedir)