66import io
77import operator
88import os
9- import re
109import shutil
1110import stat
12- import subprocess
1311import sys
1412import textwrap
1513import tempfile
1816import warnings
1917
2018from enum import StrEnum
21- from pathlib import Path
22- from test .support import REPO_ROOT
23- from test .support import TEST_HOME_DIR
24- from test .support import captured_stderr
25- from test .support import import_helper
2619from test .support import os_helper
27- from test .support import requires_subprocess
28- from test .support import script_helper
29- from test .test_tools import skip_if_missing
20+ from test .support .i18n_helper import TestTranslationsBase , update_translation_snapshots
3021from unittest import mock
3122
3223
@@ -6402,50 +6393,10 @@ def test_os_error(self):
64026393# Translation tests
64036394# =================
64046395
6405- pygettext = Path (REPO_ROOT ) / 'Tools' / 'i18n' / 'pygettext.py'
6406- snapshot_path = Path (TEST_HOME_DIR ) / 'translationdata' / 'argparse' / 'msgids.txt'
6407-
6408- msgid_pattern = re .compile (r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)' , re .DOTALL )
6409- msgid_string_pattern = re .compile (r'"((?:\\"|[^"])*)"' )
6410-
6411-
6412- @requires_subprocess ()
6413- class TestTranslations (unittest .TestCase ):
6396+ class TestTranslations (TestTranslationsBase ):
64146397
64156398 def test_translations (self ):
6416- # Test messages extracted from the argparse module against a snapshot
6417- skip_if_missing ('i18n' )
6418- res = generate_po_file (stdout_only = False )
6419- self .assertEqual (res .returncode , 0 )
6420- self .assertEqual (res .stderr , '' )
6421- msgids = extract_msgids (res .stdout )
6422- snapshot = snapshot_path .read_text ().splitlines ()
6423- self .assertListEqual (msgids , snapshot )
6424-
6425-
6426- def generate_po_file (* , stdout_only = True ):
6427- res = subprocess .run ([sys .executable , pygettext ,
6428- '--no-location' , '-o' , '-' , argparse .__file__ ],
6429- stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
6430- if stdout_only :
6431- return res .stdout
6432- return res
6433-
6434-
6435- def extract_msgids (po ):
6436- msgids = []
6437- for msgid in msgid_pattern .findall (po ):
6438- msgid_string = '' .join (msgid_string_pattern .findall (msgid ))
6439- msgid_string = msgid_string .replace (r'\"' , '"' )
6440- if msgid_string :
6441- msgids .append (msgid_string )
6442- return sorted (msgids )
6443-
6444-
6445- def update_translation_snapshots ():
6446- contents = generate_po_file ()
6447- msgids = extract_msgids (contents )
6448- snapshot_path .write_text ('\n ' .join (msgids ))
6399+ self .assertMsgidsEqual (argparse )
64496400
64506401
64516402def tearDownModule ():
@@ -6457,6 +6408,6 @@ def tearDownModule():
64576408if __name__ == '__main__' :
64586409 # To regenerate translation snapshots
64596410 if len (sys .argv ) > 1 and sys .argv [1 ] == '--snapshot-update' :
6460- update_translation_snapshots ()
6411+ update_translation_snapshots (argparse )
64616412 sys .exit (0 )
64626413 unittest .main ()
0 commit comments