| 1 |
"""Tests for the Tools/i18n/msgfmt.py tool.""" |
| 2 |
|
| 3 |
import json |
| 4 |
import sys |
| 5 |
import unittest |
| 6 |
from gettext import GNUTranslations |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
from test.support.os_helper import temp_cwd |
| 10 |
from test.support.script_helper import assert_python_failure, assert_python_ok |
| 11 |
from test.test_tools import imports_under_tool, skip_if_missing, toolsdir |
| 12 |
|
| 13 |
|
| 14 |
skip_if_missing('i18n') |
| 15 |
|
| 16 |
data_dir = (Path(__file__).parent / 'msgfmt_data').resolve() |
| 17 |
script_dir = Path(toolsdir) / 'i18n' |
| 18 |
msgfmt_py = script_dir / 'msgfmt.py' |
| 19 |
|
| 20 |
with imports_under_tool("i18n"): |
| 21 |
import msgfmt |
| 22 |
|
| 23 |
|
| 24 |
def compile_messages(po_file, mo_file): |
| 25 |
assert_python_ok(msgfmt_py, '-o', mo_file, po_file) |
| 26 |
|
| 27 |
|
| 28 |
class CompilationTest(unittest.TestCase): |
| 29 |
|
| 30 |
def test_compilation(self): |
| 31 |
self.maxDiff = None |
| 32 |
with temp_cwd(): |
| 33 |
for po_file in data_dir.glob('*.po'): |
| 34 |
with self.subTest(po_file=po_file): |
| 35 |
mo_file = po_file.with_suffix('.mo') |
| 36 |
with open(mo_file, 'rb') as f: |
| 37 |
expected = GNUTranslations(f) |
| 38 |
|
| 39 |
tmp_mo_file = mo_file.name |
| 40 |
compile_messages(po_file, tmp_mo_file) |
| 41 |
with open(tmp_mo_file, 'rb') as f: |
| 42 |
actual = GNUTranslations(f) |
| 43 |
|
| 44 |
self.assertDictEqual(actual._catalog, expected._catalog) |
| 45 |
|
| 46 |
def test_translations(self): |
| 47 |
with open(data_dir / 'general.mo', 'rb') as f: |
| 48 |
t = GNUTranslations(f) |
| 49 |
|
| 50 |
self.assertEqual(t.gettext('foo'), 'foo') |
| 51 |
self.assertEqual(t.gettext('bar'), 'baz') |
| 52 |
self.assertEqual(t.pgettext('abc', 'foo'), 'bar') |
| 53 |
self.assertEqual(t.pgettext('xyz', 'foo'), 'bar') |
| 54 |
self.assertEqual(t.gettext('Multilinestring'), 'Multilinetranslation') |
| 55 |
self.assertEqual(t.gettext('"escapes"'), '"translated"') |
| 56 |
self.assertEqual(t.gettext('\n newlines \n'), '\n translated \n') |
| 57 |
self.assertEqual(t.ngettext('One email sent.', '%d emails sent.', 1), |
| 58 |
'One email sent.') |
| 59 |
self.assertEqual(t.ngettext('One email sent.', '%d emails sent.', 2), |
| 60 |
'%d emails sent.') |
| 61 |
self.assertEqual(t.npgettext('abc', 'One email sent.', |
| 62 |
'%d emails sent.', 1), |
| 63 |
'One email sent.') |
| 64 |
self.assertEqual(t.npgettext('abc', 'One email sent.', |
| 65 |
'%d emails sent.', 2), |
| 66 |
'%d emails sent.') |
| 67 |
|
| 68 |
def test_invalid_msgid_plural(self): |
| 69 |
with temp_cwd(): |
| 70 |
Path('invalid.po').write_text('''\ |
| 71 |
msgid_plural "plural" |
| 72 |
msgstr[0] "singular" |
| 73 |
''') |
| 74 |
|
| 75 |
res = assert_python_failure(msgfmt_py, 'invalid.po') |
| 76 |
err = res.err.decode('utf-8') |
| 77 |
self.assertIn('msgid_plural not preceded by msgid', err) |
| 78 |
|
| 79 |
def test_plural_without_msgid_plural(self): |
| 80 |
with temp_cwd(): |
| 81 |
Path('invalid.po').write_text('''\ |
| 82 |
msgid "foo" |
| 83 |
msgstr[0] "bar" |
| 84 |
''') |
| 85 |
|
| 86 |
res = assert_python_failure(msgfmt_py, 'invalid.po') |
| 87 |
err = res.err.decode('utf-8') |
| 88 |
self.assertIn('plural without msgid_plural', err) |
| 89 |
|
| 90 |
def test_indexed_msgstr_without_msgid_plural(self): |
| 91 |
with temp_cwd(): |
| 92 |
Path('invalid.po').write_text('''\ |
| 93 |
msgid "foo" |
| 94 |
msgid_plural "foos" |
| 95 |
msgstr "bar" |
| 96 |
''') |
| 97 |
|
| 98 |
res = assert_python_failure(msgfmt_py, 'invalid.po') |
| 99 |
err = res.err.decode('utf-8') |
| 100 |
self.assertIn('indexed msgstr required for plural', err) |
| 101 |
|
| 102 |
def test_generic_syntax_error(self): |
| 103 |
with temp_cwd(): |
| 104 |
Path('invalid.po').write_text('''\ |
| 105 |
"foo" |
| 106 |
''') |
| 107 |
|
| 108 |
res = assert_python_failure(msgfmt_py, 'invalid.po') |
| 109 |
err = res.err.decode('utf-8') |
| 110 |
self.assertIn('Syntax error', err) |
| 111 |
|
| 112 |
|
| 113 |
class POParserTest(unittest.TestCase): |
| 114 |
@classmethod |
| 115 |
def tearDownClass(cls): |
| 116 |
# msgfmt uses a global variable to store messages, |
| 117 |
# clear it after the tests. |
| 118 |
msgfmt.MESSAGES.clear() |
| 119 |
|
| 120 |
def test_strings(self): |
| 121 |
# Test that the PO parser correctly handles and unescape |
| 122 |
# strings in the PO file. |
| 123 |
# The PO file format allows for a variety of escape sequences, |
| 124 |
# octal and hex escapes. |
| 125 |
valid_strings = ( |
| 126 |
# empty strings |
| 127 |
('""', ''), |
| 128 |
('"" "" ""', ''), |
| 129 |
# allowed escape sequences |
| 130 |
(r'"\\"', '\\'), |
| 131 |
(r'"\""', '"'), |
| 132 |
(r'"\t"', '\t'), |
| 133 |
(r'"\n"', '\n'), |
| 134 |
(r'"\r"', '\r'), |
| 135 |
(r'"\f"', '\f'), |
| 136 |
(r'"\a"', '\a'), |
| 137 |
(r'"\b"', '\b'), |
| 138 |
(r'"\v"', '\v'), |
| 139 |
# non-empty strings |
| 140 |
('"foo"', 'foo'), |
| 141 |
('"foo" "bar"', 'foobar'), |
| 142 |
('"foo""bar"', 'foobar'), |
| 143 |
('"" "foo" ""', 'foo'), |
| 144 |
# newlines and tabs |
| 145 |
(r'"foo\nbar"', 'foo\nbar'), |
| 146 |
(r'"foo\n" "bar"', 'foo\nbar'), |
| 147 |
(r'"foo\tbar"', 'foo\tbar'), |
| 148 |
(r'"foo\t" "bar"', 'foo\tbar'), |
| 149 |
# escaped quotes |
| 150 |
(r'"foo\"bar"', 'foo"bar'), |
| 151 |
(r'"foo\"" "bar"', 'foo"bar'), |
| 152 |
(r'"foo\\" "bar"', 'foo\\bar'), |
| 153 |
# octal escapes |
| 154 |
(r'"\120\171\164\150\157\156"', 'Python'), |
| 155 |
(r'"\120\171\164" "\150\157\156"', 'Python'), |
| 156 |
(r'"\"\120\171\164" "\150\157\156\""', '"Python"'), |
| 157 |
# hex escapes |
| 158 |
(r'"\x50\x79\x74\x68\x6f\x6e"', 'Python'), |
| 159 |
(r'"\x50\x79\x74" "\x68\x6f\x6e"', 'Python'), |
| 160 |
(r'"\"\x50\x79\x74" "\x68\x6f\x6e\""', '"Python"'), |
| 161 |
) |
| 162 |
|
| 163 |
with temp_cwd(): |
| 164 |
for po_string, expected in valid_strings: |
| 165 |
with self.subTest(po_string=po_string): |
| 166 |
# Construct a PO file with a single entry, |
| 167 |
# compile it, read it into a catalog and |
| 168 |
# check the result. |
| 169 |
po = f'msgid {po_string}\nmsgstr "translation"' |
| 170 |
Path('messages.po').write_text(po) |
| 171 |
# Reset the global MESSAGES dictionary |
| 172 |
msgfmt.MESSAGES.clear() |
| 173 |
msgfmt.make('messages.po', 'messages.mo') |
| 174 |
|
| 175 |
with open('messages.mo', 'rb') as f: |
| 176 |
actual = GNUTranslations(f) |
| 177 |
|
| 178 |
self.assertDictEqual(actual._catalog, {expected: 'translation'}) |
| 179 |
|
| 180 |
invalid_strings = ( |
| 181 |
# "''", # invalid but currently accepted |
| 182 |
'"', |
| 183 |
'"""', |
| 184 |
'"" "', |
| 185 |
'foo', |
| 186 |
'"" "foo', |
| 187 |
'"foo" foo', |
| 188 |
'42', |
| 189 |
'"" 42 ""', |
| 190 |
# disallowed escape sequences |
| 191 |
# r'"\'"', # invalid but currently accepted |
| 192 |
# r'"\e"', # invalid but currently accepted |
| 193 |
# r'"\8"', # invalid but currently accepted |
| 194 |
# r'"\9"', # invalid but currently accepted |
| 195 |
r'"\x"', |
| 196 |
r'"\u1234"', |
| 197 |
r'"\N{ROMAN NUMERAL NINE}"' |
| 198 |
) |
| 199 |
with temp_cwd(): |
| 200 |
for invalid_string in invalid_strings: |
| 201 |
with self.subTest(string=invalid_string): |
| 202 |
po = f'msgid {invalid_string}\nmsgstr "translation"' |
| 203 |
Path('messages.po').write_text(po) |
| 204 |
# Reset the global MESSAGES dictionary |
| 205 |
msgfmt.MESSAGES.clear() |
| 206 |
with self.assertRaises(Exception): |
| 207 |
msgfmt.make('messages.po', 'messages.mo') |
| 208 |
|
| 209 |
|
| 210 |
class CLITest(unittest.TestCase): |
| 211 |
|
| 212 |
def test_help(self): |
| 213 |
for option in ('--help', '-h'): |
| 214 |
res = assert_python_ok(msgfmt_py, option) |
| 215 |
err = res.err.decode('utf-8') |
| 216 |
self.assertIn('Generate binary message catalog from textual translation description.', err) |
| 217 |
|
| 218 |
def test_version(self): |
| 219 |
for option in ('--version', '-V'): |
| 220 |
res = assert_python_ok(msgfmt_py, option) |
| 221 |
out = res.out.decode('utf-8').strip() |
| 222 |
self.assertEqual('msgfmt.py 1.2', out) |
| 223 |
|
| 224 |
def test_invalid_option(self): |
| 225 |
res = assert_python_failure(msgfmt_py, '--invalid-option') |
| 226 |
err = res.err.decode('utf-8') |
| 227 |
self.assertIn('Generate binary message catalog from textual translation description.', err) |
| 228 |
self.assertIn('option --invalid-option not recognized', err) |
| 229 |
|
| 230 |
def test_no_input_file(self): |
| 231 |
res = assert_python_ok(msgfmt_py) |
| 232 |
err = res.err.decode('utf-8').replace('\r\n', '\n') |
| 233 |
self.assertIn('No input file given\n' |
| 234 |
"Try `msgfmt --help' for more information.", err) |
| 235 |
|
| 236 |
def test_nonexistent_file(self): |
| 237 |
assert_python_failure(msgfmt_py, 'nonexistent.po') |
| 238 |
|
| 239 |
|
| 240 |
def update_catalog_snapshots(): |
| 241 |
for po_file in data_dir.glob('*.po'): |
| 242 |
mo_file = po_file.with_suffix('.mo') |
| 243 |
compile_messages(po_file, mo_file) |
| 244 |
# Create a human-readable JSON file which is |
| 245 |
# easier to review than the binary .mo file. |
| 246 |
with open(mo_file, 'rb') as f: |
| 247 |
translations = GNUTranslations(f) |
| 248 |
catalog_file = po_file.with_suffix('.json') |
| 249 |
with open(catalog_file, 'w') as f: |
| 250 |
data = translations._catalog.items() |
| 251 |
data = sorted(data, key=lambda x: (isinstance(x[0], tuple), x[0])) |
| 252 |
json.dump(data, f, indent=4) |
| 253 |
f.write('\n') |
| 254 |
|
| 255 |
|
| 256 |
if __name__ == '__main__': |
| 257 |
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update': |
| 258 |
update_catalog_snapshots() |
| 259 |
sys.exit(0) |
| 260 |
unittest.main() |