|
| 1 | +"""Tests for the Tools/i18n/msgfmt.py tool.""" |
| 2 | + |
| 3 | +import sys |
| 4 | +import unittest |
| 5 | +from gettext import GNUTranslations |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from test.support.os_helper import temp_cwd |
| 9 | +from test.support.script_helper import assert_python_failure, assert_python_ok |
| 10 | +from test.test_tools import skip_if_missing, toolsdir |
| 11 | + |
| 12 | + |
| 13 | +skip_if_missing('i18n') |
| 14 | + |
| 15 | +data_dir = (Path(__file__).parent / 'msgfmt_data').resolve() |
| 16 | +script_dir = Path(toolsdir) / 'i18n' |
| 17 | +msgfmt = script_dir / 'msgfmt.py' |
| 18 | + |
| 19 | + |
| 20 | +def compile_messages(po_file, mo_file): |
| 21 | + assert_python_ok(msgfmt, '-o', mo_file, po_file) |
| 22 | + |
| 23 | + |
| 24 | +class CompilationTest(unittest.TestCase): |
| 25 | + |
| 26 | + def test_compilation(self): |
| 27 | + self.maxDiff = None |
| 28 | + with temp_cwd(): |
| 29 | + for po_file in data_dir.glob('*.po'): |
| 30 | + with self.subTest(po_file=po_file): |
| 31 | + mo_file = po_file.with_suffix('.mo') |
| 32 | + with open(mo_file, 'rb') as f: |
| 33 | + expected = GNUTranslations(f) |
| 34 | + |
| 35 | + tmp_mo_file = mo_file.name |
| 36 | + compile_messages(po_file, tmp_mo_file) |
| 37 | + with open(tmp_mo_file, 'rb') as f: |
| 38 | + actual = GNUTranslations(f) |
| 39 | + |
| 40 | + self.assertDictEqual(actual._catalog, expected._catalog) |
| 41 | + |
| 42 | + def test_invalid_msgid_plural(self): |
| 43 | + with temp_cwd(): |
| 44 | + Path('invalid.po').write_text('''\ |
| 45 | +msgid_plural "plural" |
| 46 | +msgstr[0] "singular" |
| 47 | +''') |
| 48 | + |
| 49 | + res = assert_python_failure(msgfmt, 'invalid.po') |
| 50 | + err = res.err.decode('utf-8') |
| 51 | + self.assertIn('msgid_plural not preceded by msgid', err) |
| 52 | + |
| 53 | + def test_plural_without_msgid_plural(self): |
| 54 | + with temp_cwd(): |
| 55 | + Path('invalid.po').write_text('''\ |
| 56 | +msgid "foo" |
| 57 | +msgstr[0] "bar" |
| 58 | +''') |
| 59 | + |
| 60 | + res = assert_python_failure(msgfmt, 'invalid.po') |
| 61 | + err = res.err.decode('utf-8') |
| 62 | + self.assertIn('plural without msgid_plural', err) |
| 63 | + |
| 64 | + def test_indexed_msgstr_without_msgid_plural(self): |
| 65 | + with temp_cwd(): |
| 66 | + Path('invalid.po').write_text('''\ |
| 67 | +msgid "foo" |
| 68 | +msgid_plural "foos" |
| 69 | +msgstr "bar" |
| 70 | +''') |
| 71 | + |
| 72 | + res = assert_python_failure(msgfmt, 'invalid.po') |
| 73 | + err = res.err.decode('utf-8') |
| 74 | + self.assertIn('indexed msgstr required for plural', err) |
| 75 | + |
| 76 | + def test_generic_syntax_error(self): |
| 77 | + with temp_cwd(): |
| 78 | + Path('invalid.po').write_text('''\ |
| 79 | +"foo" |
| 80 | +''') |
| 81 | + |
| 82 | + res = assert_python_failure(msgfmt, 'invalid.po') |
| 83 | + err = res.err.decode('utf-8') |
| 84 | + self.assertIn('Syntax error', err) |
| 85 | + |
| 86 | +class CLITest(unittest.TestCase): |
| 87 | + |
| 88 | + def test_help(self): |
| 89 | + for option in ('--help', '-h'): |
| 90 | + res = assert_python_ok(msgfmt, option) |
| 91 | + err = res.err.decode('utf-8') |
| 92 | + self.assertIn('Generate binary message catalog from textual translation description.', err) |
| 93 | + |
| 94 | + def test_version(self): |
| 95 | + for option in ('--version', '-V'): |
| 96 | + res = assert_python_ok(msgfmt, option) |
| 97 | + out = res.out.decode('utf-8').strip() |
| 98 | + self.assertEqual('msgfmt.py 1.2', out) |
| 99 | + |
| 100 | + def test_invalid_option(self): |
| 101 | + res = assert_python_failure(msgfmt, '--invalid-option') |
| 102 | + err = res.err.decode('utf-8') |
| 103 | + self.assertIn('Generate binary message catalog from textual translation description.', err) |
| 104 | + self.assertIn('option --invalid-option not recognized', err) |
| 105 | + |
| 106 | + def test_no_input_file(self): |
| 107 | + res = assert_python_ok(msgfmt) |
| 108 | + err = res.err.decode('utf-8').replace('\r\n', '\n') |
| 109 | + self.assertIn('No input file given\n' |
| 110 | + "Try `msgfmt --help' for more information.", err) |
| 111 | + |
| 112 | + def test_nonexistent_file(self): |
| 113 | + assert_python_failure(msgfmt, 'nonexistent.po') |
| 114 | + |
| 115 | + |
| 116 | +def update_catalog_snapshots(): |
| 117 | + for po_file in data_dir.glob('*.po'): |
| 118 | + mo_file = po_file.with_suffix('.mo') |
| 119 | + compile_messages(po_file, mo_file) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update': |
| 124 | + update_catalog_snapshots() |
| 125 | + sys.exit(0) |
| 126 | + unittest.main() |
0 commit comments