Skip to content

Commit 1c15870

Browse files
authored
[3.13] gh-126413: Add translation tests for getopt and optparse (GH-126698) (GH-126755)
(cherry picked from commit dff074d)
1 parent cb07c44 commit 1c15870

File tree

7 files changed

+114
-57
lines changed

7 files changed

+114
-57
lines changed

Lib/test/support/i18n_helper.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import re
2+
import subprocess
3+
import sys
4+
import unittest
5+
from pathlib import Path
6+
from test.support import REPO_ROOT, TEST_HOME_DIR, requires_subprocess
7+
from test.test_tools import skip_if_missing
8+
9+
10+
pygettext = Path(REPO_ROOT) / 'Tools' / 'i18n' / 'pygettext.py'
11+
12+
msgid_pattern = re.compile(r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)',
13+
re.DOTALL)
14+
msgid_string_pattern = re.compile(r'"((?:\\"|[^"])*)"')
15+
16+
17+
def _generate_po_file(path, *, stdout_only=True):
18+
res = subprocess.run([sys.executable, pygettext,
19+
'--no-location', '-o', '-', path],
20+
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
21+
text=True)
22+
if stdout_only:
23+
return res.stdout
24+
return res
25+
26+
27+
def _extract_msgids(po):
28+
msgids = []
29+
for msgid in msgid_pattern.findall(po):
30+
msgid_string = ''.join(msgid_string_pattern.findall(msgid))
31+
msgid_string = msgid_string.replace(r'\"', '"')
32+
if msgid_string:
33+
msgids.append(msgid_string)
34+
return sorted(msgids)
35+
36+
37+
def _get_snapshot_path(module_name):
38+
return Path(TEST_HOME_DIR) / 'translationdata' / module_name / 'msgids.txt'
39+
40+
41+
@requires_subprocess()
42+
class TestTranslationsBase(unittest.TestCase):
43+
44+
def assertMsgidsEqual(self, module):
45+
'''Assert that msgids extracted from a given module match a
46+
snapshot.
47+
48+
'''
49+
skip_if_missing('i18n')
50+
res = _generate_po_file(module.__file__, stdout_only=False)
51+
self.assertEqual(res.returncode, 0)
52+
self.assertEqual(res.stderr, '')
53+
msgids = _extract_msgids(res.stdout)
54+
snapshot_path = _get_snapshot_path(module.__name__)
55+
snapshot = snapshot_path.read_text().splitlines()
56+
self.assertListEqual(msgids, snapshot)
57+
58+
59+
def update_translation_snapshots(module):
60+
contents = _generate_po_file(module.__file__)
61+
msgids = _extract_msgids(contents)
62+
snapshot_path = _get_snapshot_path(module.__name__)
63+
snapshot_path.write_text('\n'.join(msgids))

Lib/test/test_argparse.py

+4-52
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
import io
77
import operator
88
import os
9-
import re
109
import shutil
1110
import stat
12-
import subprocess
1311
import sys
1412
import textwrap
1513
import tempfile
@@ -18,15 +16,9 @@
1816
import warnings
1917

2018
from enum import StrEnum
21-
from pathlib import Path
22-
from test.support import REPO_ROOT
23-
from test.support import TEST_HOME_DIR
2419
from test.support import captured_stderr
25-
from test.support import import_helper
2620
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
21+
from test.support.i18n_helper import TestTranslationsBase, update_translation_snapshots
3022
from unittest import mock
3123

3224

@@ -6766,50 +6758,10 @@ def test_os_error(self):
67666758
# Translation tests
67676759
# =================
67686760

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):
6761+
class TestTranslations(TestTranslationsBase):
67786762

67796763
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))
6764+
self.assertMsgidsEqual(argparse)
68136765

68146766

68156767
def tearDownModule():
@@ -6821,6 +6773,6 @@ def tearDownModule():
68216773
if __name__ == '__main__':
68226774
# To regenerate translation snapshots
68236775
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
6824-
update_translation_snapshots()
6776+
update_translation_snapshots(argparse)
68256777
sys.exit(0)
68266778
unittest.main()

Lib/test/test_getopt.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# test_getopt.py
22
# David Goodger <[email protected]> 2000-08-19
33

4-
from test.support.os_helper import EnvironmentVarGuard
54
import doctest
6-
import unittest
7-
85
import getopt
6+
import sys
7+
import unittest
8+
from test.support.i18n_helper import TestTranslationsBase, update_translation_snapshots
9+
from test.support.os_helper import EnvironmentVarGuard
910

1011
sentinel = object()
1112

@@ -173,10 +174,20 @@ def test_libref_examples():
173174
['a1', 'a2']
174175
"""
175176

177+
178+
class TestTranslations(TestTranslationsBase):
179+
def test_translations(self):
180+
self.assertMsgidsEqual(getopt)
181+
182+
176183
def load_tests(loader, tests, pattern):
177184
tests.addTest(doctest.DocTestSuite())
178185
return tests
179186

180187

181-
if __name__ == "__main__":
188+
if __name__ == '__main__':
189+
# To regenerate translation snapshots
190+
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
191+
update_translation_snapshots(getopt)
192+
sys.exit(0)
182193
unittest.main()

Lib/test/test_optparse.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from io import StringIO
1616
from test import support
1717
from test.support import os_helper
18-
18+
from test.support.i18n_helper import TestTranslationsBase, update_translation_snapshots
1919

2020
import optparse
2121
from optparse import make_option, Option, \
@@ -1656,5 +1656,14 @@ def test__all__(self):
16561656
support.check__all__(self, optparse, not_exported=not_exported)
16571657

16581658

1659+
class TestTranslations(TestTranslationsBase):
1660+
def test_translations(self):
1661+
self.assertMsgidsEqual(optparse)
1662+
1663+
16591664
if __name__ == '__main__':
1665+
# To regenerate translation snapshots
1666+
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
1667+
update_translation_snapshots(optparse)
1668+
sys.exit(0)
16601669
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
option -%s not recognized
2+
option -%s requires argument
3+
option --%s must not have an argument
4+
option --%s not a unique prefix
5+
option --%s not recognized
6+
option --%s requires argument
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
%prog [options]
2+
%s option does not take a value
3+
Options
4+
Usage
5+
Usage: %s\n
6+
ambiguous option: %s (%s?)
7+
complex
8+
floating-point
9+
integer
10+
no such option: %s
11+
option %s: invalid %s value: %r
12+
option %s: invalid choice: %r (choose from %s)
13+
show program's version number and exit
14+
show this help message and exit

Makefile.pre.in

+2
Original file line numberDiff line numberDiff line change
@@ -2491,6 +2491,8 @@ TESTSUBDIRS= idlelib/idle_test \
24912491
test/tracedmodules \
24922492
test/translationdata \
24932493
test/translationdata/argparse \
2494+
test/translationdata/getopt \
2495+
test/translationdata/optparse \
24942496
test/typinganndata \
24952497
test/wheeldata \
24962498
test/xmltestdata \

0 commit comments

Comments
 (0)