mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	gh-126413: Add translation tests for getopt and optparse (GH-126698)
This commit is contained in:
		
							parent
							
								
									b697d8c48e
								
							
						
					
					
						commit
						dff074d144
					
				
					 7 changed files with 114 additions and 55 deletions
				
			
		| 
						 | 
				
			
			@ -7,10 +7,8 @@
 | 
			
		|||
import operator
 | 
			
		||||
import os
 | 
			
		||||
import py_compile
 | 
			
		||||
import re
 | 
			
		||||
import shutil
 | 
			
		||||
import stat
 | 
			
		||||
import subprocess
 | 
			
		||||
import sys
 | 
			
		||||
import textwrap
 | 
			
		||||
import tempfile
 | 
			
		||||
| 
						 | 
				
			
			@ -19,15 +17,11 @@
 | 
			
		|||
import warnings
 | 
			
		||||
 | 
			
		||||
from enum import StrEnum
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
from test.support import REPO_ROOT
 | 
			
		||||
from test.support import TEST_HOME_DIR
 | 
			
		||||
from test.support import captured_stderr
 | 
			
		||||
from test.support import import_helper
 | 
			
		||||
from test.support import os_helper
 | 
			
		||||
from test.support import requires_subprocess
 | 
			
		||||
from test.support import script_helper
 | 
			
		||||
from test.test_tools import skip_if_missing
 | 
			
		||||
from test.support.i18n_helper import TestTranslationsBase, update_translation_snapshots
 | 
			
		||||
from unittest import mock
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -7056,50 +7050,10 @@ def test_directory_in_zipfile_compiled(self):
 | 
			
		|||
# Translation tests
 | 
			
		||||
# =================
 | 
			
		||||
 | 
			
		||||
pygettext = Path(REPO_ROOT) / 'Tools' / 'i18n' / 'pygettext.py'
 | 
			
		||||
snapshot_path = Path(TEST_HOME_DIR) / 'translationdata' / 'argparse' / 'msgids.txt'
 | 
			
		||||
 | 
			
		||||
msgid_pattern = re.compile(r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)', re.DOTALL)
 | 
			
		||||
msgid_string_pattern = re.compile(r'"((?:\\"|[^"])*)"')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@requires_subprocess()
 | 
			
		||||
class TestTranslations(unittest.TestCase):
 | 
			
		||||
class TestTranslations(TestTranslationsBase):
 | 
			
		||||
 | 
			
		||||
    def test_translations(self):
 | 
			
		||||
        # Test messages extracted from the argparse module against a snapshot
 | 
			
		||||
        skip_if_missing('i18n')
 | 
			
		||||
        res = generate_po_file(stdout_only=False)
 | 
			
		||||
        self.assertEqual(res.returncode, 0)
 | 
			
		||||
        self.assertEqual(res.stderr, '')
 | 
			
		||||
        msgids = extract_msgids(res.stdout)
 | 
			
		||||
        snapshot = snapshot_path.read_text().splitlines()
 | 
			
		||||
        self.assertListEqual(msgids, snapshot)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def generate_po_file(*, stdout_only=True):
 | 
			
		||||
    res = subprocess.run([sys.executable, pygettext,
 | 
			
		||||
                          '--no-location', '-o', '-', argparse.__file__],
 | 
			
		||||
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
 | 
			
		||||
    if stdout_only:
 | 
			
		||||
        return res.stdout
 | 
			
		||||
    return res
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def extract_msgids(po):
 | 
			
		||||
    msgids = []
 | 
			
		||||
    for msgid in msgid_pattern.findall(po):
 | 
			
		||||
        msgid_string = ''.join(msgid_string_pattern.findall(msgid))
 | 
			
		||||
        msgid_string = msgid_string.replace(r'\"', '"')
 | 
			
		||||
        if msgid_string:
 | 
			
		||||
            msgids.append(msgid_string)
 | 
			
		||||
    return sorted(msgids)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def update_translation_snapshots():
 | 
			
		||||
    contents = generate_po_file()
 | 
			
		||||
    msgids = extract_msgids(contents)
 | 
			
		||||
    snapshot_path.write_text('\n'.join(msgids))
 | 
			
		||||
        self.assertMsgidsEqual(argparse)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def tearDownModule():
 | 
			
		||||
| 
						 | 
				
			
			@ -7111,6 +7065,6 @@ def tearDownModule():
 | 
			
		|||
if __name__ == '__main__':
 | 
			
		||||
    # To regenerate translation snapshots
 | 
			
		||||
    if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
 | 
			
		||||
        update_translation_snapshots()
 | 
			
		||||
        update_translation_snapshots(argparse)
 | 
			
		||||
        sys.exit(0)
 | 
			
		||||
    unittest.main()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue