-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
9ec2cce
6ba1488
05a205f
98917e5
2955e90
389827a
d6bdd7c
544f05e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]', | ||
|
||
tomasr8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# unclosed string | ||
''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added, I think it reads better with |
||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
tomasr8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with self.assertRaises(SystemExit): | ||
msgfmt.make('messages.po', 'messages.mo') | ||
|
||
|
||
class CLITest(unittest.TestCase): | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.