Skip to content

Commit 6557ed1

Browse files
De-duplicate code as per Petr's suggestion
1 parent ee77877 commit 6557ed1

File tree

1 file changed

+48
-55
lines changed

1 file changed

+48
-55
lines changed

Lib/test/test_gettext.py

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -717,83 +717,76 @@ def test_expand_lang(self):
717717
return_value=locale):
718718
self.assertEqual(gettext._expand_lang(locale), expanded)
719719

720-
# helper for FindTestCase
721-
def _clearvars(env):
722-
for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
723-
env.unset(key)
724720

725721
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 createMo(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(b"\xDE\x12\x04\x95")
737+
return mo_file
738+
726739
def test_find_with_env_vars(self):
727740
# test that find correctly finds the environment variable LANGUAGE
728741
# when languages are not supplied
729-
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
730-
env.set('LANGUAGE', 'ga_IE')
731-
732-
locale_dir = os.path.join(tempdir, "locale")
733-
mofile_dir = os.path.join(locale_dir, "ga_IE", "LC_MESSAGES")
734-
os.makedirs(mofile_dir)
735-
mo_file = os.path.join(mofile_dir, "mofile.mo")
736-
with open(mo_file, "wb") as f:
737-
f.write(b"\xDE\x12\x04\x95")
742+
self.setUp()
743+
self.env.set('LANGUAGE', 'ga_IE')
744+
mo_file =self.createMo("ga_IE")
738745

739-
result = gettext.find("mofile", localedir=locale_dir)
740-
self.assertEqual(result, mo_file)
746+
result = gettext.find("mofile",
747+
localedir=os.path.join(self.tempdir, "locale"))
748+
self.assertEqual(result, mo_file)
741749

742750
def test_find_with_lanuages(self):
743751
# test that passed languages are used
744-
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
745-
env.set('LANGUAGE', 'pt_BR')
752+
self.setUp()
753+
self.env.set('LANGUAGE', 'pt_BR')
754+
mo_file = self.createMo("ga_IE")
746755

747-
locale_dir = os.path.join(tempdir, "locale")
748-
mofile_dir = os.path.join(locale_dir, "ga_IE", "LC_MESSAGES")
749-
os.makedirs(mofile_dir)
750-
mo_file = os.path.join(mofile_dir, "mofile.mo")
751-
with open(mo_file, "wb") as f:
752-
f.write(b"\xDE\x12\x04\x95")
753-
754-
result = gettext.find("mofile", localedir=locale_dir, languages=['ga_IE'])
755-
self.assertEqual(result, mo_file)
756+
result = gettext.find("mofile",
757+
localedir=os.path.join(self.tempdir, "locale"),
758+
languages=['ga_IE'])
759+
self.assertEqual(result, mo_file)
756760

757761
def test_find_with_no_lang(self):
758762
# no language can be found
759-
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
760-
_clearvars(env)
761-
result = gettext.find('foo')
762-
self.assertEqual(result, None)
763+
self.setUp()
764+
result = gettext.find('foo')
765+
self.assertEqual(result, None)
763766

764767
def test_find_with_c(self):
765768
# 'C' is already in languages
766-
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
767-
env.set('LANGUAGE', 'C')
768-
result = gettext.find('foo')
769-
self.assertEqual(result, None)
769+
self.setUp()
770+
self.env.set('LANGUAGE', 'C')
771+
result = gettext.find('foo')
772+
self.assertEqual(result, None)
770773

771774
def test_find_all(self):
772775
# test that all are returned when all is set
773-
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
774-
_clearvars(env)
775-
776-
locale_dir = os.path.join(tempdir, "locale")
777-
paths = []
778-
for lang in ["ga_IE", "es_ES"]:
779-
mofile_dir = os.path.join(locale_dir, lang, "LC_MESSAGES")
780-
os.makedirs(mofile_dir)
781-
mo_file = os.path.join(mofile_dir, "mofile.mo")
782-
with open(mo_file, "wb") as f:
783-
f.write(b"\xDE\x12\x04\x95")
784-
paths.append(mo_file)
785-
786-
result = gettext.find('mofile', localedir=locale_dir,
787-
languages=["ga_IE", "es_ES"], all=True)
788-
self.assertEqual(sorted(result), sorted(paths))
776+
self.setUp()
777+
paths = []
778+
for lang in ["ga_IE", "es_ES"]:
779+
paths.append(self.createMo(lang))
780+
result = gettext.find('mofile',
781+
localedir=os.path.join(self.tempdir, "locale"),
782+
languages=["ga_IE", "es_ES"], all=True)
783+
self.assertEqual(sorted(result), sorted(paths))
789784

790785
def test_find_dedupelication(self):
791786
# test that find removes duplicate languages
792-
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
793-
_clearvars(env)
794-
795-
result = gettext.find("foo", languages=['ga_IE', 'ga_IE'])
796-
self.assertEqual(result, None)
787+
self.setUp()
788+
result = gettext.find("foo", languages=['ga_IE', 'ga_IE'])
789+
self.assertEqual(result, None)
797790

798791

799792
class MiscTestCase(unittest.TestCase):

0 commit comments

Comments
 (0)