From b93716f17c66ece75065bff9ea68fd95f1a9843e Mon Sep 17 00:00:00 2001 From: stan Date: Fri, 28 Feb 2025 16:24:58 +0000 Subject: [PATCH 01/11] Add find tests --- Lib/test/test_gettext.py | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index 970ab9fe7acb4a..f23ddab0f8b881 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -717,6 +717,61 @@ def test_expand_lang(self): return_value=locale): self.assertEqual(gettext._expand_lang(locale), expanded) +class FindTestCase(unittest.TestCase): + def test_find_with_env_vars(self): + # test that find correctly finds the environment variable LANGUAGE + # when languages are not supplied + with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: + for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): + env.unset(key) + env.set('LANGUAGE', 'ga_IE') + + locale_dir = os.path.join(tempdir, "locale") + mofile_dir = os.path.join(locale_dir, "ga_IE", "LC_MESSAGES") + os.makedirs(mofile_dir) + mo_file = os.path.join(mofile_dir, "mofile.mo") + with open(mo_file, "wb") as f: + f.write(b"\xDE\x12\x04\x95") + + result = gettext.find("mofile", localedir=locale_dir) + self.assertEqual(result, mo_file) + + def test_find_with_lanuages(self): + # test that passed languages are used + with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: + for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): + env.set(key, 'pt_BR') + + locale_dir = os.path.join(tempdir, "locale") + mofile_dir = os.path.join(locale_dir, "ga_IE", "LC_MESSAGES") + os.makedirs(mofile_dir) + mo_file = os.path.join(mofile_dir, "mofile.mo") + with open(mo_file, "wb") as f: + f.write(b"\xDE\x12\x04\x95") + + result = gettext.find("mofile", localedir=locale_dir, languages=['ga_IE']) + self.assertEqual(result, mo_file) + + def test_find_with_all(self): + # test that all are returned when all is set + with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: + for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): + env.unset(key) + env.set('LANGUAGE', 'ga_IE') + + locale_dir = os.path.join(tempdir, "locale") + paths = [] + for lang in ["ga_IE", "ga"]: + 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(b"\xDE\x12\x04\x95") + paths.append(mo_file) + + result = gettext.find('mofile', localedir=locale_dir, all=True) + self.assertEqual(sorted(result), sorted(paths)) + class MiscTestCase(unittest.TestCase): def test__all__(self): From ee77877ebb7338b3331f881c8cf550e579f41de7 Mon Sep 17 00:00:00 2001 From: stan Date: Fri, 28 Feb 2025 23:15:26 +0000 Subject: [PATCH 02/11] 100% coverage of find --- Lib/test/test_gettext.py | 43 ++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index f23ddab0f8b881..db8e7127add9a7 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -717,13 +717,16 @@ def test_expand_lang(self): return_value=locale): self.assertEqual(gettext._expand_lang(locale), expanded) +# helper for FindTestCase +def _clearvars(env): + for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): + env.unset(key) + class FindTestCase(unittest.TestCase): def test_find_with_env_vars(self): # test that find correctly finds the environment variable LANGUAGE # when languages are not supplied with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: - for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): - env.unset(key) env.set('LANGUAGE', 'ga_IE') locale_dir = os.path.join(tempdir, "locale") @@ -739,8 +742,7 @@ def test_find_with_env_vars(self): def test_find_with_lanuages(self): # test that passed languages are used with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: - for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): - env.set(key, 'pt_BR') + env.set('LANGUAGE', 'pt_BR') locale_dir = os.path.join(tempdir, "locale") mofile_dir = os.path.join(locale_dir, "ga_IE", "LC_MESSAGES") @@ -752,16 +754,28 @@ def test_find_with_lanuages(self): result = gettext.find("mofile", localedir=locale_dir, languages=['ga_IE']) self.assertEqual(result, mo_file) - def test_find_with_all(self): + def test_find_with_no_lang(self): + # no language can be found + with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: + _clearvars(env) + result = gettext.find('foo') + self.assertEqual(result, None) + + def test_find_with_c(self): + # 'C' is already in languages + with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: + 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 with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: - for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): - env.unset(key) - env.set('LANGUAGE', 'ga_IE') + _clearvars(env) locale_dir = os.path.join(tempdir, "locale") paths = [] - for lang in ["ga_IE", "ga"]: + for lang in ["ga_IE", "es_ES"]: mofile_dir = os.path.join(locale_dir, lang, "LC_MESSAGES") os.makedirs(mofile_dir) mo_file = os.path.join(mofile_dir, "mofile.mo") @@ -769,9 +783,18 @@ def test_find_with_all(self): f.write(b"\xDE\x12\x04\x95") paths.append(mo_file) - result = gettext.find('mofile', localedir=locale_dir, all=True) + result = gettext.find('mofile', localedir=locale_dir, + languages=["ga_IE", "es_ES"], all=True) self.assertEqual(sorted(result), sorted(paths)) + def test_find_dedupelication(self): + # test that find removes duplicate languages + with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: + _clearvars(env) + + result = gettext.find("foo", languages=['ga_IE', 'ga_IE']) + self.assertEqual(result, None) + class MiscTestCase(unittest.TestCase): def test__all__(self): From 6557ed18480fabf5d6988b2c8afa099553a20f23 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 3 Mar 2025 19:09:07 +0000 Subject: [PATCH 03/11] De-duplicate code as per Petr's suggestion --- Lib/test/test_gettext.py | 103 ++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 55 deletions(-) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index db8e7127add9a7..d498adfe842fbd 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -717,83 +717,76 @@ def test_expand_lang(self): return_value=locale): self.assertEqual(gettext._expand_lang(locale), expanded) -# helper for FindTestCase -def _clearvars(env): - for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): - env.unset(key) 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(b"\xDE\x12\x04\x95") + return mo_file + def test_find_with_env_vars(self): # test that find correctly finds the environment variable LANGUAGE # when languages are not supplied - with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: - env.set('LANGUAGE', 'ga_IE') - - locale_dir = os.path.join(tempdir, "locale") - mofile_dir = os.path.join(locale_dir, "ga_IE", "LC_MESSAGES") - os.makedirs(mofile_dir) - mo_file = os.path.join(mofile_dir, "mofile.mo") - with open(mo_file, "wb") as f: - f.write(b"\xDE\x12\x04\x95") + self.setUp() + self.env.set('LANGUAGE', 'ga_IE') + mo_file =self.createMo("ga_IE") - result = gettext.find("mofile", localedir=locale_dir) - self.assertEqual(result, mo_file) + 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 - with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: - env.set('LANGUAGE', 'pt_BR') + self.setUp() + self.env.set('LANGUAGE', 'pt_BR') + mo_file = self.createMo("ga_IE") - locale_dir = os.path.join(tempdir, "locale") - mofile_dir = os.path.join(locale_dir, "ga_IE", "LC_MESSAGES") - os.makedirs(mofile_dir) - mo_file = os.path.join(mofile_dir, "mofile.mo") - with open(mo_file, "wb") as f: - f.write(b"\xDE\x12\x04\x95") - - result = gettext.find("mofile", localedir=locale_dir, languages=['ga_IE']) - self.assertEqual(result, mo_file) + 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 - with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: - _clearvars(env) - result = gettext.find('foo') - self.assertEqual(result, None) + self.setUp() + result = gettext.find('foo') + self.assertEqual(result, None) def test_find_with_c(self): # 'C' is already in languages - with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: - env.set('LANGUAGE', 'C') - result = gettext.find('foo') - self.assertEqual(result, None) + 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 - with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: - _clearvars(env) - - locale_dir = os.path.join(tempdir, "locale") - paths = [] - for lang in ["ga_IE", "es_ES"]: - 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(b"\xDE\x12\x04\x95") - paths.append(mo_file) - - result = gettext.find('mofile', localedir=locale_dir, - languages=["ga_IE", "es_ES"], all=True) - self.assertEqual(sorted(result), sorted(paths)) + 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 - with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir: - _clearvars(env) - - result = gettext.find("foo", languages=['ga_IE', 'ga_IE']) - self.assertEqual(result, None) + self.setUp() + result = gettext.find("foo", languages=['ga_IE', 'ga_IE']) + self.assertEqual(result, None) class MiscTestCase(unittest.TestCase): From 3420631708a1267ddbbcff2beda95a90ea0ac415 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 3 Mar 2025 19:10:06 +0000 Subject: [PATCH 04/11] Use existing data --- Lib/test/test_gettext.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index d498adfe842fbd..9658d10bf00512 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -733,7 +733,7 @@ def createMo(self, lang): os.makedirs(mofile_dir) mo_file = os.path.join(mofile_dir, "mofile.mo") with open(mo_file, "wb") as f: - f.write(b"\xDE\x12\x04\x95") + f.write(GNU_MO_DATA) return mo_file def test_find_with_env_vars(self): From 3fc3589503d0005df68578e2046cf73f3292b726 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Date: Mon, 3 Mar 2025 20:59:32 +0000 Subject: [PATCH 05/11] Apply suggestions from Tomas Co-authored-by: Tomas R. --- Lib/test/test_gettext.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index 9658d10bf00512..9b26b73cd76892 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -727,7 +727,7 @@ def setUp(self): for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): self.env.unset(key) - def createMo(self, lang): + 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) @@ -741,7 +741,7 @@ def test_find_with_env_vars(self): # when languages are not supplied self.setUp() self.env.set('LANGUAGE', 'ga_IE') - mo_file =self.createMo("ga_IE") + mo_file = self.createMo("ga_IE") result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale")) @@ -782,7 +782,7 @@ def test_find_all(self): languages=["ga_IE", "es_ES"], all=True) self.assertEqual(sorted(result), sorted(paths)) - def test_find_dedupelication(self): + def test_find_deduplication(self): # test that find removes duplicate languages self.setUp() result = gettext.find("foo", languages=['ga_IE', 'ga_IE']) From 5c6260b6efabf2949352a383ec12416babec83f6 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 3 Mar 2025 21:01:20 +0000 Subject: [PATCH 06/11] Complete some suggestions --- Lib/test/test_gettext.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index 9b26b73cd76892..9337d5c89b45fb 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -739,9 +739,8 @@ def create_mo_file(self, lang): 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") + mo_file = self.create_mo_file("ga_IE") result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale")) @@ -749,9 +748,8 @@ def test_find_with_env_vars(self): 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") + mo_file = self.create_mo_file("ga_IE") result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"), @@ -760,23 +758,20 @@ def test_find_with_lanuages(self): 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)) + 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) @@ -784,7 +779,6 @@ def test_find_all(self): def test_find_deduplication(self): # test that find removes duplicate languages - self.setUp() result = gettext.find("foo", languages=['ga_IE', 'ga_IE']) self.assertEqual(result, None) From 715e88e173ed339a8fb0551f0ef4580ceaac15a4 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 3 Mar 2025 21:08:45 +0000 Subject: [PATCH 07/11] Add last suggestions from Tomas --- Lib/test/test_gettext.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index 9337d5c89b45fb..d8c5784ecfeda9 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -779,8 +779,10 @@ def test_find_all(self): def test_find_deduplication(self): # test that find removes duplicate languages - result = gettext.find("foo", languages=['ga_IE', 'ga_IE']) - self.assertEqual(result, None) + 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): From a22b6882540c7d771982050e0be15a430626c8c9 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 10 Mar 2025 18:13:42 +0000 Subject: [PATCH 08/11] Tomas's suggestions --- Lib/test/test_gettext.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index d8c5784ecfeda9..0193d3d52ea379 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -756,16 +756,18 @@ def test_find_with_lanuages(self): languages=['ga_IE']) self.assertEqual(result, mo_file) - def test_find_with_no_lang(self): + @unittest.mock.patch('gettext._expand_lang') + def test_find_with_no_lang(self, patch_expand_lang): # no language can be found - result = gettext.find('foo') - self.assertEqual(result, None) + gettext.find('foo') + patch_expand_lang.assert_called_with('C') - def test_find_with_c(self): + @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') - result = gettext.find('foo') - self.assertEqual(result, None) + gettext.find('foo') + patch_expand_lang.assert_called_with('C') def test_find_all(self): # test that all are returned when all is set From 849fae9e1ec2a3852117549defe27a695a373bbf Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 10 Mar 2025 19:08:13 +0000 Subject: [PATCH 09/11] Test all variables --- Lib/test/test_gettext.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index 0193d3d52ea379..5ce474e4b9bc58 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -737,14 +737,15 @@ def create_mo_file(self, lang): return mo_file def test_find_with_env_vars(self): - # test that find correctly finds the environment variable LANGUAGE + # test that find correctly finds the environment variables # when languages are not supplied - self.env.set('LANGUAGE', 'ga_IE') mo_file = self.create_mo_file("ga_IE") - - result = gettext.find("mofile", - localedir=os.path.join(self.tempdir, "locale")) - self.assertEqual(result, mo_file) + 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_lanuages(self): # test that passed languages are used From de6a2f1159afc870348a0127d7789f48922b2a7a Mon Sep 17 00:00:00 2001 From: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Date: Mon, 10 Mar 2025 19:41:52 +0000 Subject: [PATCH 10/11] Update Lib/test/test_gettext.py Co-authored-by: Tomas R. --- Lib/test/test_gettext.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index 5ce474e4b9bc58..0fbd90dcb485f8 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -747,7 +747,7 @@ def test_find_with_env_vars(self): self.assertEqual(result, mo_file) self.env.unset(var) - def test_find_with_lanuages(self): + 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") From 043009f3a757baecc4a6caa43f5b440b67eee327 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Date: Mon, 10 Mar 2025 19:44:05 +0000 Subject: [PATCH 11/11] Fix typo Co-authored-by: Tomas R.