~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: John Arbash Meinel
  • Date: 2007-07-26 21:18:35 UTC
  • mfrom: (2592.2.5 show-diff-trees-110092)
  • mto: This revision was merged to the branch mainline in revision 2654.
  • Revision ID: john@arbash-meinel.com-20070726211835-z5v8xam1hk2omnq1
(Jonathan Lange) Fix bug #110092, when displaying a diff that includes a binary file, make sure the binary filenames are encoded

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
from bzrlib.errors import BinaryFile, NoDiff
25
25
import bzrlib.osutils as osutils
26
26
import bzrlib.patiencediff
27
 
from bzrlib.tests import (TestCase, TestCaseWithTransport,
 
27
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
28
28
                          TestCaseInTempDir, TestSkipped)
29
29
 
30
30
 
 
31
class _UnicodeFilename(Feature):
 
32
    """Does the filesystem support Unicode filenames?"""
 
33
 
 
34
    def _probe(self):
 
35
        try:
 
36
            os.stat(u'\u03b1')
 
37
        except UnicodeEncodeError:
 
38
            return False
 
39
        except (IOError, OSError):
 
40
            # The filesystem allows the Unicode filename but the file doesn't
 
41
            # exist.
 
42
            return True
 
43
        else:
 
44
            # The filesystem allows the Unicode filename and the file exists,
 
45
            # for some reason.
 
46
            return True
 
47
 
 
48
UnicodeFilename = _UnicodeFilename()
 
49
 
 
50
 
 
51
class TestUnicodeFilename(TestCase):
 
52
 
 
53
    def test_probe_passes(self):
 
54
        """UnicodeFilename._probe passes."""
 
55
        # We can't test much more than that because the behaviour depends
 
56
        # on the platform.
 
57
        UnicodeFilename._probe()
 
58
        
 
59
 
31
60
def udiff_lines(old, new, allow_binary=False):
32
61
    output = StringIO()
33
62
    internal_diff('old', old, 'new', new, output, allow_binary)
441
470
        self.assertContainsRe(diff, '-contents\n'
442
471
                                    '\\+new contents\n')
443
472
 
 
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.
 
476
        """
 
477
        # See https://bugs.launchpad.net/bugs/110092.
 
478
        self.requireFeature(UnicodeFilename)
 
479
 
 
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)),
 
487
             ('tree/' + omega,
 
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,))
 
500
 
444
501
 
445
502
class TestPatienceDiffLib(TestCase):
446
503