Skip to content

Commit c6755ec

Browse files
StanFromIrelandtomasr8
authored andcommitted
pythongh-130655: Add tests for gettext.find() (pythonGH-130691)
(cherry picked from commit 3118693) Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
1 parent d30052a commit c6755ec

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
@@ -768,6 +768,76 @@ def test_expand_lang(self):
768768
self.assertEqual(gettext._expand_lang(locale), expanded)
769769

770770

771+
class FindTestCase(unittest.TestCase):
772+
773+
def setUp(self):
774+
self.env = self.enterContext(os_helper.EnvironmentVarGuard())
775+
self.tempdir = self.enterContext(os_helper.temp_cwd())
776+
777+
for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
778+
self.env.unset(key)
779+
780+
def create_mo_file(self, lang):
781+
locale_dir = os.path.join(self.tempdir, "locale")
782+
mofile_dir = os.path.join(locale_dir, lang, "LC_MESSAGES")
783+
os.makedirs(mofile_dir)
784+
mo_file = os.path.join(mofile_dir, "mofile.mo")
785+
with open(mo_file, "wb") as f:
786+
f.write(GNU_MO_DATA)
787+
return mo_file
788+
789+
def test_find_with_env_vars(self):
790+
# test that find correctly finds the environment variables
791+
# when languages are not supplied
792+
mo_file = self.create_mo_file("ga_IE")
793+
for var in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
794+
self.env.set(var, 'ga_IE')
795+
result = gettext.find("mofile",
796+
localedir=os.path.join(self.tempdir, "locale"))
797+
self.assertEqual(result, mo_file)
798+
self.env.unset(var)
799+
800+
def test_find_with_languages(self):
801+
# test that passed languages are used
802+
self.env.set('LANGUAGE', 'pt_BR')
803+
mo_file = self.create_mo_file("ga_IE")
804+
805+
result = gettext.find("mofile",
806+
localedir=os.path.join(self.tempdir, "locale"),
807+
languages=['ga_IE'])
808+
self.assertEqual(result, mo_file)
809+
810+
@unittest.mock.patch('gettext._expand_lang')
811+
def test_find_with_no_lang(self, patch_expand_lang):
812+
# no language can be found
813+
gettext.find('foo')
814+
patch_expand_lang.assert_called_with('C')
815+
816+
@unittest.mock.patch('gettext._expand_lang')
817+
def test_find_with_c(self, patch_expand_lang):
818+
# 'C' is already in languages
819+
self.env.set('LANGUAGE', 'C')
820+
gettext.find('foo')
821+
patch_expand_lang.assert_called_with('C')
822+
823+
def test_find_all(self):
824+
# test that all are returned when all is set
825+
paths = []
826+
for lang in ["ga_IE", "es_ES"]:
827+
paths.append(self.create_mo_file(lang))
828+
result = gettext.find('mofile',
829+
localedir=os.path.join(self.tempdir, "locale"),
830+
languages=["ga_IE", "es_ES"], all=True)
831+
self.assertEqual(sorted(result), sorted(paths))
832+
833+
def test_find_deduplication(self):
834+
# test that find removes duplicate languages
835+
mo_file = [self.create_mo_file('ga_IE')]
836+
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"),
837+
languages=['ga_IE', 'ga_IE'], all=True)
838+
self.assertEqual(result, mo_file)
839+
840+
771841
class MiscTestCase(unittest.TestCase):
772842
def test__all__(self):
773843
support.check__all__(self, gettext,

0 commit comments

Comments
 (0)