/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Aaron Bentley
  • Date: 2007-12-29 21:43:44 UTC
  • mto: This revision was merged to the branch mainline in revision 3162.
  • Revision ID: aaron.bentley@utoronto.ca-20071229214344-f24le3b1p943rywx
Fix locking issues in revert tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
import os
 
18
from cStringIO import StringIO
 
19
import errno
 
20
import subprocess
 
21
from tempfile import TemporaryFile
 
22
 
 
23
from bzrlib import tests
 
24
from bzrlib.diff import (
 
25
    DiffFromTool,
 
26
    DiffPath,
 
27
    DiffSymlink,
 
28
    DiffTree,
 
29
    DiffText,
 
30
    external_diff,
 
31
    internal_diff,
 
32
    show_diff_trees,
 
33
    )
 
34
from bzrlib.errors import BinaryFile, NoDiff
 
35
import bzrlib.osutils as osutils
 
36
import bzrlib.patiencediff
 
37
import bzrlib._patiencediff_py
 
38
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
 
39
                          TestCaseInTempDir, TestSkipped)
 
40
 
 
41
 
 
42
class _CompiledPatienceDiffFeature(Feature):
 
43
 
 
44
    def _probe(self):
 
45
        try:
 
46
            import bzrlib._patiencediff_c
 
47
        except ImportError:
 
48
            return False
 
49
        return True
 
50
 
 
51
    def feature_name(self):
 
52
        return 'bzrlib._patiencediff_c'
 
53
 
 
54
CompiledPatienceDiffFeature = _CompiledPatienceDiffFeature()
 
55
 
 
56
 
 
57
class _UnicodeFilename(Feature):
 
58
    """Does the filesystem support Unicode filenames?"""
 
59
 
 
60
    def _probe(self):
 
61
        try:
 
62
            os.stat(u'\u03b1')
 
63
        except UnicodeEncodeError:
 
64
            return False
 
65
        except (IOError, OSError):
 
66
            # The filesystem allows the Unicode filename but the file doesn't
 
67
            # exist.
 
68
            return True
 
69
        else:
 
70
            # The filesystem allows the Unicode filename and the file exists,
 
71
            # for some reason.
 
72
            return True
 
73
 
 
74
UnicodeFilename = _UnicodeFilename()
 
75
 
 
76
 
 
77
class TestUnicodeFilename(TestCase):
 
78
 
 
79
    def test_probe_passes(self):
 
80
        """UnicodeFilename._probe passes."""
 
81
        # We can't test much more than that because the behaviour depends
 
82
        # on the platform.
 
83
        UnicodeFilename._probe()
 
84
        
 
85
 
 
86
def udiff_lines(old, new, allow_binary=False):
 
87
    output = StringIO()
 
88
    internal_diff('old', old, 'new', new, output, allow_binary)
 
89
    output.seek(0, 0)
 
90
    return output.readlines()
 
91
 
 
92
 
 
93
def external_udiff_lines(old, new, use_stringio=False):
 
94
    if use_stringio:
 
95
        # StringIO has no fileno, so it tests a different codepath
 
96
        output = StringIO()
 
97
    else:
 
98
        output = TemporaryFile()
 
99
    try:
 
100
        external_diff('old', old, 'new', new, output, diff_opts=['-u'])
 
101
    except NoDiff:
 
102
        raise TestSkipped('external "diff" not present to test')
 
103
    output.seek(0, 0)
 
104
    lines = output.readlines()
 
105
    output.close()
 
106
    return lines
 
107
 
 
108
 
 
109
class TestDiff(TestCase):
 
110
 
 
111
    def test_add_nl(self):
 
112
        """diff generates a valid diff for patches that add a newline"""
 
113
        lines = udiff_lines(['boo'], ['boo\n'])
 
114
        self.check_patch(lines)
 
115
        self.assertEquals(lines[4], '\\ No newline at end of file\n')
 
116
            ## "expected no-nl, got %r" % lines[4]
 
117
 
 
118
    def test_add_nl_2(self):
 
119
        """diff generates a valid diff for patches that change last line and
 
120
        add a newline.
 
121
        """
 
122
        lines = udiff_lines(['boo'], ['goo\n'])
 
123
        self.check_patch(lines)
 
124
        self.assertEquals(lines[4], '\\ No newline at end of file\n')
 
125
            ## "expected no-nl, got %r" % lines[4]
 
126
 
 
127
    def test_remove_nl(self):
 
128
        """diff generates a valid diff for patches that change last line and
 
129
        add a newline.
 
130
        """
 
131
        lines = udiff_lines(['boo\n'], ['boo'])
 
132
        self.check_patch(lines)
 
133
        self.assertEquals(lines[5], '\\ No newline at end of file\n')
 
134
            ## "expected no-nl, got %r" % lines[5]
 
135
 
 
136
    def check_patch(self, lines):
 
137
        self.assert_(len(lines) > 1)
 
138
            ## "Not enough lines for a file header for patch:\n%s" % "".join(lines)
 
139
        self.assert_(lines[0].startswith ('---'))
 
140
            ## 'No orig line for patch:\n%s' % "".join(lines)
 
141
        self.assert_(lines[1].startswith ('+++'))
 
142
            ## 'No mod line for patch:\n%s' % "".join(lines)
 
143
        self.assert_(len(lines) > 2)
 
144
            ## "No hunks for patch:\n%s" % "".join(lines)
 
145
        self.assert_(lines[2].startswith('@@'))
 
146
            ## "No hunk header for patch:\n%s" % "".join(lines)
 
147
        self.assert_('@@' in lines[2][2:])
 
148
            ## "Unterminated hunk header for patch:\n%s" % "".join(lines)
 
149
 
 
150
    def test_binary_lines(self):
 
151
        self.assertRaises(BinaryFile, udiff_lines, [1023 * 'a' + '\x00'], [])
 
152
        self.assertRaises(BinaryFile, udiff_lines, [], [1023 * 'a' + '\x00'])
 
153
        udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
 
154
        udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
 
155
 
 
156
    def test_external_diff(self):
 
157
        lines = external_udiff_lines(['boo\n'], ['goo\n'])
 
158
        self.check_patch(lines)
 
159
        self.assertEqual('\n', lines[-1])
 
160
 
 
161
    def test_external_diff_no_fileno(self):
 
162
        # Make sure that we can handle not having a fileno, even
 
163
        # if the diff is large
 
164
        lines = external_udiff_lines(['boo\n']*10000,
 
165
                                     ['goo\n']*10000,
 
166
                                     use_stringio=True)
 
167
        self.check_patch(lines)
 
168
 
 
169
    def test_external_diff_binary_lang_c(self):
 
170
        old_env = {}
 
171
        for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
 
172
            old_env[lang] = osutils.set_or_unset_env(lang, 'C')
 
173
        try:
 
174
            lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
 
175
            # Older versions of diffutils say "Binary files", newer
 
176
            # versions just say "Files".
 
177
            self.assertContainsRe(lines[0],
 
178
                                  '(Binary f|F)iles old and new differ\n')
 
179
            self.assertEquals(lines[1:], ['\n'])
 
180
        finally:
 
181
            for lang, old_val in old_env.iteritems():
 
182
                osutils.set_or_unset_env(lang, old_val)
 
183
 
 
184
    def test_no_external_diff(self):
 
185
        """Check that NoDiff is raised when diff is not available"""
 
186
        # Use os.environ['PATH'] to make sure no 'diff' command is available
 
187
        orig_path = os.environ['PATH']
 
188
        try:
 
189
            os.environ['PATH'] = ''
 
190
            self.assertRaises(NoDiff, external_diff,
 
191
                              'old', ['boo\n'], 'new', ['goo\n'],
 
192
                              StringIO(), diff_opts=['-u'])
 
193
        finally:
 
194
            os.environ['PATH'] = orig_path
 
195
        
 
196
    def test_internal_diff_default(self):
 
197
        # Default internal diff encoding is utf8
 
198
        output = StringIO()
 
199
        internal_diff(u'old_\xb5', ['old_text\n'],
 
200
                    u'new_\xe5', ['new_text\n'], output)
 
201
        lines = output.getvalue().splitlines(True)
 
202
        self.check_patch(lines)
 
203
        self.assertEquals(['--- old_\xc2\xb5\n',
 
204
                           '+++ new_\xc3\xa5\n',
 
205
                           '@@ -1,1 +1,1 @@\n',
 
206
                           '-old_text\n',
 
207
                           '+new_text\n',
 
208
                           '\n',
 
209
                          ]
 
210
                          , lines)
 
211
 
 
212
    def test_internal_diff_utf8(self):
 
213
        output = StringIO()
 
214
        internal_diff(u'old_\xb5', ['old_text\n'],
 
215
                    u'new_\xe5', ['new_text\n'], output,
 
216
                    path_encoding='utf8')
 
217
        lines = output.getvalue().splitlines(True)
 
218
        self.check_patch(lines)
 
219
        self.assertEquals(['--- old_\xc2\xb5\n',
 
220
                           '+++ new_\xc3\xa5\n',
 
221
                           '@@ -1,1 +1,1 @@\n',
 
222
                           '-old_text\n',
 
223
                           '+new_text\n',
 
224
                           '\n',
 
225
                          ]
 
226
                          , lines)
 
227
 
 
228
    def test_internal_diff_iso_8859_1(self):
 
229
        output = StringIO()
 
230
        internal_diff(u'old_\xb5', ['old_text\n'],
 
231
                    u'new_\xe5', ['new_text\n'], output,
 
232
                    path_encoding='iso-8859-1')
 
233
        lines = output.getvalue().splitlines(True)
 
234
        self.check_patch(lines)
 
235
        self.assertEquals(['--- old_\xb5\n',
 
236
                           '+++ new_\xe5\n',
 
237
                           '@@ -1,1 +1,1 @@\n',
 
238
                           '-old_text\n',
 
239
                           '+new_text\n',
 
240
                           '\n',
 
241
                          ]
 
242
                          , lines)
 
243
 
 
244
    def test_internal_diff_no_content(self):
 
245
        output = StringIO()
 
246
        internal_diff(u'old', [], u'new', [], output)
 
247
        self.assertEqual('', output.getvalue())
 
248
 
 
249
    def test_internal_diff_no_changes(self):
 
250
        output = StringIO()
 
251
        internal_diff(u'old', ['text\n', 'contents\n'],
 
252
                      u'new', ['text\n', 'contents\n'],
 
253
                      output)
 
254
        self.assertEqual('', output.getvalue())
 
255
 
 
256
    def test_internal_diff_returns_bytes(self):
 
257
        import StringIO
 
258
        output = StringIO.StringIO()
 
259
        internal_diff(u'old_\xb5', ['old_text\n'],
 
260
                    u'new_\xe5', ['new_text\n'], output)
 
261
        self.failUnless(isinstance(output.getvalue(), str),
 
262
            'internal_diff should return bytestrings')
 
263
 
 
264
 
 
265
class TestDiffFiles(TestCaseInTempDir):
 
266
 
 
267
    def test_external_diff_binary(self):
 
268
        """The output when using external diff should use diff's i18n error"""
 
269
        # Make sure external_diff doesn't fail in the current LANG
 
270
        lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
 
271
 
 
272
        cmd = ['diff', '-u', '--binary', 'old', 'new']
 
273
        open('old', 'wb').write('\x00foobar\n')
 
274
        open('new', 'wb').write('foo\x00bar\n')
 
275
        pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
 
276
                                     stdin=subprocess.PIPE)
 
277
        out, err = pipe.communicate()
 
278
        # Diff returns '2' on Binary files.
 
279
        self.assertEqual(2, pipe.returncode)
 
280
        # We should output whatever diff tells us, plus a trailing newline
 
281
        self.assertEqual(out.splitlines(True) + ['\n'], lines)
 
282
 
 
283
 
 
284
class TestShowDiffTreesHelper(TestCaseWithTransport):
 
285
    """Has a helper for running show_diff_trees"""
 
286
 
 
287
    def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
 
288
        output = StringIO()
 
289
        if working_tree is not None:
 
290
            extra_trees = (working_tree,)
 
291
        else:
 
292
            extra_trees = ()
 
293
        show_diff_trees(tree1, tree2, output, specific_files=specific_files,
 
294
                        extra_trees=extra_trees, old_label='old/',
 
295
                        new_label='new/')
 
296
        return output.getvalue()
 
297
 
 
298
 
 
299
class TestDiffDates(TestShowDiffTreesHelper):
 
300
 
 
301
    def setUp(self):
 
302
        super(TestDiffDates, self).setUp()
 
303
        self.wt = self.make_branch_and_tree('.')
 
304
        self.b = self.wt.branch
 
305
        self.build_tree_contents([
 
306
            ('file1', 'file1 contents at rev 1\n'),
 
307
            ('file2', 'file2 contents at rev 1\n')
 
308
            ])
 
309
        self.wt.add(['file1', 'file2'])
 
310
        self.wt.commit(
 
311
            message='Revision 1',
 
312
            timestamp=1143849600, # 2006-04-01 00:00:00 UTC
 
313
            timezone=0,
 
314
            rev_id='rev-1')
 
315
        self.build_tree_contents([('file1', 'file1 contents at rev 2\n')])
 
316
        self.wt.commit(
 
317
            message='Revision 2',
 
318
            timestamp=1143936000, # 2006-04-02 00:00:00 UTC
 
319
            timezone=28800,
 
320
            rev_id='rev-2')
 
321
        self.build_tree_contents([('file2', 'file2 contents at rev 3\n')])
 
322
        self.wt.commit(
 
323
            message='Revision 3',
 
324
            timestamp=1144022400, # 2006-04-03 00:00:00 UTC
 
325
            timezone=-3600,
 
326
            rev_id='rev-3')
 
327
        self.wt.remove(['file2'])
 
328
        self.wt.commit(
 
329
            message='Revision 4',
 
330
            timestamp=1144108800, # 2006-04-04 00:00:00 UTC
 
331
            timezone=0,
 
332
            rev_id='rev-4')
 
333
        self.build_tree_contents([
 
334
            ('file1', 'file1 contents in working tree\n')
 
335
            ])
 
336
        # set the date stamps for files in the working tree to known values
 
337
        os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
 
338
 
 
339
    def test_diff_rev_tree_working_tree(self):
 
340
        output = self.get_diff(self.wt.basis_tree(), self.wt)
 
341
        # note that the date for old/file1 is from rev 2 rather than from
 
342
        # the basis revision (rev 4)
 
343
        self.assertEqualDiff(output, '''\
 
344
=== modified file 'file1'
 
345
--- old/file1\t2006-04-02 00:00:00 +0000
 
346
+++ new/file1\t2006-04-05 00:00:00 +0000
 
347
@@ -1,1 +1,1 @@
 
348
-file1 contents at rev 2
 
349
+file1 contents in working tree
 
350
 
 
351
''')
 
352
 
 
353
    def test_diff_rev_tree_rev_tree(self):
 
354
        tree1 = self.b.repository.revision_tree('rev-2')
 
355
        tree2 = self.b.repository.revision_tree('rev-3')
 
356
        output = self.get_diff(tree1, tree2)
 
357
        self.assertEqualDiff(output, '''\
 
358
=== modified file 'file2'
 
359
--- old/file2\t2006-04-01 00:00:00 +0000
 
360
+++ new/file2\t2006-04-03 00:00:00 +0000
 
361
@@ -1,1 +1,1 @@
 
362
-file2 contents at rev 1
 
363
+file2 contents at rev 3
 
364
 
 
365
''')
 
366
        
 
367
    def test_diff_add_files(self):
 
368
        tree1 = self.b.repository.revision_tree(None)
 
369
        tree2 = self.b.repository.revision_tree('rev-1')
 
370
        output = self.get_diff(tree1, tree2)
 
371
        # the files have the epoch time stamp for the tree in which
 
372
        # they don't exist.
 
373
        self.assertEqualDiff(output, '''\
 
374
=== added file 'file1'
 
375
--- old/file1\t1970-01-01 00:00:00 +0000
 
376
+++ new/file1\t2006-04-01 00:00:00 +0000
 
377
@@ -0,0 +1,1 @@
 
378
+file1 contents at rev 1
 
379
 
 
380
=== added file 'file2'
 
381
--- old/file2\t1970-01-01 00:00:00 +0000
 
382
+++ new/file2\t2006-04-01 00:00:00 +0000
 
383
@@ -0,0 +1,1 @@
 
384
+file2 contents at rev 1
 
385
 
 
386
''')
 
387
 
 
388
    def test_diff_remove_files(self):
 
389
        tree1 = self.b.repository.revision_tree('rev-3')
 
390
        tree2 = self.b.repository.revision_tree('rev-4')
 
391
        output = self.get_diff(tree1, tree2)
 
392
        # the file has the epoch time stamp for the tree in which
 
393
        # it doesn't exist.
 
394
        self.assertEqualDiff(output, '''\
 
395
=== removed file 'file2'
 
396
--- old/file2\t2006-04-03 00:00:00 +0000
 
397
+++ new/file2\t1970-01-01 00:00:00 +0000
 
398
@@ -1,1 +0,0 @@
 
399
-file2 contents at rev 3
 
400
 
 
401
''')
 
402
 
 
403
    def test_show_diff_specified(self):
 
404
        """A working tree filename can be used to identify a file"""
 
405
        self.wt.rename_one('file1', 'file1b')
 
406
        old_tree = self.b.repository.revision_tree('rev-1')
 
407
        new_tree = self.b.repository.revision_tree('rev-4')
 
408
        out = self.get_diff(old_tree, new_tree, specific_files=['file1b'], 
 
409
                            working_tree=self.wt)
 
410
        self.assertContainsRe(out, 'file1\t')
 
411
 
 
412
    def test_recursive_diff(self):
 
413
        """Children of directories are matched"""
 
414
        os.mkdir('dir1')
 
415
        os.mkdir('dir2')
 
416
        self.wt.add(['dir1', 'dir2'])
 
417
        self.wt.rename_one('file1', 'dir1/file1')
 
418
        old_tree = self.b.repository.revision_tree('rev-1')
 
419
        new_tree = self.b.repository.revision_tree('rev-4')
 
420
        out = self.get_diff(old_tree, new_tree, specific_files=['dir1'], 
 
421
                            working_tree=self.wt)
 
422
        self.assertContainsRe(out, 'file1\t')
 
423
        out = self.get_diff(old_tree, new_tree, specific_files=['dir2'], 
 
424
                            working_tree=self.wt)
 
425
        self.assertNotContainsRe(out, 'file1\t')
 
426
 
 
427
 
 
428
 
 
429
class TestShowDiffTrees(TestShowDiffTreesHelper):
 
430
    """Direct tests for show_diff_trees"""
 
431
 
 
432
    def test_modified_file(self):
 
433
        """Test when a file is modified."""
 
434
        tree = self.make_branch_and_tree('tree')
 
435
        self.build_tree_contents([('tree/file', 'contents\n')])
 
436
        tree.add(['file'], ['file-id'])
 
437
        tree.commit('one', rev_id='rev-1')
 
438
 
 
439
        self.build_tree_contents([('tree/file', 'new contents\n')])
 
440
        diff = self.get_diff(tree.basis_tree(), tree)
 
441
        self.assertContainsRe(diff, "=== modified file 'file'\n")
 
442
        self.assertContainsRe(diff, '--- old/file\t')
 
443
        self.assertContainsRe(diff, '\\+\\+\\+ new/file\t')
 
444
        self.assertContainsRe(diff, '-contents\n'
 
445
                                    '\\+new contents\n')
 
446
 
 
447
    def test_modified_file_in_renamed_dir(self):
 
448
        """Test when a file is modified in a renamed directory."""
 
449
        tree = self.make_branch_and_tree('tree')
 
450
        self.build_tree(['tree/dir/'])
 
451
        self.build_tree_contents([('tree/dir/file', 'contents\n')])
 
452
        tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
453
        tree.commit('one', rev_id='rev-1')
 
454
 
 
455
        tree.rename_one('dir', 'other')
 
456
        self.build_tree_contents([('tree/other/file', 'new contents\n')])
 
457
        diff = self.get_diff(tree.basis_tree(), tree)
 
458
        self.assertContainsRe(diff, "=== renamed directory 'dir' => 'other'\n")
 
459
        self.assertContainsRe(diff, "=== modified file 'other/file'\n")
 
460
        # XXX: This is technically incorrect, because it used to be at another
 
461
        # location. What to do?
 
462
        self.assertContainsRe(diff, '--- old/dir/file\t')
 
463
        self.assertContainsRe(diff, '\\+\\+\\+ new/other/file\t')
 
464
        self.assertContainsRe(diff, '-contents\n'
 
465
                                    '\\+new contents\n')
 
466
 
 
467
    def test_renamed_directory(self):
 
468
        """Test when only a directory is only renamed."""
 
469
        tree = self.make_branch_and_tree('tree')
 
470
        self.build_tree(['tree/dir/'])
 
471
        self.build_tree_contents([('tree/dir/file', 'contents\n')])
 
472
        tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
473
        tree.commit('one', rev_id='rev-1')
 
474
 
 
475
        tree.rename_one('dir', 'newdir')
 
476
        diff = self.get_diff(tree.basis_tree(), tree)
 
477
        # Renaming a directory should be a single "you renamed this dir" even
 
478
        # when there are files inside.
 
479
        self.assertEqual("=== renamed directory 'dir' => 'newdir'\n", diff)
 
480
 
 
481
    def test_renamed_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')
 
487
 
 
488
        tree.rename_one('file', 'newname')
 
489
        diff = self.get_diff(tree.basis_tree(), tree)
 
490
        self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
 
491
        # We shouldn't have a --- or +++ line, because there is no content
 
492
        # change
 
493
        self.assertNotContainsRe(diff, '---')
 
494
 
 
495
    def test_renamed_and_modified_file(self):
 
496
        """Test when a file is only renamed."""
 
497
        tree = self.make_branch_and_tree('tree')
 
498
        self.build_tree_contents([('tree/file', 'contents\n')])
 
499
        tree.add(['file'], ['file-id'])
 
500
        tree.commit('one', rev_id='rev-1')
 
501
 
 
502
        tree.rename_one('file', 'newname')
 
503
        self.build_tree_contents([('tree/newname', 'new contents\n')])
 
504
        diff = self.get_diff(tree.basis_tree(), tree)
 
505
        self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
 
506
        self.assertContainsRe(diff, '--- old/file\t')
 
507
        self.assertContainsRe(diff, '\\+\\+\\+ new/newname\t')
 
508
        self.assertContainsRe(diff, '-contents\n'
 
509
                                    '\\+new contents\n')
 
510
 
 
511
    def test_binary_unicode_filenames(self):
 
512
        """Test that contents of files are *not* encoded in UTF-8 when there
 
513
        is a binary file in the diff.
 
514
        """
 
515
        # See https://bugs.launchpad.net/bugs/110092.
 
516
        self.requireFeature(UnicodeFilename)
 
517
 
 
518
        # This bug isn't triggered with cStringIO.
 
519
        from StringIO import StringIO
 
520
        tree = self.make_branch_and_tree('tree')
 
521
        alpha, omega = u'\u03b1', u'\u03c9'
 
522
        alpha_utf8, omega_utf8 = alpha.encode('utf8'), omega.encode('utf8')
 
523
        self.build_tree_contents(
 
524
            [('tree/' + alpha, chr(0)),
 
525
             ('tree/' + omega,
 
526
              ('The %s and the %s\n' % (alpha_utf8, omega_utf8)))])
 
527
        tree.add([alpha], ['file-id'])
 
528
        tree.add([omega], ['file-id-2'])
 
529
        diff_content = StringIO()
 
530
        show_diff_trees(tree.basis_tree(), tree, diff_content)
 
531
        diff = diff_content.getvalue()
 
532
        self.assertContainsRe(diff, r"=== added file '%s'" % alpha_utf8)
 
533
        self.assertContainsRe(
 
534
            diff, "Binary files a/%s.*and b/%s.* differ\n" % (alpha_utf8, alpha_utf8))
 
535
        self.assertContainsRe(diff, r"=== added file '%s'" % omega_utf8)
 
536
        self.assertContainsRe(diff, r"--- a/%s" % (omega_utf8,))
 
537
        self.assertContainsRe(diff, r"\+\+\+ b/%s" % (omega_utf8,))
 
538
 
 
539
    def test_unicode_filename(self):
 
540
        """Test when the filename are unicode."""
 
541
        self.requireFeature(UnicodeFilename)
 
542
 
 
543
        alpha, omega = u'\u03b1', u'\u03c9'
 
544
        autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
 
545
 
 
546
        tree = self.make_branch_and_tree('tree')
 
547
        self.build_tree_contents([('tree/ren_'+alpha, 'contents\n')])
 
548
        tree.add(['ren_'+alpha], ['file-id-2'])
 
549
        self.build_tree_contents([('tree/del_'+alpha, 'contents\n')])
 
550
        tree.add(['del_'+alpha], ['file-id-3'])
 
551
        self.build_tree_contents([('tree/mod_'+alpha, 'contents\n')])
 
552
        tree.add(['mod_'+alpha], ['file-id-4'])
 
553
 
 
554
        tree.commit('one', rev_id='rev-1')
 
555
 
 
556
        tree.rename_one('ren_'+alpha, 'ren_'+omega)
 
557
        tree.remove('del_'+alpha)
 
558
        self.build_tree_contents([('tree/add_'+alpha, 'contents\n')])
 
559
        tree.add(['add_'+alpha], ['file-id'])
 
560
        self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
 
561
 
 
562
        diff = self.get_diff(tree.basis_tree(), tree)
 
563
        self.assertContainsRe(diff,
 
564
                "=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
 
565
        self.assertContainsRe(diff, "=== added file 'add_%s'"%autf8)
 
566
        self.assertContainsRe(diff, "=== modified file 'mod_%s'"%autf8)
 
567
        self.assertContainsRe(diff, "=== removed file 'del_%s'"%autf8)
 
568
 
 
569
 
 
570
class DiffWasIs(DiffPath):
 
571
 
 
572
    def diff(self, file_id, old_path, new_path, old_kind, new_kind):
 
573
        self.to_file.write('was: ')
 
574
        self.to_file.write(self.old_tree.get_file(file_id).read())
 
575
        self.to_file.write('is: ')
 
576
        self.to_file.write(self.new_tree.get_file(file_id).read())
 
577
        pass
 
578
 
 
579
 
 
580
class TestDiffTree(TestCaseWithTransport):
 
581
 
 
582
    def setUp(self):
 
583
        TestCaseWithTransport.setUp(self)
 
584
        self.old_tree = self.make_branch_and_tree('old-tree')
 
585
        self.old_tree.lock_write()
 
586
        self.addCleanup(self.old_tree.unlock)
 
587
        self.new_tree = self.make_branch_and_tree('new-tree')
 
588
        self.new_tree.lock_write()
 
589
        self.addCleanup(self.new_tree.unlock)
 
590
        self.differ = DiffTree(self.old_tree, self.new_tree, StringIO())
 
591
 
 
592
    def test_diff_text(self):
 
593
        self.build_tree_contents([('old-tree/olddir/',),
 
594
                                  ('old-tree/olddir/oldfile', 'old\n')])
 
595
        self.old_tree.add('olddir')
 
596
        self.old_tree.add('olddir/oldfile', 'file-id')
 
597
        self.build_tree_contents([('new-tree/newdir/',),
 
598
                                  ('new-tree/newdir/newfile', 'new\n')])
 
599
        self.new_tree.add('newdir')
 
600
        self.new_tree.add('newdir/newfile', 'file-id')
 
601
        differ = DiffText(self.old_tree, self.new_tree, StringIO())
 
602
        differ.diff_text('file-id', None, 'old label', 'new label')
 
603
        self.assertEqual(
 
604
            '--- old label\n+++ new label\n@@ -1,1 +0,0 @@\n-old\n\n',
 
605
            differ.to_file.getvalue())
 
606
        differ.to_file.seek(0)
 
607
        differ.diff_text(None, 'file-id', 'old label', 'new label')
 
608
        self.assertEqual(
 
609
            '--- old label\n+++ new label\n@@ -0,0 +1,1 @@\n+new\n\n',
 
610
            differ.to_file.getvalue())
 
611
        differ.to_file.seek(0)
 
612
        differ.diff_text('file-id', 'file-id', 'old label', 'new label')
 
613
        self.assertEqual(
 
614
            '--- old label\n+++ new label\n@@ -1,1 +1,1 @@\n-old\n+new\n\n',
 
615
            differ.to_file.getvalue())
 
616
 
 
617
    def test_diff_deletion(self):
 
618
        self.build_tree_contents([('old-tree/file', 'contents'),
 
619
                                  ('new-tree/file', 'contents')])
 
620
        self.old_tree.add('file', 'file-id')
 
621
        self.new_tree.add('file', 'file-id')
 
622
        os.unlink('new-tree/file')
 
623
        self.differ.show_diff(None)
 
624
        self.assertContainsRe(self.differ.to_file.getvalue(), '-contents')
 
625
 
 
626
    def test_diff_creation(self):
 
627
        self.build_tree_contents([('old-tree/file', 'contents'),
 
628
                                  ('new-tree/file', 'contents')])
 
629
        self.old_tree.add('file', 'file-id')
 
630
        self.new_tree.add('file', 'file-id')
 
631
        os.unlink('old-tree/file')
 
632
        self.differ.show_diff(None)
 
633
        self.assertContainsRe(self.differ.to_file.getvalue(), '\+contents')
 
634
 
 
635
    def test_diff_symlink(self):
 
636
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
 
637
        differ.diff_symlink('old target', None)
 
638
        self.assertEqual("=== target was 'old target'\n",
 
639
                         differ.to_file.getvalue())
 
640
 
 
641
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
 
642
        differ.diff_symlink(None, 'new target')
 
643
        self.assertEqual("=== target is 'new target'\n",
 
644
                         differ.to_file.getvalue())
 
645
 
 
646
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
 
647
        differ.diff_symlink('old target', 'new target')
 
648
        self.assertEqual("=== target changed 'old target' => 'new target'\n",
 
649
                         differ.to_file.getvalue())
 
650
 
 
651
    def test_diff(self):
 
652
        self.build_tree_contents([('old-tree/olddir/',),
 
653
                                  ('old-tree/olddir/oldfile', 'old\n')])
 
654
        self.old_tree.add('olddir')
 
655
        self.old_tree.add('olddir/oldfile', 'file-id')
 
656
        self.build_tree_contents([('new-tree/newdir/',),
 
657
                                  ('new-tree/newdir/newfile', 'new\n')])
 
658
        self.new_tree.add('newdir')
 
659
        self.new_tree.add('newdir/newfile', 'file-id')
 
660
        self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
 
661
        self.assertContainsRe(
 
662
            self.differ.to_file.getvalue(),
 
663
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
 
664
             ' \@\@\n-old\n\+new\n\n')
 
665
 
 
666
    def test_diff_kind_change(self):
 
667
        self.requireFeature(tests.SymlinkFeature)
 
668
        self.build_tree_contents([('old-tree/olddir/',),
 
669
                                  ('old-tree/olddir/oldfile', 'old\n')])
 
670
        self.old_tree.add('olddir')
 
671
        self.old_tree.add('olddir/oldfile', 'file-id')
 
672
        self.build_tree(['new-tree/newdir/'])
 
673
        os.symlink('new', 'new-tree/newdir/newfile')
 
674
        self.new_tree.add('newdir')
 
675
        self.new_tree.add('newdir/newfile', 'file-id')
 
676
        self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
 
677
        self.assertContainsRe(
 
678
            self.differ.to_file.getvalue(),
 
679
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+0,0'
 
680
             ' \@\@\n-old\n\n')
 
681
        self.assertContainsRe(self.differ.to_file.getvalue(),
 
682
                              "=== target is 'new'\n")
 
683
 
 
684
    def test_diff_directory(self):
 
685
        self.build_tree(['new-tree/new-dir/'])
 
686
        self.new_tree.add('new-dir', 'new-dir-id')
 
687
        self.differ.diff('new-dir-id', None, 'new-dir')
 
688
        self.assertEqual(self.differ.to_file.getvalue(), '')
 
689
 
 
690
    def create_old_new(self):
 
691
        self.build_tree_contents([('old-tree/olddir/',),
 
692
                                  ('old-tree/olddir/oldfile', 'old\n')])
 
693
        self.old_tree.add('olddir')
 
694
        self.old_tree.add('olddir/oldfile', 'file-id')
 
695
        self.build_tree_contents([('new-tree/newdir/',),
 
696
                                  ('new-tree/newdir/newfile', 'new\n')])
 
697
        self.new_tree.add('newdir')
 
698
        self.new_tree.add('newdir/newfile', 'file-id')
 
699
 
 
700
    def test_register_diff(self):
 
701
        self.create_old_new()
 
702
        old_diff_factories = DiffTree.diff_factories
 
703
        DiffTree.diff_factories=old_diff_factories[:]
 
704
        DiffTree.diff_factories.insert(0, DiffWasIs.from_diff_tree)
 
705
        try:
 
706
            differ = DiffTree(self.old_tree, self.new_tree, StringIO())
 
707
        finally:
 
708
            DiffTree.diff_factories = old_diff_factories
 
709
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
 
710
        self.assertNotContainsRe(
 
711
            differ.to_file.getvalue(),
 
712
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
 
713
             ' \@\@\n-old\n\+new\n\n')
 
714
        self.assertContainsRe(differ.to_file.getvalue(),
 
715
                              'was: old\nis: new\n')
 
716
 
 
717
    def test_extra_factories(self):
 
718
        self.create_old_new()
 
719
        differ = DiffTree(self.old_tree, self.new_tree, StringIO(),
 
720
                            extra_factories=[DiffWasIs.from_diff_tree])
 
721
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
 
722
        self.assertNotContainsRe(
 
723
            differ.to_file.getvalue(),
 
724
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
 
725
             ' \@\@\n-old\n\+new\n\n')
 
726
        self.assertContainsRe(differ.to_file.getvalue(),
 
727
                              'was: old\nis: new\n')
 
728
 
 
729
    def test_alphabetical_order(self):
 
730
        self.build_tree(['new-tree/a-file'])
 
731
        self.new_tree.add('a-file')
 
732
        self.build_tree(['old-tree/b-file'])
 
733
        self.old_tree.add('b-file')
 
734
        self.differ.show_diff(None)
 
735
        self.assertContainsRe(self.differ.to_file.getvalue(),
 
736
            '.*a-file(.|\n)*b-file')
 
737
 
 
738
 
 
739
class TestPatienceDiffLib(TestCase):
 
740
 
 
741
    def setUp(self):
 
742
        super(TestPatienceDiffLib, self).setUp()
 
743
        self._unique_lcs = bzrlib._patiencediff_py.unique_lcs_py
 
744
        self._recurse_matches = bzrlib._patiencediff_py.recurse_matches_py
 
745
        self._PatienceSequenceMatcher = \
 
746
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
 
747
 
 
748
    def test_unique_lcs(self):
 
749
        unique_lcs = self._unique_lcs
 
750
        self.assertEquals(unique_lcs('', ''), [])
 
751
        self.assertEquals(unique_lcs('', 'a'), [])
 
752
        self.assertEquals(unique_lcs('a', ''), [])
 
753
        self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
 
754
        self.assertEquals(unique_lcs('a', 'b'), [])
 
755
        self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
 
756
        self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
 
757
        self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
 
758
        self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1), 
 
759
                                                         (3,3), (4,4)])
 
760
        self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
 
761
 
 
762
    def test_recurse_matches(self):
 
763
        def test_one(a, b, matches):
 
764
            test_matches = []
 
765
            self._recurse_matches(
 
766
                a, b, 0, 0, len(a), len(b), test_matches, 10)
 
767
            self.assertEquals(test_matches, matches)
 
768
 
 
769
        test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
 
770
                 [(0, 0), (2, 2), (4, 4)])
 
771
        test_one(['a', 'c', 'b', 'a', 'c'], ['a', 'b', 'c'],
 
772
                 [(0, 0), (2, 1), (4, 2)])
 
773
        # Even though 'bc' is not unique globally, and is surrounded by
 
774
        # non-matching lines, we should still match, because they are locally
 
775
        # unique
 
776
        test_one('abcdbce', 'afbcgdbce', [(0,0), (1, 2), (2, 3), (3, 5),
 
777
                                          (4, 6), (5, 7), (6, 8)])
 
778
 
 
779
        # recurse_matches doesn't match non-unique 
 
780
        # lines surrounded by bogus text.
 
781
        # The update has been done in patiencediff.SequenceMatcher instead
 
782
 
 
783
        # This is what it could be
 
784
        #test_one('aBccDe', 'abccde', [(0,0), (2,2), (3,3), (5,5)])
 
785
 
 
786
        # This is what it currently gives:
 
787
        test_one('aBccDe', 'abccde', [(0,0), (5,5)])
 
788
 
 
789
    def assertDiffBlocks(self, a, b, expected_blocks):
 
790
        """Check that the sequence matcher returns the correct blocks.
 
791
 
 
792
        :param a: A sequence to match
 
793
        :param b: Another sequence to match
 
794
        :param expected_blocks: The expected output, not including the final
 
795
            matching block (len(a), len(b), 0)
 
796
        """
 
797
        matcher = self._PatienceSequenceMatcher(None, a, b)
 
798
        blocks = matcher.get_matching_blocks()
 
799
        last = blocks.pop()
 
800
        self.assertEqual((len(a), len(b), 0), last)
 
801
        self.assertEqual(expected_blocks, blocks)
 
802
 
 
803
    def test_matching_blocks(self):
 
804
        # Some basic matching tests
 
805
        self.assertDiffBlocks('', '', [])
 
806
        self.assertDiffBlocks([], [], [])
 
807
        self.assertDiffBlocks('abc', '', [])
 
808
        self.assertDiffBlocks('', 'abc', [])
 
809
        self.assertDiffBlocks('abcd', 'abcd', [(0, 0, 4)])
 
810
        self.assertDiffBlocks('abcd', 'abce', [(0, 0, 3)])
 
811
        self.assertDiffBlocks('eabc', 'abce', [(1, 0, 3)])
 
812
        self.assertDiffBlocks('eabce', 'abce', [(1, 0, 4)])
 
813
        self.assertDiffBlocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
 
814
        self.assertDiffBlocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
 
815
        self.assertDiffBlocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
 
816
        # This may check too much, but it checks to see that
 
817
        # a copied block stays attached to the previous section,
 
818
        # not the later one.
 
819
        # difflib would tend to grab the trailing longest match
 
820
        # which would make the diff not look right
 
821
        self.assertDiffBlocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
822
                              [(0, 0, 6), (6, 11, 10)])
 
823
 
 
824
        # make sure it supports passing in lists
 
825
        self.assertDiffBlocks(
 
826
                   ['hello there\n',
 
827
                    'world\n',
 
828
                    'how are you today?\n'],
 
829
                   ['hello there\n',
 
830
                    'how are you today?\n'],
 
831
                [(0, 0, 1), (2, 1, 1)])
 
832
 
 
833
        # non unique lines surrounded by non-matching lines
 
834
        # won't be found
 
835
        self.assertDiffBlocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
 
836
 
 
837
        # But they only need to be locally unique
 
838
        self.assertDiffBlocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
 
839
 
 
840
        # non unique blocks won't be matched
 
841
        self.assertDiffBlocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
 
842
 
 
843
        # but locally unique ones will
 
844
        self.assertDiffBlocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
 
845
                                              (5,4,1), (7,5,2), (10,8,1)])
 
846
 
 
847
        self.assertDiffBlocks('abbabbXd', 'cabbabxd', [(7,7,1)])
 
848
        self.assertDiffBlocks('abbabbbb', 'cabbabbc', [])
 
849
        self.assertDiffBlocks('bbbbbbbb', 'cbbbbbbc', [])
 
850
 
 
851
    def test_matching_blocks_tuples(self):
 
852
        # Some basic matching tests
 
853
        self.assertDiffBlocks([], [], [])
 
854
        self.assertDiffBlocks([('a',), ('b',), ('c,')], [], [])
 
855
        self.assertDiffBlocks([], [('a',), ('b',), ('c,')], [])
 
856
        self.assertDiffBlocks([('a',), ('b',), ('c,')],
 
857
                              [('a',), ('b',), ('c,')],
 
858
                              [(0, 0, 3)])
 
859
        self.assertDiffBlocks([('a',), ('b',), ('c,')],
 
860
                              [('a',), ('b',), ('d,')],
 
861
                              [(0, 0, 2)])
 
862
        self.assertDiffBlocks([('d',), ('b',), ('c,')],
 
863
                              [('a',), ('b',), ('c,')],
 
864
                              [(1, 1, 2)])
 
865
        self.assertDiffBlocks([('d',), ('a',), ('b',), ('c,')],
 
866
                              [('a',), ('b',), ('c,')],
 
867
                              [(1, 0, 3)])
 
868
        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
 
869
                              [('a', 'b'), ('c', 'X'), ('e', 'f')],
 
870
                              [(0, 0, 1), (2, 2, 1)])
 
871
        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
 
872
                              [('a', 'b'), ('c', 'dX'), ('e', 'f')],
 
873
                              [(0, 0, 1), (2, 2, 1)])
 
874
 
 
875
    def test_opcodes(self):
 
876
        def chk_ops(a, b, expected_codes):
 
877
            s = self._PatienceSequenceMatcher(None, a, b)
 
878
            self.assertEquals(expected_codes, s.get_opcodes())
 
879
 
 
880
        chk_ops('', '', [])
 
881
        chk_ops([], [], [])
 
882
        chk_ops('abc', '', [('delete', 0,3, 0,0)])
 
883
        chk_ops('', 'abc', [('insert', 0,0, 0,3)])
 
884
        chk_ops('abcd', 'abcd', [('equal',    0,4, 0,4)])
 
885
        chk_ops('abcd', 'abce', [('equal',   0,3, 0,3),
 
886
                                 ('replace', 3,4, 3,4)
 
887
                                ])
 
888
        chk_ops('eabc', 'abce', [('delete', 0,1, 0,0),
 
889
                                 ('equal',  1,4, 0,3),
 
890
                                 ('insert', 4,4, 3,4)
 
891
                                ])
 
892
        chk_ops('eabce', 'abce', [('delete', 0,1, 0,0),
 
893
                                  ('equal',  1,5, 0,4)
 
894
                                 ])
 
895
        chk_ops('abcde', 'abXde', [('equal',   0,2, 0,2),
 
896
                                   ('replace', 2,3, 2,3),
 
897
                                   ('equal',   3,5, 3,5)
 
898
                                  ])
 
899
        chk_ops('abcde', 'abXYZde', [('equal',   0,2, 0,2),
 
900
                                     ('replace', 2,3, 2,5),
 
901
                                     ('equal',   3,5, 5,7)
 
902
                                    ])
 
903
        chk_ops('abde', 'abXYZde', [('equal',  0,2, 0,2),
 
904
                                    ('insert', 2,2, 2,5),
 
905
                                    ('equal',  2,4, 5,7)
 
906
                                   ])
 
907
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
908
                [('equal',  0,6,  0,6),
 
909
                 ('insert', 6,6,  6,11),
 
910
                 ('equal',  6,16, 11,21)
 
911
                ])
 
912
        chk_ops(
 
913
                [ 'hello there\n'
 
914
                , 'world\n'
 
915
                , 'how are you today?\n'],
 
916
                [ 'hello there\n'
 
917
                , 'how are you today?\n'],
 
918
                [('equal',  0,1, 0,1),
 
919
                 ('delete', 1,2, 1,1),
 
920
                 ('equal',  2,3, 1,2),
 
921
                ])
 
922
        chk_ops('aBccDe', 'abccde', 
 
923
                [('equal',   0,1, 0,1),
 
924
                 ('replace', 1,5, 1,5),
 
925
                 ('equal',   5,6, 5,6),
 
926
                ])
 
927
        chk_ops('aBcDec', 'abcdec', 
 
928
                [('equal',   0,1, 0,1),
 
929
                 ('replace', 1,2, 1,2),
 
930
                 ('equal',   2,3, 2,3),
 
931
                 ('replace', 3,4, 3,4),
 
932
                 ('equal',   4,6, 4,6),
 
933
                ])
 
934
        chk_ops('aBcdEcdFg', 'abcdecdfg', 
 
935
                [('equal',   0,1, 0,1),
 
936
                 ('replace', 1,8, 1,8),
 
937
                 ('equal',   8,9, 8,9)
 
938
                ])
 
939
        chk_ops('aBcdEeXcdFg', 'abcdecdfg', 
 
940
                [('equal',   0,1, 0,1),
 
941
                 ('replace', 1,2, 1,2),
 
942
                 ('equal',   2,4, 2,4),
 
943
                 ('delete', 4,5, 4,4),
 
944
                 ('equal',   5,6, 4,5),
 
945
                 ('delete', 6,7, 5,5),
 
946
                 ('equal',   7,9, 5,7),
 
947
                 ('replace', 9,10, 7,8),
 
948
                 ('equal',   10,11, 8,9)
 
949
                ])
 
950
 
 
951
    def test_grouped_opcodes(self):
 
952
        def chk_ops(a, b, expected_codes, n=3):
 
953
            s = self._PatienceSequenceMatcher(None, a, b)
 
954
            self.assertEquals(expected_codes, list(s.get_grouped_opcodes(n)))
 
955
 
 
956
        chk_ops('', '', [])
 
957
        chk_ops([], [], [])
 
958
        chk_ops('abc', '', [[('delete', 0,3, 0,0)]])
 
959
        chk_ops('', 'abc', [[('insert', 0,0, 0,3)]])
 
960
        chk_ops('abcd', 'abcd', [])
 
961
        chk_ops('abcd', 'abce', [[('equal',   0,3, 0,3),
 
962
                                  ('replace', 3,4, 3,4)
 
963
                                 ]])
 
964
        chk_ops('eabc', 'abce', [[('delete', 0,1, 0,0),
 
965
                                 ('equal',  1,4, 0,3),
 
966
                                 ('insert', 4,4, 3,4)
 
967
                                ]])
 
968
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
969
                [[('equal',  3,6, 3,6),
 
970
                  ('insert', 6,6, 6,11),
 
971
                  ('equal',  6,9, 11,14)
 
972
                  ]])
 
973
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
974
                [[('equal',  2,6, 2,6),
 
975
                  ('insert', 6,6, 6,11),
 
976
                  ('equal',  6,10, 11,15)
 
977
                  ]], 4)
 
978
        chk_ops('Xabcdef', 'abcdef',
 
979
                [[('delete', 0,1, 0,0),
 
980
                  ('equal',  1,4, 0,3)
 
981
                  ]])
 
982
        chk_ops('abcdef', 'abcdefX',
 
983
                [[('equal',  3,6, 3,6),
 
984
                  ('insert', 6,6, 6,7)
 
985
                  ]])
 
986
 
 
987
 
 
988
    def test_multiple_ranges(self):
 
989
        # There was an earlier bug where we used a bad set of ranges,
 
990
        # this triggers that specific bug, to make sure it doesn't regress
 
991
        self.assertDiffBlocks('abcdefghijklmnop',
 
992
                              'abcXghiYZQRSTUVWXYZijklmnop',
 
993
                              [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
 
994
 
 
995
        self.assertDiffBlocks('ABCd efghIjk  L',
 
996
                              'AxyzBCn mo pqrstuvwI1 2  L',
 
997
                              [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
 
998
 
 
999
        # These are rot13 code snippets.
 
1000
        self.assertDiffBlocks('''\
 
1001
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
 
1002
    """
 
1003
    gnxrf_netf = ['svyr*']
 
1004
    gnxrf_bcgvbaf = ['ab-erphefr']
 
1005
  
 
1006
    qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr):
 
1007
        sebz omeyvo.nqq vzcbeg fzneg_nqq, nqq_ercbegre_cevag, nqq_ercbegre_ahyy
 
1008
        vs vf_dhvrg():
 
1009
            ercbegre = nqq_ercbegre_ahyy
 
1010
        ryfr:
 
1011
            ercbegre = nqq_ercbegre_cevag
 
1012
        fzneg_nqq(svyr_yvfg, abg ab_erphefr, ercbegre)
 
1013
 
 
1014
 
 
1015
pynff pzq_zxqve(Pbzznaq):
 
1016
'''.splitlines(True), '''\
 
1017
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
 
1018
 
 
1019
    --qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl 
 
1020
    nqq gurz.
 
1021
    """
 
1022
    gnxrf_netf = ['svyr*']
 
1023
    gnxrf_bcgvbaf = ['ab-erphefr', 'qel-eha']
 
1024
 
 
1025
    qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr, qel_eha=Snyfr):
 
1026
        vzcbeg omeyvo.nqq
 
1027
 
 
1028
        vs qel_eha:
 
1029
            vs vf_dhvrg():
 
1030
                # Guvf vf cbvagyrff, ohg V'q engure abg envfr na reebe
 
1031
                npgvba = omeyvo.nqq.nqq_npgvba_ahyy
 
1032
            ryfr:
 
1033
  npgvba = omeyvo.nqq.nqq_npgvba_cevag
 
1034
        ryvs vf_dhvrg():
 
1035
            npgvba = omeyvo.nqq.nqq_npgvba_nqq
 
1036
        ryfr:
 
1037
       npgvba = omeyvo.nqq.nqq_npgvba_nqq_naq_cevag
 
1038
 
 
1039
        omeyvo.nqq.fzneg_nqq(svyr_yvfg, abg ab_erphefr, npgvba)
 
1040
 
 
1041
 
 
1042
pynff pzq_zxqve(Pbzznaq):
 
1043
'''.splitlines(True)
 
1044
, [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
 
1045
 
 
1046
    def test_patience_unified_diff(self):
 
1047
        txt_a = ['hello there\n',
 
1048
                 'world\n',
 
1049
                 'how are you today?\n']
 
1050
        txt_b = ['hello there\n',
 
1051
                 'how are you today?\n']
 
1052
        unified_diff = bzrlib.patiencediff.unified_diff
 
1053
        psm = self._PatienceSequenceMatcher
 
1054
        self.assertEquals([ '---  \n',
 
1055
                           '+++  \n',
 
1056
                           '@@ -1,3 +1,2 @@\n',
 
1057
                           ' hello there\n',
 
1058
                           '-world\n',
 
1059
                           ' how are you today?\n'
 
1060
                          ]
 
1061
                          , list(unified_diff(txt_a, txt_b,
 
1062
                                 sequencematcher=psm)))
 
1063
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
 
1064
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
 
1065
        # This is the result with LongestCommonSubstring matching
 
1066
        self.assertEquals(['---  \n',
 
1067
                           '+++  \n',
 
1068
                           '@@ -1,6 +1,11 @@\n',
 
1069
                           ' a\n',
 
1070
                           ' b\n',
 
1071
                           ' c\n',
 
1072
                           '+d\n',
 
1073
                           '+e\n',
 
1074
                           '+f\n',
 
1075
                           '+x\n',
 
1076
                           '+y\n',
 
1077
                           ' d\n',
 
1078
                           ' e\n',
 
1079
                           ' f\n']
 
1080
                          , list(unified_diff(txt_a, txt_b)))
 
1081
        # And the patience diff
 
1082
        self.assertEquals(['---  \n',
 
1083
                           '+++  \n',
 
1084
                           '@@ -4,6 +4,11 @@\n',
 
1085
                           ' d\n',
 
1086
                           ' e\n',
 
1087
                           ' f\n',
 
1088
                           '+x\n',
 
1089
                           '+y\n',
 
1090
                           '+d\n',
 
1091
                           '+e\n',
 
1092
                           '+f\n',
 
1093
                           ' g\n',
 
1094
                           ' h\n',
 
1095
                           ' i\n',
 
1096
                          ]
 
1097
                          , list(unified_diff(txt_a, txt_b,
 
1098
                                 sequencematcher=psm)))
 
1099
 
 
1100
 
 
1101
class TestPatienceDiffLib_c(TestPatienceDiffLib):
 
1102
 
 
1103
    _test_needs_features = [CompiledPatienceDiffFeature]
 
1104
 
 
1105
    def setUp(self):
 
1106
        super(TestPatienceDiffLib_c, self).setUp()
 
1107
        import bzrlib._patiencediff_c
 
1108
        self._unique_lcs = bzrlib._patiencediff_c.unique_lcs_c
 
1109
        self._recurse_matches = bzrlib._patiencediff_c.recurse_matches_c
 
1110
        self._PatienceSequenceMatcher = \
 
1111
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
 
1112
 
 
1113
    def test_unhashable(self):
 
1114
        """We should get a proper exception here."""
 
1115
        # We need to be able to hash items in the sequence, lists are
 
1116
        # unhashable, and thus cannot be diffed
 
1117
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
 
1118
                                         None, [[]], [])
 
1119
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
 
1120
                                         None, ['valid', []], [])
 
1121
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
 
1122
                                         None, ['valid'], [[]])
 
1123
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
 
1124
                                         None, ['valid'], ['valid', []])
 
1125
 
 
1126
 
 
1127
class TestPatienceDiffLibFiles(TestCaseInTempDir):
 
1128
 
 
1129
    def setUp(self):
 
1130
        super(TestPatienceDiffLibFiles, self).setUp()
 
1131
        self._PatienceSequenceMatcher = \
 
1132
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
 
1133
 
 
1134
    def test_patience_unified_diff_files(self):
 
1135
        txt_a = ['hello there\n',
 
1136
                 'world\n',
 
1137
                 'how are you today?\n']
 
1138
        txt_b = ['hello there\n',
 
1139
                 'how are you today?\n']
 
1140
        open('a1', 'wb').writelines(txt_a)
 
1141
        open('b1', 'wb').writelines(txt_b)
 
1142
 
 
1143
        unified_diff_files = bzrlib.patiencediff.unified_diff_files
 
1144
        psm = self._PatienceSequenceMatcher
 
1145
        self.assertEquals(['--- a1 \n',
 
1146
                           '+++ b1 \n',
 
1147
                           '@@ -1,3 +1,2 @@\n',
 
1148
                           ' hello there\n',
 
1149
                           '-world\n',
 
1150
                           ' how are you today?\n',
 
1151
                          ]
 
1152
                          , list(unified_diff_files('a1', 'b1',
 
1153
                                 sequencematcher=psm)))
 
1154
 
 
1155
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
 
1156
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
 
1157
        open('a2', 'wb').writelines(txt_a)
 
1158
        open('b2', 'wb').writelines(txt_b)
 
1159
 
 
1160
        # This is the result with LongestCommonSubstring matching
 
1161
        self.assertEquals(['--- a2 \n',
 
1162
                           '+++ b2 \n',
 
1163
                           '@@ -1,6 +1,11 @@\n',
 
1164
                           ' a\n',
 
1165
                           ' b\n',
 
1166
                           ' c\n',
 
1167
                           '+d\n',
 
1168
                           '+e\n',
 
1169
                           '+f\n',
 
1170
                           '+x\n',
 
1171
                           '+y\n',
 
1172
                           ' d\n',
 
1173
                           ' e\n',
 
1174
                           ' f\n']
 
1175
                          , list(unified_diff_files('a2', 'b2')))
 
1176
 
 
1177
        # And the patience diff
 
1178
        self.assertEquals(['--- a2 \n',
 
1179
                           '+++ b2 \n',
 
1180
                           '@@ -4,6 +4,11 @@\n',
 
1181
                           ' d\n',
 
1182
                           ' e\n',
 
1183
                           ' f\n',
 
1184
                           '+x\n',
 
1185
                           '+y\n',
 
1186
                           '+d\n',
 
1187
                           '+e\n',
 
1188
                           '+f\n',
 
1189
                           ' g\n',
 
1190
                           ' h\n',
 
1191
                           ' i\n',
 
1192
                          ]
 
1193
                          , list(unified_diff_files('a2', 'b2',
 
1194
                                 sequencematcher=psm)))
 
1195
 
 
1196
 
 
1197
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
 
1198
 
 
1199
    _test_needs_features = [CompiledPatienceDiffFeature]
 
1200
 
 
1201
    def setUp(self):
 
1202
        super(TestPatienceDiffLibFiles_c, self).setUp()
 
1203
        import bzrlib._patiencediff_c
 
1204
        self._PatienceSequenceMatcher = \
 
1205
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
 
1206
 
 
1207
 
 
1208
class TestUsingCompiledIfAvailable(TestCase):
 
1209
 
 
1210
    def test_PatienceSequenceMatcher(self):
 
1211
        if CompiledPatienceDiffFeature.available():
 
1212
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
 
1213
            self.assertIs(PatienceSequenceMatcher_c,
 
1214
                          bzrlib.patiencediff.PatienceSequenceMatcher)
 
1215
        else:
 
1216
            from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
 
1217
            self.assertIs(PatienceSequenceMatcher_py,
 
1218
                          bzrlib.patiencediff.PatienceSequenceMatcher)
 
1219
 
 
1220
    def test_unique_lcs(self):
 
1221
        if CompiledPatienceDiffFeature.available():
 
1222
            from bzrlib._patiencediff_c import unique_lcs_c
 
1223
            self.assertIs(unique_lcs_c,
 
1224
                          bzrlib.patiencediff.unique_lcs)
 
1225
        else:
 
1226
            from bzrlib._patiencediff_py import unique_lcs_py
 
1227
            self.assertIs(unique_lcs_py,
 
1228
                          bzrlib.patiencediff.unique_lcs)
 
1229
 
 
1230
    def test_recurse_matches(self):
 
1231
        if CompiledPatienceDiffFeature.available():
 
1232
            from bzrlib._patiencediff_c import recurse_matches_c
 
1233
            self.assertIs(recurse_matches_c,
 
1234
                          bzrlib.patiencediff.recurse_matches)
 
1235
        else:
 
1236
            from bzrlib._patiencediff_py import recurse_matches_py
 
1237
            self.assertIs(recurse_matches_py,
 
1238
                          bzrlib.patiencediff.recurse_matches)
 
1239
 
 
1240
 
 
1241
class TestDiffFromTool(TestCaseWithTransport):
 
1242
 
 
1243
    def test_from_string(self):
 
1244
        diff_obj = DiffFromTool.from_string('diff', None, None, None)
 
1245
        self.addCleanup(diff_obj.finish)
 
1246
        self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
 
1247
            diff_obj.command_template)
 
1248
        diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
 
1249
        self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
 
1250
                         diff_obj.command_template)
 
1251
        self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
 
1252
                         diff_obj._get_command('old-path', 'new-path'))
 
1253
 
 
1254
    def test_execute(self):
 
1255
        output = StringIO()
 
1256
        diff_obj = DiffFromTool(['python', '-c',
 
1257
                                 'print "%(old_path)s %(new_path)s"'],
 
1258
                                None, None, output)
 
1259
        self.addCleanup(diff_obj.finish)
 
1260
        diff_obj._execute('old', 'new')
 
1261
        self.assertEqual(output.getvalue().rstrip(), 'old new')
 
1262
 
 
1263
    def test_prepare_files(self):
 
1264
        output = StringIO()
 
1265
        tree = self.make_branch_and_tree('tree')
 
1266
        self.build_tree_contents([('tree/file', 'oldcontent')])
 
1267
        tree.add('file', 'file-id')
 
1268
        tree.commit('old tree')
 
1269
        self.build_tree_contents([('tree/file', 'newcontent')])
 
1270
        old_tree = tree.basis_tree()
 
1271
        old_tree.lock_read()
 
1272
        self.addCleanup(old_tree.unlock)
 
1273
        diff_obj = DiffFromTool(['python', '-c',
 
1274
                                 'print "%(old_path)s %(new_path)s"'],
 
1275
                                old_tree, tree, output)
 
1276
        self.addCleanup(diff_obj.finish)
 
1277
        self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
 
1278
        old_path, new_path = diff_obj._prepare_files('file-id', 'oldname',
 
1279
                                                     'newname')
 
1280
        self.assertContainsRe(old_path, 'old/oldname$')
 
1281
        self.assertContainsRe(new_path, 'new/newname$')
 
1282
        self.assertFileEqual('oldcontent', old_path)
 
1283
        self.assertFileEqual('newcontent', new_path)
 
1284
        # make sure we can create files with the same parent directories
 
1285
        diff_obj._prepare_files('file-id', 'oldname2', 'newname2')