diff --git a/Lib/test/test_argparse/test_argparse.py b/Lib/test/test_argparse.py similarity index 99% rename from Lib/test/test_argparse/test_argparse.py rename to Lib/test/test_argparse.py index ed1c5c34e526aa..e3070216aefd7b 100644 --- a/Lib/test/test_argparse/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -7,8 +7,10 @@ import operator import os import py_compile +import re import shutil import stat +import subprocess import sys import textwrap import tempfile @@ -17,7 +19,9 @@ import warnings from enum import StrEnum +from pathlib import Path from test.support import captured_stderr +from test.support import has_subprocess_support from test.support import import_helper from test.support import os_helper from test.support import script_helper @@ -7014,6 +7018,55 @@ def test_directory_in_zipfile(self, compiled=False): def test_directory_in_zipfile_compiled(self): self.test_directory_in_zipfile(compiled=True) +# ================= +# Translation tests +# ================= + +i18n_tools = Path(__file__).parents[2] / 'Tools' / 'i18n' +pygettext = i18n_tools / 'pygettext.py' +snapshot_path = Path(__file__).parent / 'translationdata' / 'argparse' / 'msgids.txt' + +msgid_pattern = re.compile(r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)', re.DOTALL) +msgid_string_pattern = re.compile(r'"((?:\\"|[^"])*)"') + + +@unittest.skipIf(not has_subprocess_support, "test requires subprocess") +class TestTranslations(unittest.TestCase): + + def test_translations(self): + # Test messages extracted from the argparse module against a snapshot + res = generate_po_file(stdout_only=False) + self.assertEqual(res.returncode, 0) + self.assertEqual(res.stderr, '') + msgids = extract_msgids(res.stdout) + snapshot = snapshot_path.read_text().splitlines() + self.assertListEqual(msgids, snapshot) + + +def generate_po_file(*, stdout_only=True): + res = subprocess.run([sys.executable, pygettext, + '--no-location', '-o', '-', argparse.__file__], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + if stdout_only: + return res.stdout + return res + + +def extract_msgids(po): + msgids = [] + for msgid in msgid_pattern.findall(po): + msgid_string = ''.join(msgid_string_pattern.findall(msgid)) + msgid_string = msgid_string.replace(r'\"', '"') + if msgid_string: + msgids.append(msgid_string) + return sorted(msgids) + + +def update_translation_snapshots(): + contents = generate_po_file() + msgids = extract_msgids(contents) + snapshot_path.write_text('\n'.join(msgids)) + def tearDownModule(): # Remove global references to avoid looking like we have refleaks. @@ -7022,4 +7075,8 @@ def tearDownModule(): if __name__ == '__main__': + # To regenerate translation snapshots + if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update': + update_translation_snapshots() + sys.exit(0) unittest.main() diff --git a/Lib/test/test_argparse/__init__.py b/Lib/test/test_argparse/__init__.py deleted file mode 100644 index 9a89d27ba9f979..00000000000000 --- a/Lib/test/test_argparse/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -import os - -from test import support - - -def load_tests(*args): - return support.load_package_tests(os.path.dirname(__file__), *args) diff --git a/Lib/test/test_argparse/test_translations.py b/Lib/test/test_argparse/test_translations.py deleted file mode 100644 index d3838f4b6dd8e5..00000000000000 --- a/Lib/test/test_argparse/test_translations.py +++ /dev/null @@ -1,64 +0,0 @@ -import argparse -import re -import subprocess -import sys -import unittest - -from pathlib import Path -from test import support - - -if not support.has_subprocess_support: - raise unittest.SkipTest("test module requires subprocess") - -i18n_tools = Path(__file__).parents[3] / 'Tools' / 'i18n' -pygettext = i18n_tools / 'pygettext.py' -snapshot_path = Path(__file__).parents[0] / 'data' / 'msgids.txt' - -msgid_pattern = re.compile(r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)', re.DOTALL) -msgid_string_pattern = re.compile(r'"((?:\\"|[^"])*)"') - - -class TestTranslations(unittest.TestCase): - - def test_translations(self): - # Test messages extracted from the argparse module against a snapshot - res = generate_po_file(stdout_only=False) - self.assertEqual(res.returncode, 0) - self.assertEqual(res.stderr, '') - msgids = extract_msgids(res.stdout) - snapshot = snapshot_path.read_text().splitlines() - self.assertListEqual(msgids, snapshot) - - -def generate_po_file(*, stdout_only=True): - res = subprocess.run([sys.executable, pygettext, - '--no-location', '-o', '-', argparse.__file__], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) - if stdout_only: - return res.stdout - return res - - -def extract_msgids(po): - msgids = [] - for msgid in msgid_pattern.findall(po): - msgid_string = ''.join(msgid_string_pattern.findall(msgid)) - msgid_string = msgid_string.replace(r'\"', '"') - if msgid_string: - msgids.append(msgid_string) - return sorted(msgids) - - -def update_translation_snapshots(): - contents = generate_po_file() - msgids = extract_msgids(contents) - snapshot_path.write_text('\n'.join(msgids)) - - -if __name__ == '__main__': - # To regenerate translation snapshots - if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update': - update_translation_snapshots() - sys.exit(0) - unittest.main() diff --git a/Lib/test/test_argparse/data/msgids.txt b/Lib/test/translationdata/argparse/msgids.txt similarity index 100% rename from Lib/test/test_argparse/data/msgids.txt rename to Lib/test/translationdata/argparse/msgids.txt diff --git a/Makefile.pre.in b/Makefile.pre.in index 8d3898a7b818a6..751e7ab8b427f5 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -2418,8 +2418,6 @@ LIBSUBDIRS= asyncio \ __phello__ TESTSUBDIRS= idlelib/idle_test \ test \ - test/test_argparse \ - test/test_argparse/data \ test/test_ast \ test/test_ast/data \ test/archivetestdata \ @@ -2550,6 +2548,8 @@ TESTSUBDIRS= idlelib/idle_test \ test/tkinterdata \ test/tokenizedata \ test/tracedmodules \ + test/translationdata \ + test/translationdata/argparse \ test/typinganndata \ test/wheeldata \ test/xmltestdata \