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
Next Next commit
Improve msgfmt.py test coverage
  • Loading branch information
tomasr8 committed May 2, 2025
commit 9ec2cce20c8d5932c15250f29e8cbe22863c941e
356 changes: 356 additions & 0 deletions Lib/test/test_tools/test_msgfmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,362 @@ def test_strings(self):
with self.assertRaises(Exception):
msgfmt.make('messages.po', 'messages.mo')

def test_general_syntax_errors(self):
invalid_po_files = (
'',
'"',
'""',
'"foo"',
# 'msgid', # invalid but currently accepted
'msgstr',
'msgid_plural',
# 'msgctxt', # invalid but currently accepted
'msgstr',
'msgstr[0]',
'[0]',

# unclosed string
'''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use textwrap.dedent() and strip the initial newlines. It's up to you, but perhaps the indented code would look better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, I think it reads better with textwrap.dedent()

msgid "
msgstr "bar"
''',

# unclosed string
'''
msgid "foo
msgstr "bar"
''',

# unclosed string
'''
msgid "foo" "
msgstr "bar"
''',

# unclosed string
'''
msgid "foo"
"
msgstr "bar"
''',

# illegal backslash
'''
msgid "foo\\"
"
msgstr "bar"
''',

# msgid with an index
'''
msgid[0] "foo"
msgstr "bar"
''',

# invalid plural index
# invalid but currently accepted
# '''
# msgid "foo"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that it is worth to add such large blocks of the commented out code. It only increases the diff, in this PR, and in the future ones.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed, I'll add them back (uncommented) when I fix the parser

# msgid_plural "foos"
# msgstr[foo] "baz"
# ''',

# invalid plural index
'''
msgid "foo"
msgid_plural "foos"
msgstr[0 "baz"
''',

# invalid plural index
# invalid but currently accepted
# '''
# msgid "foo"
# msgid_plural "foos"
# msgstr[] "baz"
# ''',

# invalid plural index
'''
msgid "foo"
msgid_plural "foos"
msgstr1] "baz"
''',

# invalid plural index
'''
msgid "foo"
msgid_plural "foos"
msgstr[[0]] "baz"
''',
)
with temp_cwd():
for invalid_po 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 self.assertRaises((SystemExit, UnboundLocalError,
IndexError, SyntaxError)):
msgfmt.make('messages.po', 'messages.mo')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It prints errors on stderr. They should be silenced. Instead, it would be nice to check the output against the expected error message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to capture the output and check error messages :)


def test_semantic_errors(self):
invalid_po_files = (
# missing msgid after msgctxt
# invalid but currently accepted
# 'msgctxt "foo"',

# missing msgstr after msgid
# invalid but currently accepted
# 'msgid "foo"',

# comment line not allowed after msgctxt
# invalid but currently accepted
# '''
# msgctxt "foo"
# # comment
# msgid "bar"
# msgstr "bar"
# ''',

# comment line not allowed after msgid
# invalid but currently accepted
# '''
# msgid "foo"
# # comment
# msgstr "bar"
# ''',

# comment line not allowed after msgid_plural
# invalid but currently accepted
# '''
# msgid "foo"
# msgid_plural "foos"
# # comment
# msgstr[0] "bar"
# ''',

# msgctxt not allowed after msgctxt
# invalid but currently accepted
# '''
# msgctxt "foo"

# msgctxt "bar"
# msgid "foo"
# msgstr "bar"
# ''',

# msgctxt not allowed after msgid
# invalid but currently accepted
# '''
# msgid "foo"
# msgctxt "bar"

# msgid "bar"
# msgstr "baz"
# ''',

# msgctxt not allowed after msgid_plural
# invalid but currently accepted
# '''
# msgid "foo"
# msgid_plural "foos"
# msgctxt "bar"

# msgid "bar"
# msgstr "baz"
# ''',

# msgid not allowed after msgid
# invalid but currently accepted
# '''
# msgid "foo"

# msgid "bar"
# msgstr "baz"
# ''',

# msgid not allowed after msgid_plural
# invalid but currently accepted
# '''
# msgid "foo"
# msgid_plural "foos"

# msgid "bar"
# msgstr "baz"
# ''',

# msgid_plural must be preceded by msgid
'''
msgid_plural "foos"

msgid "bar"
msgstr "baz"
''',

# msgid_plural not allowed after comment
'''
# comment
msgid_plural "foos"

msgid "bar"
msgstr "baz"
''',

# msgid_plural not allowed after msgid_plural
# invalid but currently accepted
# '''
# msgid "foo"
# msgid_plural "foos"
# msgid_plural "bars"

# msgid "bar"
# msgstr "baz"
# ''',

# msgid_plural not allowed after msgctxt
'''
msgctxt "foo"
msgid_plural "foos"

msgid "bar"
msgstr "baz"
''',

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

msgid "bar"
msgstr "baz"
''',

# msgstr must be preceded by msgid
'''
msgstr "foo"

msgid "bar"
msgstr "baz"
''',

# msgstr not allowed after comment
# invalid but currently accepted
# '''
# # comment
# # msgstr "foo"

# msgid "bar"
# msgstr "baz"
# ''',

# msgstr not allowed after msgctxt
'''
msgctxt "foo"
msgstr "bar"

msgid "foo"
msgstr "bar"
''',

# msgstr not allowed after msgstr
# invalid but currently accepted
# '''
# msgid "foo"
# msgstr "bar"
# msgstr "baz"

# msgid "bar"
# msgstr "baz"
# ''',

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

msgid "bar"
msgstr "baz"
'''
)
with temp_cwd():
for invalid_po 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 self.assertRaises((SystemExit, UnboundLocalError)):
msgfmt.make('messages.po', 'messages.mo')

def test_msgstr_invalid_indices(self):
invalid_po_files = (
# wrong plural form index
# invalid but currently accepted
# '''
# msgid "foo"
# msgid_plural "foos"
# msgstr[42] "bar"
# ''',

# wrong plural form index
# invalid but currently accepted
# '''
# msgid "foo"
# msgid_plural "foos"
# msgstr[0] "bar"
# msgstr[42] "bars"
# ''',

# msgstr not pluralized
'''
msgid "foo"
msgid_plural "foos"
msgstr "bar"
''',
)
with temp_cwd():
for invalid_po 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 self.assertRaises(SystemExit):
msgfmt.make('messages.po', 'messages.mo')

def test_duplicate_entries(self):
invalid_po_files = (
# duplicate msgid
# invalid but currently accepted
# '''
# msgid "foo"
# msgstr "bar"

# msgid "foo"
# msgstr "baz"
# ''',

# duplicate msgctxt+msgid
# invalid but currently accepted
# '''
# msgctxt "context"
# msgid "foo"
# msgstr "bar"

# msgctxt "context"
# msgid "foo"
# msgstr "baz"
# '''
)
with temp_cwd():
for invalid_po 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 self.assertRaises(SystemExit):
msgfmt.make('messages.po', 'messages.mo')


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