|
6 | 6 | import io
|
7 | 7 | import operator
|
8 | 8 | import os
|
| 9 | +import re |
9 | 10 | import shutil
|
10 | 11 | import stat
|
| 12 | +import subprocess |
11 | 13 | import sys
|
12 | 14 | import textwrap
|
13 | 15 | import tempfile
|
|
16 | 18 | import warnings
|
17 | 19 |
|
18 | 20 | from enum import StrEnum
|
19 |
| -from test.support import os_helper, captured_stderr |
| 21 | +from pathlib import Path |
| 22 | +from test.support import REPO_ROOT |
| 23 | +from test.support import TEST_HOME_DIR |
| 24 | +from test.support import captured_stderr |
| 25 | +from test.support import import_helper |
| 26 | +from test.support import os_helper |
| 27 | +from test.support import requires_subprocess |
| 28 | +from test.support import script_helper |
| 29 | +from test.test_tools import skip_if_missing |
20 | 30 | from unittest import mock
|
21 | 31 |
|
22 | 32 |
|
@@ -6752,11 +6762,65 @@ def test_os_error(self):
|
6752 | 6762 | self.parser.parse_args, ['@no-such-file'])
|
6753 | 6763 |
|
6754 | 6764 |
|
| 6765 | +# ================= |
| 6766 | +# Translation tests |
| 6767 | +# ================= |
| 6768 | + |
| 6769 | +pygettext = Path(REPO_ROOT) / 'Tools' / 'i18n' / 'pygettext.py' |
| 6770 | +snapshot_path = Path(TEST_HOME_DIR) / 'translationdata' / 'argparse' / 'msgids.txt' |
| 6771 | + |
| 6772 | +msgid_pattern = re.compile(r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)', re.DOTALL) |
| 6773 | +msgid_string_pattern = re.compile(r'"((?:\\"|[^"])*)"') |
| 6774 | + |
| 6775 | + |
| 6776 | +@requires_subprocess() |
| 6777 | +class TestTranslations(unittest.TestCase): |
| 6778 | + |
| 6779 | + def test_translations(self): |
| 6780 | + # Test messages extracted from the argparse module against a snapshot |
| 6781 | + skip_if_missing('i18n') |
| 6782 | + res = generate_po_file(stdout_only=False) |
| 6783 | + self.assertEqual(res.returncode, 0) |
| 6784 | + self.assertEqual(res.stderr, '') |
| 6785 | + msgids = extract_msgids(res.stdout) |
| 6786 | + snapshot = snapshot_path.read_text().splitlines() |
| 6787 | + self.assertListEqual(msgids, snapshot) |
| 6788 | + |
| 6789 | + |
| 6790 | +def generate_po_file(*, stdout_only=True): |
| 6791 | + res = subprocess.run([sys.executable, pygettext, |
| 6792 | + '--no-location', '-o', '-', argparse.__file__], |
| 6793 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
| 6794 | + if stdout_only: |
| 6795 | + return res.stdout |
| 6796 | + return res |
| 6797 | + |
| 6798 | + |
| 6799 | +def extract_msgids(po): |
| 6800 | + msgids = [] |
| 6801 | + for msgid in msgid_pattern.findall(po): |
| 6802 | + msgid_string = ''.join(msgid_string_pattern.findall(msgid)) |
| 6803 | + msgid_string = msgid_string.replace(r'\"', '"') |
| 6804 | + if msgid_string: |
| 6805 | + msgids.append(msgid_string) |
| 6806 | + return sorted(msgids) |
| 6807 | + |
| 6808 | + |
| 6809 | +def update_translation_snapshots(): |
| 6810 | + contents = generate_po_file() |
| 6811 | + msgids = extract_msgids(contents) |
| 6812 | + snapshot_path.write_text('\n'.join(msgids)) |
| 6813 | + |
| 6814 | + |
6755 | 6815 | def tearDownModule():
|
6756 | 6816 | # Remove global references to avoid looking like we have refleaks.
|
6757 | 6817 | RFile.seen = {}
|
6758 | 6818 | WFile.seen = set()
|
6759 | 6819 |
|
6760 | 6820 |
|
6761 | 6821 | if __name__ == '__main__':
|
| 6822 | + # To regenerate translation snapshots |
| 6823 | + if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update': |
| 6824 | + update_translation_snapshots() |
| 6825 | + sys.exit(0) |
6762 | 6826 | unittest.main()
|
0 commit comments