Skip to content

Commit 09a9cfd

Browse files
StanFromIrelandtomasr8
authored andcommitted
pythongh-130655: Add tests for gettext.find() (pythonGH-130691)
Co-authored-by: Tomas R. <[email protected]>
1 parent dbd7e7e commit 09a9cfd

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

Lib/test/test_gettext.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,76 @@ def test_expand_lang(self):
718718
self.assertEqual(gettext._expand_lang(locale), expanded)
719719

720720

721+
class FindTestCase(unittest.TestCase):
722+
723+
def setUp(self):
724+
self.env = self.enterContext(os_helper.EnvironmentVarGuard())
725+
self.tempdir = self.enterContext(os_helper.temp_cwd())
726+
727+
for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
728+
self.env.unset(key)
729+
730+
def create_mo_file(self, lang):
731+
locale_dir = os.path.join(self.tempdir, "locale")
732+
mofile_dir = os.path.join(locale_dir, lang, "LC_MESSAGES")
733+
os.makedirs(mofile_dir)
734+
mo_file = os.path.join(mofile_dir, "mofile.mo")
735+
with open(mo_file, "wb") as f:
736+
f.write(GNU_MO_DATA)
737+
return mo_file
738+
739+
def test_find_with_env_vars(self):
740+
# test that find correctly finds the environment variables
741+
# when languages are not supplied
742+
mo_file = self.create_mo_file("ga_IE")
743+
for var in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
744+
self.env.set(var, 'ga_IE')
745+
result = gettext.find("mofile",
746+
localedir=os.path.join(self.tempdir, "locale"))
747+
self.assertEqual(result, mo_file)
748+
self.env.unset(var)
749+
750+
def test_find_with_languages(self):
751+
# test that passed languages are used
752+
self.env.set('LANGUAGE', 'pt_BR')
753+
mo_file = self.create_mo_file("ga_IE")
754+
755+
result = gettext.find("mofile",
756+
localedir=os.path.join(self.tempdir, "locale"),
757+
languages=['ga_IE'])
758+
self.assertEqual(result, mo_file)
759+
760+
@unittest.mock.patch('gettext._expand_lang')
761+
def test_find_with_no_lang(self, patch_expand_lang):
762+
# no language can be found
763+
gettext.find('foo')
764+
patch_expand_lang.assert_called_with('C')
765+
766+
@unittest.mock.patch('gettext._expand_lang')
767+
def test_find_with_c(self, patch_expand_lang):
768+
# 'C' is already in languages
769+
self.env.set('LANGUAGE', 'C')
770+
gettext.find('foo')
771+
patch_expand_lang.assert_called_with('C')
772+
773+
def test_find_all(self):
774+
# test that all are returned when all is set
775+
paths = []
776+
for lang in ["ga_IE", "es_ES"]:
777+
paths.append(self.create_mo_file(lang))
778+
result = gettext.find('mofile',
779+
localedir=os.path.join(self.tempdir, "locale"),
780+
languages=["ga_IE", "es_ES"], all=True)
781+
self.assertEqual(sorted(result), sorted(paths))
782+
783+
def test_find_deduplication(self):
784+
# test that find removes duplicate languages
785+
mo_file = [self.create_mo_file('ga_IE')]
786+
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"),
787+
languages=['ga_IE', 'ga_IE'], all=True)
788+
self.assertEqual(result, mo_file)
789+
790+
721791
class MiscTestCase(unittest.TestCase):
722792
def test__all__(self):
723793
support.check__all__(self, gettext,

0 commit comments

Comments
 (0)