1
# Copyright (C) 2005, 2006 Canonical Ltd
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
18
from cStringIO import StringIO
 
 
21
from tempfile import TemporaryFile
 
 
23
from bzrlib.diff import (
 
 
32
from bzrlib.errors import BinaryFile, NoDiff
 
 
33
import bzrlib.osutils as osutils
 
 
34
import bzrlib.patiencediff
 
 
35
import bzrlib._patiencediff_py
 
 
36
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
 
 
37
                          TestCaseInTempDir, TestSkipped)
 
 
40
class _CompiledPatienceDiffFeature(Feature):
 
 
44
            import bzrlib._patiencediff_c
 
 
49
    def feature_name(self):
 
 
50
        return 'bzrlib._patiencediff_c'
 
 
52
CompiledPatienceDiffFeature = _CompiledPatienceDiffFeature()
 
 
55
class _UnicodeFilename(Feature):
 
 
56
    """Does the filesystem support Unicode filenames?"""
 
 
61
        except UnicodeEncodeError:
 
 
63
        except (IOError, OSError):
 
 
64
            # The filesystem allows the Unicode filename but the file doesn't
 
 
68
            # The filesystem allows the Unicode filename and the file exists,
 
 
72
UnicodeFilename = _UnicodeFilename()
 
 
75
class TestUnicodeFilename(TestCase):
 
 
77
    def test_probe_passes(self):
 
 
78
        """UnicodeFilename._probe passes."""
 
 
79
        # We can't test much more than that because the behaviour depends
 
 
81
        UnicodeFilename._probe()
 
 
84
def udiff_lines(old, new, allow_binary=False):
 
 
86
    internal_diff('old', old, 'new', new, output, allow_binary)
 
 
88
    return output.readlines()
 
 
91
def external_udiff_lines(old, new, use_stringio=False):
 
 
93
        # StringIO has no fileno, so it tests a different codepath
 
 
96
        output = TemporaryFile()
 
 
98
        external_diff('old', old, 'new', new, output, diff_opts=['-u'])
 
 
100
        raise TestSkipped('external "diff" not present to test')
 
 
102
    lines = output.readlines()
 
 
107
class TestDiff(TestCase):
 
 
109
    def test_add_nl(self):
 
 
110
        """diff generates a valid diff for patches that add a newline"""
 
 
111
        lines = udiff_lines(['boo'], ['boo\n'])
 
 
112
        self.check_patch(lines)
 
 
113
        self.assertEquals(lines[4], '\\ No newline at end of file\n')
 
 
114
            ## "expected no-nl, got %r" % lines[4]
 
 
116
    def test_add_nl_2(self):
 
 
117
        """diff generates a valid diff for patches that change last line and
 
 
120
        lines = udiff_lines(['boo'], ['goo\n'])
 
 
121
        self.check_patch(lines)
 
 
122
        self.assertEquals(lines[4], '\\ No newline at end of file\n')
 
 
123
            ## "expected no-nl, got %r" % lines[4]
 
 
125
    def test_remove_nl(self):
 
 
126
        """diff generates a valid diff for patches that change last line and
 
 
129
        lines = udiff_lines(['boo\n'], ['boo'])
 
 
130
        self.check_patch(lines)
 
 
131
        self.assertEquals(lines[5], '\\ No newline at end of file\n')
 
 
132
            ## "expected no-nl, got %r" % lines[5]
 
 
134
    def check_patch(self, lines):
 
 
135
        self.assert_(len(lines) > 1)
 
 
136
            ## "Not enough lines for a file header for patch:\n%s" % "".join(lines)
 
 
137
        self.assert_(lines[0].startswith ('---'))
 
 
138
            ## 'No orig line for patch:\n%s' % "".join(lines)
 
 
139
        self.assert_(lines[1].startswith ('+++'))
 
 
140
            ## 'No mod line for patch:\n%s' % "".join(lines)
 
 
141
        self.assert_(len(lines) > 2)
 
 
142
            ## "No hunks for patch:\n%s" % "".join(lines)
 
 
143
        self.assert_(lines[2].startswith('@@'))
 
 
144
            ## "No hunk header for patch:\n%s" % "".join(lines)
 
 
145
        self.assert_('@@' in lines[2][2:])
 
 
146
            ## "Unterminated hunk header for patch:\n%s" % "".join(lines)
 
 
148
    def test_binary_lines(self):
 
 
149
        self.assertRaises(BinaryFile, udiff_lines, [1023 * 'a' + '\x00'], [])
 
 
150
        self.assertRaises(BinaryFile, udiff_lines, [], [1023 * 'a' + '\x00'])
 
 
151
        udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
 
 
152
        udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
 
 
154
    def test_external_diff(self):
 
 
155
        lines = external_udiff_lines(['boo\n'], ['goo\n'])
 
 
156
        self.check_patch(lines)
 
 
157
        self.assertEqual('\n', lines[-1])
 
 
159
    def test_external_diff_no_fileno(self):
 
 
160
        # Make sure that we can handle not having a fileno, even
 
 
161
        # if the diff is large
 
 
162
        lines = external_udiff_lines(['boo\n']*10000,
 
 
165
        self.check_patch(lines)
 
 
167
    def test_external_diff_binary_lang_c(self):
 
 
169
        for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
 
 
170
            old_env[lang] = osutils.set_or_unset_env(lang, 'C')
 
 
172
            lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
 
 
173
            # Older versions of diffutils say "Binary files", newer
 
 
174
            # versions just say "Files".
 
 
175
            self.assertContainsRe(lines[0],
 
 
176
                                  '(Binary f|F)iles old and new differ\n')
 
 
177
            self.assertEquals(lines[1:], ['\n'])
 
 
179
            for lang, old_val in old_env.iteritems():
 
 
180
                osutils.set_or_unset_env(lang, old_val)
 
 
182
    def test_no_external_diff(self):
 
 
183
        """Check that NoDiff is raised when diff is not available"""
 
 
184
        # Use os.environ['PATH'] to make sure no 'diff' command is available
 
 
185
        orig_path = os.environ['PATH']
 
 
187
            os.environ['PATH'] = ''
 
 
188
            self.assertRaises(NoDiff, external_diff,
 
 
189
                              'old', ['boo\n'], 'new', ['goo\n'],
 
 
190
                              StringIO(), diff_opts=['-u'])
 
 
192
            os.environ['PATH'] = orig_path
 
 
194
    def test_internal_diff_default(self):
 
 
195
        # Default internal diff encoding is utf8
 
 
197
        internal_diff(u'old_\xb5', ['old_text\n'],
 
 
198
                    u'new_\xe5', ['new_text\n'], output)
 
 
199
        lines = output.getvalue().splitlines(True)
 
 
200
        self.check_patch(lines)
 
 
201
        self.assertEquals(['--- old_\xc2\xb5\n',
 
 
202
                           '+++ new_\xc3\xa5\n',
 
 
210
    def test_internal_diff_utf8(self):
 
 
212
        internal_diff(u'old_\xb5', ['old_text\n'],
 
 
213
                    u'new_\xe5', ['new_text\n'], output,
 
 
214
                    path_encoding='utf8')
 
 
215
        lines = output.getvalue().splitlines(True)
 
 
216
        self.check_patch(lines)
 
 
217
        self.assertEquals(['--- old_\xc2\xb5\n',
 
 
218
                           '+++ new_\xc3\xa5\n',
 
 
226
    def test_internal_diff_iso_8859_1(self):
 
 
228
        internal_diff(u'old_\xb5', ['old_text\n'],
 
 
229
                    u'new_\xe5', ['new_text\n'], output,
 
 
230
                    path_encoding='iso-8859-1')
 
 
231
        lines = output.getvalue().splitlines(True)
 
 
232
        self.check_patch(lines)
 
 
233
        self.assertEquals(['--- old_\xb5\n',
 
 
242
    def test_internal_diff_returns_bytes(self):
 
 
244
        output = StringIO.StringIO()
 
 
245
        internal_diff(u'old_\xb5', ['old_text\n'],
 
 
246
                    u'new_\xe5', ['new_text\n'], output)
 
 
247
        self.failUnless(isinstance(output.getvalue(), str),
 
 
248
            'internal_diff should return bytestrings')
 
 
251
class TestDiffFiles(TestCaseInTempDir):
 
 
253
    def test_external_diff_binary(self):
 
 
254
        """The output when using external diff should use diff's i18n error"""
 
 
255
        # Make sure external_diff doesn't fail in the current LANG
 
 
256
        lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
 
 
258
        cmd = ['diff', '-u', '--binary', 'old', 'new']
 
 
259
        open('old', 'wb').write('\x00foobar\n')
 
 
260
        open('new', 'wb').write('foo\x00bar\n')
 
 
261
        pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
 
 
262
                                     stdin=subprocess.PIPE)
 
 
263
        out, err = pipe.communicate()
 
 
264
        # Diff returns '2' on Binary files.
 
 
265
        self.assertEqual(2, pipe.returncode)
 
 
266
        # We should output whatever diff tells us, plus a trailing newline
 
 
267
        self.assertEqual(out.splitlines(True) + ['\n'], lines)
 
 
270
class TestShowDiffTreesHelper(TestCaseWithTransport):
 
 
271
    """Has a helper for running show_diff_trees"""
 
 
273
    def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
 
 
275
        if working_tree is not None:
 
 
276
            extra_trees = (working_tree,)
 
 
279
        show_diff_trees(tree1, tree2, output, specific_files=specific_files,
 
 
280
                        extra_trees=extra_trees, old_label='old/',
 
 
282
        return output.getvalue()
 
 
285
class TestDiffDates(TestShowDiffTreesHelper):
 
 
288
        super(TestDiffDates, self).setUp()
 
 
289
        self.wt = self.make_branch_and_tree('.')
 
 
290
        self.b = self.wt.branch
 
 
291
        self.build_tree_contents([
 
 
292
            ('file1', 'file1 contents at rev 1\n'),
 
 
293
            ('file2', 'file2 contents at rev 1\n')
 
 
295
        self.wt.add(['file1', 'file2'])
 
 
297
            message='Revision 1',
 
 
298
            timestamp=1143849600, # 2006-04-01 00:00:00 UTC
 
 
301
        self.build_tree_contents([('file1', 'file1 contents at rev 2\n')])
 
 
303
            message='Revision 2',
 
 
304
            timestamp=1143936000, # 2006-04-02 00:00:00 UTC
 
 
307
        self.build_tree_contents([('file2', 'file2 contents at rev 3\n')])
 
 
309
            message='Revision 3',
 
 
310
            timestamp=1144022400, # 2006-04-03 00:00:00 UTC
 
 
313
        self.wt.remove(['file2'])
 
 
315
            message='Revision 4',
 
 
316
            timestamp=1144108800, # 2006-04-04 00:00:00 UTC
 
 
319
        self.build_tree_contents([
 
 
320
            ('file1', 'file1 contents in working tree\n')
 
 
322
        # set the date stamps for files in the working tree to known values
 
 
323
        os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
 
 
325
    def test_diff_rev_tree_working_tree(self):
 
 
326
        output = self.get_diff(self.wt.basis_tree(), self.wt)
 
 
327
        # note that the date for old/file1 is from rev 2 rather than from
 
 
328
        # the basis revision (rev 4)
 
 
329
        self.assertEqualDiff(output, '''\
 
 
330
=== modified file 'file1'
 
 
331
--- old/file1\t2006-04-02 00:00:00 +0000
 
 
332
+++ new/file1\t2006-04-05 00:00:00 +0000
 
 
334
-file1 contents at rev 2
 
 
335
+file1 contents in working tree
 
 
339
    def test_diff_rev_tree_rev_tree(self):
 
 
340
        tree1 = self.b.repository.revision_tree('rev-2')
 
 
341
        tree2 = self.b.repository.revision_tree('rev-3')
 
 
342
        output = self.get_diff(tree1, tree2)
 
 
343
        self.assertEqualDiff(output, '''\
 
 
344
=== modified file 'file2'
 
 
345
--- old/file2\t2006-04-01 00:00:00 +0000
 
 
346
+++ new/file2\t2006-04-03 00:00:00 +0000
 
 
348
-file2 contents at rev 1
 
 
349
+file2 contents at rev 3
 
 
353
    def test_diff_add_files(self):
 
 
354
        tree1 = self.b.repository.revision_tree(None)
 
 
355
        tree2 = self.b.repository.revision_tree('rev-1')
 
 
356
        output = self.get_diff(tree1, tree2)
 
 
357
        # the files have the epoch time stamp for the tree in which
 
 
359
        self.assertEqualDiff(output, '''\
 
 
360
=== added file 'file1'
 
 
361
--- old/file1\t1970-01-01 00:00:00 +0000
 
 
362
+++ new/file1\t2006-04-01 00:00:00 +0000
 
 
364
+file1 contents at rev 1
 
 
366
=== added file 'file2'
 
 
367
--- old/file2\t1970-01-01 00:00:00 +0000
 
 
368
+++ new/file2\t2006-04-01 00:00:00 +0000
 
 
370
+file2 contents at rev 1
 
 
374
    def test_diff_remove_files(self):
 
 
375
        tree1 = self.b.repository.revision_tree('rev-3')
 
 
376
        tree2 = self.b.repository.revision_tree('rev-4')
 
 
377
        output = self.get_diff(tree1, tree2)
 
 
378
        # the file has the epoch time stamp for the tree in which
 
 
380
        self.assertEqualDiff(output, '''\
 
 
381
=== removed file 'file2'
 
 
382
--- old/file2\t2006-04-03 00:00:00 +0000
 
 
383
+++ new/file2\t1970-01-01 00:00:00 +0000
 
 
385
-file2 contents at rev 3
 
 
389
    def test_show_diff_specified(self):
 
 
390
        """A working tree filename can be used to identify a file"""
 
 
391
        self.wt.rename_one('file1', 'file1b')
 
 
392
        old_tree = self.b.repository.revision_tree('rev-1')
 
 
393
        new_tree = self.b.repository.revision_tree('rev-4')
 
 
394
        out = self.get_diff(old_tree, new_tree, specific_files=['file1b'], 
 
 
395
                            working_tree=self.wt)
 
 
396
        self.assertContainsRe(out, 'file1\t')
 
 
398
    def test_recursive_diff(self):
 
 
399
        """Children of directories are matched"""
 
 
402
        self.wt.add(['dir1', 'dir2'])
 
 
403
        self.wt.rename_one('file1', 'dir1/file1')
 
 
404
        old_tree = self.b.repository.revision_tree('rev-1')
 
 
405
        new_tree = self.b.repository.revision_tree('rev-4')
 
 
406
        out = self.get_diff(old_tree, new_tree, specific_files=['dir1'], 
 
 
407
                            working_tree=self.wt)
 
 
408
        self.assertContainsRe(out, 'file1\t')
 
 
409
        out = self.get_diff(old_tree, new_tree, specific_files=['dir2'], 
 
 
410
                            working_tree=self.wt)
 
 
411
        self.assertNotContainsRe(out, 'file1\t')
 
 
415
class TestShowDiffTrees(TestShowDiffTreesHelper):
 
 
416
    """Direct tests for show_diff_trees"""
 
 
418
    def test_modified_file(self):
 
 
419
        """Test when a file is modified."""
 
 
420
        tree = self.make_branch_and_tree('tree')
 
 
421
        self.build_tree_contents([('tree/file', 'contents\n')])
 
 
422
        tree.add(['file'], ['file-id'])
 
 
423
        tree.commit('one', rev_id='rev-1')
 
 
425
        self.build_tree_contents([('tree/file', 'new contents\n')])
 
 
426
        diff = self.get_diff(tree.basis_tree(), tree)
 
 
427
        self.assertContainsRe(diff, "=== modified file 'file'\n")
 
 
428
        self.assertContainsRe(diff, '--- old/file\t')
 
 
429
        self.assertContainsRe(diff, '\\+\\+\\+ new/file\t')
 
 
430
        self.assertContainsRe(diff, '-contents\n'
 
 
433
    def test_modified_file_in_renamed_dir(self):
 
 
434
        """Test when a file is modified in a renamed directory."""
 
 
435
        tree = self.make_branch_and_tree('tree')
 
 
436
        self.build_tree(['tree/dir/'])
 
 
437
        self.build_tree_contents([('tree/dir/file', 'contents\n')])
 
 
438
        tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
 
439
        tree.commit('one', rev_id='rev-1')
 
 
441
        tree.rename_one('dir', 'other')
 
 
442
        self.build_tree_contents([('tree/other/file', 'new contents\n')])
 
 
443
        diff = self.get_diff(tree.basis_tree(), tree)
 
 
444
        self.assertContainsRe(diff, "=== renamed directory 'dir' => 'other'\n")
 
 
445
        self.assertContainsRe(diff, "=== modified file 'other/file'\n")
 
 
446
        # XXX: This is technically incorrect, because it used to be at another
 
 
447
        # location. What to do?
 
 
448
        self.assertContainsRe(diff, '--- old/dir/file\t')
 
 
449
        self.assertContainsRe(diff, '\\+\\+\\+ new/other/file\t')
 
 
450
        self.assertContainsRe(diff, '-contents\n'
 
 
453
    def test_renamed_directory(self):
 
 
454
        """Test when only a directory is only renamed."""
 
 
455
        tree = self.make_branch_and_tree('tree')
 
 
456
        self.build_tree(['tree/dir/'])
 
 
457
        self.build_tree_contents([('tree/dir/file', 'contents\n')])
 
 
458
        tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
 
459
        tree.commit('one', rev_id='rev-1')
 
 
461
        tree.rename_one('dir', 'newdir')
 
 
462
        diff = self.get_diff(tree.basis_tree(), tree)
 
 
463
        # Renaming a directory should be a single "you renamed this dir" even
 
 
464
        # when there are files inside.
 
 
465
        self.assertEqual("=== renamed directory 'dir' => 'newdir'\n", diff)
 
 
467
    def test_renamed_file(self):
 
 
468
        """Test when a file is only renamed."""
 
 
469
        tree = self.make_branch_and_tree('tree')
 
 
470
        self.build_tree_contents([('tree/file', 'contents\n')])
 
 
471
        tree.add(['file'], ['file-id'])
 
 
472
        tree.commit('one', rev_id='rev-1')
 
 
474
        tree.rename_one('file', 'newname')
 
 
475
        diff = self.get_diff(tree.basis_tree(), tree)
 
 
476
        self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
 
 
477
        # We shouldn't have a --- or +++ line, because there is no content
 
 
479
        self.assertNotContainsRe(diff, '---')
 
 
481
    def test_renamed_and_modified_file(self):
 
 
482
        """Test when a file is only renamed."""
 
 
483
        tree = self.make_branch_and_tree('tree')
 
 
484
        self.build_tree_contents([('tree/file', 'contents\n')])
 
 
485
        tree.add(['file'], ['file-id'])
 
 
486
        tree.commit('one', rev_id='rev-1')
 
 
488
        tree.rename_one('file', 'newname')
 
 
489
        self.build_tree_contents([('tree/newname', 'new contents\n')])
 
 
490
        diff = self.get_diff(tree.basis_tree(), tree)
 
 
491
        self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
 
 
492
        self.assertContainsRe(diff, '--- old/file\t')
 
 
493
        self.assertContainsRe(diff, '\\+\\+\\+ new/newname\t')
 
 
494
        self.assertContainsRe(diff, '-contents\n'
 
 
497
    def test_binary_unicode_filenames(self):
 
 
498
        """Test that contents of files are *not* encoded in UTF-8 when there
 
 
499
        is a binary file in the diff.
 
 
501
        # See https://bugs.launchpad.net/bugs/110092.
 
 
502
        self.requireFeature(UnicodeFilename)
 
 
504
        # This bug isn't triggered with cStringIO.
 
 
505
        from StringIO import StringIO
 
 
506
        tree = self.make_branch_and_tree('tree')
 
 
507
        alpha, omega = u'\u03b1', u'\u03c9'
 
 
508
        alpha_utf8, omega_utf8 = alpha.encode('utf8'), omega.encode('utf8')
 
 
509
        self.build_tree_contents(
 
 
510
            [('tree/' + alpha, chr(0)),
 
 
512
              ('The %s and the %s\n' % (alpha_utf8, omega_utf8)))])
 
 
513
        tree.add([alpha], ['file-id'])
 
 
514
        tree.add([omega], ['file-id-2'])
 
 
515
        diff_content = StringIO()
 
 
516
        show_diff_trees(tree.basis_tree(), tree, diff_content)
 
 
517
        diff = diff_content.getvalue()
 
 
518
        self.assertContainsRe(diff, r"=== added file '%s'" % alpha_utf8)
 
 
519
        self.assertContainsRe(
 
 
520
            diff, "Binary files a/%s.*and b/%s.* differ\n" % (alpha_utf8, alpha_utf8))
 
 
521
        self.assertContainsRe(diff, r"=== added file '%s'" % omega_utf8)
 
 
522
        self.assertContainsRe(diff, r"--- a/%s" % (omega_utf8,))
 
 
523
        self.assertContainsRe(diff, r"\+\+\+ b/%s" % (omega_utf8,))
 
 
525
    def test_unicode_filename(self):
 
 
526
        """Test when the filename are unicode."""
 
 
527
        self.requireFeature(UnicodeFilename)
 
 
529
        alpha, omega = u'\u03b1', u'\u03c9'
 
 
530
        autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
 
 
532
        tree = self.make_branch_and_tree('tree')
 
 
533
        self.build_tree_contents([('tree/ren_'+alpha, 'contents\n')])
 
 
534
        tree.add(['ren_'+alpha], ['file-id-2'])
 
 
535
        self.build_tree_contents([('tree/del_'+alpha, 'contents\n')])
 
 
536
        tree.add(['del_'+alpha], ['file-id-3'])
 
 
537
        self.build_tree_contents([('tree/mod_'+alpha, 'contents\n')])
 
 
538
        tree.add(['mod_'+alpha], ['file-id-4'])
 
 
540
        tree.commit('one', rev_id='rev-1')
 
 
542
        tree.rename_one('ren_'+alpha, 'ren_'+omega)
 
 
543
        tree.remove('del_'+alpha)
 
 
544
        self.build_tree_contents([('tree/add_'+alpha, 'contents\n')])
 
 
545
        tree.add(['add_'+alpha], ['file-id'])
 
 
546
        self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
 
 
548
        diff = self.get_diff(tree.basis_tree(), tree)
 
 
549
        self.assertContainsRe(diff,
 
 
550
                "=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
 
 
551
        self.assertContainsRe(diff, "=== added file 'add_%s'"%autf8)
 
 
552
        self.assertContainsRe(diff, "=== modified file 'mod_%s'"%autf8)
 
 
553
        self.assertContainsRe(diff, "=== removed file 'del_%s'"%autf8)
 
 
556
class DiffWasIs(DiffPath):
 
 
558
    def diff(self, file_id, old_path, new_path, old_kind, new_kind):
 
 
559
        self.to_file.write('was: ')
 
 
560
        self.to_file.write(self.old_tree.get_file(file_id).read())
 
 
561
        self.to_file.write('is: ')
 
 
562
        self.to_file.write(self.new_tree.get_file(file_id).read())
 
 
566
class TestDiffTree(TestCaseWithTransport):
 
 
569
        TestCaseWithTransport.setUp(self)
 
 
570
        self.old_tree = self.make_branch_and_tree('old-tree')
 
 
571
        self.old_tree.lock_write()
 
 
572
        self.addCleanup(self.old_tree.unlock)
 
 
573
        self.new_tree = self.make_branch_and_tree('new-tree')
 
 
574
        self.new_tree.lock_write()
 
 
575
        self.addCleanup(self.new_tree.unlock)
 
 
576
        self.differ = DiffTree(self.old_tree, self.new_tree, StringIO())
 
 
578
    def test_diff_text(self):
 
 
579
        self.build_tree_contents([('old-tree/olddir/',),
 
 
580
                                  ('old-tree/olddir/oldfile', 'old\n')])
 
 
581
        self.old_tree.add('olddir')
 
 
582
        self.old_tree.add('olddir/oldfile', 'file-id')
 
 
583
        self.build_tree_contents([('new-tree/newdir/',),
 
 
584
                                  ('new-tree/newdir/newfile', 'new\n')])
 
 
585
        self.new_tree.add('newdir')
 
 
586
        self.new_tree.add('newdir/newfile', 'file-id')
 
 
587
        differ = DiffText(self.old_tree, self.new_tree, StringIO())
 
 
588
        differ.diff_text('file-id', None, 'old label', 'new label')
 
 
590
            '--- old label\n+++ new label\n@@ -1,1 +0,0 @@\n-old\n\n',
 
 
591
            differ.to_file.getvalue())
 
 
592
        differ.to_file.seek(0)
 
 
593
        differ.diff_text(None, 'file-id', 'old label', 'new label')
 
 
595
            '--- old label\n+++ new label\n@@ -0,0 +1,1 @@\n+new\n\n',
 
 
596
            differ.to_file.getvalue())
 
 
597
        differ.to_file.seek(0)
 
 
598
        differ.diff_text('file-id', 'file-id', 'old label', 'new label')
 
 
600
            '--- old label\n+++ new label\n@@ -1,1 +1,1 @@\n-old\n+new\n\n',
 
 
601
            differ.to_file.getvalue())
 
 
603
    def test_diff_symlink(self):
 
 
604
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
 
 
605
        differ.diff_symlink('old target', None)
 
 
606
        self.assertEqual("=== target was 'old target'\n",
 
 
607
                         differ.to_file.getvalue())
 
 
609
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
 
 
610
        differ.diff_symlink(None, 'new target')
 
 
611
        self.assertEqual("=== target is 'new target'\n",
 
 
612
                         differ.to_file.getvalue())
 
 
614
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
 
 
615
        differ.diff_symlink('old target', 'new target')
 
 
616
        self.assertEqual("=== target changed 'old target' => 'new target'\n",
 
 
617
                         differ.to_file.getvalue())
 
 
620
        self.build_tree_contents([('old-tree/olddir/',),
 
 
621
                                  ('old-tree/olddir/oldfile', 'old\n')])
 
 
622
        self.old_tree.add('olddir')
 
 
623
        self.old_tree.add('olddir/oldfile', 'file-id')
 
 
624
        self.build_tree_contents([('new-tree/newdir/',),
 
 
625
                                  ('new-tree/newdir/newfile', 'new\n')])
 
 
626
        self.new_tree.add('newdir')
 
 
627
        self.new_tree.add('newdir/newfile', 'file-id')
 
 
628
        self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
 
 
629
        self.assertContainsRe(
 
 
630
            self.differ.to_file.getvalue(),
 
 
631
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
 
 
632
             ' \@\@\n-old\n\+new\n\n')
 
 
634
    def test_diff_kind_change(self):
 
 
635
        self.build_tree_contents([('old-tree/olddir/',),
 
 
636
                                  ('old-tree/olddir/oldfile', 'old\n')])
 
 
637
        self.old_tree.add('olddir')
 
 
638
        self.old_tree.add('olddir/oldfile', 'file-id')
 
 
639
        self.build_tree(['new-tree/newdir/'])
 
 
640
        os.symlink('new', 'new-tree/newdir/newfile')
 
 
641
        self.new_tree.add('newdir')
 
 
642
        self.new_tree.add('newdir/newfile', 'file-id')
 
 
643
        self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
 
 
644
        self.assertContainsRe(
 
 
645
            self.differ.to_file.getvalue(),
 
 
646
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+0,0'
 
 
648
        self.assertContainsRe(self.differ.to_file.getvalue(),
 
 
649
                              "=== target is 'new'\n")
 
 
651
    def test_diff_directory(self):
 
 
652
        self.build_tree(['new-tree/new-dir/'])
 
 
653
        self.new_tree.add('new-dir', 'new-dir-id')
 
 
654
        self.differ.diff('new-dir-id', None, 'new-dir')
 
 
655
        self.assertEqual(self.differ.to_file.getvalue(), '')
 
 
657
    def create_old_new(self):
 
 
658
        self.build_tree_contents([('old-tree/olddir/',),
 
 
659
                                  ('old-tree/olddir/oldfile', 'old\n')])
 
 
660
        self.old_tree.add('olddir')
 
 
661
        self.old_tree.add('olddir/oldfile', 'file-id')
 
 
662
        self.build_tree_contents([('new-tree/newdir/',),
 
 
663
                                  ('new-tree/newdir/newfile', 'new\n')])
 
 
664
        self.new_tree.add('newdir')
 
 
665
        self.new_tree.add('newdir/newfile', 'file-id')
 
 
667
    def test_register_diff(self):
 
 
668
        self.create_old_new()
 
 
669
        old_diff_factories = DiffTree.diff_factories
 
 
670
        DiffTree.diff_factories=old_diff_factories[:]
 
 
671
        DiffTree.diff_factories.insert(0, DiffWasIs.from_diff_tree)
 
 
673
            differ = DiffTree(self.old_tree, self.new_tree, StringIO())
 
 
675
            DiffTree.diff_factories = old_diff_factories
 
 
676
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
 
 
677
        self.assertNotContainsRe(
 
 
678
            differ.to_file.getvalue(),
 
 
679
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
 
 
680
             ' \@\@\n-old\n\+new\n\n')
 
 
681
        self.assertContainsRe(differ.to_file.getvalue(),
 
 
682
                              'was: old\nis: new\n')
 
 
684
    def test_extra_factories(self):
 
 
685
        self.create_old_new()
 
 
686
        differ = DiffTree(self.old_tree, self.new_tree, StringIO(),
 
 
687
                            extra_factories=[DiffWasIs.from_diff_tree])
 
 
688
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
 
 
689
        self.assertNotContainsRe(
 
 
690
            differ.to_file.getvalue(),
 
 
691
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
 
 
692
             ' \@\@\n-old\n\+new\n\n')
 
 
693
        self.assertContainsRe(differ.to_file.getvalue(),
 
 
694
                              'was: old\nis: new\n')
 
 
697
class TestPatienceDiffLib(TestCase):
 
 
700
        super(TestPatienceDiffLib, self).setUp()
 
 
701
        self._unique_lcs = bzrlib._patiencediff_py.unique_lcs_py
 
 
702
        self._recurse_matches = bzrlib._patiencediff_py.recurse_matches_py
 
 
703
        self._PatienceSequenceMatcher = \
 
 
704
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
 
 
706
    def test_unique_lcs(self):
 
 
707
        unique_lcs = self._unique_lcs
 
 
708
        self.assertEquals(unique_lcs('', ''), [])
 
 
709
        self.assertEquals(unique_lcs('', 'a'), [])
 
 
710
        self.assertEquals(unique_lcs('a', ''), [])
 
 
711
        self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
 
 
712
        self.assertEquals(unique_lcs('a', 'b'), [])
 
 
713
        self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
 
 
714
        self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
 
 
715
        self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
 
 
716
        self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1), 
 
 
718
        self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
 
 
720
    def test_recurse_matches(self):
 
 
721
        def test_one(a, b, matches):
 
 
723
            self._recurse_matches(
 
 
724
                a, b, 0, 0, len(a), len(b), test_matches, 10)
 
 
725
            self.assertEquals(test_matches, matches)
 
 
727
        test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
 
 
728
                 [(0, 0), (2, 2), (4, 4)])
 
 
729
        test_one(['a', 'c', 'b', 'a', 'c'], ['a', 'b', 'c'],
 
 
730
                 [(0, 0), (2, 1), (4, 2)])
 
 
731
        # Even though 'bc' is not unique globally, and is surrounded by
 
 
732
        # non-matching lines, we should still match, because they are locally
 
 
734
        test_one('abcdbce', 'afbcgdbce', [(0,0), (1, 2), (2, 3), (3, 5),
 
 
735
                                          (4, 6), (5, 7), (6, 8)])
 
 
737
        # recurse_matches doesn't match non-unique 
 
 
738
        # lines surrounded by bogus text.
 
 
739
        # The update has been done in patiencediff.SequenceMatcher instead
 
 
741
        # This is what it could be
 
 
742
        #test_one('aBccDe', 'abccde', [(0,0), (2,2), (3,3), (5,5)])
 
 
744
        # This is what it currently gives:
 
 
745
        test_one('aBccDe', 'abccde', [(0,0), (5,5)])
 
 
747
    def test_matching_blocks(self):
 
 
748
        def chk_blocks(a, b, expected_blocks):
 
 
749
            # difflib always adds a signature of the total
 
 
750
            # length, with no matching entries at the end
 
 
751
            s = self._PatienceSequenceMatcher(None, a, b)
 
 
752
            blocks = s.get_matching_blocks()
 
 
753
            self.assertEquals((len(a), len(b), 0), blocks[-1])
 
 
754
            self.assertEquals(expected_blocks, blocks[:-1])
 
 
756
        # Some basic matching tests
 
 
757
        chk_blocks('', '', [])
 
 
758
        chk_blocks([], [], [])
 
 
759
        chk_blocks('abc', '', [])
 
 
760
        chk_blocks('', 'abc', [])
 
 
761
        chk_blocks('abcd', 'abcd', [(0, 0, 4)])
 
 
762
        chk_blocks('abcd', 'abce', [(0, 0, 3)])
 
 
763
        chk_blocks('eabc', 'abce', [(1, 0, 3)])
 
 
764
        chk_blocks('eabce', 'abce', [(1, 0, 4)])
 
 
765
        chk_blocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
 
 
766
        chk_blocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
 
 
767
        chk_blocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
 
 
768
        # This may check too much, but it checks to see that 
 
 
769
        # a copied block stays attached to the previous section,
 
 
771
        # difflib would tend to grab the trailing longest match
 
 
772
        # which would make the diff not look right
 
 
773
        chk_blocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
 
774
                   [(0, 0, 6), (6, 11, 10)])
 
 
776
        # make sure it supports passing in lists
 
 
780
                    'how are you today?\n'],
 
 
782
                    'how are you today?\n'],
 
 
783
                [(0, 0, 1), (2, 1, 1)])
 
 
785
        # non unique lines surrounded by non-matching lines
 
 
787
        chk_blocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
 
 
789
        # But they only need to be locally unique
 
 
790
        chk_blocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
 
 
792
        # non unique blocks won't be matched
 
 
793
        chk_blocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
 
 
795
        # but locally unique ones will
 
 
796
        chk_blocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
 
 
797
                                              (5,4,1), (7,5,2), (10,8,1)])
 
 
799
        chk_blocks('abbabbXd', 'cabbabxd', [(7,7,1)])
 
 
800
        chk_blocks('abbabbbb', 'cabbabbc', [])
 
 
801
        chk_blocks('bbbbbbbb', 'cbbbbbbc', [])
 
 
803
    def test_opcodes(self):
 
 
804
        def chk_ops(a, b, expected_codes):
 
 
805
            s = self._PatienceSequenceMatcher(None, a, b)
 
 
806
            self.assertEquals(expected_codes, s.get_opcodes())
 
 
810
        chk_ops('abc', '', [('delete', 0,3, 0,0)])
 
 
811
        chk_ops('', 'abc', [('insert', 0,0, 0,3)])
 
 
812
        chk_ops('abcd', 'abcd', [('equal',    0,4, 0,4)])
 
 
813
        chk_ops('abcd', 'abce', [('equal',   0,3, 0,3),
 
 
814
                                 ('replace', 3,4, 3,4)
 
 
816
        chk_ops('eabc', 'abce', [('delete', 0,1, 0,0),
 
 
820
        chk_ops('eabce', 'abce', [('delete', 0,1, 0,0),
 
 
823
        chk_ops('abcde', 'abXde', [('equal',   0,2, 0,2),
 
 
824
                                   ('replace', 2,3, 2,3),
 
 
827
        chk_ops('abcde', 'abXYZde', [('equal',   0,2, 0,2),
 
 
828
                                     ('replace', 2,3, 2,5),
 
 
831
        chk_ops('abde', 'abXYZde', [('equal',  0,2, 0,2),
 
 
832
                                    ('insert', 2,2, 2,5),
 
 
835
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
 
836
                [('equal',  0,6,  0,6),
 
 
837
                 ('insert', 6,6,  6,11),
 
 
838
                 ('equal',  6,16, 11,21)
 
 
843
                , 'how are you today?\n'],
 
 
845
                , 'how are you today?\n'],
 
 
846
                [('equal',  0,1, 0,1),
 
 
847
                 ('delete', 1,2, 1,1),
 
 
850
        chk_ops('aBccDe', 'abccde', 
 
 
851
                [('equal',   0,1, 0,1),
 
 
852
                 ('replace', 1,5, 1,5),
 
 
855
        chk_ops('aBcDec', 'abcdec', 
 
 
856
                [('equal',   0,1, 0,1),
 
 
857
                 ('replace', 1,2, 1,2),
 
 
859
                 ('replace', 3,4, 3,4),
 
 
862
        chk_ops('aBcdEcdFg', 'abcdecdfg', 
 
 
863
                [('equal',   0,1, 0,1),
 
 
864
                 ('replace', 1,8, 1,8),
 
 
867
        chk_ops('aBcdEeXcdFg', 'abcdecdfg', 
 
 
868
                [('equal',   0,1, 0,1),
 
 
869
                 ('replace', 1,2, 1,2),
 
 
871
                 ('delete', 4,5, 4,4),
 
 
873
                 ('delete', 6,7, 5,5),
 
 
875
                 ('replace', 9,10, 7,8),
 
 
876
                 ('equal',   10,11, 8,9)
 
 
879
    def test_grouped_opcodes(self):
 
 
880
        def chk_ops(a, b, expected_codes, n=3):
 
 
881
            s = self._PatienceSequenceMatcher(None, a, b)
 
 
882
            self.assertEquals(expected_codes, list(s.get_grouped_opcodes(n)))
 
 
886
        chk_ops('abc', '', [[('delete', 0,3, 0,0)]])
 
 
887
        chk_ops('', 'abc', [[('insert', 0,0, 0,3)]])
 
 
888
        chk_ops('abcd', 'abcd', [])
 
 
889
        chk_ops('abcd', 'abce', [[('equal',   0,3, 0,3),
 
 
890
                                  ('replace', 3,4, 3,4)
 
 
892
        chk_ops('eabc', 'abce', [[('delete', 0,1, 0,0),
 
 
896
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
 
897
                [[('equal',  3,6, 3,6),
 
 
898
                  ('insert', 6,6, 6,11),
 
 
899
                  ('equal',  6,9, 11,14)
 
 
901
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
 
902
                [[('equal',  2,6, 2,6),
 
 
903
                  ('insert', 6,6, 6,11),
 
 
904
                  ('equal',  6,10, 11,15)
 
 
906
        chk_ops('Xabcdef', 'abcdef',
 
 
907
                [[('delete', 0,1, 0,0),
 
 
910
        chk_ops('abcdef', 'abcdefX',
 
 
911
                [[('equal',  3,6, 3,6),
 
 
916
    def test_multiple_ranges(self):
 
 
917
        # There was an earlier bug where we used a bad set of ranges,
 
 
918
        # this triggers that specific bug, to make sure it doesn't regress
 
 
919
        def chk_blocks(a, b, expected_blocks):
 
 
920
            # difflib always adds a signature of the total
 
 
921
            # length, with no matching entries at the end
 
 
922
            s = self._PatienceSequenceMatcher(None, a, b)
 
 
923
            blocks = s.get_matching_blocks()
 
 
925
            self.assertEquals(x, (len(a), len(b), 0))
 
 
926
            self.assertEquals(expected_blocks, blocks)
 
 
928
        chk_blocks('abcdefghijklmnop'
 
 
929
                 , 'abcXghiYZQRSTUVWXYZijklmnop'
 
 
930
                 , [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
 
 
932
        chk_blocks('ABCd efghIjk  L'
 
 
933
                 , 'AxyzBCn mo pqrstuvwI1 2  L'
 
 
934
                 , [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
 
 
936
        # These are rot13 code snippets.
 
 
938
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
 
 
940
    gnxrf_netf = ['svyr*']
 
 
941
    gnxrf_bcgvbaf = ['ab-erphefr']
 
 
943
    qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr):
 
 
944
        sebz omeyvo.nqq vzcbeg fzneg_nqq, nqq_ercbegre_cevag, nqq_ercbegre_ahyy
 
 
946
            ercbegre = nqq_ercbegre_ahyy
 
 
948
            ercbegre = nqq_ercbegre_cevag
 
 
949
        fzneg_nqq(svyr_yvfg, abg ab_erphefr, ercbegre)
 
 
952
pynff pzq_zxqve(Pbzznaq):
 
 
953
'''.splitlines(True), '''\
 
 
954
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
 
 
956
    --qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl 
 
 
959
    gnxrf_netf = ['svyr*']
 
 
960
    gnxrf_bcgvbaf = ['ab-erphefr', 'qel-eha']
 
 
962
    qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr, qel_eha=Snyfr):
 
 
967
                # Guvf vf cbvagyrff, ohg V'q engure abg envfr na reebe
 
 
968
                npgvba = omeyvo.nqq.nqq_npgvba_ahyy
 
 
970
  npgvba = omeyvo.nqq.nqq_npgvba_cevag
 
 
972
            npgvba = omeyvo.nqq.nqq_npgvba_nqq
 
 
974
       npgvba = omeyvo.nqq.nqq_npgvba_nqq_naq_cevag
 
 
976
        omeyvo.nqq.fzneg_nqq(svyr_yvfg, abg ab_erphefr, npgvba)
 
 
979
pynff pzq_zxqve(Pbzznaq):
 
 
981
, [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
 
 
983
    def test_patience_unified_diff(self):
 
 
984
        txt_a = ['hello there\n',
 
 
986
                 'how are you today?\n']
 
 
987
        txt_b = ['hello there\n',
 
 
988
                 'how are you today?\n']
 
 
989
        unified_diff = bzrlib.patiencediff.unified_diff
 
 
990
        psm = self._PatienceSequenceMatcher
 
 
991
        self.assertEquals([ '---  \n',
 
 
996
                           ' how are you today?\n'
 
 
998
                          , list(unified_diff(txt_a, txt_b,
 
 
999
                                 sequencematcher=psm)))
 
 
1000
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
 
 
1001
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
 
 
1002
        # This is the result with LongestCommonSubstring matching
 
 
1003
        self.assertEquals(['---  \n',
 
 
1005
                           '@@ -1,6 +1,11 @@\n',
 
 
1017
                          , list(unified_diff(txt_a, txt_b)))
 
 
1018
        # And the patience diff
 
 
1019
        self.assertEquals(['---  \n',
 
 
1021
                           '@@ -4,6 +4,11 @@\n',
 
 
1034
                          , list(unified_diff(txt_a, txt_b,
 
 
1035
                                 sequencematcher=psm)))
 
 
1038
class TestPatienceDiffLib_c(TestPatienceDiffLib):
 
 
1040
    _test_needs_features = [CompiledPatienceDiffFeature]
 
 
1043
        super(TestPatienceDiffLib_c, self).setUp()
 
 
1044
        import bzrlib._patiencediff_c
 
 
1045
        self._unique_lcs = bzrlib._patiencediff_c.unique_lcs_c
 
 
1046
        self._recurse_matches = bzrlib._patiencediff_c.recurse_matches_c
 
 
1047
        self._PatienceSequenceMatcher = \
 
 
1048
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
 
 
1051
class TestPatienceDiffLibFiles(TestCaseInTempDir):
 
 
1054
        super(TestPatienceDiffLibFiles, self).setUp()
 
 
1055
        self._PatienceSequenceMatcher = \
 
 
1056
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
 
 
1058
    def test_patience_unified_diff_files(self):
 
 
1059
        txt_a = ['hello there\n',
 
 
1061
                 'how are you today?\n']
 
 
1062
        txt_b = ['hello there\n',
 
 
1063
                 'how are you today?\n']
 
 
1064
        open('a1', 'wb').writelines(txt_a)
 
 
1065
        open('b1', 'wb').writelines(txt_b)
 
 
1067
        unified_diff_files = bzrlib.patiencediff.unified_diff_files
 
 
1068
        psm = self._PatienceSequenceMatcher
 
 
1069
        self.assertEquals(['--- a1 \n',
 
 
1071
                           '@@ -1,3 +1,2 @@\n',
 
 
1074
                           ' how are you today?\n',
 
 
1076
                          , list(unified_diff_files('a1', 'b1',
 
 
1077
                                 sequencematcher=psm)))
 
 
1079
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
 
 
1080
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
 
 
1081
        open('a2', 'wb').writelines(txt_a)
 
 
1082
        open('b2', 'wb').writelines(txt_b)
 
 
1084
        # This is the result with LongestCommonSubstring matching
 
 
1085
        self.assertEquals(['--- a2 \n',
 
 
1087
                           '@@ -1,6 +1,11 @@\n',
 
 
1099
                          , list(unified_diff_files('a2', 'b2')))
 
 
1101
        # And the patience diff
 
 
1102
        self.assertEquals(['--- a2 \n',
 
 
1104
                           '@@ -4,6 +4,11 @@\n',
 
 
1117
                          , list(unified_diff_files('a2', 'b2',
 
 
1118
                                 sequencematcher=psm)))
 
 
1121
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
 
 
1123
    _test_needs_features = [CompiledPatienceDiffFeature]
 
 
1126
        super(TestPatienceDiffLibFiles_c, self).setUp()
 
 
1127
        import bzrlib._patiencediff_c
 
 
1128
        self._PatienceSequenceMatcher = \
 
 
1129
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
 
 
1132
class TestUsingCompiledIfAvailable(TestCase):
 
 
1134
    def test_PatienceSequenceMatcher(self):
 
 
1135
        if CompiledPatienceDiffFeature.available():
 
 
1136
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
 
 
1137
            self.assertIs(PatienceSequenceMatcher_c,
 
 
1138
                          bzrlib.patiencediff.PatienceSequenceMatcher)
 
 
1140
            from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
 
 
1141
            self.assertIs(PatienceSequenceMatcher_py,
 
 
1142
                          bzrlib.patiencediff.PatienceSequenceMatcher)
 
 
1144
    def test_unique_lcs(self):
 
 
1145
        if CompiledPatienceDiffFeature.available():
 
 
1146
            from bzrlib._patiencediff_c import unique_lcs_c
 
 
1147
            self.assertIs(unique_lcs_c,
 
 
1148
                          bzrlib.patiencediff.unique_lcs)
 
 
1150
            from bzrlib._patiencediff_py import unique_lcs_py
 
 
1151
            self.assertIs(unique_lcs_py,
 
 
1152
                          bzrlib.patiencediff.unique_lcs)
 
 
1154
    def test_recurse_matches(self):
 
 
1155
        if CompiledPatienceDiffFeature.available():
 
 
1156
            from bzrlib._patiencediff_c import recurse_matches_c
 
 
1157
            self.assertIs(recurse_matches_c,
 
 
1158
                          bzrlib.patiencediff.recurse_matches)
 
 
1160
            from bzrlib._patiencediff_py import recurse_matches_py
 
 
1161
            self.assertIs(recurse_matches_py,
 
 
1162
                          bzrlib.patiencediff.recurse_matches)