Update V8 to version 4.7.44.
[chromium-blink-merge.git] / build / android / pylib / results / json_results.py
blob0056c4a665a6e1e9c721762785833fdb128fe144
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import json
7 from pylib.base import base_test_result
10 def GenerateResultsDict(test_run_result):
11 """Create a results dict from |test_run_result| suitable for writing to JSON.
12 Args:
13 test_run_result: a base_test_result.TestRunResults object.
14 Returns:
15 A results dict that mirrors the one generated by
16 base/test/launcher/test_results_tracker.cc:SaveSummaryAsJSON.
17 """
18 # Example json output.
19 # {
20 # "global_tags": [],
21 # "all_tests": [
22 # "test1",
23 # "test2",
24 # ],
25 # "disabled_tests": [],
26 # "per_iteration_data": [
27 # {
28 # "test1": [
29 # {
30 # "status": "SUCCESS",
31 # "elapsed_time_ms": 1,
32 # "output_snippet": "",
33 # "output_snippet_base64": "",
34 # "losless_snippet": "",
35 # },
36 # ],
37 # "test2": [
38 # {
39 # "status": "FAILURE",
40 # "elapsed_time_ms": 12,
41 # "output_snippet": "",
42 # "output_snippet_base64": "",
43 # "losless_snippet": "",
44 # },
45 # ],
46 # },
47 # ],
48 # }
50 assert isinstance(test_run_result, base_test_result.TestRunResults)
52 def status_as_string(s):
53 if s == base_test_result.ResultType.PASS:
54 return 'SUCCESS'
55 elif s == base_test_result.ResultType.SKIP:
56 return 'SKIPPED'
57 elif s == base_test_result.ResultType.FAIL:
58 return 'FAILURE'
59 elif s == base_test_result.ResultType.CRASH:
60 return 'CRASH'
61 elif s == base_test_result.ResultType.TIMEOUT:
62 return 'TIMEOUT'
63 elif s == base_test_result.ResultType.UNKNOWN:
64 return 'UNKNOWN'
66 def generate_iteration_data(t):
67 return {
68 t.GetName(): [
70 'status': status_as_string(t.GetType()),
71 'elapsed_time_ms': t.GetDuration(),
72 'output_snippet': '',
73 'losless_snippet': '',
74 'output_snippet_base64:': '',
79 all_tests_tuple, per_iteration_data_tuple = zip(
80 *[(t.GetName(), generate_iteration_data(t))
81 for t in test_run_result.GetAll()])
83 return {
84 'global_tags': [],
85 'all_tests': list(all_tests_tuple),
86 # TODO(jbudorick): Add support for disabled tests within base_test_result.
87 'disabled_tests': [],
88 'per_iteration_data': list(per_iteration_data_tuple),
92 def GenerateJsonResultsFile(test_run_result, file_path):
93 """Write |test_run_result| to JSON.
95 This emulates the format of the JSON emitted by
96 base/test/launcher/test_results_tracker.cc:SaveSummaryAsJSON.
98 Args:
99 test_run_result: a base_test_result.TestRunResults object.
100 file_path: The path to the JSON file to write.
102 with open(file_path, 'w') as json_result_file:
103 json_result_file.write(json.dumps(GenerateResultsDict(test_run_result)))
106 def ParseResultsFromJson(json_results):
107 """Creates a list of BaseTestResult objects from JSON.
109 Args:
110 json_results: A JSON dict in the format created by
111 GenerateJsonResultsFile.
114 def string_as_status(s):
115 if s == 'SUCCESS':
116 return base_test_result.ResultType.PASS
117 elif s == 'SKIPPED':
118 return base_test_result.ResultType.SKIP
119 elif s == 'FAILURE':
120 return base_test_result.ResultType.FAIL
121 elif s == 'CRASH':
122 return base_test_result.ResultType.CRASH
123 elif s == 'TIMEOUT':
124 return base_test_result.ResultType.TIMEOUT
125 else:
126 return base_test_result.ResultType.UNKNOWN
128 results_list = []
129 testsuite_runs = json_results['per_iteration_data']
130 for testsuite_run in testsuite_runs:
131 for test, test_runs in testsuite_run.iteritems():
132 results_list.extend(
133 [base_test_result.BaseTestResult(test,
134 string_as_status(tr['status']),
135 duration=tr['elapsed_time_ms'])
136 for tr in test_runs])
137 return results_list