6
6
import io
7
7
import operator
8
8
import os
9
- import re
10
9
import shutil
11
10
import stat
12
- import subprocess
13
11
import sys
14
12
import textwrap
15
13
import tempfile
18
16
import warnings
19
17
20
18
from enum import StrEnum
21
- from pathlib import Path
22
- from test .support import REPO_ROOT
23
- from test .support import TEST_HOME_DIR
24
19
from test .support import captured_stderr
25
- from test .support import import_helper
26
20
from 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
30
22
from unittest import mock
31
23
32
24
@@ -6766,50 +6758,10 @@ def test_os_error(self):
6766
6758
# Translation tests
6767
6759
# =================
6768
6760
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 ):
6778
6762
6779
6763
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 )
6813
6765
6814
6766
6815
6767
def tearDownModule ():
@@ -6821,6 +6773,6 @@ def tearDownModule():
6821
6773
if __name__ == '__main__' :
6822
6774
# To regenerate translation snapshots
6823
6775
if len (sys .argv ) > 1 and sys .argv [1 ] == '--snapshot-update' :
6824
- update_translation_snapshots ()
6776
+ update_translation_snapshots (argparse )
6825
6777
sys .exit (0 )
6826
6778
unittest .main ()
0 commit comments