[Piglit] [PATCH 11/13] summary_tests.py: Move resultsfiles function to utils

Dylan Baker baker.dylan.c at gmail.com
Thu Mar 6 14:47:21 PST 2014


This is going to be needed in core_tests, so lets put it in the shared
module.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/tests/summary_tests.py | 41 +++----------------------------
 framework/tests/utils.py         | 53 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 53 insertions(+), 41 deletions(-)

diff --git a/framework/tests/summary_tests.py b/framework/tests/summary_tests.py
index 5101b93..68d7002 100644
--- a/framework/tests/summary_tests.py
+++ b/framework/tests/summary_tests.py
@@ -21,47 +21,12 @@
 
 """ Module providing tests for the summary module """
 
-try:
-    import simplejson as json
-except ImportError:
-    import json
-import os
-import tempfile
-from contextlib import contextmanager
 import framework.summary as summary
-
-
-JSON_DATA = {
-    "options": {
-        "profile": "tests/fake.py",
-        "filter": [],
-        "exclude_filter": []
-    },
-    "name": "fake-tests",
-    "lspci": "fake",
-    "glxinfo": "fake",
-    "tests": {
-        "sometest": {
-            "result": "pass",
-            "time": 0.01
-        }
-    }
-}
-
-
- at contextmanager
-def _resultfile():
-    """ Create a stringio with some json in it and pass that as results """
-    with tempfile.NamedTemporaryFile(delete=False) as output:
-        json.dump(JSON_DATA, output)
-
-    yield output
-
-    os.remove(output.name)
+import framework.tests.utils as utils
 
 
 def test_initialize_summary():
     """ Test that Summary initializes """
-    with _resultfile() as files:
-        test = summary.Summary([files.name])
+    with utils.resultfile() as tfile:
+        test = summary.Summary([tfile.name])
         assert test
diff --git a/framework/tests/utils.py b/framework/tests/utils.py
index 4b7746d..f337b1e 100644
--- a/framework/tests/utils.py
+++ b/framework/tests/utils.py
@@ -26,11 +26,39 @@ in a single place.
 """
 
 import os
+import shutil
 import tempfile
 from contextlib import contextmanager
-
-
-__all__ = ['with_tempfile']
+try:
+    import simplejson as json
+except ImportError:
+    import json
+
+
+__all__ = [
+    'with_tempfile',
+    'resultfile',
+    'tempdir',
+    'JSON_DATA'
+]
+
+
+JSON_DATA = {
+    "options": {
+        "profile": "tests/fake.py",
+        "filter": [],
+        "exclude_filter": []
+    },
+    "name": "fake-tests",
+    "lspci": "fake",
+    "glxinfo": "fake",
+    "tests": {
+        "sometest": {
+            "result": "pass",
+            "time": 0.01
+        }
+    }
+}
 
 
 class UtilsException(Exception):
@@ -39,6 +67,17 @@ class UtilsException(Exception):
 
 
 @contextmanager
+def resultfile():
+    """ Create a stringio with some json in it and pass that as results """
+    with tempfile.NamedTemporaryFile(delete=False) as output:
+        json.dump(JSON_DATA, output)
+
+    yield output
+
+    os.remove(output.name)
+
+
+ at contextmanager
 def with_tempfile(contents):
     """ Provides a context manager for a named tempfile
 
@@ -59,3 +98,11 @@ def with_tempfile(contents):
     yield temp.name
 
     os.remove(temp.name)
+
+
+ at contextmanager
+def tempdir():
+    """ Creates a temporary directory, returns it, and then deletes it """
+    tdir = tempfile.mkdtemp()
+    yield tdir
+    shutil.rmtree(tdir)
-- 
1.9.0



More information about the Piglit mailing list