-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-127488: Add tests for Tools/i18n/msgfmt.py
#127540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Fuzzy translations are not written to the .mo file. | ||
#, fuzzy | ||
msgid "foo" | ||
msgstr "bar" | ||
|
||
# comment | ||
#, fuzzy | ||
msgctxt "abc" | ||
msgid "foo" | ||
msgstr "bar" | ||
|
||
#, fuzzy | ||
# comment | ||
msgctxt "xyz" | ||
msgid "foo" | ||
msgstr "bar" | ||
|
||
#, fuzzy | ||
msgctxt "abc" | ||
msgid "One email sent." | ||
msgid_plural "%d emails sent." | ||
msgstr[0] "One email sent." | ||
msgstr[1] "%d emails sent." |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: PACKAGE VERSION\n" | ||
"POT-Creation-Date: 2024-10-26 18:06+0200\n" | ||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
"Language-Team: LANGUAGE <[email protected]>\n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=UTF-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
|
||
msgid "foo" | ||
msgstr "" | ||
|
||
msgid "bar" | ||
msgstr "baz" | ||
|
||
msgctxt "abc" | ||
msgid "foo" | ||
msgstr "bar" | ||
|
||
# comment | ||
msgctxt "xyz" | ||
msgid "foo" | ||
msgstr "bar" | ||
|
||
msgid "Multiline" | ||
"string" | ||
msgstr "Multiline" | ||
"translation" | ||
|
||
msgid "\"escapes\"" | ||
msgstr "\"translated\"" | ||
|
||
msgid "\n newlines \n" | ||
msgstr "\n translated \n" | ||
|
||
msgid "One email sent." | ||
msgid_plural "%d emails sent." | ||
msgstr[0] "One email sent." | ||
msgstr[1] "%d emails sent." | ||
|
||
msgctxt "abc" | ||
msgid "One email sent." | ||
msgid_plural "%d emails sent." | ||
msgstr[0] "One email sent." | ||
msgstr[1] "%d emails sent." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
"""Tests for the Tools/i18n/msgfmt.py tool.""" | ||
|
||
import sys | ||
import unittest | ||
from gettext import GNUTranslations | ||
from pathlib import Path | ||
|
||
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 skip_if_missing, toolsdir | ||
|
||
|
||
skip_if_missing('i18n') | ||
|
||
data_dir = (Path(__file__) / '../msgfmt_data').resolve() | ||
script_dir = Path(toolsdir) / 'i18n' | ||
msgfmt = script_dir / 'msgfmt.py' | ||
|
||
|
||
def compile_messages(po_file, mo_file): | ||
assert_python_ok(msgfmt, '-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(expected._catalog, actual._catalog) | ||
tomasr8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
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, '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, '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, '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, 'invalid.po') | ||
err = res.err.decode('utf-8') | ||
self.assertIn('Syntax error', err) | ||
|
||
class CLITest(unittest.TestCase): | ||
|
||
def test_help(self): | ||
for option in ('--help', '-h'): | ||
res = assert_python_ok(msgfmt, 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, 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, '--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) | ||
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, '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) | ||
|
||
|
||
if __name__ == '__main__': | ||
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update': | ||
update_catalog_snapshots() | ||
sys.exit(0) | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.