[Piglit] [Patch v7 10/13] results.py: Simplify TestrunResult.write()

Dylan Baker baker.dylan.c at gmail.com
Mon Jun 23 16:38:30 PDT 2014


This simplifies TestrunResult.write() to use a generator expression.
This method isn't currently used, but it will be useful in a patch later
in this series.

This also adds a testcase for this function to ensure that what it
writes is equivalent to what it reads.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/results.py             |  9 +++++----
 framework/tests/results_tests.py | 18 ++++++++++++++++++
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/framework/results.py b/framework/results.py
index 300cdef..2321a89 100644
--- a/framework/results.py
+++ b/framework/results.py
@@ -291,10 +291,11 @@ class TestrunResult(object):
         return new_file
 
     def write(self, file_):
-        # Serialize only the keys in serialized_keys.
-        keys = set(self.__dict__.keys()).intersection(self.serialized_keys)
-        raw_dict = dict([(k, self.__dict__[k]) for k in keys])
-        json.dump(raw_dict, file_, indent=JSONWriter.INDENT)
+        """ Write only values of the serialized_keys out to file """
+        with open(file_, 'w') as f:
+            json.dump(dict((k, v) for k, v in self.__dict__.iteritems()
+                           if k in self.serialized_keys),
+                      f, default=_piglit_encoder, indent=JSONWriter.INDENT)
 
 
 def load_results(filename):
diff --git a/framework/tests/results_tests.py b/framework/tests/results_tests.py
index d4be74c..b31c505 100644
--- a/framework/tests/results_tests.py
+++ b/framework/tests/results_tests.py
@@ -24,6 +24,7 @@
 import os
 import tempfile
 import json
+import nose.tools as nt
 import framework.tests.utils as utils
 import framework.results as results
 import framework.status as status
@@ -89,3 +90,20 @@ def test_testresult_to_status():
     result = results.TestResult({'result': 'pass'})
     assert isinstance(result['result'], status.Status), \
         "Result key not converted to a status object"
+
+
+def test_testrunresult_write():
+    """ TestrunResult.write() works
+
+    This tests for a bug where TestrunResult.write() wrote a file containing
+    {}, essentially if it dumps a file that is equal to what was provided then
+    it's probably working
+
+    """
+    with utils.resultfile() as f:
+        result = results.load_results(f.name)
+        with utils.tempdir() as tdir:
+            result.write(os.path.join(tdir, 'results.json'))
+            new = results.load_results(os.path.join(tdir, 'results.json'))
+
+    nt.assert_dict_equal(result.__dict__, new.__dict__)
-- 
2.0.0



More information about the Piglit mailing list