Skip to content

Commit ff044ed

Browse files
[3.13] gh-124295: Add translation tests for argparse (GH-124803) (GH-126046)
(cherry picked from commit 0922a4a) Co-authored-by: Tomas R. <[email protected]>
1 parent ac31a26 commit ff044ed

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

Lib/test/test_argparse.py

+65-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import io
77
import operator
88
import os
9+
import re
910
import shutil
1011
import stat
12+
import subprocess
1113
import sys
1214
import textwrap
1315
import tempfile
@@ -16,7 +18,15 @@
1618
import warnings
1719

1820
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
2030
from unittest import mock
2131

2232

@@ -6752,11 +6762,65 @@ def test_os_error(self):
67526762
self.parser.parse_args, ['@no-such-file'])
67536763

67546764

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+
67556815
def tearDownModule():
67566816
# Remove global references to avoid looking like we have refleaks.
67576817
RFile.seen = {}
67586818
WFile.seen = set()
67596819

67606820

67616821
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)
67626826
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
(default: %(default)s)
2+
%(heading)s:
3+
%(prog)s: error: %(message)s\n
4+
%(prog)s: warning: %(message)s\n
5+
%r is not callable
6+
'required' is an invalid argument for positionals
7+
.__call__() not defined
8+
ambiguous option: %(option)s could match %(matches)s
9+
argument "-" with mode %r
10+
argument %(argument_name)s: %(message)s
11+
argument '%(argument_name)s' is deprecated
12+
can't open '%(filename)s': %(error)s
13+
cannot have multiple subparser arguments
14+
cannot merge actions - two groups are named %r
15+
command '%(parser_name)s' is deprecated
16+
conflicting subparser alias: %s
17+
conflicting subparser: %s
18+
dest= is required for options like %r
19+
expected at least one argument
20+
expected at most one argument
21+
expected one argument
22+
ignored explicit argument %r
23+
invalid %(type)s value: %(value)r
24+
invalid choice: %(value)r (choose from %(choices)s)
25+
invalid conflict_resolution value: %r
26+
invalid option string %(option)r: must start with a character %(prefix_chars)r
27+
mutually exclusive arguments must be optional
28+
not allowed with argument %s
29+
one of the arguments %s is required
30+
option '%(option)s' is deprecated
31+
options
32+
positional arguments
33+
show program's version number and exit
34+
show this help message and exit
35+
subcommands
36+
the following arguments are required: %s
37+
unexpected option string: %s
38+
unknown parser %(parser_name)r (choices: %(choices)s)
39+
unrecognized arguments: %s
40+
usage:

Makefile.pre.in

+2
Original file line numberDiff line numberDiff line change
@@ -2488,6 +2488,8 @@ TESTSUBDIRS= idlelib/idle_test \
24882488
test/tkinterdata \
24892489
test/tokenizedata \
24902490
test/tracedmodules \
2491+
test/translationdata \
2492+
test/translationdata/argparse \
24912493
test/typinganndata \
24922494
test/wheeldata \
24932495
test/xmltestdata \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add translation tests to the :mod:`argparse` module.

0 commit comments

Comments
 (0)