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
2419from test .support import captured_stderr
25- from test .support import import_helper
2620from 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
21+ from test .support .i18n_helper import TestTranslationsBase , update_translation_snapshots
3022from unittest import mock
3123
3224
@@ -6766,50 +6758,10 @@ def test_os_error(self):
67666758# Translation tests
67676759# =================
67686760
6769- pygettext = Path (REPO_ROOT ) / 'Tools' / 'i18n' / 'pygettext.py'
6770- snapshot_path = Path (TEST_HOME_DIR ) / 'translationdata' / 'argparse' / 'msgids.txt'
6771-
6772- msgid_pattern = re .compile (r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)' , re .DOTALL )
6773- msgid_string_pattern = re .compile (r'"((?:\\"|[^"])*)"' )
6774-
6775-
6776- @requires_subprocess ()
6777- class TestTranslations (unittest .TestCase ):
6761+ class TestTranslations (TestTranslationsBase ):
67786762
67796763 def test_translations (self ):
6780- # Test messages extracted from the argparse module against a snapshot
6781- skip_if_missing ('i18n' )
6782- res = generate_po_file (stdout_only = False )
6783- self .assertEqual (res .returncode , 0 )
6784- self .assertEqual (res .stderr , '' )
6785- msgids = extract_msgids (res .stdout )
6786- snapshot = snapshot_path .read_text ().splitlines ()
6787- self .assertListEqual (msgids , snapshot )
6788-
6789-
6790- def generate_po_file (* , stdout_only = True ):
6791- res = subprocess .run ([sys .executable , pygettext ,
6792- '--no-location' , '-o' , '-' , argparse .__file__ ],
6793- stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
6794- if stdout_only :
6795- return res .stdout
6796- return res
6797-
6798-
6799- def extract_msgids (po ):
6800- msgids = []
6801- for msgid in msgid_pattern .findall (po ):
6802- msgid_string = '' .join (msgid_string_pattern .findall (msgid ))
6803- msgid_string = msgid_string .replace (r'\"' , '"' )
6804- if msgid_string :
6805- msgids .append (msgid_string )
6806- return sorted (msgids )
6807-
6808-
6809- def update_translation_snapshots ():
6810- contents = generate_po_file ()
6811- msgids = extract_msgids (contents )
6812- snapshot_path .write_text ('\n ' .join (msgids ))
6764+ self .assertMsgidsEqual (argparse )
68136765
68146766
68156767def tearDownModule ():
@@ -6821,6 +6773,6 @@ def tearDownModule():
68216773if __name__ == '__main__' :
68226774 # To regenerate translation snapshots
68236775 if len (sys .argv ) > 1 and sys .argv [1 ] == '--snapshot-update' :
6824- update_translation_snapshots ()
6776+ update_translation_snapshots (argparse )
68256777 sys .exit (0 )
68266778 unittest .main ()
0 commit comments