[Piglit] [PATCH 5/8] results.py: Simplify TestrunResult.write()
Dylan Baker
baker.dylan.c at gmail.com
Thu Jun 5 15:26:14 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 | 17 +++++++++++++++++
2 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/framework/results.py b/framework/results.py
index ad799c7..b700fce 100644
--- a/framework/results.py
+++ b/framework/results.py
@@ -290,10 +290,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..64fb679 100644
--- a/framework/tests/results_tests.py
+++ b/framework/tests/results_tests.py
@@ -89,3 +89,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'))
+
+ assert result.__dict__ == new.__dict__
--
2.0.0
More information about the Piglit
mailing list