[Piglit] [PATCH] framework/backends/json.py: present a nice error message if a json resluts file is corrupt
Dylan Baker
baker.dylan.c at gmail.com
Tue May 19 13:29:38 PDT 2015
If a complete result is corrupt, currently an exception will be raised
and caught at the root (this will be an Exception, but a
PiglitFatalError), this will result in a bug message.
With this patch the corrupt file will be identified as well as the error
presented by the json module about the underlying file.
Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
framework/backends/json.py | 9 ++++++++-
framework/tests/json_backend_tests.py | 12 ++++++++++--
2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/framework/backends/json.py b/framework/backends/json.py
index 9d76c49..2fe239c 100644
--- a/framework/backends/json.py
+++ b/framework/backends/json.py
@@ -212,7 +212,14 @@ def _load(results_file):
"""
result = results.TestrunResult()
result.results_version = 0 # This should get overwritten
- result.__dict__.update(json.load(results_file, object_hook=piglit_decoder))
+ try:
+ result.__dict__.update(
+ json.load(results_file, object_hook=piglit_decoder))
+ except ValueError as e:
+ raise exceptions.PiglitFatalError(
+ 'While loading json results file: "{}",\n'
+ 'the following error occured:\n{}'.format(results_file.name,
+ e.message))
return result
diff --git a/framework/tests/json_backend_tests.py b/framework/tests/json_backend_tests.py
index e537362..889332a 100644
--- a/framework/tests/json_backend_tests.py
+++ b/framework/tests/json_backend_tests.py
@@ -18,7 +18,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
-# pylint: disable=missing-docstring
+# pylint: disable=missing-docstring,protected-access
""" Tests for the backend package """
@@ -31,7 +31,7 @@ except ImportError:
import json
import nose.tools as nt
-from framework import results, backends
+from framework import results, backends, exceptions
import framework.tests.utils as utils
from .backends_tests import BACKEND_INITIAL_META
@@ -295,3 +295,11 @@ def test_piglit_decoder():
test = json.loads('{"foo": {"result": "pass"}}',
object_hook=backends.json.piglit_decoder)
nt.assert_is_instance(test['foo'], results.TestResult)
+
+
+ at nt.raises(exceptions.PiglitFatalError)
+def test_load_bad_json():
+ """backends.json._load: Raises fatal error if json is corrupt"""
+ with utils.tempfile('{"bad json": }') as f:
+ with open(f, 'r') as tfile:
+ backends.json._load(tfile)
--
2.4.0
More information about the Piglit
mailing list