Skip to content

Commit cf7290c

Browse files
tomasr8picnixz
authored andcommitted
pythongh-124295: Add translation tests for argparse (pythonGH-124803)
1 parent bad4fd3 commit cf7290c

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

Lib/test/test_argparse.py

+58
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import operator
88
import os
99
import py_compile
10+
import re
1011
import shutil
1112
import stat
13+
import subprocess
1214
import sys
1315
import textwrap
1416
import tempfile
@@ -17,9 +19,13 @@
1719
import warnings
1820

1921
from enum import StrEnum
22+
from pathlib import Path
23+
from test.support import REPO_ROOT
24+
from test.support import TEST_HOME_DIR
2025
from test.support import captured_stderr
2126
from test.support import import_helper
2227
from test.support import os_helper
28+
from test.support import requires_subprocess
2329
from test.support import script_helper
2430
from unittest import mock
2531

@@ -7014,6 +7020,54 @@ def test_directory_in_zipfile(self, compiled=False):
70147020
def test_directory_in_zipfile_compiled(self):
70157021
self.test_directory_in_zipfile(compiled=True)
70167022

7023+
# =================
7024+
# Translation tests
7025+
# =================
7026+
7027+
pygettext = Path(REPO_ROOT) / 'Tools' / 'i18n' / 'pygettext.py'
7028+
snapshot_path = Path(TEST_HOME_DIR) / 'translationdata' / 'argparse' / 'msgids.txt'
7029+
7030+
msgid_pattern = re.compile(r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)', re.DOTALL)
7031+
msgid_string_pattern = re.compile(r'"((?:\\"|[^"])*)"')
7032+
7033+
7034+
@requires_subprocess()
7035+
class TestTranslations(unittest.TestCase):
7036+
7037+
def test_translations(self):
7038+
# Test messages extracted from the argparse module against a snapshot
7039+
res = generate_po_file(stdout_only=False)
7040+
self.assertEqual(res.returncode, 0)
7041+
self.assertEqual(res.stderr, '')
7042+
msgids = extract_msgids(res.stdout)
7043+
snapshot = snapshot_path.read_text().splitlines()
7044+
self.assertListEqual(msgids, snapshot)
7045+
7046+
7047+
def generate_po_file(*, stdout_only=True):
7048+
res = subprocess.run([sys.executable, pygettext,
7049+
'--no-location', '-o', '-', argparse.__file__],
7050+
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
7051+
if stdout_only:
7052+
return res.stdout
7053+
return res
7054+
7055+
7056+
def extract_msgids(po):
7057+
msgids = []
7058+
for msgid in msgid_pattern.findall(po):
7059+
msgid_string = ''.join(msgid_string_pattern.findall(msgid))
7060+
msgid_string = msgid_string.replace(r'\"', '"')
7061+
if msgid_string:
7062+
msgids.append(msgid_string)
7063+
return sorted(msgids)
7064+
7065+
7066+
def update_translation_snapshots():
7067+
contents = generate_po_file()
7068+
msgids = extract_msgids(contents)
7069+
snapshot_path.write_text('\n'.join(msgids))
7070+
70177071

70187072
def tearDownModule():
70197073
# Remove global references to avoid looking like we have refleaks.
@@ -7022,4 +7076,8 @@ def tearDownModule():
70227076

70237077

70247078
if __name__ == '__main__':
7079+
# To regenerate translation snapshots
7080+
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
7081+
update_translation_snapshots()
7082+
sys.exit(0)
70257083
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 choice: %(value)r, maybe you meant %(closest)r? (choose from %(choices)s)
26+
invalid conflict_resolution value: %r
27+
invalid option string %(option)r: must start with a character %(prefix_chars)r
28+
mutually exclusive arguments must be optional
29+
not allowed with argument %s
30+
one of the arguments %s is required
31+
option '%(option)s' is deprecated
32+
options
33+
positional arguments
34+
show program's version number and exit
35+
show this help message and exit
36+
subcommands
37+
the following arguments are required: %s
38+
unexpected option string: %s
39+
unknown parser %(parser_name)r (choices: %(choices)s)
40+
unrecognized arguments: %s
41+
usage:

Makefile.pre.in

+2
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,8 @@ TESTSUBDIRS= idlelib/idle_test \
25482548
test/tkinterdata \
25492549
test/tokenizedata \
25502550
test/tracedmodules \
2551+
test/translationdata \
2552+
test/translationdata/argparse \
25512553
test/typinganndata \
25522554
test/wheeldata \
25532555
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)