|
28 | 28 | from __future__ import absolute_import
|
29 | 29 | from __future__ import unicode_literals
|
30 | 30 |
|
| 31 | +import six |
| 32 | + |
31 | 33 | import pygit2
|
32 | 34 | from . import utils
|
33 | 35 |
|
|
84 | 86 | """
|
85 | 87 |
|
86 | 88 |
|
| 89 | +class PatchEncodingTest(utils.AutoRepoTestCase): |
| 90 | + repo_spec = 'tar', 'encoding' |
| 91 | + expected_diff = b"""diff --git a/iso-8859-1.txt b/iso-8859-1.txt |
| 92 | +index e84e339..201e0c9 100644 |
| 93 | +--- a/iso-8859-1.txt |
| 94 | ++++ b/iso-8859-1.txt |
| 95 | +@@ -1 +1,2 @@ |
| 96 | + Kristian H\xf8gsberg |
| 97 | ++foo |
| 98 | +""" |
| 99 | + |
| 100 | + def test_patch_from_non_utf8(self): |
| 101 | + # blobs encoded in ISO-8859-1 |
| 102 | + old_content = b'Kristian H\xf8gsberg\n' |
| 103 | + new_content = old_content + b'foo\n' |
| 104 | + patch = pygit2.Patch.create_from( |
| 105 | + old_content, |
| 106 | + new_content, |
| 107 | + old_as_path='iso-8859-1.txt', |
| 108 | + new_as_path='iso-8859-1.txt', |
| 109 | + ) |
| 110 | + |
| 111 | + # `patch.patch` corrupted the ISO-8859-1 content as it forced UTF-8 |
| 112 | + # decoding, so assert that we cannot get the original content back: |
| 113 | + self.assertNotEqual(patch.patch.encode('utf8'), self.expected_diff) |
| 114 | + |
| 115 | + if six.PY2: |
| 116 | + self.assertIsInstance(str(patch), str) |
| 117 | + self.assertEqual(str(patch), self.expected_diff) |
| 118 | + |
| 119 | + self.assertIsInstance(patch.__bytes__(), str) |
| 120 | + self.assertEqual(patch.__bytes__(), self.expected_diff) |
| 121 | + |
| 122 | + else: |
| 123 | + self.assertIsInstance(str(patch), str) |
| 124 | + self.assertEqual(bytes(patch), self.expected_diff) |
| 125 | + self.assertEqual(str(patch), |
| 126 | + str(self.expected_diff, 'utf8', errors='replace')) |
| 127 | + |
| 128 | + def test_patch_create_from_blobs(self): |
| 129 | + patch = pygit2.Patch.create_from( |
| 130 | + self.repo['e84e339ac7fcc823106efa65a6972d7a20016c85'], |
| 131 | + self.repo['201e0c908e3d9f526659df3e556c3d06384ef0df'], |
| 132 | + old_as_path='iso-8859-1.txt', |
| 133 | + new_as_path='iso-8859-1.txt', |
| 134 | + ) |
| 135 | + # `patch.patch` corrupted the ISO-8859-1 content as it forced UTF-8 |
| 136 | + # decoding, so assert that we cannot get the original content back: |
| 137 | + self.assertNotEqual(patch.patch.encode('utf8'), self.expected_diff) |
| 138 | + |
| 139 | + if six.PY2: |
| 140 | + self.assertIsInstance(str(patch), str) |
| 141 | + self.assertEqual(str(patch), self.expected_diff) |
| 142 | + |
| 143 | + self.assertIsInstance(patch.__bytes__(), str) |
| 144 | + self.assertEqual(patch.__bytes__(), self.expected_diff) |
| 145 | + |
| 146 | + else: |
| 147 | + self.assertIsInstance(str(patch), str) |
| 148 | + self.assertEqual(bytes(patch), self.expected_diff) |
| 149 | + self.assertEqual(str(patch), |
| 150 | + str(self.expected_diff, 'utf8', errors='replace')) |
| 151 | + |
| 152 | + |
87 | 153 | class PatchTest(utils.RepoTestCase):
|
88 | 154 |
|
89 | 155 | def test_patch_create_from_buffers(self):
|
|
0 commit comments