|
8 | 8 | from pathlib import Path |
9 | 9 |
|
10 | 10 | from test.support.script_helper import assert_python_ok |
11 | | -from test.test_tools import skip_if_missing, toolsdir |
| 11 | +from test.test_tools import imports_under_tool, skip_if_missing, toolsdir |
12 | 12 | from test.support.os_helper import temp_cwd, temp_dir |
13 | 13 |
|
14 | 14 |
|
|
17 | 17 | DATA_DIR = Path(__file__).resolve().parent / 'i18n_data' |
18 | 18 |
|
19 | 19 |
|
| 20 | +with imports_under_tool("i18n"): |
| 21 | + from pygettext import parse_spec |
| 22 | + |
| 23 | + |
20 | 24 | def normalize_POT_file(pot): |
21 | 25 | """Normalize the POT creation timestamp, charset and |
22 | 26 | file locations to make the POT file easier to compare. |
@@ -377,16 +381,8 @@ class _(object): |
377 | 381 |
|
378 | 382 | def test_pygettext_output(self): |
379 | 383 | """Test that the pygettext output exactly matches snapshots.""" |
380 | | - for input_file in DATA_DIR.glob('*.py'): |
381 | | - output_file = input_file.with_suffix('.pot') |
382 | | - with self.subTest(input_file=f'i18n_data/{input_file}'): |
383 | | - contents = input_file.read_text(encoding='utf-8') |
384 | | - with temp_cwd(None): |
385 | | - Path(input_file.name).write_text(contents) |
386 | | - assert_python_ok('-Xutf8', self.script, '--docstrings', |
387 | | - '--add-comments=i18n:', input_file.name) |
388 | | - output = Path('messages.pot').read_text(encoding='utf-8') |
389 | | - |
| 384 | + for input_file, output_file, output in extract_from_snapshots(): |
| 385 | + with self.subTest(input_file=input_file): |
390 | 386 | expected = output_file.read_text(encoding='utf-8') |
391 | 387 | self.assert_POT_equal(expected, output) |
392 | 388 |
|
@@ -485,17 +481,67 @@ def test_comments_not_extracted_without_tags(self): |
485 | 481 | '''), raw=True) |
486 | 482 | self.assertNotIn('#.', data) |
487 | 483 |
|
488 | | - |
489 | | -def update_POT_snapshots(): |
490 | | - for input_file in DATA_DIR.glob('*.py'): |
| 484 | + def test_parse_keyword_spec(self): |
| 485 | + valid = ( |
| 486 | + ('foo', ('foo', {0: 'msgid'})), |
| 487 | + ('foo:1', ('foo', {0: 'msgid'})), |
| 488 | + ('foo:1,2', ('foo', {0: 'msgid', 1: 'msgid_plural'})), |
| 489 | + ('foo:1, 2', ('foo', {0: 'msgid', 1: 'msgid_plural'})), |
| 490 | + ('foo:1,2c', ('foo', {0: 'msgid', 1: 'msgctxt'})), |
| 491 | + ('foo:2c,1', ('foo', {0: 'msgid', 1: 'msgctxt'})), |
| 492 | + ('foo:2c ,1', ('foo', {0: 'msgid', 1: 'msgctxt'})), |
| 493 | + ('foo:1,2,3c', ('foo', {0: 'msgid', 1: 'msgid_plural', 2: 'msgctxt'})), |
| 494 | + ('foo:1, 2, 3c', ('foo', {0: 'msgid', 1: 'msgid_plural', 2: 'msgctxt'})), |
| 495 | + ('foo:3c,1,2', ('foo', {0: 'msgid', 1: 'msgid_plural', 2: 'msgctxt'})), |
| 496 | + ) |
| 497 | + for spec, expected in valid: |
| 498 | + with self.subTest(spec=spec): |
| 499 | + self.assertEqual(parse_spec(spec), expected) |
| 500 | + |
| 501 | + invalid = ( |
| 502 | + ('foo:', "Invalid keyword spec 'foo:': missing argument positions"), |
| 503 | + ('foo:bar', "Invalid keyword spec 'foo:bar': position is not an integer"), |
| 504 | + ('foo:0', "Invalid keyword spec 'foo:0': argument positions must be strictly positive"), |
| 505 | + ('foo:-2', "Invalid keyword spec 'foo:-2': argument positions must be strictly positive"), |
| 506 | + ('foo:1,1', "Invalid keyword spec 'foo:1,1': duplicate positions"), |
| 507 | + ('foo:1,2,1', "Invalid keyword spec 'foo:1,2,1': duplicate positions"), |
| 508 | + ('foo:1c,2,1c', "Invalid keyword spec 'foo:1c,2,1c': duplicate positions"), |
| 509 | + ('foo:1c,2,3c', "Invalid keyword spec 'foo:1c,2,3c': msgctxt can only appear once"), |
| 510 | + ('foo:1,2,3', "Invalid keyword spec 'foo:1,2,3': too many positions"), |
| 511 | + ('foo:1c', "Invalid keyword spec 'foo:1c': msgctxt cannot appear without msgid"), |
| 512 | + ) |
| 513 | + for spec, message in invalid: |
| 514 | + with self.subTest(spec=spec): |
| 515 | + with self.assertRaises(ValueError) as cm: |
| 516 | + parse_spec(spec) |
| 517 | + self.assertEqual(str(cm.exception), message) |
| 518 | + |
| 519 | + |
| 520 | +def extract_from_snapshots(): |
| 521 | + snapshots = { |
| 522 | + 'messages.py': (), |
| 523 | + 'fileloc.py': ('--docstrings',), |
| 524 | + 'docstrings.py': ('--docstrings',), |
| 525 | + 'comments.py': ('--add-comments=i18n:',), |
| 526 | + 'custom_keywords.py': ('--keyword=foo', '--keyword=nfoo:1,2', |
| 527 | + '--keyword=pfoo:1c,2', |
| 528 | + '--keyword=npfoo:1c,2,3'), |
| 529 | + } |
| 530 | + |
| 531 | + for filename, args in snapshots.items(): |
| 532 | + input_file = DATA_DIR / filename |
491 | 533 | output_file = input_file.with_suffix('.pot') |
492 | 534 | contents = input_file.read_bytes() |
493 | 535 | with temp_cwd(None): |
494 | 536 | Path(input_file.name).write_bytes(contents) |
495 | | - assert_python_ok('-Xutf8', Test_pygettext.script, '--docstrings', |
496 | | - '--add-comments=i18n:', input_file.name) |
497 | | - output = Path('messages.pot').read_text(encoding='utf-8') |
| 537 | + assert_python_ok('-Xutf8', Test_pygettext.script, *args, |
| 538 | + input_file.name) |
| 539 | + yield (input_file, output_file, |
| 540 | + Path('messages.pot').read_text(encoding='utf-8')) |
498 | 541 |
|
| 542 | + |
| 543 | +def update_POT_snapshots(): |
| 544 | + for _, output_file, output in extract_from_snapshots(): |
499 | 545 | output = normalize_POT_file(output) |
500 | 546 | output_file.write_text(output, encoding='utf-8') |
501 | 547 |
|
|
0 commit comments