OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
epoger
2013/05/17 17:56:45
This file was renamed from "confirm_no_failures_in
| |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Utility to confirm that a JSON summary written by GM contains no failures. | 6 """Utility to display a summary of JSON-format GM results, and exit with |
7 a nonzero errorcode if there were non-ignored failures in the GM results. | |
7 | 8 |
8 Usage: | 9 Usage: |
9 python confirm_no_failures_in_json.py <filename> | 10 python display_json_results.py <filename> |
11 | |
12 TODO(epoger): We may want to add flags so that the caller can choose which | |
13 error types cause a nonzero return code. | |
borenet
2013/05/17 19:48:33
Other options might be nice as well, for example t
epoger
2013/05/18 03:09:54
Good idea... added to the TODO. If it's OK with y
borenet
2013/05/20 12:09:55
Agreed.
| |
10 """ | 14 """ |
11 | 15 |
12 __author__ = 'Elliot Poger' | 16 __author__ = 'Elliot Poger' |
13 | 17 |
14 | 18 |
15 import json | 19 import json |
16 import sys | 20 import sys |
17 | 21 |
18 | 22 |
19 # These constants must be kept in sync with the kJsonKey_ constants in | 23 # These constants must be kept in sync with the kJsonKey_ constants in |
20 # gm_expectations.cpp ! | 24 # gm_expectations.cpp ! |
21 JSONKEY_ACTUALRESULTS = 'actual-results' | 25 JSONKEY_ACTUALRESULTS = 'actual-results' |
22 JSONKEY_ACTUALRESULTS_FAILED = 'failed' | 26 JSONKEY_ACTUALRESULTS_FAILED = 'failed' |
23 | 27 JSONKEY_ACTUALRESULTS_FAILUREIGNORED = 'failure-ignored' |
24 # This is the same indent level as used by jsoncpp, just for consistency. | 28 JSONKEY_ACTUALRESULTS_NOCOMPARISON = 'no-comparison' |
25 JSON_INDENTLEVEL = 3 | 29 JSONKEY_ACTUALRESULTS_SUCCEEDED = 'succeeded' |
26 | 30 |
27 | 31 |
28 def Assert(filepath): | 32 class ResultAccumulator(object): |
29 """Raises an exception if the JSON summary at filepath contains any failed | 33 """Object that accumulates results of a given type, and can generate a |
30 tests, or if we were unable to read the JSON summary.""" | 34 summary upon request.""" |
31 failed_tests = GetFailedTests(filepath) | 35 |
32 if failed_tests: | 36 def __init__(self, name, do_list, do_fail): |
33 raise Exception('JSON file %s contained these test failures...\n%s' % ( | 37 """name: name of the category this result type falls into |
34 filepath, json.dumps(failed_tests, indent=JSON_INDENTLEVEL))) | 38 do_list: whether to list all of the tests with this results type |
39 do_fail: whether to return with nonzero exit code if there are any | |
40 results of this type | |
41 """ | |
42 self._name = name | |
43 self._do_list = do_list | |
44 self._do_fail = do_fail | |
45 self._testnames = [] | |
46 | |
47 def AddResult(self, testname): | |
48 """Adds a result of this particular type. | |
49 testname: (string) name of the test""" | |
50 self._testnames.append(testname) | |
51 | |
52 def ShouldSignalFailure(self): | |
53 """Returns true if this result type is serious (self._do_fail is True) | |
54 and there were any results of this type.""" | |
55 if self._do_fail and self._testnames: | |
56 return True | |
57 else: | |
58 return False | |
59 | |
60 def GetSummaryLine(self): | |
61 """Returns a single-line string summary of all results added to this | |
62 accumulator so far.""" | |
63 summary = '' | |
64 if self._do_fail: | |
65 summary += '[*] ' | |
66 else: | |
67 summary += '[ ] ' | |
68 summary += str(len(self._testnames)) | |
69 summary += ' ' | |
70 summary += self._name | |
71 if self._do_list: | |
72 summary += ': ' | |
73 for testname in self._testnames: | |
74 summary += testname | |
75 summary += ' ' | |
76 return summary | |
35 | 77 |
36 | 78 |
37 def GetFailedTests(filepath): | 79 # Map labels within the JSON file to the ResultAccumulator for each label. |
38 """Returns the dictionary of failed tests from the JSON file at filepath.""" | 80 RESULTS_MAP = { |
borenet
2013/05/17 19:48:33
Is there any reason for this to be global? I'm al
epoger
2013/05/18 03:09:54
No need for it to be global... I moved its definit
| |
81 JSONKEY_ACTUALRESULTS_FAILED: | |
82 ResultAccumulator(name='ExpectationsMismatch', | |
83 do_list=True, do_fail=True), | |
84 JSONKEY_ACTUALRESULTS_FAILUREIGNORED: | |
85 ResultAccumulator(name='IgnoredExpectationsMismatch', | |
86 do_list=True, do_fail=False), | |
87 JSONKEY_ACTUALRESULTS_NOCOMPARISON: | |
88 ResultAccumulator(name='MissingExpectations', | |
89 do_list=False, do_fail=False), | |
90 JSONKEY_ACTUALRESULTS_SUCCEEDED: | |
91 ResultAccumulator(name='Passed', | |
92 do_list=False, do_fail=False), | |
93 } | |
94 | |
95 | |
96 def Display(filepath): | |
97 """Displays a summary of the results in a JSON file, and returns True if | |
98 those results indicate a significant failure. | |
99 filepath: (string) path to JSON file""" | |
100 failure = False | |
39 json_dict = json.load(open(filepath)) | 101 json_dict = json.load(open(filepath)) |
40 actual_results = json_dict[JSONKEY_ACTUALRESULTS] | 102 actual_results = json_dict[JSONKEY_ACTUALRESULTS] |
41 return actual_results[JSONKEY_ACTUALRESULTS_FAILED] | 103 for label, accumulator in RESULTS_MAP.iteritems(): |
104 results = actual_results[label] | |
105 if results: | |
106 for result in results: | |
107 accumulator.AddResult(result) | |
108 print accumulator.GetSummaryLine() | |
109 failure = failure or accumulator.ShouldSignalFailure() | |
110 print '(results marked with [*] will cause nonzero return value)' | |
111 return failure | |
42 | 112 |
43 | 113 |
44 if '__main__' == __name__: | 114 if '__main__' == __name__: |
epoger
2013/05/17 17:56:45
Here are two sample runs:
epoger@wpgntat-ubiq141:
| |
45 if len(sys.argv) != 2: | 115 if len(sys.argv) != 2: |
46 raise Exception('usage: %s <input-json-filepath>' % sys.argv[0]) | 116 raise Exception('usage: %s <input-json-filepath>' % sys.argv[0]) |
47 Assert(sys.argv[1]) | 117 failure = Display(sys.argv[1]) |
borenet
2013/05/17 19:48:33
You could also do:
sys.exit(Display(sys.argv[1]))
epoger
2013/05/18 03:09:54
If it's OK with you, I would really rather keep th
borenet
2013/05/20 12:09:55
Yep! Now the only thing that's slightly confusing
epoger
2013/05/21 16:02:51
Good point. I inverted the meaning of the returne
| |
118 if failure: | |
119 exit(1) | |
120 else: | |
121 exit(0) | |
OLD | NEW |