Skip to content

Commit 65a0103

Browse files
miss-islingtonStanFromIrelandtomasr8
authored
[3.12] gh-130655: Add tests for gettext.find() (GH-130691) (#132083)
gh-130655: Add tests for `gettext.find()` (GH-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 10f73f7 commit 65a0103

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

Lib/test/test_gettext.py

+70
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,76 @@ def test_expand_lang(self):
766766
self.assertEqual(gettext._expand_lang(locale), expanded)
767767

768768

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

0 commit comments

Comments
 (0)