~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

(vila) Make all transport put_bytes() raises TypeError when given unicode
 strings rather than bytes (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005-2014 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import os
18
 
import os.path
19
18
from cStringIO import StringIO
20
 
import errno
21
19
import subprocess
22
 
from tempfile import TemporaryFile
 
20
import tempfile
23
21
 
24
 
from bzrlib import tests
25
 
from bzrlib.diff import (
26
 
    DiffFromTool,
27
 
    DiffPath,
28
 
    DiffSymlink,
29
 
    DiffTree,
30
 
    DiffText,
31
 
    external_diff,
32
 
    internal_diff,
33
 
    show_diff_trees,
 
22
from bzrlib import (
 
23
    diff,
 
24
    errors,
 
25
    osutils,
 
26
    patiencediff,
 
27
    _patiencediff_py,
 
28
    revision as _mod_revision,
 
29
    revisionspec,
 
30
    revisiontree,
 
31
    tests,
 
32
    transform,
34
33
    )
35
 
from bzrlib.errors import BinaryFile, NoDiff, ExecutableMissing
36
 
import bzrlib.osutils as osutils
37
 
import bzrlib.transform as transform
38
 
import bzrlib.patiencediff
39
 
import bzrlib._patiencediff_py
40
 
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
41
 
                          TestCaseInTempDir, TestSkipped)
42
 
 
43
 
 
44
 
class _CompiledPatienceDiffFeature(Feature):
45
 
 
46
 
    def _probe(self):
47
 
        try:
48
 
            import bzrlib._patiencediff_c
49
 
        except ImportError:
50
 
            return False
51
 
        return True
52
 
 
53
 
    def feature_name(self):
54
 
        return 'bzrlib._patiencediff_c'
55
 
 
56
 
CompiledPatienceDiffFeature = _CompiledPatienceDiffFeature()
57
 
 
58
 
 
59
 
class _UnicodeFilename(Feature):
60
 
    """Does the filesystem support Unicode filenames?"""
61
 
 
62
 
    def _probe(self):
63
 
        try:
64
 
            os.stat(u'\u03b1')
65
 
        except UnicodeEncodeError:
66
 
            return False
67
 
        except (IOError, OSError):
68
 
            # The filesystem allows the Unicode filename but the file doesn't
69
 
            # exist.
70
 
            return True
71
 
        else:
72
 
            # The filesystem allows the Unicode filename and the file exists,
73
 
            # for some reason.
74
 
            return True
75
 
 
76
 
UnicodeFilename = _UnicodeFilename()
77
 
 
78
 
 
79
 
class TestUnicodeFilename(TestCase):
80
 
 
81
 
    def test_probe_passes(self):
82
 
        """UnicodeFilename._probe passes."""
83
 
        # We can't test much more than that because the behaviour depends
84
 
        # on the platform.
85
 
        UnicodeFilename._probe()
86
 
        
 
34
from bzrlib.tests import (
 
35
    features,
 
36
    EncodingAdapter,
 
37
)
 
38
from bzrlib.tests.blackbox.test_diff import subst_dates
 
39
from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
40
 
 
41
 
 
42
load_tests = load_tests_apply_scenarios
 
43
 
87
44
 
88
45
def udiff_lines(old, new, allow_binary=False):
89
46
    output = StringIO()
90
 
    internal_diff('old', old, 'new', new, output, allow_binary)
 
47
    diff.internal_diff('old', old, 'new', new, output, allow_binary)
91
48
    output.seek(0, 0)
92
49
    return output.readlines()
93
50
 
97
54
        # StringIO has no fileno, so it tests a different codepath
98
55
        output = StringIO()
99
56
    else:
100
 
        output = TemporaryFile()
 
57
        output = tempfile.TemporaryFile()
101
58
    try:
102
 
        external_diff('old', old, 'new', new, output, diff_opts=['-u'])
103
 
    except NoDiff:
104
 
        raise TestSkipped('external "diff" not present to test')
 
59
        diff.external_diff('old', old, 'new', new, output, diff_opts=['-u'])
 
60
    except errors.NoDiff:
 
61
        raise tests.TestSkipped('external "diff" not present to test')
105
62
    output.seek(0, 0)
106
63
    lines = output.readlines()
107
64
    output.close()
108
65
    return lines
109
66
 
110
67
 
111
 
class TestDiff(TestCase):
 
68
class TestDiffOptions(tests.TestCase):
 
69
 
 
70
    def test_unified_added(self):
 
71
        """Check for default style '-u' only if no other style specified
 
72
        in 'diff-options'.
 
73
        """
 
74
        # Verify that style defaults to unified, id est '-u' appended
 
75
        # to option list, in the absence of an alternative style.
 
76
        self.assertEqual(['-a', '-u'], diff.default_style_unified(['-a']))
 
77
 
 
78
 
 
79
class TestDiffOptionsScenarios(tests.TestCase):
 
80
 
 
81
    scenarios = [(s, dict(style=s)) for s in diff.style_option_list]
 
82
    style = None # Set by load_tests_apply_scenarios from scenarios
 
83
 
 
84
    def test_unified_not_added(self):
 
85
        # Verify that for all valid style options, '-u' is not
 
86
        # appended to option list.
 
87
        ret_opts = diff.default_style_unified(diff_opts=["%s" % (self.style,)])
 
88
        self.assertEqual(["%s" % (self.style,)], ret_opts)
 
89
 
 
90
 
 
91
class TestDiff(tests.TestCase):
112
92
 
113
93
    def test_add_nl(self):
114
94
        """diff generates a valid diff for patches that add a newline"""
150
130
            ## "Unterminated hunk header for patch:\n%s" % "".join(lines)
151
131
 
152
132
    def test_binary_lines(self):
153
 
        self.assertRaises(BinaryFile, udiff_lines, [1023 * 'a' + '\x00'], [])
154
 
        self.assertRaises(BinaryFile, udiff_lines, [], [1023 * 'a' + '\x00'])
155
 
        udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
156
 
        udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
 
133
        empty = []
 
134
        uni_lines = [1023 * 'a' + '\x00']
 
135
        self.assertRaises(errors.BinaryFile, udiff_lines, uni_lines , empty)
 
136
        self.assertRaises(errors.BinaryFile, udiff_lines, empty, uni_lines)
 
137
        udiff_lines(uni_lines , empty, allow_binary=True)
 
138
        udiff_lines(empty, uni_lines, allow_binary=True)
157
139
 
158
140
    def test_external_diff(self):
159
141
        lines = external_udiff_lines(['boo\n'], ['goo\n'])
169
151
        self.check_patch(lines)
170
152
 
171
153
    def test_external_diff_binary_lang_c(self):
172
 
        old_env = {}
173
154
        for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
174
 
            old_env[lang] = osutils.set_or_unset_env(lang, 'C')
175
 
        try:
176
 
            lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
177
 
            # Older versions of diffutils say "Binary files", newer
178
 
            # versions just say "Files".
179
 
            self.assertContainsRe(lines[0],
180
 
                                  '(Binary f|F)iles old and new differ\n')
181
 
            self.assertEquals(lines[1:], ['\n'])
182
 
        finally:
183
 
            for lang, old_val in old_env.iteritems():
184
 
                osutils.set_or_unset_env(lang, old_val)
 
155
            self.overrideEnv(lang, 'C')
 
156
        lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
 
157
        # Older versions of diffutils say "Binary files", newer
 
158
        # versions just say "Files".
 
159
        self.assertContainsRe(lines[0], '(Binary f|F)iles old and new differ\n')
 
160
        self.assertEquals(lines[1:], ['\n'])
185
161
 
186
162
    def test_no_external_diff(self):
187
163
        """Check that NoDiff is raised when diff is not available"""
188
 
        # Use os.environ['PATH'] to make sure no 'diff' command is available
189
 
        orig_path = os.environ['PATH']
190
 
        try:
191
 
            os.environ['PATH'] = ''
192
 
            self.assertRaises(NoDiff, external_diff,
193
 
                              'old', ['boo\n'], 'new', ['goo\n'],
194
 
                              StringIO(), diff_opts=['-u'])
195
 
        finally:
196
 
            os.environ['PATH'] = orig_path
197
 
        
 
164
        # Make sure no 'diff' command is available
 
165
        # XXX: Weird, using None instead of '' breaks the test -- vila 20101216
 
166
        self.overrideEnv('PATH', '')
 
167
        self.assertRaises(errors.NoDiff, diff.external_diff,
 
168
                          'old', ['boo\n'], 'new', ['goo\n'],
 
169
                          StringIO(), diff_opts=['-u'])
 
170
 
198
171
    def test_internal_diff_default(self):
199
172
        # Default internal diff encoding is utf8
200
173
        output = StringIO()
201
 
        internal_diff(u'old_\xb5', ['old_text\n'],
202
 
                    u'new_\xe5', ['new_text\n'], output)
 
174
        diff.internal_diff(u'old_\xb5', ['old_text\n'],
 
175
                           u'new_\xe5', ['new_text\n'], output)
203
176
        lines = output.getvalue().splitlines(True)
204
177
        self.check_patch(lines)
205
178
        self.assertEquals(['--- old_\xc2\xb5\n',
213
186
 
214
187
    def test_internal_diff_utf8(self):
215
188
        output = StringIO()
216
 
        internal_diff(u'old_\xb5', ['old_text\n'],
217
 
                    u'new_\xe5', ['new_text\n'], output,
218
 
                    path_encoding='utf8')
 
189
        diff.internal_diff(u'old_\xb5', ['old_text\n'],
 
190
                           u'new_\xe5', ['new_text\n'], output,
 
191
                           path_encoding='utf8')
219
192
        lines = output.getvalue().splitlines(True)
220
193
        self.check_patch(lines)
221
194
        self.assertEquals(['--- old_\xc2\xb5\n',
229
202
 
230
203
    def test_internal_diff_iso_8859_1(self):
231
204
        output = StringIO()
232
 
        internal_diff(u'old_\xb5', ['old_text\n'],
233
 
                    u'new_\xe5', ['new_text\n'], output,
234
 
                    path_encoding='iso-8859-1')
 
205
        diff.internal_diff(u'old_\xb5', ['old_text\n'],
 
206
                           u'new_\xe5', ['new_text\n'], output,
 
207
                           path_encoding='iso-8859-1')
235
208
        lines = output.getvalue().splitlines(True)
236
209
        self.check_patch(lines)
237
210
        self.assertEquals(['--- old_\xb5\n',
245
218
 
246
219
    def test_internal_diff_no_content(self):
247
220
        output = StringIO()
248
 
        internal_diff(u'old', [], u'new', [], output)
 
221
        diff.internal_diff(u'old', [], u'new', [], output)
249
222
        self.assertEqual('', output.getvalue())
250
223
 
251
224
    def test_internal_diff_no_changes(self):
252
225
        output = StringIO()
253
 
        internal_diff(u'old', ['text\n', 'contents\n'],
254
 
                      u'new', ['text\n', 'contents\n'],
255
 
                      output)
 
226
        diff.internal_diff(u'old', ['text\n', 'contents\n'],
 
227
                           u'new', ['text\n', 'contents\n'],
 
228
                           output)
256
229
        self.assertEqual('', output.getvalue())
257
230
 
258
231
    def test_internal_diff_returns_bytes(self):
259
232
        import StringIO
260
233
        output = StringIO.StringIO()
261
 
        internal_diff(u'old_\xb5', ['old_text\n'],
262
 
                    u'new_\xe5', ['new_text\n'], output)
263
 
        self.failUnless(isinstance(output.getvalue(), str),
 
234
        diff.internal_diff(u'old_\xb5', ['old_text\n'],
 
235
                            u'new_\xe5', ['new_text\n'], output)
 
236
        self.assertIsInstance(output.getvalue(), str,
264
237
            'internal_diff should return bytestrings')
265
238
 
266
 
 
267
 
class TestDiffFiles(TestCaseInTempDir):
 
239
    def test_internal_diff_default_context(self):
 
240
        output = StringIO()
 
241
        diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
 
242
                           'same_text\n','same_text\n','old_text\n'],
 
243
                           'new', ['same_text\n','same_text\n','same_text\n',
 
244
                           'same_text\n','same_text\n','new_text\n'], output)
 
245
        lines = output.getvalue().splitlines(True)
 
246
        self.check_patch(lines)
 
247
        self.assertEquals(['--- old\n',
 
248
                           '+++ new\n',
 
249
                           '@@ -3,4 +3,4 @@\n',
 
250
                           ' same_text\n',
 
251
                           ' same_text\n',
 
252
                           ' same_text\n',
 
253
                           '-old_text\n',
 
254
                           '+new_text\n',
 
255
                           '\n',
 
256
                          ]
 
257
                          , lines)
 
258
 
 
259
    def test_internal_diff_no_context(self):
 
260
        output = StringIO()
 
261
        diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
 
262
                           'same_text\n','same_text\n','old_text\n'],
 
263
                           'new', ['same_text\n','same_text\n','same_text\n',
 
264
                           'same_text\n','same_text\n','new_text\n'], output,
 
265
                           context_lines=0)
 
266
        lines = output.getvalue().splitlines(True)
 
267
        self.check_patch(lines)
 
268
        self.assertEquals(['--- old\n',
 
269
                           '+++ new\n',
 
270
                           '@@ -6,1 +6,1 @@\n',
 
271
                           '-old_text\n',
 
272
                           '+new_text\n',
 
273
                           '\n',
 
274
                          ]
 
275
                          , lines)
 
276
 
 
277
    def test_internal_diff_more_context(self):
 
278
        output = StringIO()
 
279
        diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
 
280
                           'same_text\n','same_text\n','old_text\n'],
 
281
                           'new', ['same_text\n','same_text\n','same_text\n',
 
282
                           'same_text\n','same_text\n','new_text\n'], output,
 
283
                           context_lines=4)
 
284
        lines = output.getvalue().splitlines(True)
 
285
        self.check_patch(lines)
 
286
        self.assertEquals(['--- old\n',
 
287
                           '+++ new\n',
 
288
                           '@@ -2,5 +2,5 @@\n',
 
289
                           ' same_text\n',
 
290
                           ' same_text\n',
 
291
                           ' same_text\n',
 
292
                           ' same_text\n',
 
293
                           '-old_text\n',
 
294
                           '+new_text\n',
 
295
                           '\n',
 
296
                          ]
 
297
                          , lines)
 
298
 
 
299
 
 
300
 
 
301
 
 
302
 
 
303
class TestDiffFiles(tests.TestCaseInTempDir):
268
304
 
269
305
    def test_external_diff_binary(self):
270
306
        """The output when using external diff should use diff's i18n error"""
272
308
        lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
273
309
 
274
310
        cmd = ['diff', '-u', '--binary', 'old', 'new']
275
 
        open('old', 'wb').write('\x00foobar\n')
276
 
        open('new', 'wb').write('foo\x00bar\n')
 
311
        with open('old', 'wb') as f: f.write('\x00foobar\n')
 
312
        with open('new', 'wb') as f: f.write('foo\x00bar\n')
277
313
        pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
278
314
                                     stdin=subprocess.PIPE)
279
315
        out, err = pipe.communicate()
283
319
        self.assertEqual(out.splitlines(True) + ['\n'], lines)
284
320
 
285
321
 
286
 
class TestShowDiffTreesHelper(TestCaseWithTransport):
287
 
    """Has a helper for running show_diff_trees"""
288
 
 
289
 
    def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
290
 
        output = StringIO()
291
 
        if working_tree is not None:
292
 
            extra_trees = (working_tree,)
293
 
        else:
294
 
            extra_trees = ()
295
 
        show_diff_trees(tree1, tree2, output, specific_files=specific_files,
296
 
                        extra_trees=extra_trees, old_label='old/',
297
 
                        new_label='new/')
298
 
        return output.getvalue()
299
 
 
300
 
 
301
 
class TestDiffDates(TestShowDiffTreesHelper):
 
322
def get_diff_as_string(tree1, tree2, specific_files=None, working_tree=None):
 
323
    output = StringIO()
 
324
    if working_tree is not None:
 
325
        extra_trees = (working_tree,)
 
326
    else:
 
327
        extra_trees = ()
 
328
    diff.show_diff_trees(tree1, tree2, output,
 
329
        specific_files=specific_files,
 
330
        extra_trees=extra_trees, old_label='old/',
 
331
        new_label='new/')
 
332
    return output.getvalue()
 
333
 
 
334
 
 
335
class TestDiffDates(tests.TestCaseWithTransport):
302
336
 
303
337
    def setUp(self):
304
338
        super(TestDiffDates, self).setUp()
339
373
        os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
340
374
 
341
375
    def test_diff_rev_tree_working_tree(self):
342
 
        output = self.get_diff(self.wt.basis_tree(), self.wt)
 
376
        output = get_diff_as_string(self.wt.basis_tree(), self.wt)
343
377
        # note that the date for old/file1 is from rev 2 rather than from
344
378
        # the basis revision (rev 4)
345
379
        self.assertEqualDiff(output, '''\
355
389
    def test_diff_rev_tree_rev_tree(self):
356
390
        tree1 = self.b.repository.revision_tree('rev-2')
357
391
        tree2 = self.b.repository.revision_tree('rev-3')
358
 
        output = self.get_diff(tree1, tree2)
 
392
        output = get_diff_as_string(tree1, tree2)
359
393
        self.assertEqualDiff(output, '''\
360
394
=== modified file 'file2'
361
395
--- old/file2\t2006-04-01 00:00:00 +0000
365
399
+file2 contents at rev 3
366
400
 
367
401
''')
368
 
        
 
402
 
369
403
    def test_diff_add_files(self):
370
 
        tree1 = self.b.repository.revision_tree(None)
 
404
        tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
371
405
        tree2 = self.b.repository.revision_tree('rev-1')
372
 
        output = self.get_diff(tree1, tree2)
 
406
        output = get_diff_as_string(tree1, tree2)
373
407
        # the files have the epoch time stamp for the tree in which
374
408
        # they don't exist.
375
409
        self.assertEqualDiff(output, '''\
390
424
    def test_diff_remove_files(self):
391
425
        tree1 = self.b.repository.revision_tree('rev-3')
392
426
        tree2 = self.b.repository.revision_tree('rev-4')
393
 
        output = self.get_diff(tree1, tree2)
 
427
        output = get_diff_as_string(tree1, tree2)
394
428
        # the file has the epoch time stamp for the tree in which
395
429
        # it doesn't exist.
396
430
        self.assertEqualDiff(output, '''\
407
441
        self.wt.rename_one('file1', 'file1b')
408
442
        old_tree = self.b.repository.revision_tree('rev-1')
409
443
        new_tree = self.b.repository.revision_tree('rev-4')
410
 
        out = self.get_diff(old_tree, new_tree, specific_files=['file1b'], 
 
444
        out = get_diff_as_string(old_tree, new_tree, specific_files=['file1b'],
411
445
                            working_tree=self.wt)
412
446
        self.assertContainsRe(out, 'file1\t')
413
447
 
419
453
        self.wt.rename_one('file1', 'dir1/file1')
420
454
        old_tree = self.b.repository.revision_tree('rev-1')
421
455
        new_tree = self.b.repository.revision_tree('rev-4')
422
 
        out = self.get_diff(old_tree, new_tree, specific_files=['dir1'], 
 
456
        out = get_diff_as_string(old_tree, new_tree, specific_files=['dir1'],
423
457
                            working_tree=self.wt)
424
458
        self.assertContainsRe(out, 'file1\t')
425
 
        out = self.get_diff(old_tree, new_tree, specific_files=['dir2'], 
 
459
        out = get_diff_as_string(old_tree, new_tree, specific_files=['dir2'],
426
460
                            working_tree=self.wt)
427
461
        self.assertNotContainsRe(out, 'file1\t')
428
462
 
429
463
 
430
 
 
431
 
class TestShowDiffTrees(TestShowDiffTreesHelper):
 
464
class TestShowDiffTrees(tests.TestCaseWithTransport):
432
465
    """Direct tests for show_diff_trees"""
433
466
 
434
467
    def test_modified_file(self):
439
472
        tree.commit('one', rev_id='rev-1')
440
473
 
441
474
        self.build_tree_contents([('tree/file', 'new contents\n')])
442
 
        diff = self.get_diff(tree.basis_tree(), tree)
443
 
        self.assertContainsRe(diff, "=== modified file 'file'\n")
444
 
        self.assertContainsRe(diff, '--- old/file\t')
445
 
        self.assertContainsRe(diff, '\\+\\+\\+ new/file\t')
446
 
        self.assertContainsRe(diff, '-contents\n'
447
 
                                    '\\+new contents\n')
 
475
        d = get_diff_as_string(tree.basis_tree(), tree)
 
476
        self.assertContainsRe(d, "=== modified file 'file'\n")
 
477
        self.assertContainsRe(d, '--- old/file\t')
 
478
        self.assertContainsRe(d, '\\+\\+\\+ new/file\t')
 
479
        self.assertContainsRe(d, '-contents\n'
 
480
                                 '\\+new contents\n')
448
481
 
449
482
    def test_modified_file_in_renamed_dir(self):
450
483
        """Test when a file is modified in a renamed directory."""
456
489
 
457
490
        tree.rename_one('dir', 'other')
458
491
        self.build_tree_contents([('tree/other/file', 'new contents\n')])
459
 
        diff = self.get_diff(tree.basis_tree(), tree)
460
 
        self.assertContainsRe(diff, "=== renamed directory 'dir' => 'other'\n")
461
 
        self.assertContainsRe(diff, "=== modified file 'other/file'\n")
 
492
        d = get_diff_as_string(tree.basis_tree(), tree)
 
493
        self.assertContainsRe(d, "=== renamed directory 'dir' => 'other'\n")
 
494
        self.assertContainsRe(d, "=== modified file 'other/file'\n")
462
495
        # XXX: This is technically incorrect, because it used to be at another
463
496
        # location. What to do?
464
 
        self.assertContainsRe(diff, '--- old/dir/file\t')
465
 
        self.assertContainsRe(diff, '\\+\\+\\+ new/other/file\t')
466
 
        self.assertContainsRe(diff, '-contents\n'
467
 
                                    '\\+new contents\n')
 
497
        self.assertContainsRe(d, '--- old/dir/file\t')
 
498
        self.assertContainsRe(d, '\\+\\+\\+ new/other/file\t')
 
499
        self.assertContainsRe(d, '-contents\n'
 
500
                                 '\\+new contents\n')
468
501
 
469
502
    def test_renamed_directory(self):
470
503
        """Test when only a directory is only renamed."""
475
508
        tree.commit('one', rev_id='rev-1')
476
509
 
477
510
        tree.rename_one('dir', 'newdir')
478
 
        diff = self.get_diff(tree.basis_tree(), tree)
 
511
        d = get_diff_as_string(tree.basis_tree(), tree)
479
512
        # Renaming a directory should be a single "you renamed this dir" even
480
513
        # when there are files inside.
481
 
        self.assertEqual("=== renamed directory 'dir' => 'newdir'\n", diff)
 
514
        self.assertEqual(d, "=== renamed directory 'dir' => 'newdir'\n")
482
515
 
483
516
    def test_renamed_file(self):
484
517
        """Test when a file is only renamed."""
488
521
        tree.commit('one', rev_id='rev-1')
489
522
 
490
523
        tree.rename_one('file', 'newname')
491
 
        diff = self.get_diff(tree.basis_tree(), tree)
492
 
        self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
 
524
        d = get_diff_as_string(tree.basis_tree(), tree)
 
525
        self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
493
526
        # We shouldn't have a --- or +++ line, because there is no content
494
527
        # change
495
 
        self.assertNotContainsRe(diff, '---')
 
528
        self.assertNotContainsRe(d, '---')
496
529
 
497
530
    def test_renamed_and_modified_file(self):
498
531
        """Test when a file is only renamed."""
503
536
 
504
537
        tree.rename_one('file', 'newname')
505
538
        self.build_tree_contents([('tree/newname', 'new contents\n')])
506
 
        diff = self.get_diff(tree.basis_tree(), tree)
507
 
        self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
508
 
        self.assertContainsRe(diff, '--- old/file\t')
509
 
        self.assertContainsRe(diff, '\\+\\+\\+ new/newname\t')
510
 
        self.assertContainsRe(diff, '-contents\n'
511
 
                                    '\\+new contents\n')
 
539
        d = get_diff_as_string(tree.basis_tree(), tree)
 
540
        self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
 
541
        self.assertContainsRe(d, '--- old/file\t')
 
542
        self.assertContainsRe(d, '\\+\\+\\+ new/newname\t')
 
543
        self.assertContainsRe(d, '-contents\n'
 
544
                                 '\\+new contents\n')
512
545
 
513
546
 
514
547
    def test_internal_diff_exec_property(self):
533
566
        tree.rename_one('c', 'new-c')
534
567
        tree.rename_one('d', 'new-d')
535
568
 
536
 
        diff = self.get_diff(tree.basis_tree(), tree)
537
 
 
538
 
        self.assertContainsRe(diff, r"file 'a'.*\(properties changed:.*\+x to -x.*\)")
539
 
        self.assertContainsRe(diff, r"file 'b'.*\(properties changed:.*-x to \+x.*\)")
540
 
        self.assertContainsRe(diff, r"file 'c'.*\(properties changed:.*\+x to -x.*\)")
541
 
        self.assertContainsRe(diff, r"file 'd'.*\(properties changed:.*-x to \+x.*\)")
542
 
        self.assertNotContainsRe(diff, r"file 'e'")
543
 
        self.assertNotContainsRe(diff, r"file 'f'")
544
 
 
 
569
        d = get_diff_as_string(tree.basis_tree(), tree)
 
570
 
 
571
        self.assertContainsRe(d, r"file 'a'.*\(properties changed:"
 
572
                                  ".*\+x to -x.*\)")
 
573
        self.assertContainsRe(d, r"file 'b'.*\(properties changed:"
 
574
                                  ".*-x to \+x.*\)")
 
575
        self.assertContainsRe(d, r"file 'c'.*\(properties changed:"
 
576
                                  ".*\+x to -x.*\)")
 
577
        self.assertContainsRe(d, r"file 'd'.*\(properties changed:"
 
578
                                  ".*-x to \+x.*\)")
 
579
        self.assertNotContainsRe(d, r"file 'e'")
 
580
        self.assertNotContainsRe(d, r"file 'f'")
545
581
 
546
582
    def test_binary_unicode_filenames(self):
547
583
        """Test that contents of files are *not* encoded in UTF-8 when there
548
584
        is a binary file in the diff.
549
585
        """
550
586
        # See https://bugs.launchpad.net/bugs/110092.
551
 
        self.requireFeature(UnicodeFilename)
 
587
        self.requireFeature(features.UnicodeFilenameFeature)
552
588
 
553
589
        # This bug isn't triggered with cStringIO.
554
590
        from StringIO import StringIO
562
598
        tree.add([alpha], ['file-id'])
563
599
        tree.add([omega], ['file-id-2'])
564
600
        diff_content = StringIO()
565
 
        show_diff_trees(tree.basis_tree(), tree, diff_content)
566
 
        diff = diff_content.getvalue()
567
 
        self.assertContainsRe(diff, r"=== added file '%s'" % alpha_utf8)
568
 
        self.assertContainsRe(
569
 
            diff, "Binary files a/%s.*and b/%s.* differ\n" % (alpha_utf8, alpha_utf8))
570
 
        self.assertContainsRe(diff, r"=== added file '%s'" % omega_utf8)
571
 
        self.assertContainsRe(diff, r"--- a/%s" % (omega_utf8,))
572
 
        self.assertContainsRe(diff, r"\+\+\+ b/%s" % (omega_utf8,))
 
601
        diff.show_diff_trees(tree.basis_tree(), tree, diff_content)
 
602
        d = diff_content.getvalue()
 
603
        self.assertContainsRe(d, r"=== added file '%s'" % alpha_utf8)
 
604
        self.assertContainsRe(d, "Binary files a/%s.*and b/%s.* differ\n"
 
605
                              % (alpha_utf8, alpha_utf8))
 
606
        self.assertContainsRe(d, r"=== added file '%s'" % omega_utf8)
 
607
        self.assertContainsRe(d, r"--- a/%s" % (omega_utf8,))
 
608
        self.assertContainsRe(d, r"\+\+\+ b/%s" % (omega_utf8,))
573
609
 
574
610
    def test_unicode_filename(self):
575
611
        """Test when the filename are unicode."""
576
 
        self.requireFeature(UnicodeFilename)
 
612
        self.requireFeature(features.UnicodeFilenameFeature)
577
613
 
578
614
        alpha, omega = u'\u03b1', u'\u03c9'
579
615
        autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
594
630
        tree.add(['add_'+alpha], ['file-id'])
595
631
        self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
596
632
 
597
 
        diff = self.get_diff(tree.basis_tree(), tree)
598
 
        self.assertContainsRe(diff,
 
633
        d = get_diff_as_string(tree.basis_tree(), tree)
 
634
        self.assertContainsRe(d,
599
635
                "=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
600
 
        self.assertContainsRe(diff, "=== added file 'add_%s'"%autf8)
601
 
        self.assertContainsRe(diff, "=== modified file 'mod_%s'"%autf8)
602
 
        self.assertContainsRe(diff, "=== removed file 'del_%s'"%autf8)
603
 
 
604
 
 
605
 
class DiffWasIs(DiffPath):
 
636
        self.assertContainsRe(d, "=== added file 'add_%s'"%autf8)
 
637
        self.assertContainsRe(d, "=== modified file 'mod_%s'"%autf8)
 
638
        self.assertContainsRe(d, "=== removed file 'del_%s'"%autf8)
 
639
 
 
640
    def test_unicode_filename_path_encoding(self):
 
641
        """Test for bug #382699: unicode filenames on Windows should be shown
 
642
        in user encoding.
 
643
        """
 
644
        self.requireFeature(features.UnicodeFilenameFeature)
 
645
        # The word 'test' in Russian
 
646
        _russian_test = u'\u0422\u0435\u0441\u0442'
 
647
        directory = _russian_test + u'/'
 
648
        test_txt = _russian_test + u'.txt'
 
649
        u1234 = u'\u1234.txt'
 
650
 
 
651
        tree = self.make_branch_and_tree('.')
 
652
        self.build_tree_contents([
 
653
            (test_txt, 'foo\n'),
 
654
            (u1234, 'foo\n'),
 
655
            (directory, None),
 
656
            ])
 
657
        tree.add([test_txt, u1234, directory])
 
658
 
 
659
        sio = StringIO()
 
660
        diff.show_diff_trees(tree.basis_tree(), tree, sio,
 
661
            path_encoding='cp1251')
 
662
 
 
663
        output = subst_dates(sio.getvalue())
 
664
        shouldbe = ('''\
 
665
=== added directory '%(directory)s'
 
666
=== added file '%(test_txt)s'
 
667
--- a/%(test_txt)s\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
668
+++ b/%(test_txt)s\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
669
@@ -0,0 +1,1 @@
 
670
+foo
 
671
 
 
672
=== added file '?.txt'
 
673
--- a/?.txt\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
674
+++ b/?.txt\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
675
@@ -0,0 +1,1 @@
 
676
+foo
 
677
 
 
678
''' % {'directory': _russian_test.encode('cp1251'),
 
679
       'test_txt': test_txt.encode('cp1251'),
 
680
      })
 
681
        self.assertEqualDiff(output, shouldbe)
 
682
 
 
683
 
 
684
class DiffWasIs(diff.DiffPath):
606
685
 
607
686
    def diff(self, file_id, old_path, new_path, old_kind, new_kind):
608
687
        self.to_file.write('was: ')
612
691
        pass
613
692
 
614
693
 
615
 
class TestDiffTree(TestCaseWithTransport):
 
694
class TestDiffTree(tests.TestCaseWithTransport):
616
695
 
617
696
    def setUp(self):
618
 
        TestCaseWithTransport.setUp(self)
 
697
        super(TestDiffTree, self).setUp()
619
698
        self.old_tree = self.make_branch_and_tree('old-tree')
620
699
        self.old_tree.lock_write()
621
700
        self.addCleanup(self.old_tree.unlock)
622
701
        self.new_tree = self.make_branch_and_tree('new-tree')
623
702
        self.new_tree.lock_write()
624
703
        self.addCleanup(self.new_tree.unlock)
625
 
        self.differ = DiffTree(self.old_tree, self.new_tree, StringIO())
 
704
        self.differ = diff.DiffTree(self.old_tree, self.new_tree, StringIO())
626
705
 
627
706
    def test_diff_text(self):
628
707
        self.build_tree_contents([('old-tree/olddir/',),
633
712
                                  ('new-tree/newdir/newfile', 'new\n')])
634
713
        self.new_tree.add('newdir')
635
714
        self.new_tree.add('newdir/newfile', 'file-id')
636
 
        differ = DiffText(self.old_tree, self.new_tree, StringIO())
 
715
        differ = diff.DiffText(self.old_tree, self.new_tree, StringIO())
637
716
        differ.diff_text('file-id', None, 'old label', 'new label')
638
717
        self.assertEqual(
639
718
            '--- old label\n+++ new label\n@@ -1,1 +0,0 @@\n-old\n\n',
668
747
        self.assertContainsRe(self.differ.to_file.getvalue(), '\+contents')
669
748
 
670
749
    def test_diff_symlink(self):
671
 
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
 
750
        differ = diff.DiffSymlink(self.old_tree, self.new_tree, StringIO())
672
751
        differ.diff_symlink('old target', None)
673
752
        self.assertEqual("=== target was 'old target'\n",
674
753
                         differ.to_file.getvalue())
675
754
 
676
 
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
 
755
        differ = diff.DiffSymlink(self.old_tree, self.new_tree, StringIO())
677
756
        differ.diff_symlink(None, 'new target')
678
757
        self.assertEqual("=== target is 'new target'\n",
679
758
                         differ.to_file.getvalue())
680
759
 
681
 
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
 
760
        differ = diff.DiffSymlink(self.old_tree, self.new_tree, StringIO())
682
761
        differ.diff_symlink('old target', 'new target')
683
762
        self.assertEqual("=== target changed 'old target' => 'new target'\n",
684
763
                         differ.to_file.getvalue())
699
778
             ' \@\@\n-old\n\+new\n\n')
700
779
 
701
780
    def test_diff_kind_change(self):
702
 
        self.requireFeature(tests.SymlinkFeature)
 
781
        self.requireFeature(features.SymlinkFeature)
703
782
        self.build_tree_contents([('old-tree/olddir/',),
704
783
                                  ('old-tree/olddir/oldfile', 'old\n')])
705
784
        self.old_tree.add('olddir')
714
793
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+0,0'
715
794
             ' \@\@\n-old\n\n')
716
795
        self.assertContainsRe(self.differ.to_file.getvalue(),
717
 
                              "=== target is 'new'\n")
 
796
                              "=== target is u'new'\n")
718
797
 
719
798
    def test_diff_directory(self):
720
799
        self.build_tree(['new-tree/new-dir/'])
734
813
 
735
814
    def test_register_diff(self):
736
815
        self.create_old_new()
737
 
        old_diff_factories = DiffTree.diff_factories
738
 
        DiffTree.diff_factories=old_diff_factories[:]
739
 
        DiffTree.diff_factories.insert(0, DiffWasIs.from_diff_tree)
 
816
        old_diff_factories = diff.DiffTree.diff_factories
 
817
        diff.DiffTree.diff_factories=old_diff_factories[:]
 
818
        diff.DiffTree.diff_factories.insert(0, DiffWasIs.from_diff_tree)
740
819
        try:
741
 
            differ = DiffTree(self.old_tree, self.new_tree, StringIO())
 
820
            differ = diff.DiffTree(self.old_tree, self.new_tree, StringIO())
742
821
        finally:
743
 
            DiffTree.diff_factories = old_diff_factories
 
822
            diff.DiffTree.diff_factories = old_diff_factories
744
823
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
745
824
        self.assertNotContainsRe(
746
825
            differ.to_file.getvalue(),
751
830
 
752
831
    def test_extra_factories(self):
753
832
        self.create_old_new()
754
 
        differ = DiffTree(self.old_tree, self.new_tree, StringIO(),
755
 
                            extra_factories=[DiffWasIs.from_diff_tree])
 
833
        differ = diff.DiffTree(self.old_tree, self.new_tree, StringIO(),
 
834
                               extra_factories=[DiffWasIs.from_diff_tree])
756
835
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
757
836
        self.assertNotContainsRe(
758
837
            differ.to_file.getvalue(),
771
850
            '.*a-file(.|\n)*b-file')
772
851
 
773
852
 
774
 
class TestPatienceDiffLib(TestCase):
 
853
class TestPatienceDiffLib(tests.TestCase):
775
854
 
776
855
    def setUp(self):
777
856
        super(TestPatienceDiffLib, self).setUp()
778
 
        self._unique_lcs = bzrlib._patiencediff_py.unique_lcs_py
779
 
        self._recurse_matches = bzrlib._patiencediff_py.recurse_matches_py
 
857
        self._unique_lcs = _patiencediff_py.unique_lcs_py
 
858
        self._recurse_matches = _patiencediff_py.recurse_matches_py
780
859
        self._PatienceSequenceMatcher = \
781
 
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
 
860
            _patiencediff_py.PatienceSequenceMatcher_py
 
861
 
 
862
    def test_diff_unicode_string(self):
 
863
        a = ''.join([unichr(i) for i in range(4000, 4500, 3)])
 
864
        b = ''.join([unichr(i) for i in range(4300, 4800, 2)])
 
865
        sm = self._PatienceSequenceMatcher(None, a, b)
 
866
        mb = sm.get_matching_blocks()
 
867
        self.assertEquals(35, len(mb))
782
868
 
783
869
    def test_unique_lcs(self):
784
870
        unique_lcs = self._unique_lcs
790
876
        self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
791
877
        self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
792
878
        self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
793
 
        self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1), 
 
879
        self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
794
880
                                                         (3,3), (4,4)])
795
881
        self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
796
882
 
811
897
        test_one('abcdbce', 'afbcgdbce', [(0,0), (1, 2), (2, 3), (3, 5),
812
898
                                          (4, 6), (5, 7), (6, 8)])
813
899
 
814
 
        # recurse_matches doesn't match non-unique 
 
900
        # recurse_matches doesn't match non-unique
815
901
        # lines surrounded by bogus text.
816
902
        # The update has been done in patiencediff.SequenceMatcher instead
817
903
 
954
1040
                 ('delete', 1,2, 1,1),
955
1041
                 ('equal',  2,3, 1,2),
956
1042
                ])
957
 
        chk_ops('aBccDe', 'abccde', 
 
1043
        chk_ops('aBccDe', 'abccde',
958
1044
                [('equal',   0,1, 0,1),
959
1045
                 ('replace', 1,5, 1,5),
960
1046
                 ('equal',   5,6, 5,6),
961
1047
                ])
962
 
        chk_ops('aBcDec', 'abcdec', 
 
1048
        chk_ops('aBcDec', 'abcdec',
963
1049
                [('equal',   0,1, 0,1),
964
1050
                 ('replace', 1,2, 1,2),
965
1051
                 ('equal',   2,3, 2,3),
966
1052
                 ('replace', 3,4, 3,4),
967
1053
                 ('equal',   4,6, 4,6),
968
1054
                ])
969
 
        chk_ops('aBcdEcdFg', 'abcdecdfg', 
 
1055
        chk_ops('aBcdEcdFg', 'abcdecdfg',
970
1056
                [('equal',   0,1, 0,1),
971
1057
                 ('replace', 1,8, 1,8),
972
1058
                 ('equal',   8,9, 8,9)
973
1059
                ])
974
 
        chk_ops('aBcdEeXcdFg', 'abcdecdfg', 
 
1060
        chk_ops('aBcdEeXcdFg', 'abcdecdfg',
975
1061
                [('equal',   0,1, 0,1),
976
1062
                 ('replace', 1,2, 1,2),
977
1063
                 ('equal',   2,4, 2,4),
1037
1123
    """
1038
1124
    gnxrf_netf = ['svyr*']
1039
1125
    gnxrf_bcgvbaf = ['ab-erphefr']
1040
 
  
 
1126
 
1041
1127
    qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr):
1042
1128
        sebz omeyvo.nqq vzcbeg fzneg_nqq, nqq_ercbegre_cevag, nqq_ercbegre_ahyy
1043
1129
        vs vf_dhvrg():
1051
1137
'''.splitlines(True), '''\
1052
1138
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
1053
1139
 
1054
 
    --qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl 
 
1140
    --qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl
1055
1141
    nqq gurz.
1056
1142
    """
1057
1143
    gnxrf_netf = ['svyr*']
1084
1170
                 'how are you today?\n']
1085
1171
        txt_b = ['hello there\n',
1086
1172
                 'how are you today?\n']
1087
 
        unified_diff = bzrlib.patiencediff.unified_diff
 
1173
        unified_diff = patiencediff.unified_diff
1088
1174
        psm = self._PatienceSequenceMatcher
1089
 
        self.assertEquals([ '---  \n',
1090
 
                           '+++  \n',
 
1175
        self.assertEquals(['--- \n',
 
1176
                           '+++ \n',
1091
1177
                           '@@ -1,3 +1,2 @@\n',
1092
1178
                           ' hello there\n',
1093
1179
                           '-world\n',
1098
1184
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1099
1185
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1100
1186
        # This is the result with LongestCommonSubstring matching
1101
 
        self.assertEquals(['---  \n',
1102
 
                           '+++  \n',
 
1187
        self.assertEquals(['--- \n',
 
1188
                           '+++ \n',
1103
1189
                           '@@ -1,6 +1,11 @@\n',
1104
1190
                           ' a\n',
1105
1191
                           ' b\n',
1114
1200
                           ' f\n']
1115
1201
                          , list(unified_diff(txt_a, txt_b)))
1116
1202
        # And the patience diff
1117
 
        self.assertEquals(['---  \n',
1118
 
                           '+++  \n',
 
1203
        self.assertEquals(['--- \n',
 
1204
                           '+++ \n',
1119
1205
                           '@@ -4,6 +4,11 @@\n',
1120
1206
                           ' d\n',
1121
1207
                           ' e\n',
1132
1218
                          , list(unified_diff(txt_a, txt_b,
1133
1219
                                 sequencematcher=psm)))
1134
1220
 
 
1221
    def test_patience_unified_diff_with_dates(self):
 
1222
        txt_a = ['hello there\n',
 
1223
                 'world\n',
 
1224
                 'how are you today?\n']
 
1225
        txt_b = ['hello there\n',
 
1226
                 'how are you today?\n']
 
1227
        unified_diff = patiencediff.unified_diff
 
1228
        psm = self._PatienceSequenceMatcher
 
1229
        self.assertEquals(['--- a\t2008-08-08\n',
 
1230
                           '+++ b\t2008-09-09\n',
 
1231
                           '@@ -1,3 +1,2 @@\n',
 
1232
                           ' hello there\n',
 
1233
                           '-world\n',
 
1234
                           ' how are you today?\n'
 
1235
                          ]
 
1236
                          , list(unified_diff(txt_a, txt_b,
 
1237
                                 fromfile='a', tofile='b',
 
1238
                                 fromfiledate='2008-08-08',
 
1239
                                 tofiledate='2008-09-09',
 
1240
                                 sequencematcher=psm)))
 
1241
 
1135
1242
 
1136
1243
class TestPatienceDiffLib_c(TestPatienceDiffLib):
1137
1244
 
1138
 
    _test_needs_features = [CompiledPatienceDiffFeature]
 
1245
    _test_needs_features = [features.compiled_patiencediff_feature]
1139
1246
 
1140
1247
    def setUp(self):
1141
1248
        super(TestPatienceDiffLib_c, self).setUp()
1142
 
        import bzrlib._patiencediff_c
1143
 
        self._unique_lcs = bzrlib._patiencediff_c.unique_lcs_c
1144
 
        self._recurse_matches = bzrlib._patiencediff_c.recurse_matches_c
 
1249
        from bzrlib import _patiencediff_c
 
1250
        self._unique_lcs = _patiencediff_c.unique_lcs_c
 
1251
        self._recurse_matches = _patiencediff_c.recurse_matches_c
1145
1252
        self._PatienceSequenceMatcher = \
1146
 
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
 
1253
            _patiencediff_c.PatienceSequenceMatcher_c
1147
1254
 
1148
1255
    def test_unhashable(self):
1149
1256
        """We should get a proper exception here."""
1159
1266
                                         None, ['valid'], ['valid', []])
1160
1267
 
1161
1268
 
1162
 
class TestPatienceDiffLibFiles(TestCaseInTempDir):
 
1269
class TestPatienceDiffLibFiles(tests.TestCaseInTempDir):
1163
1270
 
1164
1271
    def setUp(self):
1165
1272
        super(TestPatienceDiffLibFiles, self).setUp()
1166
1273
        self._PatienceSequenceMatcher = \
1167
 
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
 
1274
            _patiencediff_py.PatienceSequenceMatcher_py
1168
1275
 
1169
1276
    def test_patience_unified_diff_files(self):
1170
1277
        txt_a = ['hello there\n',
1172
1279
                 'how are you today?\n']
1173
1280
        txt_b = ['hello there\n',
1174
1281
                 'how are you today?\n']
1175
 
        open('a1', 'wb').writelines(txt_a)
1176
 
        open('b1', 'wb').writelines(txt_b)
 
1282
        with open('a1', 'wb') as f: f.writelines(txt_a)
 
1283
        with open('b1', 'wb') as f: f.writelines(txt_b)
1177
1284
 
1178
 
        unified_diff_files = bzrlib.patiencediff.unified_diff_files
 
1285
        unified_diff_files = patiencediff.unified_diff_files
1179
1286
        psm = self._PatienceSequenceMatcher
1180
 
        self.assertEquals(['--- a1 \n',
1181
 
                           '+++ b1 \n',
 
1287
        self.assertEquals(['--- a1\n',
 
1288
                           '+++ b1\n',
1182
1289
                           '@@ -1,3 +1,2 @@\n',
1183
1290
                           ' hello there\n',
1184
1291
                           '-world\n',
1189
1296
 
1190
1297
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1191
1298
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1192
 
        open('a2', 'wb').writelines(txt_a)
1193
 
        open('b2', 'wb').writelines(txt_b)
 
1299
        with open('a2', 'wb') as f: f.writelines(txt_a)
 
1300
        with open('b2', 'wb') as f: f.writelines(txt_b)
1194
1301
 
1195
1302
        # This is the result with LongestCommonSubstring matching
1196
 
        self.assertEquals(['--- a2 \n',
1197
 
                           '+++ b2 \n',
 
1303
        self.assertEquals(['--- a2\n',
 
1304
                           '+++ b2\n',
1198
1305
                           '@@ -1,6 +1,11 @@\n',
1199
1306
                           ' a\n',
1200
1307
                           ' b\n',
1210
1317
                          , list(unified_diff_files('a2', 'b2')))
1211
1318
 
1212
1319
        # And the patience diff
1213
 
        self.assertEquals(['--- a2 \n',
1214
 
                           '+++ b2 \n',
 
1320
        self.assertEquals(['--- a2\n',
 
1321
                           '+++ b2\n',
1215
1322
                           '@@ -4,6 +4,11 @@\n',
1216
1323
                           ' d\n',
1217
1324
                           ' e\n',
1231
1338
 
1232
1339
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1233
1340
 
1234
 
    _test_needs_features = [CompiledPatienceDiffFeature]
 
1341
    _test_needs_features = [features.compiled_patiencediff_feature]
1235
1342
 
1236
1343
    def setUp(self):
1237
1344
        super(TestPatienceDiffLibFiles_c, self).setUp()
1238
 
        import bzrlib._patiencediff_c
 
1345
        from bzrlib import _patiencediff_c
1239
1346
        self._PatienceSequenceMatcher = \
1240
 
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1241
 
 
1242
 
 
1243
 
class TestUsingCompiledIfAvailable(TestCase):
 
1347
            _patiencediff_c.PatienceSequenceMatcher_c
 
1348
 
 
1349
 
 
1350
class TestUsingCompiledIfAvailable(tests.TestCase):
1244
1351
 
1245
1352
    def test_PatienceSequenceMatcher(self):
1246
 
        if CompiledPatienceDiffFeature.available():
 
1353
        if features.compiled_patiencediff_feature.available():
1247
1354
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1248
1355
            self.assertIs(PatienceSequenceMatcher_c,
1249
 
                          bzrlib.patiencediff.PatienceSequenceMatcher)
 
1356
                          patiencediff.PatienceSequenceMatcher)
1250
1357
        else:
1251
1358
            from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
1252
1359
            self.assertIs(PatienceSequenceMatcher_py,
1253
 
                          bzrlib.patiencediff.PatienceSequenceMatcher)
 
1360
                          patiencediff.PatienceSequenceMatcher)
1254
1361
 
1255
1362
    def test_unique_lcs(self):
1256
 
        if CompiledPatienceDiffFeature.available():
 
1363
        if features.compiled_patiencediff_feature.available():
1257
1364
            from bzrlib._patiencediff_c import unique_lcs_c
1258
1365
            self.assertIs(unique_lcs_c,
1259
 
                          bzrlib.patiencediff.unique_lcs)
 
1366
                          patiencediff.unique_lcs)
1260
1367
        else:
1261
1368
            from bzrlib._patiencediff_py import unique_lcs_py
1262
1369
            self.assertIs(unique_lcs_py,
1263
 
                          bzrlib.patiencediff.unique_lcs)
 
1370
                          patiencediff.unique_lcs)
1264
1371
 
1265
1372
    def test_recurse_matches(self):
1266
 
        if CompiledPatienceDiffFeature.available():
 
1373
        if features.compiled_patiencediff_feature.available():
1267
1374
            from bzrlib._patiencediff_c import recurse_matches_c
1268
1375
            self.assertIs(recurse_matches_c,
1269
 
                          bzrlib.patiencediff.recurse_matches)
 
1376
                          patiencediff.recurse_matches)
1270
1377
        else:
1271
1378
            from bzrlib._patiencediff_py import recurse_matches_py
1272
1379
            self.assertIs(recurse_matches_py,
1273
 
                          bzrlib.patiencediff.recurse_matches)
1274
 
 
1275
 
 
1276
 
class TestDiffFromTool(TestCaseWithTransport):
 
1380
                          patiencediff.recurse_matches)
 
1381
 
 
1382
 
 
1383
class TestDiffFromTool(tests.TestCaseWithTransport):
1277
1384
 
1278
1385
    def test_from_string(self):
1279
 
        diff_obj = DiffFromTool.from_string('diff', None, None, None)
 
1386
        diff_obj = diff.DiffFromTool.from_string('diff', None, None, None)
1280
1387
        self.addCleanup(diff_obj.finish)
1281
 
        self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
 
1388
        self.assertEqual(['diff', '@old_path', '@new_path'],
1282
1389
            diff_obj.command_template)
1283
1390
 
1284
1391
    def test_from_string_u5(self):
1285
 
        diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
 
1392
        diff_obj = diff.DiffFromTool.from_string('diff "-u 5"',
 
1393
                                                 None, None, None)
1286
1394
        self.addCleanup(diff_obj.finish)
1287
 
        self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
 
1395
        self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
1288
1396
                         diff_obj.command_template)
1289
1397
        self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1290
1398
                         diff_obj._get_command('old-path', 'new-path'))
1291
1399
 
 
1400
    def test_from_string_path_with_backslashes(self):
 
1401
        self.requireFeature(features.backslashdir_feature)
 
1402
        tool = 'C:\\Tools\\Diff.exe'
 
1403
        diff_obj = diff.DiffFromTool.from_string(tool, None, None, None)
 
1404
        self.addCleanup(diff_obj.finish)
 
1405
        self.assertEqual(['C:\\Tools\\Diff.exe', '@old_path', '@new_path'],
 
1406
                         diff_obj.command_template)
 
1407
        self.assertEqual(['C:\\Tools\\Diff.exe', 'old-path', 'new-path'],
 
1408
                         diff_obj._get_command('old-path', 'new-path'))
 
1409
 
1292
1410
    def test_execute(self):
1293
1411
        output = StringIO()
1294
 
        diff_obj = DiffFromTool(['python', '-c',
1295
 
                                 'print "%(old_path)s %(new_path)s"'],
1296
 
                                None, None, output)
 
1412
        diff_obj = diff.DiffFromTool(['python', '-c',
 
1413
                                      'print "@old_path @new_path"'],
 
1414
                                     None, None, output)
1297
1415
        self.addCleanup(diff_obj.finish)
1298
1416
        diff_obj._execute('old', 'new')
1299
1417
        self.assertEqual(output.getvalue().rstrip(), 'old new')
1300
1418
 
1301
 
    def test_excute_missing(self):
1302
 
        diff_obj = DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1303
 
                                None, None, None)
 
1419
    def test_execute_missing(self):
 
1420
        diff_obj = diff.DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
 
1421
                                     None, None, None)
1304
1422
        self.addCleanup(diff_obj.finish)
1305
 
        e = self.assertRaises(ExecutableMissing, diff_obj._execute, 'old',
1306
 
                              'new')
 
1423
        e = self.assertRaises(errors.ExecutableMissing, diff_obj._execute,
 
1424
                              'old', 'new')
1307
1425
        self.assertEqual('a-tool-which-is-unlikely-to-exist could not be found'
1308
1426
                         ' on this machine', str(e))
1309
1427
 
 
1428
    def test_prepare_files_creates_paths_readable_by_windows_tool(self):
 
1429
        self.requireFeature(features.AttribFeature)
 
1430
        output = StringIO()
 
1431
        tree = self.make_branch_and_tree('tree')
 
1432
        self.build_tree_contents([('tree/file', 'content')])
 
1433
        tree.add('file', 'file-id')
 
1434
        tree.commit('old tree')
 
1435
        tree.lock_read()
 
1436
        self.addCleanup(tree.unlock)
 
1437
        basis_tree = tree.basis_tree()
 
1438
        basis_tree.lock_read()
 
1439
        self.addCleanup(basis_tree.unlock)
 
1440
        diff_obj = diff.DiffFromTool(['python', '-c',
 
1441
                                      'print "@old_path @new_path"'],
 
1442
                                     basis_tree, tree, output)
 
1443
        diff_obj._prepare_files('file-id', 'file', 'file')
 
1444
        # The old content should be readonly
 
1445
        self.assertReadableByAttrib(diff_obj._root, 'old\\file',
 
1446
                                    r'R.*old\\file$')
 
1447
        # The new content should use the tree object, not a 'new' file anymore
 
1448
        self.assertEndsWith(tree.basedir, 'work/tree')
 
1449
        self.assertReadableByAttrib(tree.basedir, 'file', r'work\\tree\\file$')
 
1450
 
 
1451
    def assertReadableByAttrib(self, cwd, relpath, regex):
 
1452
        proc = subprocess.Popen(['attrib', relpath],
 
1453
                                stdout=subprocess.PIPE,
 
1454
                                cwd=cwd)
 
1455
        (result, err) = proc.communicate()
 
1456
        self.assertContainsRe(result.replace('\r\n', '\n'), regex)
 
1457
 
1310
1458
    def test_prepare_files(self):
1311
1459
        output = StringIO()
1312
1460
        tree = self.make_branch_and_tree('tree')
1313
1461
        self.build_tree_contents([('tree/oldname', 'oldcontent')])
 
1462
        self.build_tree_contents([('tree/oldname2', 'oldcontent2')])
1314
1463
        tree.add('oldname', 'file-id')
1315
 
        tree.commit('old tree', timestamp=0)
 
1464
        tree.add('oldname2', 'file2-id')
 
1465
        # Earliest allowable date on FAT32 filesystems is 1980-01-01
 
1466
        tree.commit('old tree', timestamp=315532800)
1316
1467
        tree.rename_one('oldname', 'newname')
 
1468
        tree.rename_one('oldname2', 'newname2')
1317
1469
        self.build_tree_contents([('tree/newname', 'newcontent')])
 
1470
        self.build_tree_contents([('tree/newname2', 'newcontent2')])
1318
1471
        old_tree = tree.basis_tree()
1319
1472
        old_tree.lock_read()
1320
1473
        self.addCleanup(old_tree.unlock)
1321
1474
        tree.lock_read()
1322
1475
        self.addCleanup(tree.unlock)
1323
 
        diff_obj = DiffFromTool(['python', '-c',
1324
 
                                 'print "%(old_path)s %(new_path)s"'],
1325
 
                                old_tree, tree, output)
 
1476
        diff_obj = diff.DiffFromTool(['python', '-c',
 
1477
                                      'print "@old_path @new_path"'],
 
1478
                                     old_tree, tree, output)
1326
1479
        self.addCleanup(diff_obj.finish)
1327
1480
        self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1328
1481
        old_path, new_path = diff_obj._prepare_files('file-id', 'oldname',
1329
1482
                                                     'newname')
1330
1483
        self.assertContainsRe(old_path, 'old/oldname$')
1331
 
        self.assertEqual(0, os.stat(old_path).st_mtime)
1332
 
        self.assertContainsRe(new_path, 'new/newname$')
 
1484
        self.assertEqual(315532800, os.stat(old_path).st_mtime)
 
1485
        self.assertContainsRe(new_path, 'tree/newname$')
1333
1486
        self.assertFileEqual('oldcontent', old_path)
1334
1487
        self.assertFileEqual('newcontent', new_path)
1335
 
        if osutils.has_symlinks():
 
1488
        if osutils.host_os_dereferences_symlinks():
1336
1489
            self.assertTrue(os.path.samefile('tree/newname', new_path))
1337
1490
        # make sure we can create files with the same parent directories
1338
 
        diff_obj._prepare_files('file-id', 'oldname2', 'newname2')
 
1491
        diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
 
1492
 
 
1493
 
 
1494
class TestDiffFromToolEncodedFilename(tests.TestCaseWithTransport):
 
1495
 
 
1496
    def test_encodable_filename(self):
 
1497
        # Just checks file path for external diff tool.
 
1498
        # We cannot change CPython's internal encoding used by os.exec*.
 
1499
        diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
 
1500
                                    None, None, None)
 
1501
        for _, scenario in EncodingAdapter.encoding_scenarios:
 
1502
            encoding = scenario['encoding']
 
1503
            dirname  = scenario['info']['directory']
 
1504
            filename = scenario['info']['filename']
 
1505
 
 
1506
            self.overrideAttr(diffobj, '_fenc', lambda: encoding)
 
1507
            relpath = dirname + u'/' + filename
 
1508
            fullpath = diffobj._safe_filename('safe', relpath)
 
1509
            self.assertEqual(
 
1510
                    fullpath,
 
1511
                    fullpath.encode(encoding).decode(encoding)
 
1512
                    )
 
1513
            self.assert_(fullpath.startswith(diffobj._root + '/safe'))
 
1514
 
 
1515
    def test_unencodable_filename(self):
 
1516
        diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
 
1517
                                    None, None, None)
 
1518
        for _, scenario in EncodingAdapter.encoding_scenarios:
 
1519
            encoding = scenario['encoding']
 
1520
            dirname  = scenario['info']['directory']
 
1521
            filename = scenario['info']['filename']
 
1522
 
 
1523
            if encoding == 'iso-8859-1':
 
1524
                encoding = 'iso-8859-2'
 
1525
            else:
 
1526
                encoding = 'iso-8859-1'
 
1527
 
 
1528
            self.overrideAttr(diffobj, '_fenc', lambda: encoding)
 
1529
            relpath = dirname + u'/' + filename
 
1530
            fullpath = diffobj._safe_filename('safe', relpath)
 
1531
            self.assertEqual(
 
1532
                    fullpath,
 
1533
                    fullpath.encode(encoding).decode(encoding)
 
1534
                    )
 
1535
            self.assert_(fullpath.startswith(diffobj._root + '/safe'))
 
1536
 
 
1537
 
 
1538
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
 
1539
 
 
1540
    def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
 
1541
        """Call get_trees_and_branches_to_diff_locked."""
 
1542
        return diff.get_trees_and_branches_to_diff_locked(
 
1543
            path_list, revision_specs, old_url, new_url, self.addCleanup)
 
1544
 
 
1545
    def test_basic(self):
 
1546
        tree = self.make_branch_and_tree('tree')
 
1547
        (old_tree, new_tree,
 
1548
         old_branch, new_branch,
 
1549
         specific_files, extra_trees) = self.call_gtabtd(
 
1550
             ['tree'], None, None, None)
 
1551
 
 
1552
        self.assertIsInstance(old_tree, revisiontree.RevisionTree)
 
1553
        self.assertEqual(_mod_revision.NULL_REVISION,
 
1554
                         old_tree.get_revision_id())
 
1555
        self.assertEqual(tree.basedir, new_tree.basedir)
 
1556
        self.assertEqual(tree.branch.base, old_branch.base)
 
1557
        self.assertEqual(tree.branch.base, new_branch.base)
 
1558
        self.assertIs(None, specific_files)
 
1559
        self.assertIs(None, extra_trees)
 
1560
 
 
1561
    def test_with_rev_specs(self):
 
1562
        tree = self.make_branch_and_tree('tree')
 
1563
        self.build_tree_contents([('tree/file', 'oldcontent')])
 
1564
        tree.add('file', 'file-id')
 
1565
        tree.commit('old tree', timestamp=0, rev_id="old-id")
 
1566
        self.build_tree_contents([('tree/file', 'newcontent')])
 
1567
        tree.commit('new tree', timestamp=0, rev_id="new-id")
 
1568
 
 
1569
        revisions = [revisionspec.RevisionSpec.from_string('1'),
 
1570
                     revisionspec.RevisionSpec.from_string('2')]
 
1571
        (old_tree, new_tree,
 
1572
         old_branch, new_branch,
 
1573
         specific_files, extra_trees) = self.call_gtabtd(
 
1574
            ['tree'], revisions, None, None)
 
1575
 
 
1576
        self.assertIsInstance(old_tree, revisiontree.RevisionTree)
 
1577
        self.assertEqual("old-id", old_tree.get_revision_id())
 
1578
        self.assertIsInstance(new_tree, revisiontree.RevisionTree)
 
1579
        self.assertEqual("new-id", new_tree.get_revision_id())
 
1580
        self.assertEqual(tree.branch.base, old_branch.base)
 
1581
        self.assertEqual(tree.branch.base, new_branch.base)
 
1582
        self.assertIs(None, specific_files)
 
1583
        self.assertEqual(tree.basedir, extra_trees[0].basedir)