[Piglit] [PATCH 2/4] unittests: Fix tests that leave temporary files laying around

Dylan Baker baker.dylan.c at gmail.com
Wed Apr 6 22:46:44 UTC 2016


These are a bit more complicated, and the solution is more difficult.
Basically the problem is that the temporary file gets renamed out from
under the test, and the name of the file is a generated and unknown. To
get rid of these I've wrapped the shared private method in the
utils.test_in_tempdir, and extended that decorator to return a value.

This is kind of a hack, and all of this should be fixed by mocking out
things rather than using files.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 unittests/json_results_update_tests.py | 16 ++++------------
 unittests/utils.py                     |  2 +-
 2 files changed, 5 insertions(+), 13 deletions(-)

diff --git a/unittests/json_results_update_tests.py b/unittests/json_results_update_tests.py
index 0fcf1ec..e0d2d6a 100644
--- a/unittests/json_results_update_tests.py
+++ b/unittests/json_results_update_tests.py
@@ -226,6 +226,7 @@ class TestV0toV1(object):
         """backends.json.update_results (0 -> 1): Correctly handle new single entry subtests correctly"""
         nt.ok_('group3/groupA/test' in six.iterkeys(self.RESULT.tests))
 
+    @utils.test_in_tempdir
     def _load_with_update(self, data=None):
         """If the file is not results.json, it will be renamed.
 
@@ -235,18 +236,9 @@ class TestV0toV1(object):
         if not data:
             data = self.DATA
 
-        try:
-            with utils.tempfile(
-                    json.dumps(data, default=backends.json.piglit_encoder)) as t:
-                result = backends.json.load_results(t, 'none')
-        except OSError as e:
-            # There is the potential that the file will be renamed. In that event
-            # remove the renamed files
-            if e.errno == 2:
-                os.unlink(os.path.join(tempfile.tempdir, 'results.json'))
-                os.unlink(os.path.join(tempfile.tempdir, 'results.json.old'))
-            else:
-                raise
+        with open('results.json', 'w') as f:
+            json.dump(data, f, default=backends.json.piglit_encoder)
+        result = backends.json.load_results('results.json', 'none')
 
         return result
 
diff --git a/unittests/utils.py b/unittests/utils.py
index 089f479..3bfafcd 100644
--- a/unittests/utils.py
+++ b/unittests/utils.py
@@ -368,7 +368,7 @@ def test_in_tempdir(func):
         with tempdir() as tdir:
             try:
                 os.chdir(tdir)
-                func(*args, **kwargs)
+                return func(*args, **kwargs)
             finally:
                 os.chdir(original_dir)
 
-- 
2.8.0



More information about the Piglit mailing list