Skip to content

gh-130197: Improve test coverage of msgfmt.py part 2 #133309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Check error messages
  • Loading branch information
tomasr8 committed May 4, 2025
commit 2955e906503654be640b669ac8908b41104222fe
262 changes: 161 additions & 101 deletions Lib/test/test_tools/test_msgfmt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for the Tools/i18n/msgfmt.py tool."""

import json
import os
import struct
import sys
import unittest
Expand Down Expand Up @@ -244,80 +245,108 @@ def test_strings(self):
msgfmt.make('messages.po', 'messages.mo')

def test_general_syntax_errors(self):
# 2-tuples of input PO files and expected error messages
invalid_po_files = (
'',
'"',
'""',
'"foo"',
('', None),
('"', None),
('""', 'Syntax error on messages.po:1 before:'),
('"foo"', f'Syntax error on messages.po:1 before:{os.linesep}foo'),
# 'msgid', # invalid but currently accepted
'msgstr',
'msgid_plural',
('msgstr', None),
('msgid_plural', 'msgid_plural not preceded by msgid on messages.po:1'),
# 'msgctxt', # invalid but currently accepted
'msgstr',
'msgstr[0]',
'[0]',
('msgstr', None),
('msgstr[0]', None),
('[0]', f'Syntax error on messages.po:1 before:{os.linesep}[0]'),

# unclosed string
dedent('''\
msgid "
msgstr "bar"
'''),
(
dedent('''\
msgid "
msgstr "bar"
'''),
None
),

# unclosed string
dedent('''\
msgid "foo
msgstr "bar"
'''),
(
dedent('''\
msgid "foo
msgstr "bar"
'''),
None
),

# unclosed string
dedent('''\
msgid "foo" "
msgstr "bar"
'''),
(
dedent('''\
msgid "foo" "
msgstr "bar"
'''),
None
),

# unclosed string
dedent('''\
msgid "foo"
"
msgstr "bar"
'''),
(
dedent('''\
msgid "foo"
"
msgstr "bar"
'''),
None
),

# illegal backslash
dedent('''\
msgid "foo\\"
"
msgstr "bar"
'''),
(
dedent('''\
msgid "foo\\"
"
msgstr "bar"
'''),
None
),

# msgid with an index
dedent('''\
msgid[0] "foo"
msgstr "bar"
'''),
(
dedent('''\
msgid[0] "foo"
msgstr "bar"
'''),
None
),

# invalid plural index
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr[0 "baz"
'''),
(
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr[0 "baz"
'''),
None
),

# invalid plural index
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr1] "baz"
'''),
(
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr1] "baz"
'''),
'indexed msgstr required for plural on messages.po:3'
),

# invalid plural index
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr[[0]] "baz"
'''),
(
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr[[0]] "baz"
'''),
None
)
)
with temp_cwd():
for invalid_po in invalid_po_files:
for invalid_po, err_msg in invalid_po_files:
with self.subTest(invalid_po=invalid_po):
Path('messages.po').write_text(invalid_po)
# Reset the global MESSAGES dictionary
Expand All @@ -326,99 +355,130 @@ def test_general_syntax_errors(self):
with self.assertRaises((SystemExit, UnboundLocalError,
IndexError, SyntaxError)):
msgfmt.make('messages.po', 'messages.mo')
if err_msg:
self.assertEqual(output.getvalue().strip(), err_msg)

def test_semantic_errors(self):
# 2-tuples of input PO files and expected error messages
invalid_po_files = (
# msgid_plural must be preceded by msgid
dedent('''\
msgid_plural "foos"
(
dedent('''\
msgid_plural "foos"

msgid "bar"
msgstr "baz"
'''),
msgid "bar"
msgstr "baz"
'''),
'msgid_plural not preceded by msgid on messages.po:1'
),

# msgid_plural not allowed after comment
dedent('''\
# comment
msgid_plural "foos"
(
dedent('''\
# comment
msgid_plural "foos"

msgid "bar"
msgstr "baz"
'''),
msgid "bar"
msgstr "baz"
'''),
'msgid_plural not preceded by msgid on messages.po:2'
),

# msgid_plural not allowed after msgctxt
dedent('''\
msgctxt "foo"
msgid_plural "foos"
(
dedent('''\
msgctxt "foo"
msgid_plural "foos"

msgid "bar"
msgstr "baz"
'''),
msgid "bar"
msgstr "baz"
'''),
'msgid_plural not preceded by msgid on messages.po:2'
),

# msgid_plural not allowed after msgstr
dedent('''\
msgid "foo"
msgstr "bar"
msgid_plural "foos"

msgid "bar"
msgstr "baz"
'''),
(
dedent('''\
msgid "foo"
msgstr "bar"
msgid_plural "foos"

msgid "bar"
msgstr "baz"
'''),
'msgid_plural not preceded by msgid on messages.po:3'
),

# msgstr must be preceded by msgid
dedent('''\
msgstr "foo"
(
dedent('''\
msgstr "foo"

msgid "bar"
msgstr "baz"
'''),
msgid "bar"
msgstr "baz"
'''),
None
),

# msgstr not allowed after msgctxt
dedent('''\
msgctxt "foo"
msgstr "bar"
(
dedent('''\
msgctxt "foo"
msgstr "bar"

msgid "foo"
msgstr "bar"
'''),
msgid "foo"
msgstr "bar"
'''),
None
),

# missing msgid_plural section
dedent('''\
msgid "foo"
msgstr[0] "bar"

msgid "bar"
msgstr "baz"
'''),
(
dedent('''\
msgid "foo"
msgstr[0] "bar"

msgid "bar"
msgstr "baz"
'''),
'plural without msgid_plural on messages.po:2'
),
)
with temp_cwd():
for invalid_po in invalid_po_files:
for invalid_po, err_msg in invalid_po_files:
with self.subTest(invalid_po=invalid_po):
Path('messages.po').write_text(invalid_po)
# Reset the global MESSAGES dictionary
msgfmt.MESSAGES.clear()
with redirect_stderr(StringIO()) as output:
with self.assertRaises((SystemExit, UnboundLocalError)):
msgfmt.make('messages.po', 'messages.mo')
if err_msg:
self.assertEqual(output.getvalue().strip(), err_msg)

def test_msgstr_invalid_indices(self):
# 2-tuples of input PO files and expected error messages
invalid_po_files = (
# msgstr not pluralized
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr "bar"
'''),
(
dedent('''\
msgid "foo"
msgid_plural "foos"
msgstr "bar"
'''),
'indexed msgstr required for plural on messages.po:3'
),
)
with temp_cwd():
for invalid_po in invalid_po_files:
for invalid_po, err_msg in invalid_po_files:
with self.subTest(invalid_po=invalid_po):
Path('messages.po').write_text(invalid_po)
# Reset the global MESSAGES dictionary
msgfmt.MESSAGES.clear()
with redirect_stderr(StringIO()) as output:
with self.assertRaises(SystemExit):
msgfmt.make('messages.po', 'messages.mo')
self.assertEqual(output.getvalue().strip(), err_msg)


class CLITest(unittest.TestCase):
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy