initial import
[vuplus_webkit] / Tools / Scripts / webkitpy / common / net / resultsjsonparser.py
1 # Copyright (c) 2010, Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
7 #     * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 #     * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 #     * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
30 try:
31     import json
32 except ImportError:
33     # python 2.5 compatibility
34     import webkitpy.thirdparty.simplejson as json
35
36 from webkitpy.common.memoized import memoized
37 from webkitpy.common.system.deprecated_logging import log
38 # FIXME: common should never import from new-run-webkit-tests, one of these files needs to move.
39 from webkitpy.layout_tests.layout_package import json_results_generator
40 from webkitpy.layout_tests.models import test_expectations, test_results, test_failures
41
42
43 # These are helper functions for navigating the results json structure.
44 def for_each_test(tree, handler, prefix=''):
45     for key in tree:
46         new_prefix = (prefix + '/' + key) if prefix else key
47         if 'actual' not in tree[key]:
48             for_each_test(tree[key], handler, new_prefix)
49         else:
50             handler(new_prefix, tree[key])
51
52
53 def result_for_test(tree, test):
54     parts = test.split('/')
55     for part in parts:
56         tree = tree[part]
57     return tree
58
59
60 # Wrapper around the dictionaries returned from the json.
61 # Eventually the .json should just serialize the TestFailure objects
62 # directly and we won't need this.
63 class JSONTestResult(object):
64     def __init__(self, test_name, result_dict):
65         self._test_name = test_name
66         self._result_dict = result_dict
67
68     def did_pass_or_run_as_expected(self):
69         return self.did_pass() or self.did_run_as_expected()
70
71     def did_pass(self):
72         return test_expectations.PASS in self._actual_as_tokens()
73
74     def did_run_as_expected(self):
75         actual_results = self._actual_as_tokens()
76         expected_results = self._expected_as_tokens()
77         # FIXME: We should only call remove_pixel_failures when this JSONResult
78         # came from a test run without pixel tests!
79         if not test_expectations.has_pixel_failures(actual_results):
80             expected_results = test_expectations.remove_pixel_failures(expected_results)
81         for actual_result in actual_results:
82             if not test_expectations.result_was_expected(actual_result, expected_results, False, False):
83                 return False
84         return True
85
86     def _tokenize(self, results_string):
87         tokens = map(test_expectations.TestExpectations.expectation_from_string, results_string.split(' '))
88         if None in tokens:
89             log("Unrecognized result in %s" % results_string)
90         return set(tokens)
91
92     @memoized
93     def _actual_as_tokens(self):
94         actual_results = self._result_dict['actual']
95         return self._tokenize(actual_results)
96
97     @memoized
98     def _expected_as_tokens(self):
99         actual_results = self._result_dict['expected']
100         return self._tokenize(actual_results)
101
102     def _failure_types_from_actual_result(self, actual):
103         # FIXME: There doesn't seem to be a full list of all possible values of
104         # 'actual' anywhere.  However JSONLayoutResultsGenerator.FAILURE_TO_CHAR
105         # is a useful reference as that's for "old" style results.json files
106         if actual == test_expectations.PASS:
107             return []
108         elif actual == test_expectations.TEXT:
109             return [test_failures.FailureTextMismatch()]
110         elif actual == test_expectations.IMAGE:
111             return [test_failures.FailureImageHashMismatch()]
112         elif actual == test_expectations.IMAGE_PLUS_TEXT:
113             return [test_failures.FailureImageHashMismatch(), test_failures.FailureTextMismatch()]
114         elif actual == test_expectations.AUDIO:
115             return [test_failures.FailureAudioMismatch()]
116         elif actual == test_expectations.TIMEOUT:
117             return [test_failures.FailureTimeout()]
118         elif actual == test_expectations.CRASH:
119             return [test_failures.FailureCrash()]
120         elif actual == test_expectations.MISSING:
121             return [test_failures.FailureMissingResult(), test_failures.FailureMissingImageHash(), test_failures.FailureMissingImage()]
122         else:
123             log("Failed to handle: %s" % self._result_dict['actual'])
124             return []
125
126     def _failures(self):
127         if self.did_pass():
128             return []
129         return sum(map(self._failure_types_from_actual_result, self._actual_as_tokens()), [])
130
131     def test_result(self):
132         # FIXME: Optionally pull in the test runtime from times_ms.json.
133         return test_results.TestResult(self._test_name, self._failures())
134
135
136 class ResultsJSONParser(object):
137     @classmethod
138     def parse_results_json(cls, json_string):
139         if not json_results_generator.has_json_wrapper(json_string):
140             return None
141
142         content_string = json_results_generator.strip_json_wrapper(json_string)
143         json_dict = json.loads(content_string)
144
145         json_results = []
146         for_each_test(json_dict['tests'], lambda test, result: json_results.append(JSONTestResult(test, result)))
147
148         # FIXME: What's the short sexy python way to filter None?
149         # I would use [foo.bar() for foo in foos if foo.bar()] but bar() is expensive.
150         unexpected_failures = [result.test_result() for result in json_results if not result.did_pass_or_run_as_expected()]
151         return filter(lambda a: a, unexpected_failures)