/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: Canonical.com Patch Queue Manager
  • Date: 2008-01-03 18:09:01 UTC
  • mfrom: (3159.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20080103180901-w987y1ftqoh02qbm
(vila) Fix #179368 by keeping the current range hint on
        ShortReadvErrors

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
import os.path
 
19
from cStringIO import StringIO
 
20
import errno
 
21
import subprocess
 
22
from tempfile import TemporaryFile
 
23
 
 
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, ExecutableMissing
 
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.build_tree_contents([('old-tree/olddir/',),
 
668
                                  ('old-tree/olddir/oldfile', 'old\n')])
 
669
        self.old_tree.add('olddir')
 
670
        self.old_tree.add('olddir/oldfile', 'file-id')
 
671
        self.build_tree(['new-tree/newdir/'])
 
672
        os.symlink('new', 'new-tree/newdir/newfile')
 
673
        self.new_tree.add('newdir')
 
674
        self.new_tree.add('newdir/newfile', 'file-id')
 
675
        self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
 
676
        self.assertContainsRe(
 
677
            self.differ.to_file.getvalue(),
 
678
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+0,0'
 
679
             ' \@\@\n-old\n\n')
 
680
        self.assertContainsRe(self.differ.to_file.getvalue(),
 
681
                              "=== target is 'new'\n")
 
682
 
 
683
    def test_diff_directory(self):
 
684
        self.build_tree(['new-tree/new-dir/'])
 
685
        self.new_tree.add('new-dir', 'new-dir-id')
 
686
        self.differ.diff('new-dir-id', None, 'new-dir')
 
687
        self.assertEqual(self.differ.to_file.getvalue(), '')
 
688
 
 
689
    def create_old_new(self):
 
690
        self.build_tree_contents([('old-tree/olddir/',),
 
691
                                  ('old-tree/olddir/oldfile', 'old\n')])
 
692
        self.old_tree.add('olddir')
 
693
        self.old_tree.add('olddir/oldfile', 'file-id')
 
694
        self.build_tree_contents([('new-tree/newdir/',),
 
695
                                  ('new-tree/newdir/newfile', 'new\n')])
 
696
        self.new_tree.add('newdir')
 
697
        self.new_tree.add('newdir/newfile', 'file-id')
 
698
 
 
699
    def test_register_diff(self):
 
700
        self.create_old_new()
 
701
        old_diff_factories = DiffTree.diff_factories
 
702
        DiffTree.diff_factories=old_diff_factories[:]
 
703
        DiffTree.diff_factories.insert(0, DiffWasIs.from_diff_tree)
 
704
        try:
 
705
            differ = DiffTree(self.old_tree, self.new_tree, StringIO())
 
706
        finally:
 
707
            DiffTree.diff_factories = old_diff_factories
 
708
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
 
709
        self.assertNotContainsRe(
 
710
            differ.to_file.getvalue(),
 
711
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
 
712
             ' \@\@\n-old\n\+new\n\n')
 
713
        self.assertContainsRe(differ.to_file.getvalue(),
 
714
                              'was: old\nis: new\n')
 
715
 
 
716
    def test_extra_factories(self):
 
717
        self.create_old_new()
 
718
        differ = DiffTree(self.old_tree, self.new_tree, StringIO(),
 
719
                            extra_factories=[DiffWasIs.from_diff_tree])
 
720
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
 
721
        self.assertNotContainsRe(
 
722
            differ.to_file.getvalue(),
 
723
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
 
724
             ' \@\@\n-old\n\+new\n\n')
 
725
        self.assertContainsRe(differ.to_file.getvalue(),
 
726
                              'was: old\nis: new\n')
 
727
 
 
728
    def test_alphabetical_order(self):
 
729
        self.build_tree(['new-tree/a-file'])
 
730
        self.new_tree.add('a-file')
 
731
        self.build_tree(['old-tree/b-file'])
 
732
        self.old_tree.add('b-file')
 
733
        self.differ.show_diff(None)
 
734
        self.assertContainsRe(self.differ.to_file.getvalue(),
 
735
            '.*a-file(.|\n)*b-file')
 
736
 
 
737
 
 
738
class TestPatienceDiffLib(TestCase):
 
739
 
 
740
    def setUp(self):
 
741
        super(TestPatienceDiffLib, self).setUp()
 
742
        self._unique_lcs = bzrlib._patiencediff_py.unique_lcs_py
 
743
        self._recurse_matches = bzrlib._patiencediff_py.recurse_matches_py
 
744
        self._PatienceSequenceMatcher = \
 
745
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
 
746
 
 
747
    def test_unique_lcs(self):
 
748
        unique_lcs = self._unique_lcs
 
749
        self.assertEquals(unique_lcs('', ''), [])
 
750
        self.assertEquals(unique_lcs('', 'a'), [])
 
751
        self.assertEquals(unique_lcs('a', ''), [])
 
752
        self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
 
753
        self.assertEquals(unique_lcs('a', 'b'), [])
 
754
        self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
 
755
        self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
 
756
        self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
 
757
        self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1), 
 
758
                                                         (3,3), (4,4)])
 
759
        self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
 
760
 
 
761
    def test_recurse_matches(self):
 
762
        def test_one(a, b, matches):
 
763
            test_matches = []
 
764
            self._recurse_matches(
 
765
                a, b, 0, 0, len(a), len(b), test_matches, 10)
 
766
            self.assertEquals(test_matches, matches)
 
767
 
 
768
        test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
 
769
                 [(0, 0), (2, 2), (4, 4)])
 
770
        test_one(['a', 'c', 'b', 'a', 'c'], ['a', 'b', 'c'],
 
771
                 [(0, 0), (2, 1), (4, 2)])
 
772
        # Even though 'bc' is not unique globally, and is surrounded by
 
773
        # non-matching lines, we should still match, because they are locally
 
774
        # unique
 
775
        test_one('abcdbce', 'afbcgdbce', [(0,0), (1, 2), (2, 3), (3, 5),
 
776
                                          (4, 6), (5, 7), (6, 8)])
 
777
 
 
778
        # recurse_matches doesn't match non-unique 
 
779
        # lines surrounded by bogus text.
 
780
        # The update has been done in patiencediff.SequenceMatcher instead
 
781
 
 
782
        # This is what it could be
 
783
        #test_one('aBccDe', 'abccde', [(0,0), (2,2), (3,3), (5,5)])
 
784
 
 
785
        # This is what it currently gives:
 
786
        test_one('aBccDe', 'abccde', [(0,0), (5,5)])
 
787
 
 
788
    def assertDiffBlocks(self, a, b, expected_blocks):
 
789
        """Check that the sequence matcher returns the correct blocks.
 
790
 
 
791
        :param a: A sequence to match
 
792
        :param b: Another sequence to match
 
793
        :param expected_blocks: The expected output, not including the final
 
794
            matching block (len(a), len(b), 0)
 
795
        """
 
796
        matcher = self._PatienceSequenceMatcher(None, a, b)
 
797
        blocks = matcher.get_matching_blocks()
 
798
        last = blocks.pop()
 
799
        self.assertEqual((len(a), len(b), 0), last)
 
800
        self.assertEqual(expected_blocks, blocks)
 
801
 
 
802
    def test_matching_blocks(self):
 
803
        # Some basic matching tests
 
804
        self.assertDiffBlocks('', '', [])
 
805
        self.assertDiffBlocks([], [], [])
 
806
        self.assertDiffBlocks('abc', '', [])
 
807
        self.assertDiffBlocks('', 'abc', [])
 
808
        self.assertDiffBlocks('abcd', 'abcd', [(0, 0, 4)])
 
809
        self.assertDiffBlocks('abcd', 'abce', [(0, 0, 3)])
 
810
        self.assertDiffBlocks('eabc', 'abce', [(1, 0, 3)])
 
811
        self.assertDiffBlocks('eabce', 'abce', [(1, 0, 4)])
 
812
        self.assertDiffBlocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
 
813
        self.assertDiffBlocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
 
814
        self.assertDiffBlocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
 
815
        # This may check too much, but it checks to see that
 
816
        # a copied block stays attached to the previous section,
 
817
        # not the later one.
 
818
        # difflib would tend to grab the trailing longest match
 
819
        # which would make the diff not look right
 
820
        self.assertDiffBlocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
821
                              [(0, 0, 6), (6, 11, 10)])
 
822
 
 
823
        # make sure it supports passing in lists
 
824
        self.assertDiffBlocks(
 
825
                   ['hello there\n',
 
826
                    'world\n',
 
827
                    'how are you today?\n'],
 
828
                   ['hello there\n',
 
829
                    'how are you today?\n'],
 
830
                [(0, 0, 1), (2, 1, 1)])
 
831
 
 
832
        # non unique lines surrounded by non-matching lines
 
833
        # won't be found
 
834
        self.assertDiffBlocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
 
835
 
 
836
        # But they only need to be locally unique
 
837
        self.assertDiffBlocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
 
838
 
 
839
        # non unique blocks won't be matched
 
840
        self.assertDiffBlocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
 
841
 
 
842
        # but locally unique ones will
 
843
        self.assertDiffBlocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
 
844
                                              (5,4,1), (7,5,2), (10,8,1)])
 
845
 
 
846
        self.assertDiffBlocks('abbabbXd', 'cabbabxd', [(7,7,1)])
 
847
        self.assertDiffBlocks('abbabbbb', 'cabbabbc', [])
 
848
        self.assertDiffBlocks('bbbbbbbb', 'cbbbbbbc', [])
 
849
 
 
850
    def test_matching_blocks_tuples(self):
 
851
        # Some basic matching tests
 
852
        self.assertDiffBlocks([], [], [])
 
853
        self.assertDiffBlocks([('a',), ('b',), ('c,')], [], [])
 
854
        self.assertDiffBlocks([], [('a',), ('b',), ('c,')], [])
 
855
        self.assertDiffBlocks([('a',), ('b',), ('c,')],
 
856
                              [('a',), ('b',), ('c,')],
 
857
                              [(0, 0, 3)])
 
858
        self.assertDiffBlocks([('a',), ('b',), ('c,')],
 
859
                              [('a',), ('b',), ('d,')],
 
860
                              [(0, 0, 2)])
 
861
        self.assertDiffBlocks([('d',), ('b',), ('c,')],
 
862
                              [('a',), ('b',), ('c,')],
 
863
                              [(1, 1, 2)])
 
864
        self.assertDiffBlocks([('d',), ('a',), ('b',), ('c,')],
 
865
                              [('a',), ('b',), ('c,')],
 
866
                              [(1, 0, 3)])
 
867
        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
 
868
                              [('a', 'b'), ('c', 'X'), ('e', 'f')],
 
869
                              [(0, 0, 1), (2, 2, 1)])
 
870
        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
 
871
                              [('a', 'b'), ('c', 'dX'), ('e', 'f')],
 
872
                              [(0, 0, 1), (2, 2, 1)])
 
873
 
 
874
    def test_opcodes(self):
 
875
        def chk_ops(a, b, expected_codes):
 
876
            s = self._PatienceSequenceMatcher(None, a, b)
 
877
            self.assertEquals(expected_codes, s.get_opcodes())
 
878
 
 
879
        chk_ops('', '', [])
 
880
        chk_ops([], [], [])
 
881
        chk_ops('abc', '', [('delete', 0,3, 0,0)])
 
882
        chk_ops('', 'abc', [('insert', 0,0, 0,3)])
 
883
        chk_ops('abcd', 'abcd', [('equal',    0,4, 0,4)])
 
884
        chk_ops('abcd', 'abce', [('equal',   0,3, 0,3),
 
885
                                 ('replace', 3,4, 3,4)
 
886
                                ])
 
887
        chk_ops('eabc', 'abce', [('delete', 0,1, 0,0),
 
888
                                 ('equal',  1,4, 0,3),
 
889
                                 ('insert', 4,4, 3,4)
 
890
                                ])
 
891
        chk_ops('eabce', 'abce', [('delete', 0,1, 0,0),
 
892
                                  ('equal',  1,5, 0,4)
 
893
                                 ])
 
894
        chk_ops('abcde', 'abXde', [('equal',   0,2, 0,2),
 
895
                                   ('replace', 2,3, 2,3),
 
896
                                   ('equal',   3,5, 3,5)
 
897
                                  ])
 
898
        chk_ops('abcde', 'abXYZde', [('equal',   0,2, 0,2),
 
899
                                     ('replace', 2,3, 2,5),
 
900
                                     ('equal',   3,5, 5,7)
 
901
                                    ])
 
902
        chk_ops('abde', 'abXYZde', [('equal',  0,2, 0,2),
 
903
                                    ('insert', 2,2, 2,5),
 
904
                                    ('equal',  2,4, 5,7)
 
905
                                   ])
 
906
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
907
                [('equal',  0,6,  0,6),
 
908
                 ('insert', 6,6,  6,11),
 
909
                 ('equal',  6,16, 11,21)
 
910
                ])
 
911
        chk_ops(
 
912
                [ 'hello there\n'
 
913
                , 'world\n'
 
914
                , 'how are you today?\n'],
 
915
                [ 'hello there\n'
 
916
                , 'how are you today?\n'],
 
917
                [('equal',  0,1, 0,1),
 
918
                 ('delete', 1,2, 1,1),
 
919
                 ('equal',  2,3, 1,2),
 
920
                ])
 
921
        chk_ops('aBccDe', 'abccde', 
 
922
                [('equal',   0,1, 0,1),
 
923
                 ('replace', 1,5, 1,5),
 
924
                 ('equal',   5,6, 5,6),
 
925
                ])
 
926
        chk_ops('aBcDec', 'abcdec', 
 
927
                [('equal',   0,1, 0,1),
 
928
                 ('replace', 1,2, 1,2),
 
929
                 ('equal',   2,3, 2,3),
 
930
                 ('replace', 3,4, 3,4),
 
931
                 ('equal',   4,6, 4,6),
 
932
                ])
 
933
        chk_ops('aBcdEcdFg', 'abcdecdfg', 
 
934
                [('equal',   0,1, 0,1),
 
935
                 ('replace', 1,8, 1,8),
 
936
                 ('equal',   8,9, 8,9)
 
937
                ])
 
938
        chk_ops('aBcdEeXcdFg', 'abcdecdfg', 
 
939
                [('equal',   0,1, 0,1),
 
940
                 ('replace', 1,2, 1,2),
 
941
                 ('equal',   2,4, 2,4),
 
942
                 ('delete', 4,5, 4,4),
 
943
                 ('equal',   5,6, 4,5),
 
944
                 ('delete', 6,7, 5,5),
 
945
                 ('equal',   7,9, 5,7),
 
946
                 ('replace', 9,10, 7,8),
 
947
                 ('equal',   10,11, 8,9)
 
948
                ])
 
949
 
 
950
    def test_grouped_opcodes(self):
 
951
        def chk_ops(a, b, expected_codes, n=3):
 
952
            s = self._PatienceSequenceMatcher(None, a, b)
 
953
            self.assertEquals(expected_codes, list(s.get_grouped_opcodes(n)))
 
954
 
 
955
        chk_ops('', '', [])
 
956
        chk_ops([], [], [])
 
957
        chk_ops('abc', '', [[('delete', 0,3, 0,0)]])
 
958
        chk_ops('', 'abc', [[('insert', 0,0, 0,3)]])
 
959
        chk_ops('abcd', 'abcd', [])
 
960
        chk_ops('abcd', 'abce', [[('equal',   0,3, 0,3),
 
961
                                  ('replace', 3,4, 3,4)
 
962
                                 ]])
 
963
        chk_ops('eabc', 'abce', [[('delete', 0,1, 0,0),
 
964
                                 ('equal',  1,4, 0,3),
 
965
                                 ('insert', 4,4, 3,4)
 
966
                                ]])
 
967
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
968
                [[('equal',  3,6, 3,6),
 
969
                  ('insert', 6,6, 6,11),
 
970
                  ('equal',  6,9, 11,14)
 
971
                  ]])
 
972
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
973
                [[('equal',  2,6, 2,6),
 
974
                  ('insert', 6,6, 6,11),
 
975
                  ('equal',  6,10, 11,15)
 
976
                  ]], 4)
 
977
        chk_ops('Xabcdef', 'abcdef',
 
978
                [[('delete', 0,1, 0,0),
 
979
                  ('equal',  1,4, 0,3)
 
980
                  ]])
 
981
        chk_ops('abcdef', 'abcdefX',
 
982
                [[('equal',  3,6, 3,6),
 
983
                  ('insert', 6,6, 6,7)
 
984
                  ]])
 
985
 
 
986
 
 
987
    def test_multiple_ranges(self):
 
988
        # There was an earlier bug where we used a bad set of ranges,
 
989
        # this triggers that specific bug, to make sure it doesn't regress
 
990
        self.assertDiffBlocks('abcdefghijklmnop',
 
991
                              'abcXghiYZQRSTUVWXYZijklmnop',
 
992
                              [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
 
993
 
 
994
        self.assertDiffBlocks('ABCd efghIjk  L',
 
995
                              'AxyzBCn mo pqrstuvwI1 2  L',
 
996
                              [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
 
997
 
 
998
        # These are rot13 code snippets.
 
999
        self.assertDiffBlocks('''\
 
1000
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
 
1001
    """
 
1002
    gnxrf_netf = ['svyr*']
 
1003
    gnxrf_bcgvbaf = ['ab-erphefr']
 
1004
  
 
1005
    qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr):
 
1006
        sebz omeyvo.nqq vzcbeg fzneg_nqq, nqq_ercbegre_cevag, nqq_ercbegre_ahyy
 
1007
        vs vf_dhvrg():
 
1008
            ercbegre = nqq_ercbegre_ahyy
 
1009
        ryfr:
 
1010
            ercbegre = nqq_ercbegre_cevag
 
1011
        fzneg_nqq(svyr_yvfg, abg ab_erphefr, ercbegre)
 
1012
 
 
1013
 
 
1014
pynff pzq_zxqve(Pbzznaq):
 
1015
'''.splitlines(True), '''\
 
1016
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
 
1017
 
 
1018
    --qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl 
 
1019
    nqq gurz.
 
1020
    """
 
1021
    gnxrf_netf = ['svyr*']
 
1022
    gnxrf_bcgvbaf = ['ab-erphefr', 'qel-eha']
 
1023
 
 
1024
    qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr, qel_eha=Snyfr):
 
1025
        vzcbeg omeyvo.nqq
 
1026
 
 
1027
        vs qel_eha:
 
1028
            vs vf_dhvrg():
 
1029
                # Guvf vf cbvagyrff, ohg V'q engure abg envfr na reebe
 
1030
                npgvba = omeyvo.nqq.nqq_npgvba_ahyy
 
1031
            ryfr:
 
1032
  npgvba = omeyvo.nqq.nqq_npgvba_cevag
 
1033
        ryvs vf_dhvrg():
 
1034
            npgvba = omeyvo.nqq.nqq_npgvba_nqq
 
1035
        ryfr:
 
1036
       npgvba = omeyvo.nqq.nqq_npgvba_nqq_naq_cevag
 
1037
 
 
1038
        omeyvo.nqq.fzneg_nqq(svyr_yvfg, abg ab_erphefr, npgvba)
 
1039
 
 
1040
 
 
1041
pynff pzq_zxqve(Pbzznaq):
 
1042
'''.splitlines(True)
 
1043
, [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
 
1044
 
 
1045
    def test_patience_unified_diff(self):
 
1046
        txt_a = ['hello there\n',
 
1047
                 'world\n',
 
1048
                 'how are you today?\n']
 
1049
        txt_b = ['hello there\n',
 
1050
                 'how are you today?\n']
 
1051
        unified_diff = bzrlib.patiencediff.unified_diff
 
1052
        psm = self._PatienceSequenceMatcher
 
1053
        self.assertEquals([ '---  \n',
 
1054
                           '+++  \n',
 
1055
                           '@@ -1,3 +1,2 @@\n',
 
1056
                           ' hello there\n',
 
1057
                           '-world\n',
 
1058
                           ' how are you today?\n'
 
1059
                          ]
 
1060
                          , list(unified_diff(txt_a, txt_b,
 
1061
                                 sequencematcher=psm)))
 
1062
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
 
1063
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
 
1064
        # This is the result with LongestCommonSubstring matching
 
1065
        self.assertEquals(['---  \n',
 
1066
                           '+++  \n',
 
1067
                           '@@ -1,6 +1,11 @@\n',
 
1068
                           ' a\n',
 
1069
                           ' b\n',
 
1070
                           ' c\n',
 
1071
                           '+d\n',
 
1072
                           '+e\n',
 
1073
                           '+f\n',
 
1074
                           '+x\n',
 
1075
                           '+y\n',
 
1076
                           ' d\n',
 
1077
                           ' e\n',
 
1078
                           ' f\n']
 
1079
                          , list(unified_diff(txt_a, txt_b)))
 
1080
        # And the patience diff
 
1081
        self.assertEquals(['---  \n',
 
1082
                           '+++  \n',
 
1083
                           '@@ -4,6 +4,11 @@\n',
 
1084
                           ' d\n',
 
1085
                           ' e\n',
 
1086
                           ' f\n',
 
1087
                           '+x\n',
 
1088
                           '+y\n',
 
1089
                           '+d\n',
 
1090
                           '+e\n',
 
1091
                           '+f\n',
 
1092
                           ' g\n',
 
1093
                           ' h\n',
 
1094
                           ' i\n',
 
1095
                          ]
 
1096
                          , list(unified_diff(txt_a, txt_b,
 
1097
                                 sequencematcher=psm)))
 
1098
 
 
1099
 
 
1100
class TestPatienceDiffLib_c(TestPatienceDiffLib):
 
1101
 
 
1102
    _test_needs_features = [CompiledPatienceDiffFeature]
 
1103
 
 
1104
    def setUp(self):
 
1105
        super(TestPatienceDiffLib_c, self).setUp()
 
1106
        import bzrlib._patiencediff_c
 
1107
        self._unique_lcs = bzrlib._patiencediff_c.unique_lcs_c
 
1108
        self._recurse_matches = bzrlib._patiencediff_c.recurse_matches_c
 
1109
        self._PatienceSequenceMatcher = \
 
1110
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
 
1111
 
 
1112
    def test_unhashable(self):
 
1113
        """We should get a proper exception here."""
 
1114
        # We need to be able to hash items in the sequence, lists are
 
1115
        # unhashable, and thus cannot be diffed
 
1116
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
 
1117
                                         None, [[]], [])
 
1118
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
 
1119
                                         None, ['valid', []], [])
 
1120
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
 
1121
                                         None, ['valid'], [[]])
 
1122
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
 
1123
                                         None, ['valid'], ['valid', []])
 
1124
 
 
1125
 
 
1126
class TestPatienceDiffLibFiles(TestCaseInTempDir):
 
1127
 
 
1128
    def setUp(self):
 
1129
        super(TestPatienceDiffLibFiles, self).setUp()
 
1130
        self._PatienceSequenceMatcher = \
 
1131
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
 
1132
 
 
1133
    def test_patience_unified_diff_files(self):
 
1134
        txt_a = ['hello there\n',
 
1135
                 'world\n',
 
1136
                 'how are you today?\n']
 
1137
        txt_b = ['hello there\n',
 
1138
                 'how are you today?\n']
 
1139
        open('a1', 'wb').writelines(txt_a)
 
1140
        open('b1', 'wb').writelines(txt_b)
 
1141
 
 
1142
        unified_diff_files = bzrlib.patiencediff.unified_diff_files
 
1143
        psm = self._PatienceSequenceMatcher
 
1144
        self.assertEquals(['--- a1 \n',
 
1145
                           '+++ b1 \n',
 
1146
                           '@@ -1,3 +1,2 @@\n',
 
1147
                           ' hello there\n',
 
1148
                           '-world\n',
 
1149
                           ' how are you today?\n',
 
1150
                          ]
 
1151
                          , list(unified_diff_files('a1', 'b1',
 
1152
                                 sequencematcher=psm)))
 
1153
 
 
1154
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
 
1155
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
 
1156
        open('a2', 'wb').writelines(txt_a)
 
1157
        open('b2', 'wb').writelines(txt_b)
 
1158
 
 
1159
        # This is the result with LongestCommonSubstring matching
 
1160
        self.assertEquals(['--- a2 \n',
 
1161
                           '+++ b2 \n',
 
1162
                           '@@ -1,6 +1,11 @@\n',
 
1163
                           ' a\n',
 
1164
                           ' b\n',
 
1165
                           ' c\n',
 
1166
                           '+d\n',
 
1167
                           '+e\n',
 
1168
                           '+f\n',
 
1169
                           '+x\n',
 
1170
                           '+y\n',
 
1171
                           ' d\n',
 
1172
                           ' e\n',
 
1173
                           ' f\n']
 
1174
                          , list(unified_diff_files('a2', 'b2')))
 
1175
 
 
1176
        # And the patience diff
 
1177
        self.assertEquals(['--- a2 \n',
 
1178
                           '+++ b2 \n',
 
1179
                           '@@ -4,6 +4,11 @@\n',
 
1180
                           ' d\n',
 
1181
                           ' e\n',
 
1182
                           ' f\n',
 
1183
                           '+x\n',
 
1184
                           '+y\n',
 
1185
                           '+d\n',
 
1186
                           '+e\n',
 
1187
                           '+f\n',
 
1188
                           ' g\n',
 
1189
                           ' h\n',
 
1190
                           ' i\n',
 
1191
                          ]
 
1192
                          , list(unified_diff_files('a2', 'b2',
 
1193
                                 sequencematcher=psm)))
 
1194
 
 
1195
 
 
1196
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
 
1197
 
 
1198
    _test_needs_features = [CompiledPatienceDiffFeature]
 
1199
 
 
1200
    def setUp(self):
 
1201
        super(TestPatienceDiffLibFiles_c, self).setUp()
 
1202
        import bzrlib._patiencediff_c
 
1203
        self._PatienceSequenceMatcher = \
 
1204
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
 
1205
 
 
1206
 
 
1207
class TestUsingCompiledIfAvailable(TestCase):
 
1208
 
 
1209
    def test_PatienceSequenceMatcher(self):
 
1210
        if CompiledPatienceDiffFeature.available():
 
1211
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
 
1212
            self.assertIs(PatienceSequenceMatcher_c,
 
1213
                          bzrlib.patiencediff.PatienceSequenceMatcher)
 
1214
        else:
 
1215
            from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
 
1216
            self.assertIs(PatienceSequenceMatcher_py,
 
1217
                          bzrlib.patiencediff.PatienceSequenceMatcher)
 
1218
 
 
1219
    def test_unique_lcs(self):
 
1220
        if CompiledPatienceDiffFeature.available():
 
1221
            from bzrlib._patiencediff_c import unique_lcs_c
 
1222
            self.assertIs(unique_lcs_c,
 
1223
                          bzrlib.patiencediff.unique_lcs)
 
1224
        else:
 
1225
            from bzrlib._patiencediff_py import unique_lcs_py
 
1226
            self.assertIs(unique_lcs_py,
 
1227
                          bzrlib.patiencediff.unique_lcs)
 
1228
 
 
1229
    def test_recurse_matches(self):
 
1230
        if CompiledPatienceDiffFeature.available():
 
1231
            from bzrlib._patiencediff_c import recurse_matches_c
 
1232
            self.assertIs(recurse_matches_c,
 
1233
                          bzrlib.patiencediff.recurse_matches)
 
1234
        else:
 
1235
            from bzrlib._patiencediff_py import recurse_matches_py
 
1236
            self.assertIs(recurse_matches_py,
 
1237
                          bzrlib.patiencediff.recurse_matches)
 
1238
 
 
1239
 
 
1240
class TestDiffFromTool(TestCaseWithTransport):
 
1241
 
 
1242
    def test_from_string(self):
 
1243
        diff_obj = DiffFromTool.from_string('diff', None, None, None)
 
1244
        self.addCleanup(diff_obj.finish)
 
1245
        self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
 
1246
            diff_obj.command_template)
 
1247
        diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
 
1248
        self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
 
1249
                         diff_obj.command_template)
 
1250
        self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
 
1251
                         diff_obj._get_command('old-path', 'new-path'))
 
1252
 
 
1253
    def test_execute(self):
 
1254
        output = StringIO()
 
1255
        diff_obj = DiffFromTool(['python', '-c',
 
1256
                                 'print "%(old_path)s %(new_path)s"'],
 
1257
                                None, None, output)
 
1258
        self.addCleanup(diff_obj.finish)
 
1259
        diff_obj._execute('old', 'new')
 
1260
        self.assertEqual(output.getvalue(), 'old new\n')
 
1261
 
 
1262
    def test_excute_missing(self):
 
1263
        diff_obj = DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
 
1264
                                None, None, None)
 
1265
        self.addCleanup(diff_obj.finish)
 
1266
        e = self.assertRaises(ExecutableMissing, diff_obj._execute, 'old',
 
1267
                              'new')
 
1268
        self.assertEqual('a-tool-which-is-unlikely-to-exist could not be found'
 
1269
                         ' on this machine', str(e))
 
1270
 
 
1271
    def test_prepare_files(self):
 
1272
        output = StringIO()
 
1273
        tree = self.make_branch_and_tree('tree')
 
1274
        self.build_tree_contents([('tree/oldname', 'oldcontent')])
 
1275
        tree.add('oldname', 'file-id')
 
1276
        tree.commit('old tree', timestamp=0)
 
1277
        tree.rename_one('oldname', 'newname')
 
1278
        self.build_tree_contents([('tree/newname', 'newcontent')])
 
1279
        old_tree = tree.basis_tree()
 
1280
        old_tree.lock_read()
 
1281
        self.addCleanup(old_tree.unlock)
 
1282
        tree.lock_read()
 
1283
        self.addCleanup(tree.unlock)
 
1284
        diff_obj = DiffFromTool(['python', '-c',
 
1285
                                 'print "%(old_path)s %(new_path)s"'],
 
1286
                                old_tree, tree, output)
 
1287
        self.addCleanup(diff_obj.finish)
 
1288
        self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
 
1289
        old_path, new_path = diff_obj._prepare_files('file-id', 'oldname',
 
1290
                                                     'newname')
 
1291
        self.assertContainsRe(old_path, 'old/oldname$')
 
1292
        self.assertEqual(0, os.stat(old_path).st_mtime)
 
1293
        self.assertContainsRe(new_path, 'new/newname$')
 
1294
        self.assertFileEqual('oldcontent', old_path)
 
1295
        self.assertFileEqual('newcontent', new_path)
 
1296
        if osutils.has_symlinks():
 
1297
            self.assertTrue(os.path.samefile('tree/newname', new_path))
 
1298
        # make sure we can create files with the same parent directories
 
1299
        diff_obj._prepare_files('file-id', 'oldname2', 'newname2')