[Piglit] [PATCH 05/42] core_tests.py: Replace complex class with simpler decorator

Dylan Baker baker.dylan.c at gmail.com
Wed Apr 22 15:09:54 PDT 2015


Instead of trying to shoe-horn something into nose.tools.with_setup
(which was a complex stretch to make work), just write a simple closure
based function decorator, which will do the same thing in a much simpler
and more obvious way.

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

diff --git a/framework/tests/core_tests.py b/framework/tests/core_tests.py
index 108d5ee..65792e8 100644
--- a/framework/tests/core_tests.py
+++ b/framework/tests/core_tests.py
@@ -26,6 +26,7 @@ import collections
 import shutil
 import ConfigParser
 import textwrap
+import functools
 
 import nose.tools as nt
 
@@ -39,54 +40,53 @@ dir = foo
 """)
 
 
-class GetConfigFixture(object):
-    """A class that provides setup and teardown for core.get_env() tests.
+def _save_core_config(func):
+    """Decorator function that saves and restores environment state.
 
-    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.
+    This allows the test to modify any protected state, and guarantees that it
+    will be restored afterwords.
 
-    """
-    def __init__(self):
-        self.env = {}
-        self.restore = False
+    Curerntly this protects a <piglit path>/piglit.conf, and $XDG_CONFIG_HOME
+    and $HOME
 
-    @classmethod
-    def make(cls):
-        new = cls()
-        return new.setup, new.teardown
+    """
+    @functools.wraps(func)
+    def inner(*args, **kwargs):
+        """Save and restore state."""
+        saved_env = {}
+        restore_piglitconf = False
 
-    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)
+                    saved_env[env] = os.environ.pop(env)
 
             if os.path.exists('piglit.conf'):
                 shutil.move('piglit.conf', 'piglit.conf.restore')
-                self.restore = True
+                restore_piglitconf = True
             core.PIGLIT_CONFIG = ConfigParser.SafeConfigParser(
                 allow_no_value=True)
         except Exception as e:
             raise utils.UtilsError(e)
 
-    def teardown(self):
-        """Restore that environment."""
+        func(*args, **kwargs)
+
         try:
             for env in ['XDG_CONFIG_HOME', 'HOME']:
-                if env in self.env:
-                    os.environ[env] = self.env[env]
+                if env in saved_env:
+                    os.environ[env] = saved_env[env]
                 elif env in os.environ:
                     del os.environ[env]
 
-            if self.restore:
+            if restore_piglitconf:
                 shutil.move('piglit.conf.restore', 'piglit.conf')
             core.PIGLIT_CONFIG = ConfigParser.SafeConfigParser(
                 allow_no_value=True)
         except Exception as e:
             raise utils.UtilsError(e)
 
+    return inner
+
 
 def _reset_piglit_config():
     """ Set core.PIGLIT_CONFIG back to pristine """
@@ -179,7 +179,7 @@ def test_parse_listfile_tilde():
     assert results[0] == os.path.expandvars("$HOME/foo")
 
 
- at nt.with_setup(*GetConfigFixture.make())
+ at _save_core_config
 def test_xdg_config_home():
     """ get_config() finds $XDG_CONFIG_HOME/piglit.conf """
     with utils.tempdir() as tdir:
@@ -192,7 +192,7 @@ def test_xdg_config_home():
            msg='$XDG_CONFIG_HOME not found')
 
 
- at nt.with_setup(*GetConfigFixture.make())
+ at _save_core_config
 def test_config_home_fallback():
     """ get_config() finds $HOME/.config/piglit.conf """
     with utils.tempdir() as tdir:
@@ -206,7 +206,7 @@ def test_config_home_fallback():
            msg='$HOME/.config/piglit.conf not found')
 
 
- at nt.with_setup(*GetConfigFixture.make())
+ at _save_core_config
 @utils.test_in_tempdir
 def test_local():
     """ get_config() finds ./piglit.conf """
@@ -222,7 +222,7 @@ def test_local():
            msg='./piglit.conf not found')
 
 
- at nt.with_setup(*GetConfigFixture.make())
+ at _save_core_config
 def test_piglit_root():
     """ get_config() finds "piglit root"/piglit.conf """
     with open('piglit.conf', 'w') as f:
-- 
2.3.5



More information about the Piglit mailing list