[Piglit] [PATCH 09/10] core_tests.py: Rework to avoid os.environ pollution

Dylan Baker baker.dylan.c at gmail.com
Thu Jan 29 15:31:24 PST 2015


This module contains tests for core.get_config(), which makes decisions
based on values in os.environ (ENV from the terminal). To test different
paths the environment needs to be manipulated to force those paths to be
hit, if the environment isn't properly cleaned up then other tests that
use get_config (like most of the tests in integration_tests.py) can have
their results altered. This manifests currently, all of the tests in
integration_tests.py will skip after core_tests.py is run, but not if it
is run by itself.

This patch does away with the use of the TestWithEnvClean class, and
instead opts for individual test classes using setup and teardown
fixtures. These fixtures are actually a two methods of the same class
instance (a requirement for preserving state from the setup method to
the teardown method), and ensure that all of the mucking about done by
the tests are restored.

This fixes the integration tests to run when possible

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/tests/core_tests.py | 155 +++++++++++++++++++++++++++---------------
 1 file changed, 100 insertions(+), 55 deletions(-)

diff --git a/framework/tests/core_tests.py b/framework/tests/core_tests.py
index 9c65c31..56c7ac3 100644
--- a/framework/tests/core_tests.py
+++ b/framework/tests/core_tests.py
@@ -32,6 +32,61 @@ import nose.tools as nt
 import framework.tests.utils as utils
 import framework.core as core
 
+_CONF_FILE = textwrap.dedent("""\
+[nose-test]
+; a section for testing behavior
+dir = foo
+""")
+
+
+class GetConfigFixture(object):
+    """A class that provides setup and teardown for core.get_env() tests.
+
+    This class provides a method to save state before the test executes, and
+    then restore it afterwards. This is the reason this is a class that returns
+    two methods and not a function.
+
+    """
+    def __init__(self):
+        self.env = {}
+        self.restore = False
+
+    @classmethod
+    def make(cls):
+        new = cls()
+        return new.setup, new.teardown
+
+    def setup(self):
+        """Gather environment to preserve."""
+        try:
+            for env in ['XDG_CONFIG_HOME', 'HOME']:
+                if env in os.environ:
+                    self.env[env] = os.environ.pop(env)
+
+            if os.path.exists('piglit.conf'):
+                shutil.move('piglit.conf', 'piglit.conf.restore')
+                self.restore = True
+            core.PIGLIT_CONFIG = ConfigParser.SafeConfigParser(
+                allow_no_value=True)
+        except Exception as e:
+            raise utils.UtilsError(e)
+
+    def teardown(self):
+        """Restore that environment."""
+        try:
+            for env in ['XDG_CONFIG_HOME', 'HOME']:
+                if env in self.env:
+                    os.environ[env] = self.env[env]
+                elif env in os.environ:
+                    del os.environ[env]
+
+            if self.restore:
+                shutil.move('piglit.conf.restore', 'piglit.conf')
+            core.PIGLIT_CONFIG = ConfigParser.SafeConfigParser(
+                allow_no_value=True)
+        except Exception as e:
+            raise utils.UtilsError(e)
+
 
 def _reset_piglit_config():
     """ Set core.PIGLIT_CONFIG back to pristine """
@@ -124,74 +179,64 @@ def test_parse_listfile_tilde():
     assert results[0] == os.path.expandvars("$HOME/foo")
 
 
-class TestGetConfig(utils.TestWithEnvClean):
-    CONF_FILE = textwrap.dedent("""
-    [nose-test]
-    ; a section for testing behavior
-    dir = foo
-    """)
-
-    def __unset_config(self):
-        self.defer(_reset_piglit_config)
-        self.add_teardown('XDG_CONFIG_HOME')
-        self.add_teardown('HOME')
-
-    def __move_local(self):
-        """ Move a local piglit.conf so it isn't overwritten """
-        if os.path.exists('piglit.conf'):
-            shutil.move('piglit.conf', 'piglit.conf.restore')
-            self.defer(shutil.move, 'piglit.conf.restore', 'piglit.conf')
+ at nt.with_setup(*GetConfigFixture.make())
+def test_xdg_config_home():
+    """ get_config() finds $XDG_CONFIG_HOME/piglit.conf """
+    with utils.tempdir() as tdir:
+        os.environ['XDG_CONFIG_HOME'] = tdir
+        with open(os.path.join(tdir, 'piglit.conf'), 'w') as f:
+            f.write(_CONF_FILE)
+        core.get_config()
 
-    def setup(self):
-        self.__unset_config()
-        self.__move_local()
+    nt.ok_(core.PIGLIT_CONFIG.has_section('nose-test'),
+           msg='$XDG_CONFIG_HOME not found')
 
-    def test_xdg_config_home(self):
-        """ get_config() finds $XDG_CONFIG_HOME/piglit.conf """
-        with utils.tempdir() as tdir:
-            os.environ['XDG_CONFIG_HOME'] = tdir
-            with open(os.path.join(tdir, 'piglit.conf'), 'w') as f:
-                f.write(TestGetConfig.CONF_FILE)
-            core.get_config()
 
-        nt.ok_(core.PIGLIT_CONFIG.has_section('nose-test'),
-               msg='$XDG_CONFIG_HOME not found')
+ at nt.with_setup(*GetConfigFixture.make())
+def test_config_home_fallback():
+    """ get_config() finds $HOME/.config/piglit.conf """
+    with utils.tempdir() as tdir:
+        os.environ['HOME'] = tdir
+        os.mkdir(os.path.join(tdir, '.config'))
+        with open(os.path.join(tdir, '.config/piglit.conf'), 'w') as f:
+            f.write(_CONF_FILE)
+        core.get_config()
 
-    def test_config_home_fallback(self):
-        """ get_config() finds $HOME/.config/piglit.conf """
-        with utils.tempdir() as tdir:
-            os.environ['HOME'] = tdir
-            os.mkdir(os.path.join(tdir, '.config'))
-            with open(os.path.join(tdir, '.config/piglit.conf'), 'w') as f:
-                f.write(TestGetConfig.CONF_FILE)
-            core.get_config()
+    nt.ok_(core.PIGLIT_CONFIG.has_section('nose-test'),
+           msg='$HOME/.config/piglit.conf not found')
 
-        nt.ok_(core.PIGLIT_CONFIG.has_section('nose-test'),
-               msg='$HOME/.config/piglit.conf not found')
 
-    def test_local(self):
-        """ get_config() finds ./piglit.conf """
-        with utils.tempdir() as tdir:
-            self.defer(os.chdir, os.getcwd())
+ at nt.with_setup(*GetConfigFixture.make())
+def test_local():
+    """ get_config() finds ./piglit.conf """
+    with utils.tempdir() as tdir:
+        try:
+            return_dir = os.getcwd()
             os.chdir(tdir)
 
             with open(os.path.join(tdir, 'piglit.conf'), 'w') as f:
-                f.write(TestGetConfig.CONF_FILE)
+                f.write(_CONF_FILE)
 
             core.get_config()
+        finally:
+            os.chdir(return_dir)
 
-        nt.ok_(core.PIGLIT_CONFIG.has_section('nose-test'),
-               msg='./piglit.conf not found')
+    nt.ok_(core.PIGLIT_CONFIG.has_section('nose-test'),
+           msg='./piglit.conf not found')
 
-    def test_piglit_root(self):
-        """ get_config() finds "piglit root"/piglit.conf """
-        with open('piglit.conf', 'w') as f:
-            f.write(TestGetConfig.CONF_FILE)
-        self.defer(os.unlink, 'piglit.conf')
-        self.defer(os.chdir, os.getcwd())
-        os.chdir('..')
 
+ at nt.with_setup(*GetConfigFixture.make())
+def test_piglit_root():
+    """ get_config() finds "piglit root"/piglit.conf """
+    with open('piglit.conf', 'w') as f:
+        f.write(_CONF_FILE)
+    return_dir = os.getcwd()
+    try:
+        os.chdir('..')
         core.get_config()
+    finally:
+        os.chdir(return_dir)
+        os.unlink('piglit.conf')
 
-        nt.ok_(core.PIGLIT_CONFIG.has_section('nose-test'),
-               msg='$PIGLIT_ROOT not found')
+    nt.ok_(core.PIGLIT_CONFIG.has_section('nose-test'),
+           msg='$PIGLIT_ROOT not found')
-- 
2.2.2



More information about the Piglit mailing list