Skip to content

Commit 1adb2d6

Browse files
tomasr8picnixz
authored andcommitted
pythongh-126413: Add translation tests for getopt and optparse (pythonGH-126698)
1 parent 79a3fa1 commit 1adb2d6

File tree

7 files changed

+114
-55
lines changed

7 files changed

+114
-55
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-50
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import operator
88
import os
99
import py_compile
10-
import re
1110
import shutil
1211
import stat
13-
import subprocess
1412
import sys
1513
import textwrap
1614
import tempfile
@@ -19,15 +17,11 @@
1917
import warnings
2018

2119
from enum import StrEnum
22-
from pathlib import Path
23-
from test.support import REPO_ROOT
24-
from test.support import TEST_HOME_DIR
2520
from test.support import captured_stderr
2621
from test.support import import_helper
2722
from test.support import os_helper
28-
from test.support import requires_subprocess
2923
from test.support import script_helper
30-
from test.test_tools import skip_if_missing
24+
from test.support.i18n_helper import TestTranslationsBase, update_translation_snapshots
3125
from unittest import mock
3226

3327

@@ -7056,50 +7050,10 @@ def test_directory_in_zipfile_compiled(self):
70567050
# Translation tests
70577051
# =================
70587052

7059-
pygettext = Path(REPO_ROOT) / 'Tools' / 'i18n' / 'pygettext.py'
7060-
snapshot_path = Path(TEST_HOME_DIR) / 'translationdata' / 'argparse' / 'msgids.txt'
7061-
7062-
msgid_pattern = re.compile(r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)', re.DOTALL)
7063-
msgid_string_pattern = re.compile(r'"((?:\\"|[^"])*)"')
7064-
7065-
7066-
@requires_subprocess()
7067-
class TestTranslations(unittest.TestCase):
7053+
class TestTranslations(TestTranslationsBase):
70687054

70697055
def test_translations(self):
7070-
# Test messages extracted from the argparse module against a snapshot
7071-
skip_if_missing('i18n')
7072-
res = generate_po_file(stdout_only=False)
7073-
self.assertEqual(res.returncode, 0)
7074-
self.assertEqual(res.stderr, '')
7075-
msgids = extract_msgids(res.stdout)
7076-
snapshot = snapshot_path.read_text().splitlines()
7077-
self.assertListEqual(msgids, snapshot)
7078-
7079-
7080-
def generate_po_file(*, stdout_only=True):
7081-
res = subprocess.run([sys.executable, pygettext,
7082-
'--no-location', '-o', '-', argparse.__file__],
7083-
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
7084-
if stdout_only:
7085-
return res.stdout
7086-
return res
7087-
7088-
7089-
def extract_msgids(po):
7090-
msgids = []
7091-
for msgid in msgid_pattern.findall(po):
7092-
msgid_string = ''.join(msgid_string_pattern.findall(msgid))
7093-
msgid_string = msgid_string.replace(r'\"', '"')
7094-
if msgid_string:
7095-
msgids.append(msgid_string)
7096-
return sorted(msgids)
7097-
7098-
7099-
def update_translation_snapshots():
7100-
contents = generate_po_file()
7101-
msgids = extract_msgids(contents)
7102-
snapshot_path.write_text('\n'.join(msgids))
7056+
self.assertMsgidsEqual(argparse)
71037057

71047058

71057059
def tearDownModule():
@@ -7111,6 +7065,6 @@ def tearDownModule():
71117065
if __name__ == '__main__':
71127066
# To regenerate translation snapshots
71137067
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
7114-
update_translation_snapshots()
7068+
update_translation_snapshots(argparse)
71157069
sys.exit(0)
71167070
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

@@ -224,10 +225,20 @@ def test_libref_examples():
224225
['a1', 'a2']
225226
"""
226227

228+
229+
class TestTranslations(TestTranslationsBase):
230+
def test_translations(self):
231+
self.assertMsgidsEqual(getopt)
232+
233+
227234
def load_tests(loader, tests, pattern):
228235
tests.addTest(doctest.DocTestSuite())
229236
return tests
230237

231238

232-
if __name__ == "__main__":
239+
if __name__ == '__main__':
240+
# To regenerate translation snapshots
241+
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
242+
update_translation_snapshots(getopt)
243+
sys.exit(0)
233244
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
@@ -2567,6 +2567,8 @@ TESTSUBDIRS= idlelib/idle_test \
25672567
test/tracedmodules \
25682568
test/translationdata \
25692569
test/translationdata/argparse \
2570+
test/translationdata/getopt \
2571+
test/translationdata/optparse \
25702572
test/typinganndata \
25712573
test/wheeldata \
25722574
test/xmltestdata \

0 commit comments

Comments
 (0)