This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
convert_results.py
132 lines (109 loc) · 4.17 KB
/
convert_results.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This script converts JSON result files to Markdown summaries.
The JSON format is meant to contain all details from tests. For human readable
outputs, use this script to extract the most interest information from your JSON
reports and write them into an eye-friendly markdown file.
Typical usage example:
python convert_results.py --source=results/ --output=results/summaries/
"""
import argparse
from collections import defaultdict
import json
from pathlib import Path
import jinja2
from tabulate import tabulate
from tqdm.auto import tqdm
def summary_table(tests):
"""Creates a Markdown table summarizing passed tests for all tags.
Args:
tests: A list of tests, that have a 'result' entry, and possibly 'tags'.
The value of 'result' is 'pass' for a passing test.
If a test contains the key 'tags', it must be a list of strings.
Returns:
A string with a summary table in Markdown.
"""
tags = defaultdict(lambda: defaultdict(int))
for test in tests:
# The 'All' tag is added to count all test results.
for tag in test.get('tags', []) + ['All']:
if test['result'] == 'pass':
tags[tag]['pass'] += 1
tags[tag]['total'] += 1
headers = ['Category', 'Passed', 'Total']
table = []
for tag, counts in tags.items():
table.append([tag, counts['pass'], counts['total']])
return tabulate(table, headers, tablefmt='github')
def convert(result, template_file='template.md'):
"""Converts the result file JSON content to a Markdown text.
Args:
result: A string with the content of a JSON file output by the test tool.
template_file: Markdown template for jinja.
Returns:
A string with the text of a Markdown file.
"""
try:
result = json.loads(result)
except ValueError as _:
print('File is not valid JSON.')
return ''
for key in ['capabilities', 'device_under_test', 'tests']:
if key not in result:
print('JSON was missing the mandatory key {}.'.format(key))
return ''
capabilities = result['capabilities']
device_under_test = result['device_under_test']
tests = result['tests']
for test in tests:
if 'result' not in test:
test['result'] = 'fail'
failed_tests = [test for test in tests if test['result'] == 'fail']
summary = summary_table(tests)
loader = jinja2.FileSystemLoader(searchpath='./')
env = jinja2.Environment(loader=loader)
template = env.get_template(template_file)
return template.render(capabilities=capabilities,
device_under_test=device_under_test,
failed_tests=failed_tests,
summary=summary)
def main(args):
"""Converts all JSON files from the source directory to Markdown."""
output_path = Path(args.output_dir)
if output_path.exists():
print('Directory', args.output_dir, 'exists, files may be overwritten.')
else:
output_path.mkdir(parents=True)
paths = list(Path(args.source_dir).glob('*.json'))
progress_bar = tqdm(total=len(paths), unit='files')
for path in paths:
progress_bar.update()
output_file = output_path / path.with_suffix('.md').name
json_text = path.read_text()
output_file.write_text(convert(json_text))
if __name__ == '__main__':
main_parser = argparse.ArgumentParser()
main_parser.add_argument(
'--source',
default='results/',
dest='source_dir',
help=('Directory containing the test result files in JSON.'),
)
main_parser.add_argument(
'--output',
default='results/summaries/',
dest='output_dir',
help=('Directory for writing the converted Markdown files.'),
)
main(main_parser.parse_args())