-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-124295: Add translation tests for argparse #124803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b0f80b5
Move test_argparse.py into a folder
tomasr8 97537ca
Add translation tests
tomasr8 7885932
Add news entry
tomasr8 78d005f
Merge remote-tracking branch 'upstream/main' into argparse-tests
tomasr8 136af62
Fix news entry typo
tomasr8 6e535fb
Merge branch 'main' into argparse-tests
tomasr8 f5decd8
Add test folders to Makefile
tomasr8 80a4bad
Update snapshots
tomasr8 da3986c
Skip on platforms without subprocess support
tomasr8 a9349a5
Create a new global folder for all translation data.
tomasr8 28ae5f2
Use `requires_subprocess`
tomasr8 21c9aaf
Use `REPO_ROOT` and `TEST_HOME_DIR`
tomasr8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Tests/2024-09-30-22-52-44.gh-issue-124295.VZy5kx.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add translation tests to the mod:`argparse` module. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.