77import operator
88import os
99import py_compile
10- import re
1110import shutil
1211import stat
13- import subprocess
1412import sys
1513import textwrap
1614import tempfile
1917import warnings
2018
2119from enum import StrEnum
22- from pathlib import Path
23- from test .support import REPO_ROOT
24- from test .support import TEST_HOME_DIR
2520from test .support import captured_stderr
2621from test .support import import_helper
2722from test .support import os_helper
28- from test .support import requires_subprocess
2923from 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
3125from 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
71057059def tearDownModule ():
@@ -7111,6 +7065,6 @@ def tearDownModule():
71117065if __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 ()
0 commit comments