https://github.com/python/cpython/commit/093c587388d0e4b87b9511232f47c9f4eaa22da9
commit: 093c587388d0e4b87b9511232f47c9f4eaa22da9
branch: 3.13
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: picnixz <10796600+picn...@users.noreply.github.com>
date: 2025-04-04T16:39:35+02:00
summary:

[3.13] gh-130655: Add tests for `gettext.find()` (GH-130691) (#132084)

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

(cherry picked from commit 3118693a1a3db0da96c565a2de015a806c892625)

Co-authored-by: Stan Ulbrych <89152624+stanfromirel...@users.noreply.github.com>
Co-authored-by: Tomas R. <tomas.ro...@gmail.com>

files:
M Lib/test/test_gettext.py

diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py
index a0d77dcceeff61..3d38ccf6cbba50 100644
--- a/Lib/test/test_gettext.py
+++ b/Lib/test/test_gettext.py
@@ -768,6 +768,76 @@ 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 create_mo_file(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 variables
+        # when languages are not supplied
+        mo_file = self.create_mo_file("ga_IE")
+        for var in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
+            self.env.set(var, 'ga_IE')
+            result = gettext.find("mofile",
+                                  localedir=os.path.join(self.tempdir, 
"locale"))
+            self.assertEqual(result, mo_file)
+            self.env.unset(var)
+
+    def test_find_with_languages(self):
+        # test that passed languages are used
+        self.env.set('LANGUAGE', 'pt_BR')
+        mo_file = self.create_mo_file("ga_IE")
+
+        result = gettext.find("mofile",
+                              localedir=os.path.join(self.tempdir, "locale"),
+                              languages=['ga_IE'])
+        self.assertEqual(result, mo_file)
+
+    @unittest.mock.patch('gettext._expand_lang')
+    def test_find_with_no_lang(self, patch_expand_lang):
+        # no language can be found
+        gettext.find('foo')
+        patch_expand_lang.assert_called_with('C')
+
+    @unittest.mock.patch('gettext._expand_lang')
+    def test_find_with_c(self, patch_expand_lang):
+        # 'C' is already in languages
+        self.env.set('LANGUAGE', 'C')
+        gettext.find('foo')
+        patch_expand_lang.assert_called_with('C')
+
+    def test_find_all(self):
+        # test that all are returned when all is set
+        paths = []
+        for lang in ["ga_IE", "es_ES"]:
+            paths.append(self.create_mo_file(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_deduplication(self):
+        # test that find removes duplicate languages
+        mo_file = [self.create_mo_file('ga_IE')]
+        result = gettext.find("mofile", localedir=os.path.join(self.tempdir, 
"locale"),
+                              languages=['ga_IE', 'ga_IE'], all=True)
+        self.assertEqual(result, mo_file)
+
+
 class MiscTestCase(unittest.TestCase):
     def test__all__(self):
         support.check__all__(self, gettext,

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to