Skip to content
7 changes: 7 additions & 0 deletions Lib/test/test_argparse/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

from test import support


def load_tests(*args):
return support.load_package_tests(os.path.dirname(__file__), *args)
40 changes: 40 additions & 0 deletions Lib/test/test_argparse/data/msgids.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(default: %(default)s)
%(heading)s:
%(prog)s: error: %(message)s\n
%(prog)s: warning: %(message)s\n
%r is not callable
'required' is an invalid argument for positionals
.__call__() not defined
ambiguous option: %(option)s could match %(matches)s
argument "-" with mode %r
argument %(argument_name)s: %(message)s
argument '%(argument_name)s' is deprecated
can't open '%(filename)s': %(error)s
cannot have multiple subparser arguments
cannot merge actions - two groups are named %r
command '%(parser_name)s' is deprecated
conflicting subparser alias: %s
conflicting subparser: %s
dest= is required for options like %r
expected at least one argument
expected at most one argument
expected one argument
ignored explicit argument %r
invalid %(type)s value: %(value)r
invalid choice: %(value)r (choose from %(choices)s)
invalid conflict_resolution value: %r
invalid option string %(option)r: must start with a character %(prefix_chars)r
mutually exclusive arguments must be optional
not allowed with argument %s
one of the arguments %s is required
option '%(option)s' is deprecated
options
positional arguments
show program's version number and exit
show this help message and exit
subcommands
the following arguments are required: %s
unexpected option string: %s
unknown parser %(parser_name)r (choices: %(choices)s)
unrecognized arguments: %s
usage:
File renamed without changes.
60 changes: 60 additions & 0 deletions Lib/test/test_argparse/test_translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import argparse
import re
import subprocess
import sys
import unittest

from pathlib import Path


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()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add translation tests to the mod:`argparse` module.