7
7
import operator
8
8
import os
9
9
import py_compile
10
- import re
11
10
import shutil
12
11
import stat
13
- import subprocess
14
12
import sys
15
13
import textwrap
16
14
import tempfile
19
17
import warnings
20
18
21
19
from enum import StrEnum
22
- from pathlib import Path
23
- from test .support import REPO_ROOT
24
- from test .support import TEST_HOME_DIR
25
20
from test .support import captured_stderr
26
21
from test .support import import_helper
27
22
from test .support import os_helper
28
- from test .support import requires_subprocess
29
23
from 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
31
25
from unittest import mock
32
26
33
27
@@ -7056,50 +7050,10 @@ def test_directory_in_zipfile_compiled(self):
7056
7050
# Translation tests
7057
7051
# =================
7058
7052
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 ):
7068
7054
7069
7055
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 )
7103
7057
7104
7058
7105
7059
def tearDownModule ():
@@ -7111,6 +7065,6 @@ def tearDownModule():
7111
7065
if __name__ == '__main__' :
7112
7066
# To regenerate translation snapshots
7113
7067
if len (sys .argv ) > 1 and sys .argv [1 ] == '--snapshot-update' :
7114
- update_translation_snapshots ()
7068
+ update_translation_snapshots (argparse )
7115
7069
sys .exit (0 )
7116
7070
unittest .main ()
0 commit comments