Skip to content

gh-130655: add tests for dgettext #134594

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 3 commits into
base: main
Choose a base branch
from

Conversation

alex-semenyuk
Copy link
Contributor

@alex-semenyuk alex-semenyuk commented May 23, 2025

@bedevere-app bedevere-app bot added the tests Tests in the Lib/test dir label May 23, 2025
return mofile

def test_dgettext_found_translation(self):
"""Test dgettext finds translation in specified domain."""
Copy link
Member

Choose a reason for hiding this comment

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

Avoid using docstrings for test_* methods as it will be shown upon failure.


def setUp(self):
"""Set up a specific test domain and environment for dgettext tests."""
self.localedir = tempfile.mkdtemp()
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using mkdtemp which would create something inside /tmp, use test.support.temp_dir (I think, check the name) which creates something inside the build folder (it's easier to cleanup)

def setUp(self):
"""Set up a specific test domain and environment for dgettext tests."""
self.localedir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.localedir)
Copy link
Member

Choose a reason for hiding this comment

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

Prefer using os_helper.rmtree over shutil.rmtree

Copy link
Contributor

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

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

We can inherit from GettextBaseTest and avail of the mo files. You can then use the messages in the files to test it works properly.

@picnixz picnixz changed the title gh-134593: add tests for dgettext gh-130655: add tests for dgettext May 23, 2025
@picnixz picnixz added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels May 23, 2025
@alex-semenyuk alex-semenyuk requested a review from picnixz May 25, 2025 10:56
Copy link
Contributor

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

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

We can still simply them a lot, you have to remember dgettext is really just a wrapper.

@@ -11,7 +11,6 @@


# TODO:
# - Add new tests, for example for "dgettext"
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's keep the TODO, since it is still valid (the first part).

It can be updated with the issue number if you wish.

Comment on lines +964 to +972
def test_dgettext_luxury_yacht_translation(self):
result = gettext.dgettext('gettext', 'Raymond Luxury Yach-t')
self.assertEqual(result, 'Throatwobbler Mangrove')

def test_dgettext_nudge_nudge_translation(self):
result = gettext.dgettext('gettext', 'nudge nudge')
self.assertEqual(result, 'wink wink')

def test_dgettext_multiline_translation(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need these these anyway, it's long and pretty much identical to the previous two. dgettext is a pretty simple wrapper. Maybe merge the first two test_dgettext_translation for organization

Comment on lines +958 to +961
if domain == '':
expected = gettext.gettext(message)
else:
expected = message
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just store the expected, it is clearer. And this part is made simpler.

GettextBaseTest.setUp(self)
gettext.bindtextdomain('gettext', os.curdir)

def test_dgettext_found_translation(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Join this one with the other two, these all test the exact same thing (which is well tested above too).

@picnixz picnixz requested review from tomasr8 and picnixz and removed request for picnixz May 25, 2025 22:55
@tomasr8
Copy link
Member

tomasr8 commented May 26, 2025

Thanks for the PR @alex-semenyuk! Would you mind measuring the test coverage before and after this change and sharing the results here? (after you address the review comments)
Just to make sure we covered everything 🙂 Thanks!

class DGettextTest(GettextBaseTest):

def setUp(self):
GettextBaseTest.setUp(self)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
GettextBaseTest.setUp(self)
super().setUp()


def setUp(self):
GettextBaseTest.setUp(self)
gettext.bindtextdomain('gettext', os.curdir)
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need to tearDown this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants