[Piglit] [PATCH 3/3] framework/tests: reduce code duplication between test modules

Dylan Baker baker.dylan.c at gmail.com
Mon Jul 13 17:06:42 PDT 2015


Shares the setup and teardown functions for locking compression modes
between the various backend test modules.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/tests/json_backend_tests.py        | 16 ++--------------
 framework/tests/json_results_update_tests.py | 15 ++-------------
 framework/tests/json_tests.py                | 15 ++-------------
 framework/tests/junit_backends_tests.py      | 15 ++-------------
 framework/tests/utils.py                     | 27 ++++++++++++++++++++++++++-
 5 files changed, 34 insertions(+), 54 deletions(-)

diff --git a/framework/tests/json_backend_tests.py b/framework/tests/json_backend_tests.py
index 6a58534..d683883 100644
--- a/framework/tests/json_backend_tests.py
+++ b/framework/tests/json_backend_tests.py
@@ -35,24 +35,13 @@ from framework import results, backends, exceptions, grouptools
 import framework.tests.utils as utils
 from .backends_tests import BACKEND_INITIAL_META
 
-_SAVED_COMPRESSION = os.environ.get('PIGLIT_COMPRESSON')
-
 
 def setup_module():
-    # Set the compression mode to a controlled value (no compression), to
-    # ensure that we're not getting unexpected file extensions. This means that
-    # the default can be changed, or environment variables set without
-    # affecting unit tests
-    # We set PIGLIT_COMPRESSION because it is the first value to checked when
-    # setting a compressor
-    os.environ['PIGLIT_COMPRESSION'] = 'none'
+    utils.set_compression('none')
 
 
 def teardown_module():
-    if _SAVED_COMPRESSION is not None:
-        os.environ['PIGLIT_COMPRESSION'] = _SAVED_COMPRESSION
-    else:
-        del os.environ['PIGLIT_COMPRESSION']
+    utils.unset_compression()
 
 
 def test_initialize_jsonbackend():
@@ -110,7 +99,6 @@ class TestJSONTestMethod(utils.StaticDirectory):
         nt.assert_dict_equal({self.test_name: self.result}, test)
 
 
-
 class TestJSONTestFinalize(utils.StaticDirectory):
     # We're explictely setting none here since the default can change from none
     @classmethod
diff --git a/framework/tests/json_results_update_tests.py b/framework/tests/json_results_update_tests.py
index b492501..8abfb60 100644
--- a/framework/tests/json_results_update_tests.py
+++ b/framework/tests/json_results_update_tests.py
@@ -38,24 +38,13 @@ from framework import backends, results
 # protected members, or because of nose requirements, like long lines
 # pylint: disable=protected-access,invalid-name,line-too-long
 
-_SAVED_COMPRESSION = os.environ.get('PIGLIT_COMPRESSON')
-
 
 def setup_module():
-    # Set the compression mode to a controlled value (no compression), to
-    # ensure that we're not getting unexpected file extensions. This means that
-    # the default can be changed, or environment variables set without
-    # affecting unit tests
-    # We set PIGLIT_COMPRESSION because it is the first value to checked when
-    # setting a compressor
-    os.environ['PIGLIT_COMPRESSION'] = 'none'
+    utils.set_compression('none')
 
 
 def teardown_module():
-    if _SAVED_COMPRESSION is not None:
-        os.environ['PIGLIT_COMPRESSION'] = _SAVED_COMPRESSION
-    else:
-        del os.environ['PIGLIT_COMPRESSION']
+    utils.unset_compression()
 
 
 class TestV0toV1(object):
diff --git a/framework/tests/json_tests.py b/framework/tests/json_tests.py
index 9c985ab..7819ca6 100644
--- a/framework/tests/json_tests.py
+++ b/framework/tests/json_tests.py
@@ -42,24 +42,13 @@ from framework.programs.run import _create_metadata
 
 # pylint: disable=invalid-name
 
-_SAVED_COMPRESSION = os.environ.get('PIGLIT_COMPRESSON')
-
 
 def setup_module():
-    # Set the compression mode to a controlled value (no compression), to
-    # ensure that we're not getting unexpected file extensions. This means that
-    # the default can be changed, or environment variables set without
-    # affecting unit tests
-    # We set PIGLIT_COMPRESSION because it is the first value to checked when
-    # setting a compressor
-    os.environ['PIGLIT_COMPRESSION'] = 'none'
+    utils.set_compression('none')
 
 
 def teardown_module():
-    if _SAVED_COMPRESSION is not None:
-        os.environ['PIGLIT_COMPRESSION'] = _SAVED_COMPRESSION
-    else:
-        del os.environ['PIGLIT_COMPRESSION']
+    utils.unset_compression()
 
 
 # Helpers
diff --git a/framework/tests/junit_backends_tests.py b/framework/tests/junit_backends_tests.py
index df70ada..0be7383 100644
--- a/framework/tests/junit_backends_tests.py
+++ b/framework/tests/junit_backends_tests.py
@@ -53,24 +53,13 @@ _XML = """\
   </testsuites>
 """
 
-_SAVED_COMPRESSION = os.environ.get('PIGLIT_COMPRESSON')
-
 
 def setup_module():
-    # Set the compression mode to a controlled value (no compression), to
-    # ensure that we're not getting unexpected file extensions. This means that
-    # the default can be changed, or environment variables set without
-    # affecting unit tests
-    # We set PIGLIT_COMPRESSION because it is the first value to checked when
-    # setting a compressor
-    os.environ['PIGLIT_COMPRESSION'] = 'none'
+    utils.set_compression('none')
 
 
 def teardown_module():
-    if _SAVED_COMPRESSION is not None:
-        os.environ['PIGLIT_COMPRESSION'] = _SAVED_COMPRESSION
-    else:
-        del os.environ['PIGLIT_COMPRESSION']
+    utils.unset_compression()
 
 
 class TestJunitNoTests(utils.StaticDirectory):
diff --git a/framework/tests/utils.py b/framework/tests/utils.py
index f91d87a..55d2b6e 100644
--- a/framework/tests/utils.py
+++ b/framework/tests/utils.py
@@ -42,7 +42,6 @@ from nose.plugins.skip import SkipTest
 
 from framework import test, backends, results, core
 
-
 __all__ = [
     'tempfile',
     'resultfile',
@@ -81,6 +80,8 @@ JSON_DATA = {
     })
 }
 
+_SAVED_COMPRESSION = os.environ.get('PIGLIT_COMPRESSION')
+
 
 class TestFailure(AssertionError):
     pass
@@ -453,3 +454,27 @@ def set_piglit_conf(*values):
 
         return _inner
     return _decorator
+
+
+def set_compression(mode):
+    """lock piglit compression mode to specifed value.
+
+    Meant to be called from setup_module function.
+
+    """
+    # The implimentation details of this is that the environment value is the
+    # first value looked at for setting compression, so if it's set all other
+    # values will be ignored.
+    os.environ['PIGLIT_COMPRESSION'] = mode
+
+
+def unset_compression():
+    """Restore compression to the origonal value.
+
+    Counterpart to set_compression. Should be called from teardown_module.
+
+    """
+    if _SAVED_COMPRESSION is not None:
+        os.environ['PIGLIT_COMPRESSION'] = _SAVED_COMPRESSION
+    else:
+        del os.environ['PIGLIT_COMPRESSION']
-- 
2.4.5



More information about the Piglit mailing list