cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-104400: Add more tests to pygettext

Open tomasr8 opened this issue 2 years ago • 6 comments

As suggested here, I'm splitting the PR which updates pygettext into multiple smaller PRs. This first one just adds more comprehensive tests to pygettext without changing any functionality.

The test cases I added test message & docstring extraction which were not covered before. I also added a specific test for file locations.

Instead of having a test case for each possibility, I group the test cases into files on which I run the script and compare the entire script output. This has several advantages:

  • Because we check the whole script output, we can be sure that things like file location, flags, line wrapping, etc.. are also correct. The current tests only check for a presence of a given msgid which is insufficient.
  • The tests are (imo) more readable when in a single python file
  • Less code needed to add/remove tests

While writing the tests, I actually found more bugs than in the original PR. I documented these in the tests - I don't think it's worth fixing these now as most of these will be fixed automatically once pygettext is converted to use the AST.

  • Issue: gh-104400

tomasr8 avatar Aug 20 '23 15:08 tomasr8

@tomasr8 Thanks for opening the new PR! Could you look at the failing tests please?

======================================================================
FAIL: test_pygettext_output (test.test_tools.test_i18n.test_i18n.Test_pygettext.test_pygettext_output) [Input file: data/messages.py]
Test that the pygettext output exactly matches a file.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\a\cpython\cpython\Lib\test\test_tools\test_i18n\test_i18n.py", line 350, in test_pygettext_output
    self.assert_POT_equal(expected, output)
  File "D:\a\cpython\cpython\Lib\test\test_tools\test_i18n\test_i18n.py", line 75, in assert_POT_equal
    self.assertEqual(expected, actual)
AssertionError: '# SO[397 chars]rset=UTF-8\\n"\n"Content-Transfer-Encoding: 8b[682 chars]\n\n' != '# SO[397 chars]rset=cp1252\\n"\n"Content-Transfer-Encoding: 8[683 chars]\n\n'
Diff is 1325 characters long. Set self.maxDiff to None to see it.

======================================================================
FAIL: test_pygettext_output (test.test_tools.test_i18n.test_i18n.Test_pygettext.test_pygettext_output) [Input file: data/docstrings.py]
Test that the pygettext output exactly matches a file.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\a\cpython\cpython\Lib\test\test_tools\test_i18n\test_i18n.py", line 350, in test_pygettext_output
    self.assert_POT_equal(expected, output)
  File "D:\a\cpython\cpython\Lib\test\test_tools\test_i18n\test_i18n.py", line 75, in assert_POT_equal
    self.assertEqual(expected, actual)
AssertionError: '# SO[397 chars]rset=UTF-8\\n"\n"Content-Transfer-Encoding: 8b[340 chars]\n\n' != '# SO[397 chars]rset=cp1252\\n"\n"Content-Transfer-Encoding: 8[341 chars]\n\n'
Diff is 956 characters long. Set self.maxDiff to None to see it.

======================================================================
FAIL: test_pygettext_output (test.test_tools.test_i18n.test_i18n.Test_pygettext.test_pygettext_output) [Input file: data/fileloc.py]
Test that the pygettext output exactly matches a file.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\a\cpython\cpython\Lib\test\test_tools\test_i18n\test_i18n.py", line 350, in test_pygettext_output
    self.assert_POT_equal(expected, output)
  File "D:\a\cpython\cpython\Lib\test\test_tools\test_i18n\test_i18n.py", line 75, in assert_POT_equal
    self.assertEqual(expected, actual)
AssertionError: '# SO[397 chars]rset=UTF-8\\n"\n"Content-Transfer-Encoding: 8b[294 chars]\n\n' != '# SO[397 chars]rset=cp1252\\n"\n"Content-Transfer-Encoding: 8[295 chars]\n\n'
Diff is 907 characters long. Set self.maxDiff to None to see it.

A

AA-Turner avatar Aug 20 '23 17:08 AA-Turner

hmm looks like an encoding issue. Hopefully, this'll fix it.

tomasr8 avatar Aug 20 '23 17:08 tomasr8

Looks like the same three tests failed. Do you have access to a Windows computer? If not I should be able to have a look later on.

A

AA-Turner avatar Aug 20 '23 18:08 AA-Turner

No worries! luckily I have a windows machine lying around 😅 The problem is that there's no way to specify the output encoding so it uses the platform default. This makes it difficult to compare the files because the charset is also part of the header.. I'll just normalize it the same way as I do with the creation date.

tomasr8 avatar Aug 20 '23 18:08 tomasr8

Thanks!

Thanks for the review!

tomasr8 avatar Aug 29 '23 21:08 tomasr8

cc. @warsaw who asked for a ping on Discourse :)

erlend-aasland avatar Aug 30 '23 07:08 erlend-aasland

I also added a --snapshot-update CLI argument to make it easy to regenerate the snapshots (as is already the case with some ast and recently argparse tests)

tomasr8 avatar Oct 28 '24 20:10 tomasr8

Thanks @tomasr8 for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. 🐍🍒⛏🤖

miss-islington-app[bot] avatar Nov 03 '24 14:11 miss-islington-app[bot]

GH-126361 is a backport of this pull request to the 3.13 branch.

bedevere-app[bot] avatar Nov 03 '24 14:11 bedevere-app[bot]

GH-126362 is a backport of this pull request to the 3.12 branch.

bedevere-app[bot] avatar Nov 03 '24 14:11 bedevere-app[bot]