| 1 |
"""Tests to cover the Tools/i18n package""" |
| 2 |
|
| 3 |
import os |
| 4 |
import re |
| 5 |
import sys |
| 6 |
import unittest |
| 7 |
from textwrap import dedent |
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
from test.support.script_helper import assert_python_ok |
| 11 |
from test.test_tools import imports_under_tool, skip_if_missing, toolsdir |
| 12 |
from test.support.os_helper import temp_cwd, temp_dir |
| 13 |
|
| 14 |
|
| 15 |
skip_if_missing() |
| 16 |
|
| 17 |
DATA_DIR = Path(__file__).resolve().parent / 'i18n_data' |
| 18 |
|
| 19 |
|
| 20 |
with imports_under_tool("i18n"): |
| 21 |
from pygettext import (parse_spec, process_keywords, DEFAULTKEYWORDS, |
| 22 |
unparse_spec) |
| 23 |
|
| 24 |
|
| 25 |
def normalize_POT_file(pot): |
| 26 |
"""Normalize the POT creation timestamp, charset and |
| 27 |
file locations to make the POT file easier to compare. |
| 28 |
|
| 29 |
""" |
| 30 |
# Normalize the creation date. |
| 31 |
date_pattern = re.compile(r'"POT-Creation-Date: .+?\\n"') |
| 32 |
header = r'"POT-Creation-Date: 2000-01-01 00:00+0000\\n"' |
| 33 |
pot = re.sub(date_pattern, header, pot) |
| 34 |
|
| 35 |
# Normalize charset to UTF-8 (currently there's no way to specify the output charset). |
| 36 |
charset_pattern = re.compile(r'"Content-Type: text/plain; charset=.+?\\n"') |
| 37 |
charset = r'"Content-Type: text/plain; charset=UTF-8\\n"' |
| 38 |
pot = re.sub(charset_pattern, charset, pot) |
| 39 |
|
| 40 |
# Normalize file location path separators in case this test is |
| 41 |
# running on Windows (which uses '\'). |
| 42 |
fileloc_pattern = re.compile(r'#:.+') |
| 43 |
|
| 44 |
def replace(match): |
| 45 |
return match[0].replace(os.sep, "/") |
| 46 |
pot = re.sub(fileloc_pattern, replace, pot) |
| 47 |
return pot |
| 48 |
|
| 49 |
|
| 50 |
class Test_pygettext(unittest.TestCase): |
| 51 |
"""Tests for the pygettext.py tool""" |
| 52 |
|
| 53 |
script = Path(toolsdir, 'i18n', 'pygettext.py') |
| 54 |
|
| 55 |
def get_header(self, data): |
| 56 |
""" utility: return the header of a .po file as a dictionary """ |
| 57 |
headers = {} |
| 58 |
for line in data.split('\n'): |
| 59 |
if not line or line.startswith(('#', 'msgid', 'msgstr')): |
| 60 |
continue |
| 61 |
line = line.strip('"') |
| 62 |
key, val = line.split(':', 1) |
| 63 |
headers[key] = val.strip() |
| 64 |
return headers |
| 65 |
|
| 66 |
def get_msgids(self, data): |
| 67 |
""" utility: return all msgids in .po file as a list of strings """ |
| 68 |
msgids = [] |
| 69 |
reading_msgid = False |
| 70 |
cur_msgid = [] |
| 71 |
for line in data.split('\n'): |
| 72 |
if reading_msgid: |
| 73 |
if line.startswith('"'): |
| 74 |
cur_msgid.append(line.strip('"')) |
| 75 |
else: |
| 76 |
msgids.append('\n'.join(cur_msgid)) |
| 77 |
cur_msgid = [] |
| 78 |
reading_msgid = False |
| 79 |
continue |
| 80 |
if line.startswith('msgid '): |
| 81 |
line = line[len('msgid '):] |
| 82 |
cur_msgid.append(line.strip('"')) |
| 83 |
reading_msgid = True |
| 84 |
else: |
| 85 |
if reading_msgid: |
| 86 |
msgids.append('\n'.join(cur_msgid)) |
| 87 |
|
| 88 |
return msgids |
| 89 |
|
| 90 |
def assert_POT_equal(self, expected, actual): |
| 91 |
"""Check if two POT files are equal""" |
| 92 |
self.maxDiff = None |
| 93 |
self.assertEqual(normalize_POT_file(expected), normalize_POT_file(actual)) |
| 94 |
|
| 95 |
def extract_from_str(self, module_content, *, args=(), strict=True, |
| 96 |
with_stderr=False, raw=False): |
| 97 |
"""Return all msgids extracted from module_content.""" |
| 98 |
filename = 'test.py' |
| 99 |
with temp_cwd(None): |
| 100 |
with open(filename, 'w', encoding='utf-8') as fp: |
| 101 |
fp.write(module_content) |
| 102 |
res = assert_python_ok('-Xutf8', self.script, *args, filename) |
| 103 |
if strict: |
| 104 |
self.assertEqual(res.err, b'') |
| 105 |
with open('messages.pot', encoding='utf-8') as fp: |
| 106 |
data = fp.read() |
| 107 |
if not raw: |
| 108 |
data = self.get_msgids(data) |
| 109 |
if not with_stderr: |
| 110 |
return data |
| 111 |
return data, res.err |
| 112 |
|
| 113 |
def extract_docstrings_from_str(self, module_content): |
| 114 |
"""Return all docstrings extracted from module_content.""" |
| 115 |
return self.extract_from_str(module_content, args=('--docstrings',), strict=False) |
| 116 |
|
| 117 |
def get_stderr(self, module_content): |
| 118 |
return self.extract_from_str(module_content, strict=False, with_stderr=True)[1] |
| 119 |
|
| 120 |
def test_header(self): |
| 121 |
"""Make sure the required fields are in the header, according to: |
| 122 |
http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry |
| 123 |
""" |
| 124 |
with temp_cwd(None) as cwd: |
| 125 |
assert_python_ok('-Xutf8', self.script) |
| 126 |
with open('messages.pot', encoding='utf-8') as fp: |
| 127 |
data = fp.read() |
| 128 |
header = self.get_header(data) |
| 129 |
|
| 130 |
self.assertIn("Project-Id-Version", header) |
| 131 |
self.assertIn("POT-Creation-Date", header) |
| 132 |
self.assertIn("PO-Revision-Date", header) |
| 133 |
self.assertIn("Last-Translator", header) |
| 134 |
self.assertIn("Language-Team", header) |
| 135 |
self.assertIn("MIME-Version", header) |
| 136 |
self.assertIn("Content-Type", header) |
| 137 |
self.assertIn("Content-Transfer-Encoding", header) |
| 138 |
self.assertIn("Generated-By", header) |
| 139 |
|
| 140 |
# not clear if these should be required in POT (template) files |
| 141 |
#self.assertIn("Report-Msgid-Bugs-To", header) |
| 142 |
#self.assertIn("Language", header) |
| 143 |
|
| 144 |
#"Plural-Forms" is optional |
| 145 |
|
| 146 |
@unittest.skipIf(sys.platform.startswith('aix'), |
| 147 |
'bpo-29972: broken test on AIX') |
| 148 |
def test_POT_Creation_Date(self): |
| 149 |
""" Match the date format from xgettext for POT-Creation-Date """ |
| 150 |
from datetime import datetime |
| 151 |
with temp_cwd(None) as cwd: |
| 152 |
assert_python_ok('-Xutf8', self.script) |
| 153 |
with open('messages.pot', encoding='utf-8') as fp: |
| 154 |
data = fp.read() |
| 155 |
header = self.get_header(data) |
| 156 |
creationDate = header['POT-Creation-Date'] |
| 157 |
|
| 158 |
# peel off the escaped newline at the end of string |
| 159 |
if creationDate.endswith('\\n'): |
| 160 |
creationDate = creationDate[:-len('\\n')] |
| 161 |
|
| 162 |
# This will raise if the date format does not exactly match. |
| 163 |
datetime.strptime(creationDate, '%Y-%m-%d %H:%M%z') |
| 164 |
|
| 165 |
def test_output_option(self): |
| 166 |
for opt in ('-o', '--output='): |
| 167 |
with temp_cwd(): |
| 168 |
assert_python_ok(self.script, f'{opt}test') |
| 169 |
self.assertTrue(os.path.exists('test')) |
| 170 |
res = assert_python_ok(self.script, f'{opt}-') |
| 171 |
self.assertIn(b'Project-Id-Version: PACKAGE VERSION', res.out) |
| 172 |
|
| 173 |
def test_funcdocstring(self): |
| 174 |
for doc in ('"""doc"""', "r'''doc'''", "R'doc'", 'u"doc"'): |
| 175 |
with self.subTest(doc): |
| 176 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 177 |
def foo(bar): |
| 178 |
%s |
| 179 |
''' % doc)) |
| 180 |
self.assertIn('doc', msgids) |
| 181 |
|
| 182 |
def test_funcdocstring_bytes(self): |
| 183 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 184 |
def foo(bar): |
| 185 |
b"""doc""" |
| 186 |
''')) |
| 187 |
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid]) |
| 188 |
|
| 189 |
def test_funcdocstring_fstring(self): |
| 190 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 191 |
def foo(bar): |
| 192 |
f"""doc""" |
| 193 |
''')) |
| 194 |
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid]) |
| 195 |
|
| 196 |
def test_classdocstring(self): |
| 197 |
for doc in ('"""doc"""', "r'''doc'''", "R'doc'", 'u"doc"'): |
| 198 |
with self.subTest(doc): |
| 199 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 200 |
class C: |
| 201 |
%s |
| 202 |
''' % doc)) |
| 203 |
self.assertIn('doc', msgids) |
| 204 |
|
| 205 |
def test_classdocstring_bytes(self): |
| 206 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 207 |
class C: |
| 208 |
b"""doc""" |
| 209 |
''')) |
| 210 |
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid]) |
| 211 |
|
| 212 |
def test_classdocstring_fstring(self): |
| 213 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 214 |
class C: |
| 215 |
f"""doc""" |
| 216 |
''')) |
| 217 |
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid]) |
| 218 |
|
| 219 |
def test_moduledocstring(self): |
| 220 |
for doc in ('"""doc"""', "r'''doc'''", "R'doc'", 'u"doc"'): |
| 221 |
with self.subTest(doc): |
| 222 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 223 |
%s |
| 224 |
''' % doc)) |
| 225 |
self.assertIn('doc', msgids) |
| 226 |
|
| 227 |
def test_moduledocstring_bytes(self): |
| 228 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 229 |
b"""doc""" |
| 230 |
''')) |
| 231 |
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid]) |
| 232 |
|
| 233 |
def test_moduledocstring_fstring(self): |
| 234 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 235 |
f"""doc""" |
| 236 |
''')) |
| 237 |
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid]) |
| 238 |
|
| 239 |
def test_msgid(self): |
| 240 |
msgids = self.extract_docstrings_from_str( |
| 241 |
'''_("""doc""" r'str' u"ing")''') |
| 242 |
self.assertIn('docstring', msgids) |
| 243 |
|
| 244 |
def test_msgid_bytes(self): |
| 245 |
msgids = self.extract_docstrings_from_str('_(b"""doc""")') |
| 246 |
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid]) |
| 247 |
|
| 248 |
def test_msgid_fstring(self): |
| 249 |
msgids = self.extract_docstrings_from_str('_(f"""doc""")') |
| 250 |
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid]) |
| 251 |
|
| 252 |
def test_funcdocstring_annotated_args(self): |
| 253 |
""" Test docstrings for functions with annotated args """ |
| 254 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 255 |
def foo(bar: str): |
| 256 |
"""doc""" |
| 257 |
''')) |
| 258 |
self.assertIn('doc', msgids) |
| 259 |
|
| 260 |
def test_funcdocstring_annotated_return(self): |
| 261 |
""" Test docstrings for functions with annotated return type """ |
| 262 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 263 |
def foo(bar) -> str: |
| 264 |
"""doc""" |
| 265 |
''')) |
| 266 |
self.assertIn('doc', msgids) |
| 267 |
|
| 268 |
def test_funcdocstring_defvalue_args(self): |
| 269 |
""" Test docstring for functions with default arg values """ |
| 270 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 271 |
def foo(bar=()): |
| 272 |
"""doc""" |
| 273 |
''')) |
| 274 |
self.assertIn('doc', msgids) |
| 275 |
|
| 276 |
def test_funcdocstring_multiple_funcs(self): |
| 277 |
""" Test docstring extraction for multiple functions combining |
| 278 |
annotated args, annotated return types and default arg values |
| 279 |
""" |
| 280 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 281 |
def foo1(bar: tuple=()) -> str: |
| 282 |
"""doc1""" |
| 283 |
|
| 284 |
def foo2(bar: List[1:2]) -> (lambda x: x): |
| 285 |
"""doc2""" |
| 286 |
|
| 287 |
def foo3(bar: 'func'=lambda x: x) -> {1: 2}: |
| 288 |
"""doc3""" |
| 289 |
''')) |
| 290 |
self.assertIn('doc1', msgids) |
| 291 |
self.assertIn('doc2', msgids) |
| 292 |
self.assertIn('doc3', msgids) |
| 293 |
|
| 294 |
def test_classdocstring_early_colon(self): |
| 295 |
""" Test docstring extraction for a class with colons occurring within |
| 296 |
the parentheses. |
| 297 |
""" |
| 298 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 299 |
class D(L[1:2], F({1: 2}), metaclass=M(lambda x: x)): |
| 300 |
"""doc""" |
| 301 |
''')) |
| 302 |
self.assertIn('doc', msgids) |
| 303 |
|
| 304 |
def test_calls_in_fstrings(self): |
| 305 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 306 |
f"{_('foo bar')}" |
| 307 |
''')) |
| 308 |
self.assertIn('foo bar', msgids) |
| 309 |
|
| 310 |
def test_calls_in_fstrings_raw(self): |
| 311 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 312 |
rf"{_('foo bar')}" |
| 313 |
''')) |
| 314 |
self.assertIn('foo bar', msgids) |
| 315 |
|
| 316 |
def test_calls_in_fstrings_nested(self): |
| 317 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 318 |
f"""{f'{_("foo bar")}'}""" |
| 319 |
''')) |
| 320 |
self.assertIn('foo bar', msgids) |
| 321 |
|
| 322 |
def test_calls_in_fstrings_attribute(self): |
| 323 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 324 |
f"{obj._('foo bar')}" |
| 325 |
''')) |
| 326 |
self.assertIn('foo bar', msgids) |
| 327 |
|
| 328 |
def test_calls_in_fstrings_with_call_on_call(self): |
| 329 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 330 |
f"{type(str)('foo bar')}" |
| 331 |
''')) |
| 332 |
self.assertNotIn('foo bar', msgids) |
| 333 |
|
| 334 |
def test_calls_in_fstrings_with_format(self): |
| 335 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 336 |
f"{_('foo {bar}').format(bar='baz')}" |
| 337 |
''')) |
| 338 |
self.assertIn('foo {bar}', msgids) |
| 339 |
|
| 340 |
def test_calls_in_fstrings_with_wrong_input_1(self): |
| 341 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 342 |
f"{_(f'foo {bar}')}" |
| 343 |
''')) |
| 344 |
self.assertFalse([msgid for msgid in msgids if 'foo {bar}' in msgid]) |
| 345 |
|
| 346 |
def test_calls_in_fstrings_with_wrong_input_2(self): |
| 347 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 348 |
f"{_(1)}" |
| 349 |
''')) |
| 350 |
self.assertNotIn(1, msgids) |
| 351 |
|
| 352 |
def test_calls_in_fstring_with_multiple_args(self): |
| 353 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 354 |
f"{_('foo', 'bar')}" |
| 355 |
''')) |
| 356 |
self.assertIn('foo', msgids) |
| 357 |
self.assertNotIn('bar', msgids) |
| 358 |
|
| 359 |
def test_calls_in_fstring_with_keyword_args(self): |
| 360 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 361 |
f"{_('foo', bar='baz')}" |
| 362 |
''')) |
| 363 |
self.assertIn('foo', msgids) |
| 364 |
self.assertNotIn('bar', msgids) |
| 365 |
self.assertNotIn('baz', msgids) |
| 366 |
|
| 367 |
def test_calls_in_fstring_with_partially_wrong_expression(self): |
| 368 |
msgids = self.extract_docstrings_from_str(dedent('''\ |
| 369 |
f"{_(f'foo') + _('bar')}" |
| 370 |
''')) |
| 371 |
self.assertNotIn('foo', msgids) |
| 372 |
self.assertIn('bar', msgids) |
| 373 |
|
| 374 |
def test_function_and_class_names(self): |
| 375 |
"""Test that function and class names are not mistakenly extracted.""" |
| 376 |
msgids = self.extract_from_str(dedent('''\ |
| 377 |
def _(x): |
| 378 |
pass |
| 379 |
|
| 380 |
def _(x="foo"): |
| 381 |
pass |
| 382 |
|
| 383 |
async def _(x): |
| 384 |
pass |
| 385 |
|
| 386 |
class _(object): |
| 387 |
pass |
| 388 |
''')) |
| 389 |
self.assertEqual(msgids, ['']) |
| 390 |
|
| 391 |
def test_pygettext_output(self): |
| 392 |
"""Test that the pygettext output exactly matches snapshots.""" |
| 393 |
for input_file, output_file, output in extract_from_snapshots(): |
| 394 |
with self.subTest(input_file=input_file): |
| 395 |
expected = output_file.read_text(encoding='utf-8') |
| 396 |
self.assert_POT_equal(expected, output) |
| 397 |
|
| 398 |
def test_files_list(self): |
| 399 |
"""Make sure the directories are inspected for source files |
| 400 |
bpo-31920 |
| 401 |
""" |
| 402 |
text1 = 'Text to translate1' |
| 403 |
text2 = 'Text to translate2' |
| 404 |
text3 = 'Text to ignore' |
| 405 |
with temp_cwd(None), temp_dir(None) as sdir: |
| 406 |
pymod = Path(sdir, 'pypkg', 'pymod.py') |
| 407 |
pymod.parent.mkdir() |
| 408 |
pymod.write_text(f'_({text1!r})', encoding='utf-8') |
| 409 |
|
| 410 |
pymod2 = Path(sdir, 'pkg.py', 'pymod2.py') |
| 411 |
pymod2.parent.mkdir() |
| 412 |
pymod2.write_text(f'_({text2!r})', encoding='utf-8') |
| 413 |
|
| 414 |
pymod3 = Path(sdir, 'CVS', 'pymod3.py') |
| 415 |
pymod3.parent.mkdir() |
| 416 |
pymod3.write_text(f'_({text3!r})', encoding='utf-8') |
| 417 |
|
| 418 |
assert_python_ok('-Xutf8', self.script, sdir) |
| 419 |
data = Path('messages.pot').read_text(encoding='utf-8') |
| 420 |
self.assertIn(f'msgid "{text1}"', data) |
| 421 |
self.assertIn(f'msgid "{text2}"', data) |
| 422 |
self.assertNotIn(text3, data) |
| 423 |
|
| 424 |
def test_help_text(self): |
| 425 |
"""Test that the help text is displayed.""" |
| 426 |
res = assert_python_ok(self.script, '--help') |
| 427 |
self.assertEqual(res.out, b'') |
| 428 |
self.assertIn(b'pygettext -- Python equivalent of xgettext(1)', res.err) |
| 429 |
|
| 430 |
def test_error_messages(self): |
| 431 |
"""Test that pygettext outputs error messages to stderr.""" |
| 432 |
stderr = self.get_stderr(dedent('''\ |
| 433 |
_(1+2) |
| 434 |
ngettext('foo') |
| 435 |
dgettext(*args, 'foo') |
| 436 |
''')) |
| 437 |
|
| 438 |
# Normalize line endings on Windows |
| 439 |
stderr = stderr.decode('utf-8').replace('\r', '') |
| 440 |
|
| 441 |
self.assertEqual( |
| 442 |
stderr, |
| 443 |
"*** test.py:1: Expected a string constant for argument 1, got 1 + 2\n" |
| 444 |
"*** test.py:2: Expected at least 2 positional argument(s) in gettext call, got 1\n" |
| 445 |
"*** test.py:3: Variable positional arguments are not allowed in gettext calls\n" |
| 446 |
) |
| 447 |
|
| 448 |
def test_extract_all_comments(self): |
| 449 |
""" |
| 450 |
Test that the --add-comments option without an |
| 451 |
explicit tag extracts all translator comments. |
| 452 |
""" |
| 453 |
for arg in ('--add-comments', '-c'): |
| 454 |
with self.subTest(arg=arg): |
| 455 |
data = self.extract_from_str(dedent('''\ |
| 456 |
# Translator comment |
| 457 |
_("foo") |
| 458 |
'''), args=(arg,), raw=True) |
| 459 |
self.assertIn('#. Translator comment', data) |
| 460 |
|
| 461 |
def test_comments_with_multiple_tags(self): |
| 462 |
""" |
| 463 |
Test that multiple --add-comments tags can be specified. |
| 464 |
""" |
| 465 |
for arg in ('--add-comments={}', '-c{}'): |
| 466 |
with self.subTest(arg=arg): |
| 467 |
args = (arg.format('foo:'), arg.format('bar:')) |
| 468 |
data = self.extract_from_str(dedent('''\ |
| 469 |
# foo: comment |
| 470 |
_("foo") |
| 471 |
|
| 472 |
# bar: comment |
| 473 |
_("bar") |
| 474 |
|
| 475 |
# baz: comment |
| 476 |
_("baz") |
| 477 |
'''), args=args, raw=True) |
| 478 |
self.assertIn('#. foo: comment', data) |
| 479 |
self.assertIn('#. bar: comment', data) |
| 480 |
self.assertNotIn('#. baz: comment', data) |
| 481 |
|
| 482 |
def test_comments_not_extracted_without_tags(self): |
| 483 |
""" |
| 484 |
Test that translator comments are not extracted without |
| 485 |
specifying --add-comments. |
| 486 |
""" |
| 487 |
data = self.extract_from_str(dedent('''\ |
| 488 |
# Translator comment |
| 489 |
_("foo") |
| 490 |
'''), raw=True) |
| 491 |
self.assertNotIn('#.', data) |
| 492 |
|
| 493 |
def test_parse_keyword_spec(self): |
| 494 |
valid = ( |
| 495 |
('foo', ('foo', {'msgid': 0})), |
| 496 |
('foo:1', ('foo', {'msgid': 0})), |
| 497 |
('foo:1,2', ('foo', {'msgid': 0, 'msgid_plural': 1})), |
| 498 |
('foo:1, 2', ('foo', {'msgid': 0, 'msgid_plural': 1})), |
| 499 |
('foo:1,2c', ('foo', {'msgid': 0, 'msgctxt': 1})), |
| 500 |
('foo:2c,1', ('foo', {'msgid': 0, 'msgctxt': 1})), |
| 501 |
('foo:2c ,1', ('foo', {'msgid': 0, 'msgctxt': 1})), |
| 502 |
('foo:1,2,3c', ('foo', {'msgid': 0, 'msgid_plural': 1, 'msgctxt': 2})), |
| 503 |
('foo:1, 2, 3c', ('foo', {'msgid': 0, 'msgid_plural': 1, 'msgctxt': 2})), |
| 504 |
('foo:3c,1,2', ('foo', {'msgid': 0, 'msgid_plural': 1, 'msgctxt': 2})), |
| 505 |
) |
| 506 |
for spec, expected in valid: |
| 507 |
with self.subTest(spec=spec): |
| 508 |
self.assertEqual(parse_spec(spec), expected) |
| 509 |
# test unparse-parse round-trip |
| 510 |
self.assertEqual(parse_spec(unparse_spec(*expected)), expected) |
| 511 |
|
| 512 |
invalid = ( |
| 513 |
('foo:', "Invalid keyword spec 'foo:': missing argument positions"), |
| 514 |
('foo:bar', "Invalid keyword spec 'foo:bar': position is not an integer"), |
| 515 |
('foo:0', "Invalid keyword spec 'foo:0': argument positions must be strictly positive"), |
| 516 |
('foo:-2', "Invalid keyword spec 'foo:-2': argument positions must be strictly positive"), |
| 517 |
('foo:1,1', "Invalid keyword spec 'foo:1,1': duplicate positions"), |
| 518 |
('foo:1,2,1', "Invalid keyword spec 'foo:1,2,1': duplicate positions"), |
| 519 |
('foo:1c,2,1c', "Invalid keyword spec 'foo:1c,2,1c': duplicate positions"), |
| 520 |
('foo:1c,2,3c', "Invalid keyword spec 'foo:1c,2,3c': msgctxt can only appear once"), |
| 521 |
('foo:1,2,3', "Invalid keyword spec 'foo:1,2,3': too many positions"), |
| 522 |
('foo:1c', "Invalid keyword spec 'foo:1c': msgctxt cannot appear without msgid"), |
| 523 |
) |
| 524 |
for spec, message in invalid: |
| 525 |
with self.subTest(spec=spec): |
| 526 |
with self.assertRaises(ValueError) as cm: |
| 527 |
parse_spec(spec) |
| 528 |
self.assertEqual(str(cm.exception), message) |
| 529 |
|
| 530 |
def test_process_keywords(self): |
| 531 |
default_keywords = {name: [spec] for name, spec |
| 532 |
in DEFAULTKEYWORDS.items()} |
| 533 |
inputs = ( |
| 534 |
(['foo'], True), |
| 535 |
(['_:1,2'], True), |
| 536 |
(['foo', 'foo:1,2'], True), |
| 537 |
(['foo'], False), |
| 538 |
(['_:1,2', '_:1c,2,3', 'pgettext'], False), |
| 539 |
# Duplicate entries |
| 540 |
(['foo', 'foo'], True), |
| 541 |
(['_'], False) |
| 542 |
) |
| 543 |
expected = ( |
| 544 |
{'foo': [{'msgid': 0}]}, |
| 545 |
{'_': [{'msgid': 0, 'msgid_plural': 1}]}, |
| 546 |
{'foo': [{'msgid': 0}, {'msgid': 0, 'msgid_plural': 1}]}, |
| 547 |
default_keywords | {'foo': [{'msgid': 0}]}, |
| 548 |
default_keywords | {'_': [{'msgid': 0, 'msgid_plural': 1}, |
| 549 |
{'msgctxt': 0, 'msgid': 1, 'msgid_plural': 2}, |
| 550 |
{'msgid': 0}], |
| 551 |
'pgettext': [{'msgid': 0}, |
| 552 |
{'msgctxt': 0, 'msgid': 1}]}, |
| 553 |
{'foo': [{'msgid': 0}]}, |
| 554 |
default_keywords, |
| 555 |
) |
| 556 |
for (keywords, no_default_keywords), expected in zip(inputs, expected): |
| 557 |
with self.subTest(keywords=keywords, |
| 558 |
no_default_keywords=no_default_keywords): |
| 559 |
processed = process_keywords( |
| 560 |
keywords, |
| 561 |
no_default_keywords=no_default_keywords) |
| 562 |
self.assertEqual(processed, expected) |
| 563 |
|
| 564 |
def test_multiple_keywords_same_funcname_errors(self): |
| 565 |
# If at least one keyword spec for a given funcname matches, |
| 566 |
# no error should be printed. |
| 567 |
msgids, stderr = self.extract_from_str(dedent('''\ |
| 568 |
_("foo", 42) |
| 569 |
_(42, "bar") |
| 570 |
'''), args=('--keyword=_:1', '--keyword=_:2'), with_stderr=True) |
| 571 |
self.assertIn('foo', msgids) |
| 572 |
self.assertIn('bar', msgids) |
| 573 |
self.assertEqual(stderr, b'') |
| 574 |
|
| 575 |
# If no keyword spec for a given funcname matches, |
| 576 |
# all errors are printed. |
| 577 |
msgids, stderr = self.extract_from_str(dedent('''\ |
| 578 |
_(x, 42) |
| 579 |
_(42, y) |
| 580 |
'''), args=('--keyword=_:1', '--keyword=_:2'), with_stderr=True, |
| 581 |
strict=False) |
| 582 |
self.assertEqual(msgids, ['']) |
| 583 |
# Normalize line endings on Windows |
| 584 |
stderr = stderr.decode('utf-8').replace('\r', '') |
| 585 |
self.assertEqual( |
| 586 |
stderr, |
| 587 |
'*** test.py:1: No keywords matched gettext call "_":\n' |
| 588 |
'\tkeyword="_": Expected a string constant for argument 1, got x\n' |
| 589 |
'\tkeyword="_:2": Expected a string constant for argument 2, got 42\n' |
| 590 |
'*** test.py:2: No keywords matched gettext call "_":\n' |
| 591 |
'\tkeyword="_": Expected a string constant for argument 1, got 42\n' |
| 592 |
'\tkeyword="_:2": Expected a string constant for argument 2, got y\n') |
| 593 |
|
| 594 |
|
| 595 |
def extract_from_snapshots(): |
| 596 |
snapshots = { |
| 597 |
'messages.py': (), |
| 598 |
'fileloc.py': ('--docstrings',), |
| 599 |
'docstrings.py': ('--docstrings',), |
| 600 |
'comments.py': ('--add-comments=i18n:',), |
| 601 |
'custom_keywords.py': ('--keyword=foo', '--keyword=nfoo:1,2', |
| 602 |
'--keyword=pfoo:1c,2', |
| 603 |
'--keyword=npfoo:1c,2,3', '--keyword=_:1,2'), |
| 604 |
'multiple_keywords.py': ('--keyword=foo:1c,2,3', '--keyword=foo:1c,2', |
| 605 |
'--keyword=foo:1,2', |
| 606 |
# repeat a keyword to make sure it is extracted only once |
| 607 |
'--keyword=foo', '--keyword=foo'), |
| 608 |
# == Test character escaping |
| 609 |
# Escape ascii and unicode: |
| 610 |
'escapes.py': ('--escape', '--add-comments='), |
| 611 |
# Escape only ascii and let unicode pass through: |
| 612 |
('escapes.py', 'ascii-escapes.pot'): ('--add-comments=',), |
| 613 |
} |
| 614 |
|
| 615 |
for filename, args in snapshots.items(): |
| 616 |
if isinstance(filename, tuple): |
| 617 |
filename, output_file = filename |
| 618 |
output_file = DATA_DIR / output_file |
| 619 |
input_file = DATA_DIR / filename |
| 620 |
else: |
| 621 |
input_file = DATA_DIR / filename |
| 622 |
output_file = input_file.with_suffix('.pot') |
| 623 |
contents = input_file.read_bytes() |
| 624 |
with temp_cwd(None): |
| 625 |
Path(input_file.name).write_bytes(contents) |
| 626 |
assert_python_ok('-Xutf8', Test_pygettext.script, *args, |
| 627 |
input_file.name) |
| 628 |
yield (input_file, output_file, |
| 629 |
Path('messages.pot').read_text(encoding='utf-8')) |
| 630 |
|
| 631 |
|
| 632 |
def update_POT_snapshots(): |
| 633 |
for _, output_file, output in extract_from_snapshots(): |
| 634 |
output = normalize_POT_file(output) |
| 635 |
output_file.write_text(output, encoding='utf-8') |
| 636 |
|
| 637 |
|
| 638 |
if __name__ == '__main__': |
| 639 |
# To regenerate POT files |
| 640 |
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update': |
| 641 |
update_POT_snapshots() |
| 642 |
sys.exit(0) |
| 643 |
unittest.main() |