Skip to content

Commit

Permalink
Create a new global folder for all translation data.
Browse files Browse the repository at this point in the history
This avoids having to turn tests into packages.
  • Loading branch information
tomasr8 committed Oct 27, 2024
1 parent da3986c commit a9349a5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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()
7 changes: 0 additions & 7 deletions Lib/test/test_argparse/__init__.py

This file was deleted.

64 changes: 0 additions & 64 deletions Lib/test/test_argparse/test_translations.py

This file was deleted.

File renamed without changes.
4 changes: 2 additions & 2 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down

0 comments on commit a9349a5

Please sign in to comment.