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 4 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
48 changes: 47 additions & 1 deletion Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import unittest
import unittest.mock
from functools import partial
import tempfile
import shutil

from test import support
from test.support import cpython_only, os_helper
from test.support.import_helper import ensure_lazy_imports


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

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.

# - Tests should have only one assert.

GNU_MO_DATA = b'''\
Expand Down Expand Up @@ -937,6 +938,51 @@ def test_lazy_import(self):
ensure_lazy_imports("gettext", {"re", "warnings", "locale"})


class DGettextTest(unittest.TestCase):
"""Test dgettext() function, which allows translations from specific domains."""

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)

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

self.domain = 'gettext_domain'
self.mofile = self.setup_dgettext_test_env()

def setup_dgettext_test_env(self):
"""Create a mo file for dgettext testing."""
os.makedirs(os.path.join(self.localedir, 'en', 'LC_MESSAGES'), exist_ok=True)
mofile = os.path.join(self.localedir, 'en', 'LC_MESSAGES', f'{self.domain}.mo')
with open(mofile, 'wb') as fp:
fp.write(b'\x00\x00\x00\x00')
return mofile

def test_dgettext_found_translation(self):
Copy link
Member

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).

"""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.

gettext.bindtextdomain(self.domain, self.localedir)
with unittest.mock.patch('gettext.dgettext') as mock_dgettext:
mock_dgettext.return_value = 'test message translation'
result = gettext.dgettext(self.domain, 'test message')
self.assertEqual(result, 'test message translation')

def test_dgettext_missing_translation(self):
"""Test dgettext returns msgid when translation is missing."""
gettext.bindtextdomain(self.domain, self.localedir)
result = gettext.dgettext(self.domain, 'missing message')
self.assertEqual(result, 'missing message')

def test_dgettext_non_existent_domain(self):
"""Test dgettext returns msgid when domain doesn't exist."""
result = gettext.dgettext('nonexistent_domain', 'test message')
self.assertEqual(result, 'test message')

def test_dgettext_empty_domain(self):
"""Test dgettext behavior with empty domain."""
current_domain = gettext.textdomain()
result = gettext.dgettext('', 'test message')
expected = gettext.gettext('test message')
self.assertEqual(result, expected)


if __name__ == '__main__':
unittest.main()

Expand Down
Loading