~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Vincent Ladeuil
  • Date: 2007-06-20 14:25:06 UTC
  • mfrom: (2540 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2646.
  • Revision ID: v.ladeuil+lp@free.fr-20070620142506-txsb1v8538kpsafw
merge bzr.dev @ 2540

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import os
18
 
import os.path
19
18
from cStringIO import StringIO
20
19
import errno
21
20
import subprocess
22
 
import sys
23
21
from tempfile import TemporaryFile
24
22
 
25
 
from bzrlib import tests
26
 
from bzrlib.diff import (
27
 
    DiffFromTool,
28
 
    DiffPath,
29
 
    DiffSymlink,
30
 
    DiffTree,
31
 
    DiffText,
32
 
    external_diff,
33
 
    internal_diff,
34
 
    show_diff_trees,
35
 
    get_trees_and_branches_to_diff,
36
 
    )
37
 
from bzrlib.errors import BinaryFile, NoDiff, ExecutableMissing
 
23
from bzrlib.diff import internal_diff, external_diff, show_diff_trees
 
24
from bzrlib.errors import BinaryFile, NoDiff
38
25
import bzrlib.osutils as osutils
39
 
import bzrlib.revision as _mod_revision
40
 
import bzrlib.transform as transform
41
26
import bzrlib.patiencediff
42
 
import bzrlib._patiencediff_py
43
 
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
 
27
from bzrlib.tests import (TestCase, TestCaseWithTransport,
44
28
                          TestCaseInTempDir, TestSkipped)
45
 
from bzrlib.revisiontree import RevisionTree
46
 
from bzrlib.revisionspec import RevisionSpec
47
 
 
48
 
 
49
 
class _AttribFeature(Feature):
50
 
 
51
 
    def _probe(self):
52
 
        if (sys.platform not in ('cygwin', 'win32')):
53
 
            return False
54
 
        try:
55
 
            proc = subprocess.Popen(['attrib', '.'], stdout=subprocess.PIPE)
56
 
        except OSError, e:
57
 
            return False
58
 
        return (0 == proc.wait())
59
 
 
60
 
    def feature_name(self):
61
 
        return 'attrib Windows command-line tool'
62
 
 
63
 
AttribFeature = _AttribFeature()
64
 
 
65
 
 
66
 
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
67
 
                                    'bzrlib._patiencediff_c')
68
29
 
69
30
 
70
31
def udiff_lines(old, new, allow_binary=False):
176
137
                              StringIO(), diff_opts=['-u'])
177
138
        finally:
178
139
            os.environ['PATH'] = orig_path
179
 
 
 
140
        
180
141
    def test_internal_diff_default(self):
181
142
        # Default internal diff encoding is utf8
182
143
        output = StringIO()
225
186
                          ]
226
187
                          , lines)
227
188
 
228
 
    def test_internal_diff_no_content(self):
229
 
        output = StringIO()
230
 
        internal_diff(u'old', [], u'new', [], output)
231
 
        self.assertEqual('', output.getvalue())
232
 
 
233
 
    def test_internal_diff_no_changes(self):
234
 
        output = StringIO()
235
 
        internal_diff(u'old', ['text\n', 'contents\n'],
236
 
                      u'new', ['text\n', 'contents\n'],
237
 
                      output)
238
 
        self.assertEqual('', output.getvalue())
239
 
 
240
189
    def test_internal_diff_returns_bytes(self):
241
190
        import StringIO
242
191
        output = StringIO.StringIO()
347
296
+file2 contents at rev 3
348
297
 
349
298
''')
350
 
 
 
299
        
351
300
    def test_diff_add_files(self):
352
 
        tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
 
301
        tree1 = self.b.repository.revision_tree(None)
353
302
        tree2 = self.b.repository.revision_tree('rev-1')
354
303
        output = self.get_diff(tree1, tree2)
355
304
        # the files have the epoch time stamp for the tree in which
389
338
        self.wt.rename_one('file1', 'file1b')
390
339
        old_tree = self.b.repository.revision_tree('rev-1')
391
340
        new_tree = self.b.repository.revision_tree('rev-4')
392
 
        out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
 
341
        out = self.get_diff(old_tree, new_tree, specific_files=['file1b'], 
393
342
                            working_tree=self.wt)
394
343
        self.assertContainsRe(out, 'file1\t')
395
344
 
401
350
        self.wt.rename_one('file1', 'dir1/file1')
402
351
        old_tree = self.b.repository.revision_tree('rev-1')
403
352
        new_tree = self.b.repository.revision_tree('rev-4')
404
 
        out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
 
353
        out = self.get_diff(old_tree, new_tree, specific_files=['dir1'], 
405
354
                            working_tree=self.wt)
406
355
        self.assertContainsRe(out, 'file1\t')
407
 
        out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
 
356
        out = self.get_diff(old_tree, new_tree, specific_files=['dir2'], 
408
357
                            working_tree=self.wt)
409
358
        self.assertNotContainsRe(out, 'file1\t')
410
359
 
493
442
                                    '\\+new contents\n')
494
443
 
495
444
 
496
 
    def test_internal_diff_exec_property(self):
497
 
        tree = self.make_branch_and_tree('tree')
498
 
 
499
 
        tt = transform.TreeTransform(tree)
500
 
        tt.new_file('a', tt.root, 'contents\n', 'a-id', True)
501
 
        tt.new_file('b', tt.root, 'contents\n', 'b-id', False)
502
 
        tt.new_file('c', tt.root, 'contents\n', 'c-id', True)
503
 
        tt.new_file('d', tt.root, 'contents\n', 'd-id', False)
504
 
        tt.new_file('e', tt.root, 'contents\n', 'control-e-id', True)
505
 
        tt.new_file('f', tt.root, 'contents\n', 'control-f-id', False)
506
 
        tt.apply()
507
 
        tree.commit('one', rev_id='rev-1')
508
 
 
509
 
        tt = transform.TreeTransform(tree)
510
 
        tt.set_executability(False, tt.trans_id_file_id('a-id'))
511
 
        tt.set_executability(True, tt.trans_id_file_id('b-id'))
512
 
        tt.set_executability(False, tt.trans_id_file_id('c-id'))
513
 
        tt.set_executability(True, tt.trans_id_file_id('d-id'))
514
 
        tt.apply()
515
 
        tree.rename_one('c', 'new-c')
516
 
        tree.rename_one('d', 'new-d')
517
 
 
518
 
        diff = self.get_diff(tree.basis_tree(), tree)
519
 
 
520
 
        self.assertContainsRe(diff, r"file 'a'.*\(properties changed:.*\+x to -x.*\)")
521
 
        self.assertContainsRe(diff, r"file 'b'.*\(properties changed:.*-x to \+x.*\)")
522
 
        self.assertContainsRe(diff, r"file 'c'.*\(properties changed:.*\+x to -x.*\)")
523
 
        self.assertContainsRe(diff, r"file 'd'.*\(properties changed:.*-x to \+x.*\)")
524
 
        self.assertNotContainsRe(diff, r"file 'e'")
525
 
        self.assertNotContainsRe(diff, r"file 'f'")
526
 
 
527
 
 
528
 
    def test_binary_unicode_filenames(self):
529
 
        """Test that contents of files are *not* encoded in UTF-8 when there
530
 
        is a binary file in the diff.
531
 
        """
532
 
        # See https://bugs.launchpad.net/bugs/110092.
533
 
        self.requireFeature(tests.UnicodeFilenameFeature)
534
 
 
535
 
        # This bug isn't triggered with cStringIO.
536
 
        from StringIO import StringIO
537
 
        tree = self.make_branch_and_tree('tree')
538
 
        alpha, omega = u'\u03b1', u'\u03c9'
539
 
        alpha_utf8, omega_utf8 = alpha.encode('utf8'), omega.encode('utf8')
540
 
        self.build_tree_contents(
541
 
            [('tree/' + alpha, chr(0)),
542
 
             ('tree/' + omega,
543
 
              ('The %s and the %s\n' % (alpha_utf8, omega_utf8)))])
544
 
        tree.add([alpha], ['file-id'])
545
 
        tree.add([omega], ['file-id-2'])
546
 
        diff_content = StringIO()
547
 
        show_diff_trees(tree.basis_tree(), tree, diff_content)
548
 
        diff = diff_content.getvalue()
549
 
        self.assertContainsRe(diff, r"=== added file '%s'" % alpha_utf8)
550
 
        self.assertContainsRe(
551
 
            diff, "Binary files a/%s.*and b/%s.* differ\n" % (alpha_utf8, alpha_utf8))
552
 
        self.assertContainsRe(diff, r"=== added file '%s'" % omega_utf8)
553
 
        self.assertContainsRe(diff, r"--- a/%s" % (omega_utf8,))
554
 
        self.assertContainsRe(diff, r"\+\+\+ b/%s" % (omega_utf8,))
555
 
 
556
 
    def test_unicode_filename(self):
557
 
        """Test when the filename are unicode."""
558
 
        self.requireFeature(tests.UnicodeFilenameFeature)
559
 
 
560
 
        alpha, omega = u'\u03b1', u'\u03c9'
561
 
        autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
562
 
 
563
 
        tree = self.make_branch_and_tree('tree')
564
 
        self.build_tree_contents([('tree/ren_'+alpha, 'contents\n')])
565
 
        tree.add(['ren_'+alpha], ['file-id-2'])
566
 
        self.build_tree_contents([('tree/del_'+alpha, 'contents\n')])
567
 
        tree.add(['del_'+alpha], ['file-id-3'])
568
 
        self.build_tree_contents([('tree/mod_'+alpha, 'contents\n')])
569
 
        tree.add(['mod_'+alpha], ['file-id-4'])
570
 
 
571
 
        tree.commit('one', rev_id='rev-1')
572
 
 
573
 
        tree.rename_one('ren_'+alpha, 'ren_'+omega)
574
 
        tree.remove('del_'+alpha)
575
 
        self.build_tree_contents([('tree/add_'+alpha, 'contents\n')])
576
 
        tree.add(['add_'+alpha], ['file-id'])
577
 
        self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
578
 
 
579
 
        diff = self.get_diff(tree.basis_tree(), tree)
580
 
        self.assertContainsRe(diff,
581
 
                "=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
582
 
        self.assertContainsRe(diff, "=== added file 'add_%s'"%autf8)
583
 
        self.assertContainsRe(diff, "=== modified file 'mod_%s'"%autf8)
584
 
        self.assertContainsRe(diff, "=== removed file 'del_%s'"%autf8)
585
 
 
586
 
 
587
 
class DiffWasIs(DiffPath):
588
 
 
589
 
    def diff(self, file_id, old_path, new_path, old_kind, new_kind):
590
 
        self.to_file.write('was: ')
591
 
        self.to_file.write(self.old_tree.get_file(file_id).read())
592
 
        self.to_file.write('is: ')
593
 
        self.to_file.write(self.new_tree.get_file(file_id).read())
594
 
        pass
595
 
 
596
 
 
597
 
class TestDiffTree(TestCaseWithTransport):
598
 
 
599
 
    def setUp(self):
600
 
        TestCaseWithTransport.setUp(self)
601
 
        self.old_tree = self.make_branch_and_tree('old-tree')
602
 
        self.old_tree.lock_write()
603
 
        self.addCleanup(self.old_tree.unlock)
604
 
        self.new_tree = self.make_branch_and_tree('new-tree')
605
 
        self.new_tree.lock_write()
606
 
        self.addCleanup(self.new_tree.unlock)
607
 
        self.differ = DiffTree(self.old_tree, self.new_tree, StringIO())
608
 
 
609
 
    def test_diff_text(self):
610
 
        self.build_tree_contents([('old-tree/olddir/',),
611
 
                                  ('old-tree/olddir/oldfile', 'old\n')])
612
 
        self.old_tree.add('olddir')
613
 
        self.old_tree.add('olddir/oldfile', 'file-id')
614
 
        self.build_tree_contents([('new-tree/newdir/',),
615
 
                                  ('new-tree/newdir/newfile', 'new\n')])
616
 
        self.new_tree.add('newdir')
617
 
        self.new_tree.add('newdir/newfile', 'file-id')
618
 
        differ = DiffText(self.old_tree, self.new_tree, StringIO())
619
 
        differ.diff_text('file-id', None, 'old label', 'new label')
620
 
        self.assertEqual(
621
 
            '--- old label\n+++ new label\n@@ -1,1 +0,0 @@\n-old\n\n',
622
 
            differ.to_file.getvalue())
623
 
        differ.to_file.seek(0)
624
 
        differ.diff_text(None, 'file-id', 'old label', 'new label')
625
 
        self.assertEqual(
626
 
            '--- old label\n+++ new label\n@@ -0,0 +1,1 @@\n+new\n\n',
627
 
            differ.to_file.getvalue())
628
 
        differ.to_file.seek(0)
629
 
        differ.diff_text('file-id', 'file-id', 'old label', 'new label')
630
 
        self.assertEqual(
631
 
            '--- old label\n+++ new label\n@@ -1,1 +1,1 @@\n-old\n+new\n\n',
632
 
            differ.to_file.getvalue())
633
 
 
634
 
    def test_diff_deletion(self):
635
 
        self.build_tree_contents([('old-tree/file', 'contents'),
636
 
                                  ('new-tree/file', 'contents')])
637
 
        self.old_tree.add('file', 'file-id')
638
 
        self.new_tree.add('file', 'file-id')
639
 
        os.unlink('new-tree/file')
640
 
        self.differ.show_diff(None)
641
 
        self.assertContainsRe(self.differ.to_file.getvalue(), '-contents')
642
 
 
643
 
    def test_diff_creation(self):
644
 
        self.build_tree_contents([('old-tree/file', 'contents'),
645
 
                                  ('new-tree/file', 'contents')])
646
 
        self.old_tree.add('file', 'file-id')
647
 
        self.new_tree.add('file', 'file-id')
648
 
        os.unlink('old-tree/file')
649
 
        self.differ.show_diff(None)
650
 
        self.assertContainsRe(self.differ.to_file.getvalue(), '\+contents')
651
 
 
652
 
    def test_diff_symlink(self):
653
 
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
654
 
        differ.diff_symlink('old target', None)
655
 
        self.assertEqual("=== target was 'old target'\n",
656
 
                         differ.to_file.getvalue())
657
 
 
658
 
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
659
 
        differ.diff_symlink(None, 'new target')
660
 
        self.assertEqual("=== target is 'new target'\n",
661
 
                         differ.to_file.getvalue())
662
 
 
663
 
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
664
 
        differ.diff_symlink('old target', 'new target')
665
 
        self.assertEqual("=== target changed 'old target' => 'new target'\n",
666
 
                         differ.to_file.getvalue())
667
 
 
668
 
    def test_diff(self):
669
 
        self.build_tree_contents([('old-tree/olddir/',),
670
 
                                  ('old-tree/olddir/oldfile', 'old\n')])
671
 
        self.old_tree.add('olddir')
672
 
        self.old_tree.add('olddir/oldfile', 'file-id')
673
 
        self.build_tree_contents([('new-tree/newdir/',),
674
 
                                  ('new-tree/newdir/newfile', 'new\n')])
675
 
        self.new_tree.add('newdir')
676
 
        self.new_tree.add('newdir/newfile', 'file-id')
677
 
        self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
678
 
        self.assertContainsRe(
679
 
            self.differ.to_file.getvalue(),
680
 
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
681
 
             ' \@\@\n-old\n\+new\n\n')
682
 
 
683
 
    def test_diff_kind_change(self):
684
 
        self.requireFeature(tests.SymlinkFeature)
685
 
        self.build_tree_contents([('old-tree/olddir/',),
686
 
                                  ('old-tree/olddir/oldfile', 'old\n')])
687
 
        self.old_tree.add('olddir')
688
 
        self.old_tree.add('olddir/oldfile', 'file-id')
689
 
        self.build_tree(['new-tree/newdir/'])
690
 
        os.symlink('new', 'new-tree/newdir/newfile')
691
 
        self.new_tree.add('newdir')
692
 
        self.new_tree.add('newdir/newfile', 'file-id')
693
 
        self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
694
 
        self.assertContainsRe(
695
 
            self.differ.to_file.getvalue(),
696
 
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+0,0'
697
 
             ' \@\@\n-old\n\n')
698
 
        self.assertContainsRe(self.differ.to_file.getvalue(),
699
 
                              "=== target is u'new'\n")
700
 
 
701
 
    def test_diff_directory(self):
702
 
        self.build_tree(['new-tree/new-dir/'])
703
 
        self.new_tree.add('new-dir', 'new-dir-id')
704
 
        self.differ.diff('new-dir-id', None, 'new-dir')
705
 
        self.assertEqual(self.differ.to_file.getvalue(), '')
706
 
 
707
 
    def create_old_new(self):
708
 
        self.build_tree_contents([('old-tree/olddir/',),
709
 
                                  ('old-tree/olddir/oldfile', 'old\n')])
710
 
        self.old_tree.add('olddir')
711
 
        self.old_tree.add('olddir/oldfile', 'file-id')
712
 
        self.build_tree_contents([('new-tree/newdir/',),
713
 
                                  ('new-tree/newdir/newfile', 'new\n')])
714
 
        self.new_tree.add('newdir')
715
 
        self.new_tree.add('newdir/newfile', 'file-id')
716
 
 
717
 
    def test_register_diff(self):
718
 
        self.create_old_new()
719
 
        old_diff_factories = DiffTree.diff_factories
720
 
        DiffTree.diff_factories=old_diff_factories[:]
721
 
        DiffTree.diff_factories.insert(0, DiffWasIs.from_diff_tree)
722
 
        try:
723
 
            differ = DiffTree(self.old_tree, self.new_tree, StringIO())
724
 
        finally:
725
 
            DiffTree.diff_factories = old_diff_factories
726
 
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
727
 
        self.assertNotContainsRe(
728
 
            differ.to_file.getvalue(),
729
 
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
730
 
             ' \@\@\n-old\n\+new\n\n')
731
 
        self.assertContainsRe(differ.to_file.getvalue(),
732
 
                              'was: old\nis: new\n')
733
 
 
734
 
    def test_extra_factories(self):
735
 
        self.create_old_new()
736
 
        differ = DiffTree(self.old_tree, self.new_tree, StringIO(),
737
 
                            extra_factories=[DiffWasIs.from_diff_tree])
738
 
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
739
 
        self.assertNotContainsRe(
740
 
            differ.to_file.getvalue(),
741
 
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
742
 
             ' \@\@\n-old\n\+new\n\n')
743
 
        self.assertContainsRe(differ.to_file.getvalue(),
744
 
                              'was: old\nis: new\n')
745
 
 
746
 
    def test_alphabetical_order(self):
747
 
        self.build_tree(['new-tree/a-file'])
748
 
        self.new_tree.add('a-file')
749
 
        self.build_tree(['old-tree/b-file'])
750
 
        self.old_tree.add('b-file')
751
 
        self.differ.show_diff(None)
752
 
        self.assertContainsRe(self.differ.to_file.getvalue(),
753
 
            '.*a-file(.|\n)*b-file')
754
 
 
755
 
 
756
445
class TestPatienceDiffLib(TestCase):
757
446
 
758
 
    def setUp(self):
759
 
        super(TestPatienceDiffLib, self).setUp()
760
 
        self._unique_lcs = bzrlib._patiencediff_py.unique_lcs_py
761
 
        self._recurse_matches = bzrlib._patiencediff_py.recurse_matches_py
762
 
        self._PatienceSequenceMatcher = \
763
 
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
764
 
 
765
 
    def test_diff_unicode_string(self):
766
 
        a = ''.join([unichr(i) for i in range(4000, 4500, 3)])
767
 
        b = ''.join([unichr(i) for i in range(4300, 4800, 2)])
768
 
        sm = self._PatienceSequenceMatcher(None, a, b)
769
 
        mb = sm.get_matching_blocks()
770
 
        self.assertEquals(35, len(mb))
771
 
 
772
447
    def test_unique_lcs(self):
773
 
        unique_lcs = self._unique_lcs
 
448
        unique_lcs = bzrlib.patiencediff.unique_lcs
774
449
        self.assertEquals(unique_lcs('', ''), [])
775
 
        self.assertEquals(unique_lcs('', 'a'), [])
776
 
        self.assertEquals(unique_lcs('a', ''), [])
777
450
        self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
778
451
        self.assertEquals(unique_lcs('a', 'b'), [])
779
452
        self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
780
453
        self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
781
454
        self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
782
 
        self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
 
455
        self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1), 
783
456
                                                         (3,3), (4,4)])
784
457
        self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
785
458
 
786
459
    def test_recurse_matches(self):
787
460
        def test_one(a, b, matches):
788
461
            test_matches = []
789
 
            self._recurse_matches(
790
 
                a, b, 0, 0, len(a), len(b), test_matches, 10)
 
462
            bzrlib.patiencediff.recurse_matches(a, b, 0, 0, len(a), len(b),
 
463
                test_matches, 10)
791
464
            self.assertEquals(test_matches, matches)
792
465
 
793
466
        test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
794
467
                 [(0, 0), (2, 2), (4, 4)])
795
468
        test_one(['a', 'c', 'b', 'a', 'c'], ['a', 'b', 'c'],
796
469
                 [(0, 0), (2, 1), (4, 2)])
797
 
        # Even though 'bc' is not unique globally, and is surrounded by
798
 
        # non-matching lines, we should still match, because they are locally
799
 
        # unique
800
 
        test_one('abcdbce', 'afbcgdbce', [(0,0), (1, 2), (2, 3), (3, 5),
801
 
                                          (4, 6), (5, 7), (6, 8)])
802
470
 
803
 
        # recurse_matches doesn't match non-unique
 
471
        # recurse_matches doesn't match non-unique 
804
472
        # lines surrounded by bogus text.
805
473
        # The update has been done in patiencediff.SequenceMatcher instead
806
474
 
810
478
        # This is what it currently gives:
811
479
        test_one('aBccDe', 'abccde', [(0,0), (5,5)])
812
480
 
813
 
    def assertDiffBlocks(self, a, b, expected_blocks):
814
 
        """Check that the sequence matcher returns the correct blocks.
815
 
 
816
 
        :param a: A sequence to match
817
 
        :param b: Another sequence to match
818
 
        :param expected_blocks: The expected output, not including the final
819
 
            matching block (len(a), len(b), 0)
820
 
        """
821
 
        matcher = self._PatienceSequenceMatcher(None, a, b)
822
 
        blocks = matcher.get_matching_blocks()
823
 
        last = blocks.pop()
824
 
        self.assertEqual((len(a), len(b), 0), last)
825
 
        self.assertEqual(expected_blocks, blocks)
826
 
 
827
481
    def test_matching_blocks(self):
 
482
        def chk_blocks(a, b, expected_blocks):
 
483
            # difflib always adds a signature of the total
 
484
            # length, with no matching entries at the end
 
485
            s = bzrlib.patiencediff.PatienceSequenceMatcher(None, a, b)
 
486
            blocks = s.get_matching_blocks()
 
487
            self.assertEquals((len(a), len(b), 0), blocks[-1])
 
488
            self.assertEquals(expected_blocks, blocks[:-1])
 
489
 
828
490
        # Some basic matching tests
829
 
        self.assertDiffBlocks('', '', [])
830
 
        self.assertDiffBlocks([], [], [])
831
 
        self.assertDiffBlocks('abc', '', [])
832
 
        self.assertDiffBlocks('', 'abc', [])
833
 
        self.assertDiffBlocks('abcd', 'abcd', [(0, 0, 4)])
834
 
        self.assertDiffBlocks('abcd', 'abce', [(0, 0, 3)])
835
 
        self.assertDiffBlocks('eabc', 'abce', [(1, 0, 3)])
836
 
        self.assertDiffBlocks('eabce', 'abce', [(1, 0, 4)])
837
 
        self.assertDiffBlocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
838
 
        self.assertDiffBlocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
839
 
        self.assertDiffBlocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
840
 
        # This may check too much, but it checks to see that
 
491
        chk_blocks('', '', [])
 
492
        chk_blocks([], [], [])
 
493
        chk_blocks('abcd', 'abcd', [(0, 0, 4)])
 
494
        chk_blocks('abcd', 'abce', [(0, 0, 3)])
 
495
        chk_blocks('eabc', 'abce', [(1, 0, 3)])
 
496
        chk_blocks('eabce', 'abce', [(1, 0, 4)])
 
497
        chk_blocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
 
498
        chk_blocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
 
499
        chk_blocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
 
500
        # This may check too much, but it checks to see that 
841
501
        # a copied block stays attached to the previous section,
842
502
        # not the later one.
843
503
        # difflib would tend to grab the trailing longest match
844
504
        # which would make the diff not look right
845
 
        self.assertDiffBlocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
846
 
                              [(0, 0, 6), (6, 11, 10)])
 
505
        chk_blocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
506
                   [(0, 0, 6), (6, 11, 10)])
847
507
 
848
508
        # make sure it supports passing in lists
849
 
        self.assertDiffBlocks(
 
509
        chk_blocks(
850
510
                   ['hello there\n',
851
511
                    'world\n',
852
512
                    'how are you today?\n'],
856
516
 
857
517
        # non unique lines surrounded by non-matching lines
858
518
        # won't be found
859
 
        self.assertDiffBlocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
 
519
        chk_blocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
860
520
 
861
521
        # But they only need to be locally unique
862
 
        self.assertDiffBlocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
 
522
        chk_blocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
863
523
 
864
524
        # non unique blocks won't be matched
865
 
        self.assertDiffBlocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
 
525
        chk_blocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
866
526
 
867
527
        # but locally unique ones will
868
 
        self.assertDiffBlocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
 
528
        chk_blocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
869
529
                                              (5,4,1), (7,5,2), (10,8,1)])
870
530
 
871
 
        self.assertDiffBlocks('abbabbXd', 'cabbabxd', [(7,7,1)])
872
 
        self.assertDiffBlocks('abbabbbb', 'cabbabbc', [])
873
 
        self.assertDiffBlocks('bbbbbbbb', 'cbbbbbbc', [])
874
 
 
875
 
    def test_matching_blocks_tuples(self):
876
 
        # Some basic matching tests
877
 
        self.assertDiffBlocks([], [], [])
878
 
        self.assertDiffBlocks([('a',), ('b',), ('c,')], [], [])
879
 
        self.assertDiffBlocks([], [('a',), ('b',), ('c,')], [])
880
 
        self.assertDiffBlocks([('a',), ('b',), ('c,')],
881
 
                              [('a',), ('b',), ('c,')],
882
 
                              [(0, 0, 3)])
883
 
        self.assertDiffBlocks([('a',), ('b',), ('c,')],
884
 
                              [('a',), ('b',), ('d,')],
885
 
                              [(0, 0, 2)])
886
 
        self.assertDiffBlocks([('d',), ('b',), ('c,')],
887
 
                              [('a',), ('b',), ('c,')],
888
 
                              [(1, 1, 2)])
889
 
        self.assertDiffBlocks([('d',), ('a',), ('b',), ('c,')],
890
 
                              [('a',), ('b',), ('c,')],
891
 
                              [(1, 0, 3)])
892
 
        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
893
 
                              [('a', 'b'), ('c', 'X'), ('e', 'f')],
894
 
                              [(0, 0, 1), (2, 2, 1)])
895
 
        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
896
 
                              [('a', 'b'), ('c', 'dX'), ('e', 'f')],
897
 
                              [(0, 0, 1), (2, 2, 1)])
 
531
        chk_blocks('abbabbXd', 'cabbabxd', [(7,7,1)])
 
532
        chk_blocks('abbabbbb', 'cabbabbc', [])
 
533
        chk_blocks('bbbbbbbb', 'cbbbbbbc', [])
898
534
 
899
535
    def test_opcodes(self):
900
536
        def chk_ops(a, b, expected_codes):
901
 
            s = self._PatienceSequenceMatcher(None, a, b)
 
537
            s = bzrlib.patiencediff.PatienceSequenceMatcher(None, a, b)
902
538
            self.assertEquals(expected_codes, s.get_opcodes())
903
539
 
904
540
        chk_ops('', '', [])
905
541
        chk_ops([], [], [])
906
 
        chk_ops('abc', '', [('delete', 0,3, 0,0)])
907
 
        chk_ops('', 'abc', [('insert', 0,0, 0,3)])
908
542
        chk_ops('abcd', 'abcd', [('equal',    0,4, 0,4)])
909
543
        chk_ops('abcd', 'abce', [('equal',   0,3, 0,3),
910
544
                                 ('replace', 3,4, 3,4)
943
577
                 ('delete', 1,2, 1,1),
944
578
                 ('equal',  2,3, 1,2),
945
579
                ])
946
 
        chk_ops('aBccDe', 'abccde',
 
580
        chk_ops('aBccDe', 'abccde', 
947
581
                [('equal',   0,1, 0,1),
948
582
                 ('replace', 1,5, 1,5),
949
583
                 ('equal',   5,6, 5,6),
950
584
                ])
951
 
        chk_ops('aBcDec', 'abcdec',
 
585
        chk_ops('aBcDec', 'abcdec', 
952
586
                [('equal',   0,1, 0,1),
953
587
                 ('replace', 1,2, 1,2),
954
588
                 ('equal',   2,3, 2,3),
955
589
                 ('replace', 3,4, 3,4),
956
590
                 ('equal',   4,6, 4,6),
957
591
                ])
958
 
        chk_ops('aBcdEcdFg', 'abcdecdfg',
 
592
        chk_ops('aBcdEcdFg', 'abcdecdfg', 
959
593
                [('equal',   0,1, 0,1),
960
594
                 ('replace', 1,8, 1,8),
961
595
                 ('equal',   8,9, 8,9)
962
596
                ])
963
 
        chk_ops('aBcdEeXcdFg', 'abcdecdfg',
 
597
        chk_ops('aBcdEeXcdFg', 'abcdecdfg', 
964
598
                [('equal',   0,1, 0,1),
965
599
                 ('replace', 1,2, 1,2),
966
600
                 ('equal',   2,4, 2,4),
972
606
                 ('equal',   10,11, 8,9)
973
607
                ])
974
608
 
975
 
    def test_grouped_opcodes(self):
976
 
        def chk_ops(a, b, expected_codes, n=3):
977
 
            s = self._PatienceSequenceMatcher(None, a, b)
978
 
            self.assertEquals(expected_codes, list(s.get_grouped_opcodes(n)))
979
 
 
980
 
        chk_ops('', '', [])
981
 
        chk_ops([], [], [])
982
 
        chk_ops('abc', '', [[('delete', 0,3, 0,0)]])
983
 
        chk_ops('', 'abc', [[('insert', 0,0, 0,3)]])
984
 
        chk_ops('abcd', 'abcd', [])
985
 
        chk_ops('abcd', 'abce', [[('equal',   0,3, 0,3),
986
 
                                  ('replace', 3,4, 3,4)
987
 
                                 ]])
988
 
        chk_ops('eabc', 'abce', [[('delete', 0,1, 0,0),
989
 
                                 ('equal',  1,4, 0,3),
990
 
                                 ('insert', 4,4, 3,4)
991
 
                                ]])
992
 
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
993
 
                [[('equal',  3,6, 3,6),
994
 
                  ('insert', 6,6, 6,11),
995
 
                  ('equal',  6,9, 11,14)
996
 
                  ]])
997
 
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
998
 
                [[('equal',  2,6, 2,6),
999
 
                  ('insert', 6,6, 6,11),
1000
 
                  ('equal',  6,10, 11,15)
1001
 
                  ]], 4)
1002
 
        chk_ops('Xabcdef', 'abcdef',
1003
 
                [[('delete', 0,1, 0,0),
1004
 
                  ('equal',  1,4, 0,3)
1005
 
                  ]])
1006
 
        chk_ops('abcdef', 'abcdefX',
1007
 
                [[('equal',  3,6, 3,6),
1008
 
                  ('insert', 6,6, 6,7)
1009
 
                  ]])
1010
 
 
1011
 
 
1012
609
    def test_multiple_ranges(self):
1013
610
        # There was an earlier bug where we used a bad set of ranges,
1014
611
        # this triggers that specific bug, to make sure it doesn't regress
1015
 
        self.assertDiffBlocks('abcdefghijklmnop',
1016
 
                              'abcXghiYZQRSTUVWXYZijklmnop',
1017
 
                              [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
1018
 
 
1019
 
        self.assertDiffBlocks('ABCd efghIjk  L',
1020
 
                              'AxyzBCn mo pqrstuvwI1 2  L',
1021
 
                              [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
 
612
        def chk_blocks(a, b, expected_blocks):
 
613
            # difflib always adds a signature of the total
 
614
            # length, with no matching entries at the end
 
615
            s = bzrlib.patiencediff.PatienceSequenceMatcher(None, a, b)
 
616
            blocks = s.get_matching_blocks()
 
617
            x = blocks.pop()
 
618
            self.assertEquals(x, (len(a), len(b), 0))
 
619
            self.assertEquals(expected_blocks, blocks)
 
620
 
 
621
        chk_blocks('abcdefghijklmnop'
 
622
                 , 'abcXghiYZQRSTUVWXYZijklmnop'
 
623
                 , [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
 
624
 
 
625
        chk_blocks('ABCd efghIjk  L'
 
626
                 , 'AxyzBCn mo pqrstuvwI1 2  L'
 
627
                 , [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
1022
628
 
1023
629
        # These are rot13 code snippets.
1024
 
        self.assertDiffBlocks('''\
 
630
        chk_blocks('''\
1025
631
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
1026
632
    """
1027
633
    gnxrf_netf = ['svyr*']
1028
634
    gnxrf_bcgvbaf = ['ab-erphefr']
1029
 
 
 
635
  
1030
636
    qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr):
1031
637
        sebz omeyvo.nqq vzcbeg fzneg_nqq, nqq_ercbegre_cevag, nqq_ercbegre_ahyy
1032
638
        vs vf_dhvrg():
1040
646
'''.splitlines(True), '''\
1041
647
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
1042
648
 
1043
 
    --qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl
 
649
    --qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl 
1044
650
    nqq gurz.
1045
651
    """
1046
652
    gnxrf_netf = ['svyr*']
1074
680
        txt_b = ['hello there\n',
1075
681
                 'how are you today?\n']
1076
682
        unified_diff = bzrlib.patiencediff.unified_diff
1077
 
        psm = self._PatienceSequenceMatcher
1078
 
        self.assertEquals(['--- \n',
1079
 
                           '+++ \n',
 
683
        psm = bzrlib.patiencediff.PatienceSequenceMatcher
 
684
        self.assertEquals([ '---  \n',
 
685
                           '+++  \n',
1080
686
                           '@@ -1,3 +1,2 @@\n',
1081
687
                           ' hello there\n',
1082
688
                           '-world\n',
1087
693
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1088
694
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1089
695
        # This is the result with LongestCommonSubstring matching
1090
 
        self.assertEquals(['--- \n',
1091
 
                           '+++ \n',
 
696
        self.assertEquals(['---  \n',
 
697
                           '+++  \n',
1092
698
                           '@@ -1,6 +1,11 @@\n',
1093
699
                           ' a\n',
1094
700
                           ' b\n',
1103
709
                           ' f\n']
1104
710
                          , list(unified_diff(txt_a, txt_b)))
1105
711
        # And the patience diff
1106
 
        self.assertEquals(['--- \n',
1107
 
                           '+++ \n',
 
712
        self.assertEquals(['---  \n',
 
713
                           '+++  \n',
1108
714
                           '@@ -4,6 +4,11 @@\n',
1109
715
                           ' d\n',
1110
716
                           ' e\n',
1121
727
                          , list(unified_diff(txt_a, txt_b,
1122
728
                                 sequencematcher=psm)))
1123
729
 
1124
 
    def test_patience_unified_diff_with_dates(self):
1125
 
        txt_a = ['hello there\n',
1126
 
                 'world\n',
1127
 
                 'how are you today?\n']
1128
 
        txt_b = ['hello there\n',
1129
 
                 'how are you today?\n']
1130
 
        unified_diff = bzrlib.patiencediff.unified_diff
1131
 
        psm = self._PatienceSequenceMatcher
1132
 
        self.assertEquals(['--- a\t2008-08-08\n',
1133
 
                           '+++ b\t2008-09-09\n',
1134
 
                           '@@ -1,3 +1,2 @@\n',
1135
 
                           ' hello there\n',
1136
 
                           '-world\n',
1137
 
                           ' how are you today?\n'
1138
 
                          ]
1139
 
                          , list(unified_diff(txt_a, txt_b,
1140
 
                                 fromfile='a', tofile='b',
1141
 
                                 fromfiledate='2008-08-08',
1142
 
                                 tofiledate='2008-09-09',
1143
 
                                 sequencematcher=psm)))
1144
 
 
1145
 
 
1146
 
class TestPatienceDiffLib_c(TestPatienceDiffLib):
1147
 
 
1148
 
    _test_needs_features = [compiled_patiencediff_feature]
1149
 
 
1150
 
    def setUp(self):
1151
 
        super(TestPatienceDiffLib_c, self).setUp()
1152
 
        import bzrlib._patiencediff_c
1153
 
        self._unique_lcs = bzrlib._patiencediff_c.unique_lcs_c
1154
 
        self._recurse_matches = bzrlib._patiencediff_c.recurse_matches_c
1155
 
        self._PatienceSequenceMatcher = \
1156
 
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1157
 
 
1158
 
    def test_unhashable(self):
1159
 
        """We should get a proper exception here."""
1160
 
        # We need to be able to hash items in the sequence, lists are
1161
 
        # unhashable, and thus cannot be diffed
1162
 
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1163
 
                                         None, [[]], [])
1164
 
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1165
 
                                         None, ['valid', []], [])
1166
 
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1167
 
                                         None, ['valid'], [[]])
1168
 
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1169
 
                                         None, ['valid'], ['valid', []])
1170
 
 
1171
730
 
1172
731
class TestPatienceDiffLibFiles(TestCaseInTempDir):
1173
732
 
1174
 
    def setUp(self):
1175
 
        super(TestPatienceDiffLibFiles, self).setUp()
1176
 
        self._PatienceSequenceMatcher = \
1177
 
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
1178
 
 
1179
733
    def test_patience_unified_diff_files(self):
1180
734
        txt_a = ['hello there\n',
1181
735
                 'world\n',
1186
740
        open('b1', 'wb').writelines(txt_b)
1187
741
 
1188
742
        unified_diff_files = bzrlib.patiencediff.unified_diff_files
1189
 
        psm = self._PatienceSequenceMatcher
1190
 
        self.assertEquals(['--- a1\n',
1191
 
                           '+++ b1\n',
 
743
        psm = bzrlib.patiencediff.PatienceSequenceMatcher
 
744
        self.assertEquals(['--- a1 \n',
 
745
                           '+++ b1 \n',
1192
746
                           '@@ -1,3 +1,2 @@\n',
1193
747
                           ' hello there\n',
1194
748
                           '-world\n',
1203
757
        open('b2', 'wb').writelines(txt_b)
1204
758
 
1205
759
        # This is the result with LongestCommonSubstring matching
1206
 
        self.assertEquals(['--- a2\n',
1207
 
                           '+++ b2\n',
 
760
        self.assertEquals(['--- a2 \n',
 
761
                           '+++ b2 \n',
1208
762
                           '@@ -1,6 +1,11 @@\n',
1209
763
                           ' a\n',
1210
764
                           ' b\n',
1220
774
                          , list(unified_diff_files('a2', 'b2')))
1221
775
 
1222
776
        # And the patience diff
1223
 
        self.assertEquals(['--- a2\n',
1224
 
                           '+++ b2\n',
 
777
        self.assertEquals(['--- a2 \n',
 
778
                           '+++ b2 \n',
1225
779
                           '@@ -4,6 +4,11 @@\n',
1226
780
                           ' d\n',
1227
781
                           ' e\n',
1237
791
                          ]
1238
792
                          , list(unified_diff_files('a2', 'b2',
1239
793
                                 sequencematcher=psm)))
1240
 
 
1241
 
 
1242
 
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1243
 
 
1244
 
    _test_needs_features = [compiled_patiencediff_feature]
1245
 
 
1246
 
    def setUp(self):
1247
 
        super(TestPatienceDiffLibFiles_c, self).setUp()
1248
 
        import bzrlib._patiencediff_c
1249
 
        self._PatienceSequenceMatcher = \
1250
 
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1251
 
 
1252
 
 
1253
 
class TestUsingCompiledIfAvailable(TestCase):
1254
 
 
1255
 
    def test_PatienceSequenceMatcher(self):
1256
 
        if compiled_patiencediff_feature.available():
1257
 
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1258
 
            self.assertIs(PatienceSequenceMatcher_c,
1259
 
                          bzrlib.patiencediff.PatienceSequenceMatcher)
1260
 
        else:
1261
 
            from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
1262
 
            self.assertIs(PatienceSequenceMatcher_py,
1263
 
                          bzrlib.patiencediff.PatienceSequenceMatcher)
1264
 
 
1265
 
    def test_unique_lcs(self):
1266
 
        if compiled_patiencediff_feature.available():
1267
 
            from bzrlib._patiencediff_c import unique_lcs_c
1268
 
            self.assertIs(unique_lcs_c,
1269
 
                          bzrlib.patiencediff.unique_lcs)
1270
 
        else:
1271
 
            from bzrlib._patiencediff_py import unique_lcs_py
1272
 
            self.assertIs(unique_lcs_py,
1273
 
                          bzrlib.patiencediff.unique_lcs)
1274
 
 
1275
 
    def test_recurse_matches(self):
1276
 
        if compiled_patiencediff_feature.available():
1277
 
            from bzrlib._patiencediff_c import recurse_matches_c
1278
 
            self.assertIs(recurse_matches_c,
1279
 
                          bzrlib.patiencediff.recurse_matches)
1280
 
        else:
1281
 
            from bzrlib._patiencediff_py import recurse_matches_py
1282
 
            self.assertIs(recurse_matches_py,
1283
 
                          bzrlib.patiencediff.recurse_matches)
1284
 
 
1285
 
 
1286
 
class TestDiffFromTool(TestCaseWithTransport):
1287
 
 
1288
 
    def test_from_string(self):
1289
 
        diff_obj = DiffFromTool.from_string('diff', None, None, None)
1290
 
        self.addCleanup(diff_obj.finish)
1291
 
        self.assertEqual(['diff', '@old_path', '@new_path'],
1292
 
            diff_obj.command_template)
1293
 
 
1294
 
    def test_from_string_u5(self):
1295
 
        diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1296
 
        self.addCleanup(diff_obj.finish)
1297
 
        self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
1298
 
                         diff_obj.command_template)
1299
 
        self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1300
 
                         diff_obj._get_command('old-path', 'new-path'))
1301
 
 
1302
 
    def test_execute(self):
1303
 
        output = StringIO()
1304
 
        diff_obj = DiffFromTool(['python', '-c',
1305
 
                                 'print "@old_path @new_path"'],
1306
 
                                None, None, output)
1307
 
        self.addCleanup(diff_obj.finish)
1308
 
        diff_obj._execute('old', 'new')
1309
 
        self.assertEqual(output.getvalue().rstrip(), 'old new')
1310
 
 
1311
 
    def test_excute_missing(self):
1312
 
        diff_obj = DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1313
 
                                None, None, None)
1314
 
        self.addCleanup(diff_obj.finish)
1315
 
        e = self.assertRaises(ExecutableMissing, diff_obj._execute, 'old',
1316
 
                              'new')
1317
 
        self.assertEqual('a-tool-which-is-unlikely-to-exist could not be found'
1318
 
                         ' on this machine', str(e))
1319
 
 
1320
 
    def test_prepare_files_creates_paths_readable_by_windows_tool(self):
1321
 
        self.requireFeature(AttribFeature)
1322
 
        output = StringIO()
1323
 
        tree = self.make_branch_and_tree('tree')
1324
 
        self.build_tree_contents([('tree/file', 'content')])
1325
 
        tree.add('file', 'file-id')
1326
 
        tree.commit('old tree')
1327
 
        tree.lock_read()
1328
 
        self.addCleanup(tree.unlock)
1329
 
        basis_tree = tree.basis_tree()
1330
 
        basis_tree.lock_read()
1331
 
        self.addCleanup(basis_tree.unlock)
1332
 
        diff_obj = DiffFromTool(['python', '-c',
1333
 
                                 'print "@old_path @new_path"'],
1334
 
                                basis_tree, tree, output)
1335
 
        diff_obj._prepare_files('file-id', 'file', 'file')
1336
 
        # The old content should be readonly
1337
 
        self.assertReadableByAttrib(diff_obj._root, 'old\\file',
1338
 
                                    r'R.*old\\file$')
1339
 
        # The new content should use the tree object, not a 'new' file anymore
1340
 
        self.assertEndsWith(tree.basedir, 'work/tree')
1341
 
        self.assertReadableByAttrib(tree.basedir, 'file', r'work\\tree\\file$')
1342
 
 
1343
 
    def assertReadableByAttrib(self, cwd, relpath, regex):
1344
 
        proc = subprocess.Popen(['attrib', relpath],
1345
 
                                stdout=subprocess.PIPE,
1346
 
                                cwd=cwd)
1347
 
        (result, err) = proc.communicate()
1348
 
        self.assertContainsRe(result.replace('\r\n', '\n'), regex)
1349
 
 
1350
 
    def test_prepare_files(self):
1351
 
        output = StringIO()
1352
 
        tree = self.make_branch_and_tree('tree')
1353
 
        self.build_tree_contents([('tree/oldname', 'oldcontent')])
1354
 
        self.build_tree_contents([('tree/oldname2', 'oldcontent2')])
1355
 
        tree.add('oldname', 'file-id')
1356
 
        tree.add('oldname2', 'file2-id')
1357
 
        tree.commit('old tree', timestamp=0)
1358
 
        tree.rename_one('oldname', 'newname')
1359
 
        tree.rename_one('oldname2', 'newname2')
1360
 
        self.build_tree_contents([('tree/newname', 'newcontent')])
1361
 
        self.build_tree_contents([('tree/newname2', 'newcontent2')])
1362
 
        old_tree = tree.basis_tree()
1363
 
        old_tree.lock_read()
1364
 
        self.addCleanup(old_tree.unlock)
1365
 
        tree.lock_read()
1366
 
        self.addCleanup(tree.unlock)
1367
 
        diff_obj = DiffFromTool(['python', '-c',
1368
 
                                 'print "@old_path @new_path"'],
1369
 
                                old_tree, tree, output)
1370
 
        self.addCleanup(diff_obj.finish)
1371
 
        self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1372
 
        old_path, new_path = diff_obj._prepare_files('file-id', 'oldname',
1373
 
                                                     'newname')
1374
 
        self.assertContainsRe(old_path, 'old/oldname$')
1375
 
        self.assertEqual(0, os.stat(old_path).st_mtime)
1376
 
        self.assertContainsRe(new_path, 'tree/newname$')
1377
 
        self.assertFileEqual('oldcontent', old_path)
1378
 
        self.assertFileEqual('newcontent', new_path)
1379
 
        if osutils.host_os_dereferences_symlinks():
1380
 
            self.assertTrue(os.path.samefile('tree/newname', new_path))
1381
 
        # make sure we can create files with the same parent directories
1382
 
        diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1383
 
 
1384
 
 
1385
 
class TestGetTreesAndBranchesToDiff(TestCaseWithTransport):
1386
 
 
1387
 
    def test_basic(self):
1388
 
        tree = self.make_branch_and_tree('tree')
1389
 
        (old_tree, new_tree,
1390
 
         old_branch, new_branch,
1391
 
         specific_files, extra_trees) = \
1392
 
            get_trees_and_branches_to_diff(['tree'], None, None, None)
1393
 
 
1394
 
        self.assertIsInstance(old_tree, RevisionTree)
1395
 
        #print dir (old_tree)
1396
 
        self.assertEqual(_mod_revision.NULL_REVISION, old_tree.get_revision_id())
1397
 
        self.assertEqual(tree.basedir, new_tree.basedir)
1398
 
        self.assertEqual(tree.branch.base, old_branch.base)
1399
 
        self.assertEqual(tree.branch.base, new_branch.base)
1400
 
        self.assertIs(None, specific_files)
1401
 
        self.assertIs(None, extra_trees)
1402
 
 
1403
 
    def test_with_rev_specs(self):
1404
 
        tree = self.make_branch_and_tree('tree')
1405
 
        self.build_tree_contents([('tree/file', 'oldcontent')])
1406
 
        tree.add('file', 'file-id')
1407
 
        tree.commit('old tree', timestamp=0, rev_id="old-id")
1408
 
        self.build_tree_contents([('tree/file', 'newcontent')])
1409
 
        tree.commit('new tree', timestamp=0, rev_id="new-id")
1410
 
 
1411
 
        revisions = [RevisionSpec.from_string('1'),
1412
 
                     RevisionSpec.from_string('2')]
1413
 
        (old_tree, new_tree,
1414
 
         old_branch, new_branch,
1415
 
         specific_files, extra_trees) = \
1416
 
            get_trees_and_branches_to_diff(['tree'], revisions, None, None)
1417
 
 
1418
 
        self.assertIsInstance(old_tree, RevisionTree)
1419
 
        self.assertEqual("old-id", old_tree.get_revision_id())
1420
 
        self.assertIsInstance(new_tree, RevisionTree)
1421
 
        self.assertEqual("new-id", new_tree.get_revision_id())
1422
 
        self.assertEqual(tree.branch.base, old_branch.base)
1423
 
        self.assertEqual(tree.branch.base, new_branch.base)
1424
 
        self.assertIs(None, specific_files)
1425
 
        self.assertEqual(tree.basedir, extra_trees[0].basedir)