-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Expand file tree
/
Copy pathtest_msgfmt.py
More file actions
518 lines (462 loc) · 17.7 KB
/
test_msgfmt.py
File metadata and controls
518 lines (462 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"""Tests for the Tools/i18n/msgfmt.py tool."""
import json
import struct
import sys
import unittest
from contextlib import redirect_stderr
from gettext import GNUTranslations
from io import StringIO
from pathlib import Path
from textwrap import dedent
from test.support.os_helper import temp_cwd
from test.support.script_helper import assert_python_failure, assert_python_ok
from test.test_tools import imports_under_tool, skip_if_missing, toolsdir
skip_if_missing('i18n')
data_dir = (Path(__file__).parent / 'msgfmt_data').resolve()
script_dir = Path(toolsdir) / 'i18n'
msgfmt_py = script_dir / 'msgfmt.py'
with imports_under_tool("i18n"):
import msgfmt
def compile_messages(po_file, mo_file):
assert_python_ok(msgfmt_py, '-o', mo_file, po_file)
class CompilationTest(unittest.TestCase):
def test_compilation(self):
self.maxDiff = None
with temp_cwd():
for po_file in data_dir.glob('*.po'):
with self.subTest(po_file=po_file):
mo_file = po_file.with_suffix('.mo')
with open(mo_file, 'rb') as f:
expected = GNUTranslations(f)
tmp_mo_file = mo_file.name
compile_messages(po_file, tmp_mo_file)
with open(tmp_mo_file, 'rb') as f:
actual = GNUTranslations(f)
self.assertDictEqual(actual._catalog, expected._catalog)
def test_binary_header(self):
with temp_cwd():
tmp_mo_file = 'messages.mo'
compile_messages(data_dir / "general.po", tmp_mo_file)
with open(tmp_mo_file, 'rb') as f:
mo_data = f.read()
(
magic,
version,
num_strings,
orig_table_offset,
trans_table_offset,
hash_table_size,
hash_table_offset,
) = struct.unpack("=7I", mo_data[:28])
self.assertEqual(magic, 0x950412de)
self.assertEqual(version, 0)
self.assertEqual(num_strings, 9)
self.assertEqual(orig_table_offset, 28)
self.assertEqual(trans_table_offset, 100)
self.assertEqual(hash_table_size, 0)
self.assertEqual(hash_table_offset, 0)
def test_translations(self):
with open(data_dir / 'general.mo', 'rb') as f:
t = GNUTranslations(f)
self.assertEqual(t.gettext('foo'), 'foo')
self.assertEqual(t.gettext('bar'), 'baz')
self.assertEqual(t.pgettext('abc', 'foo'), 'bar')
self.assertEqual(t.pgettext('xyz', 'foo'), 'bar')
self.assertEqual(t.gettext('Multilinestring'), 'Multilinetranslation')
self.assertEqual(t.gettext('"escapes"'), '"translated"')
self.assertEqual(t.gettext('\n newlines \n'), '\n translated \n')
self.assertEqual(t.ngettext('One email sent.', '%d emails sent.', 1),
'One email sent.')
self.assertEqual(t.ngettext('One email sent.', '%d emails sent.', 2),
'%d emails sent.')
self.assertEqual(t.npgettext('abc', 'One email sent.',
'%d emails sent.', 1),
'One email sent.')
self.assertEqual(t.npgettext('abc', 'One email sent.',
'%d emails sent.', 2),
'%d emails sent.')
def test_po_with_bom(self):
with temp_cwd():
Path('bom.po').write_bytes(b'\xef\xbb\xbfmsgid "Python"\nmsgstr "Pioton"\n')
res = assert_python_failure(msgfmt_py, 'bom.po')
err = res.err.decode('utf-8')
self.assertIn('The file bom.po starts with a UTF-8 BOM', err)
def test_invalid_msgid_plural(self):
with temp_cwd():
Path('invalid.po').write_text('''\
msgid_plural "plural"
msgstr[0] "singular"
''')
res = assert_python_failure(msgfmt_py, 'invalid.po')
err = res.err.decode('utf-8')
self.assertIn('msgid_plural not preceded by msgid', err)
def test_plural_without_msgid_plural(self):
with temp_cwd():
Path('invalid.po').write_text('''\
msgid "foo"
msgstr[0] "bar"
''')
res = assert_python_failure(msgfmt_py, 'invalid.po')
err = res.err.decode('utf-8')
self.assertIn('plural without msgid_plural', err)
def test_indexed_msgstr_without_msgid_plural(self):
with temp_cwd():
Path('invalid.po').write_text('''\
msgid "foo"
msgid_plural "foos"
msgstr "bar"
''')
res = assert_python_failure(msgfmt_py, 'invalid.po')
err = res.err.decode('utf-8')
self.assertIn('indexed msgstr required for plural', err)
def test_generic_syntax_error(self):
with temp_cwd():
Path('invalid.po').write_text('''\
"foo"
''')
res = assert_python_failure(msgfmt_py, 'invalid.po')
err = res.err.decode('utf-8')
self.assertIn('Syntax error', err)
class POParserTest(unittest.TestCase):
@classmethod
def tearDownClass(cls):
# msgfmt uses a global variable to store messages,
# clear it after the tests.
msgfmt.MESSAGES.clear()
def test_strings(self):
# Test that the PO parser correctly handles and unescape
# strings in the PO file.
# The PO file format allows for a variety of escape sequences,
# octal and hex escapes.
valid_strings = (
# empty strings
('""', ''),
('"" "" ""', ''),
# allowed escape sequences
(r'"\\"', '\\'),
(r'"\""', '"'),
(r'"\t"', '\t'),
(r'"\n"', '\n'),
(r'"\r"', '\r'),
(r'"\f"', '\f'),
(r'"\a"', '\a'),
(r'"\b"', '\b'),
(r'"\v"', '\v'),
# non-empty strings
('"foo"', 'foo'),
('"foo" "bar"', 'foobar'),
('"foo""bar"', 'foobar'),
('"" "foo" ""', 'foo'),
# newlines and tabs
(r'"foo\nbar"', 'foo\nbar'),
(r'"foo\n" "bar"', 'foo\nbar'),
(r'"foo\tbar"', 'foo\tbar'),
(r'"foo\t" "bar"', 'foo\tbar'),
# escaped quotes
(r'"foo\"bar"', 'foo"bar'),
(r'"foo\"" "bar"', 'foo"bar'),
(r'"foo\\" "bar"', 'foo\\bar'),
# octal escapes
(r'"\120\171\164\150\157\156"', 'Python'),
(r'"\120\171\164" "\150\157\156"', 'Python'),
(r'"\"\120\171\164" "\150\157\156\""', '"Python"'),
# hex escapes
(r'"\x50\x79\x74\x68\x6f\x6e"', 'Python'),
(r'"\x50\x79\x74" "\x68\x6f\x6e"', 'Python'),
(r'"\"\x50\x79\x74" "\x68\x6f\x6e\""', '"Python"'),
)
with temp_cwd():
for po_string, expected in valid_strings:
with self.subTest(po_string=po_string):
# Construct a PO file with a single entry,
# compile it, read it into a catalog and
# check the result.
po = f'msgid {po_string}\nmsgstr "translation"'
Path('messages.po').write_text(po)
# Reset the global MESSAGES dictionary
msgfmt.MESSAGES.clear()
msgfmt.make('messages.po', 'messages.mo')
with open('messages.mo', 'rb') as f:
actual = GNUTranslations(f)
self.assertDictEqual(actual._catalog, {expected: 'translation'})
invalid_strings = (
# "''", # invalid but currently accepted
'"',
'"""',
'"" "',
'foo',
'"" "foo',
'"foo" foo',
'42',
'"" 42 ""',
# disallowed escape sequences
# r'"\'"', # invalid but currently accepted
# r'"\e"', # invalid but currently accepted
# r'"\8"', # invalid but currently accepted
# r'"\9"', # invalid but currently accepted
r'"\x"',
r'"\u1234"',
r'"\N{ROMAN NUMERAL NINE}"'
)
with temp_cwd():
for invalid_string in invalid_strings:
with self.subTest(string=invalid_string):
po = f'msgid {invalid_string}\nmsgstr "translation"'
Path('messages.po').write_text(po)
# Reset the global MESSAGES dictionary
msgfmt.MESSAGES.clear()
with self.assertRaises(Exception):
msgfmt.make('messages.po', 'messages.mo')
def test_general_syntax_errors(self):
# 2-tuples of input PO files and expected error messages
invalid_po_files = (
('', None),
('"', None),
('""', 'Syntax error on messages.po:1 before:'),
('"foo"', f'Syntax error on messages.po:1 before:\nfoo'),
# 'msgid', # invalid but currently accepted
('msgstr', None),
('msgid_plural', 'msgid_plural not preceded by msgid on messages.po:1'),
# 'msgctxt', # invalid but currently accepted
('msgstr', None),
('msgstr[0]', None),
('[0]', f'Syntax error on messages.po:1 before:\n[0]'),
# unclosed string
(
dedent('''\
msgid "
msgstr "bar"
'''),
None
),
# unclosed string
(
dedent('''\
msgid "foo
msgstr "bar"
'''),
None
),
# unclosed string
(
dedent('''\
msgid "foo" "
msgstr "bar"
'''),
None
),
# unclosed string
(
dedent('''\
msgid "foo"
"
msgstr "bar"
'''),
None
),
# illegal backslash
(
dedent('''\
msgid "foo\\"
"
msgstr "bar"
'''),
None
),
# msgid with an index
(
dedent('''\
msgid[0] "foo"
msgstr "bar"
'''),
None
),
# invalid plural index
(
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr[0 "baz"
'''),
None
),
# invalid plural index
(
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr1] "baz"
'''),
'indexed msgstr required for plural on messages.po:3'
),
# invalid plural index
(
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr[[0]] "baz"
'''),
None
)
)
with temp_cwd():
for invalid_po, err_msg in invalid_po_files:
with self.subTest(invalid_po=invalid_po):
Path('messages.po').write_text(invalid_po)
# Reset the global MESSAGES dictionary
msgfmt.MESSAGES.clear()
with redirect_stderr(StringIO()) as output:
with self.assertRaises((SystemExit, UnboundLocalError,
IndexError, SyntaxError)):
msgfmt.make('messages.po', 'messages.mo')
if err_msg:
self.assertEqual(output.getvalue().strip(), err_msg)
def test_semantic_errors(self):
# 2-tuples of input PO files and expected error messages
invalid_po_files = (
# msgid_plural must be preceded by msgid
(
dedent('''\
msgid_plural "foos"
msgid "bar"
msgstr "baz"
'''),
'msgid_plural not preceded by msgid on messages.po:1'
),
# msgid_plural not allowed after comment
(
dedent('''\
# comment
msgid_plural "foos"
msgid "bar"
msgstr "baz"
'''),
'msgid_plural not preceded by msgid on messages.po:2'
),
# msgid_plural not allowed after msgctxt
(
dedent('''\
msgctxt "foo"
msgid_plural "foos"
msgid "bar"
msgstr "baz"
'''),
'msgid_plural not preceded by msgid on messages.po:2'
),
# msgid_plural not allowed after msgstr
(
dedent('''\
msgid "foo"
msgstr "bar"
msgid_plural "foos"
msgid "bar"
msgstr "baz"
'''),
'msgid_plural not preceded by msgid on messages.po:3'
),
# msgstr must be preceded by msgid
(
dedent('''\
msgstr "foo"
msgid "bar"
msgstr "baz"
'''),
None
),
# msgstr not allowed after msgctxt
(
dedent('''\
msgctxt "foo"
msgstr "bar"
msgid "foo"
msgstr "bar"
'''),
None
),
# missing msgid_plural section
(
dedent('''\
msgid "foo"
msgstr[0] "bar"
msgid "bar"
msgstr "baz"
'''),
'plural without msgid_plural on messages.po:2'
),
)
with temp_cwd():
for invalid_po, err_msg in invalid_po_files:
with self.subTest(invalid_po=invalid_po):
Path('messages.po').write_text(invalid_po)
# Reset the global MESSAGES dictionary
msgfmt.MESSAGES.clear()
with redirect_stderr(StringIO()) as output:
with self.assertRaises((SystemExit, UnboundLocalError)):
msgfmt.make('messages.po', 'messages.mo')
if err_msg:
self.assertEqual(output.getvalue().strip(), err_msg)
def test_msgstr_invalid_indices(self):
# 2-tuples of input PO files and expected error messages
invalid_po_files = (
# msgstr not pluralized
(
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr "bar"
'''),
'indexed msgstr required for plural on messages.po:3'
),
)
with temp_cwd():
for invalid_po, err_msg in invalid_po_files:
with self.subTest(invalid_po=invalid_po):
Path('messages.po').write_text(invalid_po)
# Reset the global MESSAGES dictionary
msgfmt.MESSAGES.clear()
with redirect_stderr(StringIO()) as output:
with self.assertRaises(SystemExit):
msgfmt.make('messages.po', 'messages.mo')
self.assertEqual(output.getvalue().strip(), err_msg)
class CLITest(unittest.TestCase):
def test_help(self):
for option in ('--help', '-h'):
res = assert_python_ok(msgfmt_py, option)
err = res.err.decode('utf-8')
self.assertIn('Generate binary message catalog from textual translation description.', err)
def test_version(self):
for option in ('--version', '-V'):
res = assert_python_ok(msgfmt_py, option)
out = res.out.decode('utf-8').strip()
self.assertEqual('msgfmt.py 1.2', out)
def test_invalid_option(self):
res = assert_python_failure(msgfmt_py, '--invalid-option')
err = res.err.decode('utf-8')
self.assertIn('Generate binary message catalog from textual translation description.', err)
self.assertIn('option --invalid-option not recognized', err)
def test_no_input_file(self):
res = assert_python_ok(msgfmt_py)
err = res.err.decode('utf-8').replace('\r\n', '\n')
self.assertIn('No input file given\n'
"Try `msgfmt --help' for more information.", err)
def test_nonexistent_file(self):
assert_python_failure(msgfmt_py, 'nonexistent.po')
def update_catalog_snapshots():
for po_file in data_dir.glob('*.po'):
mo_file = po_file.with_suffix('.mo')
compile_messages(po_file, mo_file)
# Create a human-readable JSON file which is
# easier to review than the binary .mo file.
with open(mo_file, 'rb') as f:
translations = GNUTranslations(f)
catalog_file = po_file.with_suffix('.json')
with open(catalog_file, 'w') as f:
data = translations._catalog.items()
data = sorted(data, key=lambda x: (isinstance(x[0], tuple), x[0]))
json.dump(data, f, indent=4)
f.write('\n')
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
update_catalog_snapshots()
sys.exit(0)
unittest.main()