~bzr-pqm/bzr/bzr.dev

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