Skip to content

gh-130655: Add tests for gettext.find() #130691

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

Merged
merged 12 commits into from
Mar 19, 2025
71 changes: 71 additions & 0 deletions Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,77 @@ def test_expand_lang(self):
self.assertEqual(gettext._expand_lang(locale), expanded)


class FindTestCase(unittest.TestCase):

def setUp(self):
self.env = self.enterContext(os_helper.EnvironmentVarGuard())
self.tempdir = self.enterContext(os_helper.temp_cwd())

for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
self.env.unset(key)

def createMo(self, lang):
locale_dir = os.path.join(self.tempdir, "locale")
mofile_dir = os.path.join(locale_dir, lang, "LC_MESSAGES")
os.makedirs(mofile_dir)
mo_file = os.path.join(mofile_dir, "mofile.mo")
with open(mo_file, "wb") as f:
f.write(GNU_MO_DATA)
return mo_file

def test_find_with_env_vars(self):
# test that find correctly finds the environment variable LANGUAGE
# when languages are not supplied
self.setUp()
self.env.set('LANGUAGE', 'ga_IE')
mo_file =self.createMo("ga_IE")

result = gettext.find("mofile",
localedir=os.path.join(self.tempdir, "locale"))
self.assertEqual(result, mo_file)

def test_find_with_lanuages(self):
# test that passed languages are used
self.setUp()
self.env.set('LANGUAGE', 'pt_BR')
mo_file = self.createMo("ga_IE")

result = gettext.find("mofile",
localedir=os.path.join(self.tempdir, "locale"),
languages=['ga_IE'])
self.assertEqual(result, mo_file)

def test_find_with_no_lang(self):
# no language can be found
self.setUp()
result = gettext.find('foo')
self.assertEqual(result, None)

def test_find_with_c(self):
# 'C' is already in languages
self.setUp()
self.env.set('LANGUAGE', 'C')
result = gettext.find('foo')
self.assertEqual(result, None)

def test_find_all(self):
# test that all are returned when all is set
self.setUp()
paths = []
for lang in ["ga_IE", "es_ES"]:
paths.append(self.createMo(lang))
result = gettext.find('mofile',
localedir=os.path.join(self.tempdir, "locale"),
languages=["ga_IE", "es_ES"], all=True)
self.assertEqual(sorted(result), sorted(paths))

def test_find_dedupelication(self):
# test that find removes duplicate languages
self.setUp()
result = gettext.find("foo", languages=['ga_IE', 'ga_IE'])
self.assertEqual(result, None)


class MiscTestCase(unittest.TestCase):
def test__all__(self):
support.check__all__(self, gettext,
Expand Down
Loading