diff options
author | Ramiro Morales <ramiro@rmorales.net> | 2014-12-28 23:02:14 -0300 |
---|---|---|
committer | Ramiro Morales <ramiro@rmorales.net> | 2014-12-29 12:24:50 -0300 |
commit | 6fb9dee470d57882e378247fd2706d5f9867b5f9 (patch) | |
tree | dc34966a5164a52c330e1caaa80abf8c0893e30b /tests/i18n/test_extraction.py | |
parent | 1ee9507eb3e21ce3d2d95a84b52cef687bf0606f (diff) | |
download | django-6fb9dee470d57882e378247fd2706d5f9867b5f9.tar.gz |
Fixed #23271 -- Don't corrupt PO files on Windows when updating them.
Make sure PO catalog text fetched from gettext programs via standard
output isn't corrupted by mismatch between assumed (UTF-8) and real
(CP1252) encodings. This can cause mojibake to be written when creating
or updating PO files.
Also fixes #23311.
Thanks to contributor with Trac nick 'danielmenzel' for the report,
excellent research and fix.
Diffstat (limited to 'tests/i18n/test_extraction.py')
-rw-r--r-- | tests/i18n/test_extraction.py | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/tests/i18n/test_extraction.py b/tests/i18n/test_extraction.py index 6f8fea1bfe..ad96f978bd 100644 --- a/tests/i18n/test_extraction.py +++ b/tests/i18n/test_extraction.py @@ -67,14 +67,21 @@ class ExtractorTests(SimpleTestCase): po_contents = fp.read() return output, po_contents - def assertMsgId(self, msgid, s, use_quotes=True): + def _assertPoKeyword(self, keyword, expected_value, haystack, use_quotes=True): q = '"' if use_quotes: - msgid = '"%s"' % msgid + expected_value = '"%s"' % expected_value q = "'" - needle = 'msgid %s' % msgid - msgid = re.escape(msgid) - return self.assertTrue(re.search('^msgid %s' % msgid, s, re.MULTILINE), 'Could not find %(q)s%(n)s%(q)s in generated PO file' % {'n': needle, 'q': q}) + needle = '%s %s' % (keyword, expected_value) + expected_value = re.escape(expected_value) + return self.assertTrue(re.search('^%s %s' % (keyword, expected_value), haystack, re.MULTILINE), + 'Could not find %(q)s%(n)s%(q)s in generated PO file' % {'n': needle, 'q': q}) + + def assertMsgId(self, msgid, haystack, use_quotes=True): + return self._assertPoKeyword('msgid', msgid, haystack, use_quotes=use_quotes) + + def assertMsgStr(self, msgstr, haystack, use_quotes=True): + return self._assertPoKeyword('msgstr', msgstr, haystack, use_quotes=use_quotes) def assertNotMsgId(self, msgid, s, use_quotes=True): if use_quotes: @@ -391,6 +398,18 @@ class BasicExtractorTests(ExtractorTests): with six.assertRaisesRegex(self, CommandError, "Unable to get gettext version. Is it installed?"): cmd.gettext_version + def test_po_file_encoding_when_updating(self): + """Update of PO file doesn't corrupt it with non-UTF-8 encoding on Python3+Windows (#23271)""" + BR_PO_BASE = 'locale/pt_BR/LC_MESSAGES/django' + os.chdir(self.test_dir) + shutil.copyfile(BR_PO_BASE + '.pristine', BR_PO_BASE + '.po') + self.addCleanup(self.rmfile, os.path.join(self.test_dir, 'locale', 'pt_BR', 'LC_MESSAGES', 'django.po')) + management.call_command('makemessages', locale=['pt_BR'], verbosity=0) + self.assertTrue(os.path.exists(BR_PO_BASE + '.po')) + with io.open(BR_PO_BASE + '.po', 'r', encoding='utf-8') as fp: + po_contents = force_text(fp.read()) + self.assertMsgStr("Größe", po_contents) + class JavascriptExtractorTests(ExtractorTests): |