URL: https://github.com/freeipa/freeipa/pull/274
Author: martbab
 Title: #274: Improve the robustness FreeIPA's i18n module and its tests
Action: opened

PR body:
"""
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
"""

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/274/head:pr274
git checkout pr274
From 23d62a4de47d85d3c2400b1aa85d3539fae6703b Mon Sep 17 00:00:00 2001
From: Martin Babinsky <mbabi...@redhat.com>
Date: Fri, 25 Nov 2016 12:16:27 +0100
Subject: [PATCH] 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
---
 ipatests/i18n.py                  |  5 ++++-
 ipatests/test_ipalib/test_text.py | 34 ++++++++++++++++++++++++++++------
 2 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/ipatests/i18n.py b/ipatests/i18n.py
index 8c6e641..37a90dd 100755
--- a/ipatests/i18n.py
+++ b/ipatests/i18n.py
@@ -600,8 +600,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 d510646..3d72d2b 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 @@ def setup(self):
         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 @@ def setup(self):
         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 @@ def test_test_lang(self):
         # 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
-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to