summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Babinsky <mbabinsk@redhat.com>2016-11-25 12:16:27 +0100
committerMartin Basti <mbasti@redhat.com>2016-11-28 13:58:33 +0100
commit211c944a353dbc241ae6e280c9474145ab48dbe4 (patch)
treec200b065a2a6515c3f3f7905322b7f4dbcb1c309
parent6bbbce44733761fda1fc588397b8baddbc7f8de3 (diff)
downloadfreeipa-211c944a353dbc241ae6e280c9474145ab48dbe4.tar.gz
freeipa-211c944a353dbc241ae6e280c9474145ab48dbe4.tar.xz
freeipa-211c944a353dbc241ae6e280c9474145ab48dbe4.zip
Improve the robustness FreeIPA's i18n module and its tests
Prevent false positive errors reported by `ipatests/i18n.py` and `ipatests/test_ipalib/test_text.py` when LANGUAGE env variable is set in the environment. Additionally, also set LC_ALL and LC_MESSAGES during checks to further improve the robustness. https://fedorahosted.org/freeipa/ticket/6512 Reviewed-By: Martin Basti <mbasti@redhat.com>
-rwxr-xr-xipatests/i18n.py5
-rw-r--r--ipatests/test_ipalib/test_text.py34
2 files changed, 32 insertions, 7 deletions
diff --git a/ipatests/i18n.py b/ipatests/i18n.py
index 80f9827f5..9a3bf5b7f 100755
--- a/ipatests/i18n.py
+++ b/ipatests/i18n.py
@@ -602,8 +602,11 @@ def test_translations(po_file, lang, domain, locale_dir):
# use a dummy language not associated with any real language,
# but the setlocale function demands the locale be a valid
# known locale, Zambia Xhosa is a reasonable choice :)
+ locale_envs = ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG')
- os.environ['LANG'] = lang
+ os.environ.update(
+ {locale_env: lang for locale_env in locale_envs}
+ )
# Create a gettext translation object specifying our domain as
# 'ipa' and the locale_dir as 'test_locale' (i.e. where to
diff --git a/ipatests/test_ipalib/test_text.py b/ipatests/test_ipalib/test_text.py
index d5106461f..3d72d2b8f 100644
--- a/ipatests/test_ipalib/test_text.py
+++ b/ipatests/test_ipalib/test_text.py
@@ -52,11 +52,36 @@ def test_create_translation():
class test_TestLang(object):
+ lang_env_vars = {'LC_ALL', 'LC_MESSAGES', 'LANGUAGE', 'LANG'}
+
+ def setup_lang(self):
+ """
+ Set all env variables used by gettext to localize translation files
+ to xh_ZA
+ """
+ self.lang = 'xh_ZA'
+ self.saved_locale = {
+ k: v for k, v in os.environ.items() if k in self.lang_env_vars}
+
+ os.environ.update(
+ {env_var: self.lang for env_var in self.lang_env_vars}
+ )
+
+ def teardown_lang(self):
+ """
+ Revert the locale settings to original values. If the original env
+ variable was not set before, it will be popped off os.environ
+ """
+ for env_var in self.lang_env_vars:
+ if env_var not in self.saved_locale:
+ os.environ.pop(env_var, None)
+
+ os.environ.update(self.saved_locale)
+
def setup(self):
self.tmp_dir = None
- self.saved_lang = None
+ self.setup_lang()
- self.lang = 'xh_ZA'
self.domain = 'ipa'
self.pot_basename = '%s.pot' % self.domain
@@ -64,7 +89,6 @@ class test_TestLang(object):
self.mo_basename = '%s.mo' % self.domain
self.tmp_dir = tempfile.mkdtemp()
- self.saved_lang = os.environ['LANG']
self.locale_dir = os.path.join(self.tmp_dir, 'test_locale')
self.msg_dir = os.path.join(self.locale_dir, self.lang, 'LC_MESSAGES')
@@ -93,8 +117,7 @@ class test_TestLang(object):
self.po_file_iterate = po_file_iterate
def teardown(self):
- if self.saved_lang is not None:
- os.environ['LANG'] = self.saved_lang
+ self.teardown_lang()
if self.tmp_dir is not None:
shutil.rmtree(self.tmp_dir)
@@ -107,7 +130,6 @@ class test_TestLang(object):
# but the setlocale function demands the locale be a valid
# known locale, Zambia Xhosa is a reasonable choice :)
- os.environ['LANG'] = self.lang
# Create a gettext translation object specifying our domain as
# 'ipa' and the locale_dir as 'test_locale' (i.e. where to