23
23
from bzrlib.diff import internal_diff, external_diff, show_diff_trees
24
24
from bzrlib.errors import BinaryFile, NoDiff
25
import bzrlib.osutils as osutils
26
25
import bzrlib.patiencediff
27
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
26
from bzrlib.tests import (TestCase, TestCaseWithTransport,
28
27
TestCaseInTempDir, TestSkipped)
31
class _UnicodeFilename(Feature):
32
"""Does the filesystem support Unicode filenames?"""
37
except UnicodeEncodeError:
39
except (IOError, OSError):
40
# The filesystem allows the Unicode filename but the file doesn't
44
# The filesystem allows the Unicode filename and the file exists,
48
UnicodeFilename = _UnicodeFilename()
51
class TestUnicodeFilename(TestCase):
53
def test_probe_passes(self):
54
"""UnicodeFilename._probe passes."""
55
# We can't test much more than that because the behaviour depends
57
UnicodeFilename._probe()
60
30
def udiff_lines(old, new, allow_binary=False):
61
31
output = StringIO()
62
32
internal_diff('old', old, 'new', new, output, allow_binary)
387
357
self.assertNotContainsRe(out, 'file1\t')
391
class TestShowDiffTrees(TestShowDiffTreesHelper):
392
"""Direct tests for show_diff_trees"""
394
def test_modified_file(self):
395
"""Test when a file is modified."""
396
tree = self.make_branch_and_tree('tree')
397
self.build_tree_contents([('tree/file', 'contents\n')])
398
tree.add(['file'], ['file-id'])
399
tree.commit('one', rev_id='rev-1')
401
self.build_tree_contents([('tree/file', 'new contents\n')])
402
diff = self.get_diff(tree.basis_tree(), tree)
403
self.assertContainsRe(diff, "=== modified file 'file'\n")
404
self.assertContainsRe(diff, '--- old/file\t')
405
self.assertContainsRe(diff, '\\+\\+\\+ new/file\t')
406
self.assertContainsRe(diff, '-contents\n'
409
def test_modified_file_in_renamed_dir(self):
410
"""Test when a file is modified in a renamed directory."""
411
tree = self.make_branch_and_tree('tree')
412
self.build_tree(['tree/dir/'])
413
self.build_tree_contents([('tree/dir/file', 'contents\n')])
414
tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
415
tree.commit('one', rev_id='rev-1')
417
tree.rename_one('dir', 'other')
418
self.build_tree_contents([('tree/other/file', 'new contents\n')])
419
diff = self.get_diff(tree.basis_tree(), tree)
420
self.assertContainsRe(diff, "=== renamed directory 'dir' => 'other'\n")
421
self.assertContainsRe(diff, "=== modified file 'other/file'\n")
422
# XXX: This is technically incorrect, because it used to be at another
423
# location. What to do?
424
self.assertContainsRe(diff, '--- old/dir/file\t')
425
self.assertContainsRe(diff, '\\+\\+\\+ new/other/file\t')
426
self.assertContainsRe(diff, '-contents\n'
429
def test_renamed_directory(self):
430
"""Test when only a directory is only renamed."""
431
tree = self.make_branch_and_tree('tree')
432
self.build_tree(['tree/dir/'])
433
self.build_tree_contents([('tree/dir/file', 'contents\n')])
434
tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
435
tree.commit('one', rev_id='rev-1')
437
tree.rename_one('dir', 'newdir')
438
diff = self.get_diff(tree.basis_tree(), tree)
439
# Renaming a directory should be a single "you renamed this dir" even
440
# when there are files inside.
441
self.assertEqual("=== renamed directory 'dir' => 'newdir'\n", diff)
443
def test_renamed_file(self):
444
"""Test when a file is only renamed."""
445
tree = self.make_branch_and_tree('tree')
446
self.build_tree_contents([('tree/file', 'contents\n')])
447
tree.add(['file'], ['file-id'])
448
tree.commit('one', rev_id='rev-1')
450
tree.rename_one('file', 'newname')
451
diff = self.get_diff(tree.basis_tree(), tree)
452
self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
453
# We shouldn't have a --- or +++ line, because there is no content
455
self.assertNotContainsRe(diff, '---')
457
def test_renamed_and_modified_file(self):
458
"""Test when a file is only renamed."""
459
tree = self.make_branch_and_tree('tree')
460
self.build_tree_contents([('tree/file', 'contents\n')])
461
tree.add(['file'], ['file-id'])
462
tree.commit('one', rev_id='rev-1')
464
tree.rename_one('file', 'newname')
465
self.build_tree_contents([('tree/newname', 'new contents\n')])
466
diff = self.get_diff(tree.basis_tree(), tree)
467
self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
468
self.assertContainsRe(diff, '--- old/file\t')
469
self.assertContainsRe(diff, '\\+\\+\\+ new/newname\t')
470
self.assertContainsRe(diff, '-contents\n'
473
def test_binary_unicode_filenames(self):
474
"""Test that contents of files are *not* encoded in UTF-8 when there
475
is a binary file in the diff.
477
# See https://bugs.launchpad.net/bugs/110092.
478
self.requireFeature(UnicodeFilename)
480
# This bug isn't triggered with cStringIO.
481
from StringIO import StringIO
482
tree = self.make_branch_and_tree('tree')
483
alpha, omega = u'\u03b1', u'\u03c9'
484
alpha_utf8, omega_utf8 = alpha.encode('utf8'), omega.encode('utf8')
485
self.build_tree_contents(
486
[('tree/' + alpha, chr(0)),
488
('The %s and the %s\n' % (alpha_utf8, omega_utf8)))])
489
tree.add([alpha], ['file-id'])
490
tree.add([omega], ['file-id-2'])
491
diff_content = StringIO()
492
show_diff_trees(tree.basis_tree(), tree, diff_content)
493
diff = diff_content.getvalue()
494
self.assertContainsRe(diff, r"=== added file '%s'" % alpha_utf8)
495
self.assertContainsRe(
496
diff, "Binary files a/%s.*and b/%s.* differ\n" % (alpha_utf8, alpha_utf8))
497
self.assertContainsRe(diff, r"=== added file '%s'" % omega_utf8)
498
self.assertContainsRe(diff, r"--- a/%s" % (omega_utf8,))
499
self.assertContainsRe(diff, r"\+\+\+ b/%s" % (omega_utf8,))
502
360
class TestPatienceDiffLib(TestCase):
504
362
def test_unique_lcs(self):