Skip to content

Commit 4c039c0

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

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

Lib/test/test_argparse.py

+64
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
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
1926
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

@@ -6388,11 +6398,65 @@ def test_os_error(self):
63886398
self.parser.parse_args, ['@no-such-file'])
63896399

63906400

6401+
# =================
6402+
# Translation tests
6403+
# =================
6404+
6405+
pygettext = Path(REPO_ROOT) / 'Tools' / 'i18n' / 'pygettext.py'
6406+
snapshot_path = Path(TEST_HOME_DIR) / 'translationdata' / 'argparse' / 'msgids.txt'
6407+
6408+
msgid_pattern = re.compile(r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)', re.DOTALL)
6409+
msgid_string_pattern = re.compile(r'"((?:\\"|[^"])*)"')
6410+
6411+
6412+
@requires_subprocess()
6413+
class TestTranslations(unittest.TestCase):
6414+
6415+
def test_translations(self):
6416+
# Test messages extracted from the argparse module against a snapshot
6417+
skip_if_missing('i18n')
6418+
res = generate_po_file(stdout_only=False)
6419+
self.assertEqual(res.returncode, 0)
6420+
self.assertEqual(res.stderr, '')
6421+
msgids = extract_msgids(res.stdout)
6422+
snapshot = snapshot_path.read_text().splitlines()
6423+
self.assertListEqual(msgids, snapshot)
6424+
6425+
6426+
def generate_po_file(*, stdout_only=True):
6427+
res = subprocess.run([sys.executable, pygettext,
6428+
'--no-location', '-o', '-', argparse.__file__],
6429+
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
6430+
if stdout_only:
6431+
return res.stdout
6432+
return res
6433+
6434+
6435+
def extract_msgids(po):
6436+
msgids = []
6437+
for msgid in msgid_pattern.findall(po):
6438+
msgid_string = ''.join(msgid_string_pattern.findall(msgid))
6439+
msgid_string = msgid_string.replace(r'\"', '"')
6440+
if msgid_string:
6441+
msgids.append(msgid_string)
6442+
return sorted(msgids)
6443+
6444+
6445+
def update_translation_snapshots():
6446+
contents = generate_po_file()
6447+
msgids = extract_msgids(contents)
6448+
snapshot_path.write_text('\n'.join(msgids))
6449+
6450+
63916451
def tearDownModule():
63926452
# Remove global references to avoid looking like we have refleaks.
63936453
RFile.seen = {}
63946454
WFile.seen = set()
63956455

63966456

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

Makefile.pre.in

+2
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,8 @@ TESTSUBDIRS= idlelib/idle_test \
22462246
test/test_zoneinfo/data \
22472247
test/tokenizedata \
22482248
test/tracedmodules \
2249+
test/translationdata \
2250+
test/translationdata/argparse \
22492251
test/typinganndata \
22502252
test/wheeldata \
22512253
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)