-
Notifications
You must be signed in to change notification settings - Fork 85
/
structures.py
268 lines (220 loc) · 7.99 KB
/
structures.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
""" Utilities for reasoning about Galaxy test results.
"""
from __future__ import print_function
from __future__ import absolute_import
import os
from collections import namedtuple
import json
import xml.etree.ElementTree as ET
from planemo.io import error
RUN_TESTS_CMD = (
"sh run_tests.sh $COMMON_STARTUP_ARGS --report_file %s %s %s %s"
)
class GalaxyTestCommand(object):
def __init__(
self,
html_report_file,
xunit_report_file,
structured_report_file,
failed=False,
installed=False,
):
self.html_report_file = html_report_file
self.xunit_report_file = xunit_report_file
self.structured_report_file = structured_report_file
self.failed = failed
self.installed = installed
def build(self):
xunit_report_file = self.xunit_report_file
sd_report_file = self.structured_report_file
html_report_file = self.html_report_file
if xunit_report_file:
xunit_arg = "--xunit_report_file %s" % xunit_report_file
else:
xunit_arg = ""
if sd_report_file:
sd_arg = "--structured_data_report_file %s" % sd_report_file
else:
sd_arg = ""
if self.installed:
tests = "-installed"
else:
tests = "functional.test_toolbox"
if self.failed:
sd = StructuredData(self.structured_report_file)
failed_ids = sd.failed_ids
tests = " ".join(failed_ids)
return RUN_TESTS_CMD % (html_report_file, xunit_arg, sd_arg, tests)
class StructuredData(object):
""" Abstraction around Galaxy's structured test data output.
"""
def __init__(self, json_path):
self.json_path = json_path
if not os.path.exists(json_path):
error("Warning: Problem with target Galaxy, it did not "
"produce a structured test results files - summary "
"information and planemo reports will be incorrect."
)
else:
try:
with open(json_path, "r") as output_json_f:
structured_data = json.load(output_json_f)
structured_data_tests = structured_data["tests"]
except Exception:
error("Galaxy produced invalid JSON for structured data - summary "
"information and planemo reports will be incorrect.")
structured_data = {}
structured_data_tests = {}
self.structured_data = structured_data
self.structured_data_tests = structured_data_tests
structured_data_by_id = {}
for test in self.structured_data_tests:
structured_data_by_id[test["id"]] = test["data"]
self.structured_data_by_id = structured_data_by_id
self.has_details = "summary" in structured_data
if self.has_details:
self.read_summary()
def update(self):
with open(self.json_path, "w") as out_f:
json.dump(self.structured_data, out_f)
def set_exit_code(self, exit_code):
self.structured_data["exit_code"] = exit_code
def merge_xunit(self, xunit_root):
self.has_details = True
xunit_attrib = xunit_root.attrib
num_tests = int(xunit_attrib.get("tests", 0))
num_failures = int(xunit_attrib.get("failures", 0))
num_errors = int(xunit_attrib.get("errors", 0))
num_skips = int(xunit_attrib.get("skips", 0))
summary = dict(
num_tests=num_tests,
num_failures=num_failures,
num_errors=num_errors,
num_skips=num_skips,
)
self.structured_data["summary"] = summary
for testcase_el in xunit_t_elements_from_root(xunit_root):
test = case_id(testcase_el)
test_data = self.structured_data_by_id.get(test.id)
if not test_data:
continue
problem_el = None
for problem_type in ["skip", "failure", "error"]:
problem_el = testcase_el.find(problem_type)
if problem_el is not None:
break
if problem_el is not None:
status = problem_el.tag
test_data["problem_type"] = problem_el.attrib["type"]
test_data["problem_log"] = problem_el.text
else:
status = "success"
test_data["status"] = status
def read_summary(self):
summary = self.structured_data["summary"]
num_tests = summary["num_tests"]
num_failures = summary["num_failures"]
num_skips = summary["num_skips"]
num_errors = summary["num_errors"]
self.num_tests = num_tests
self.num_problems = num_skips + num_errors + num_failures
self.exit_code = summary["exit_code"]
@property
def failed_ids(self):
ids = set([])
for test_data in self.structured_data_tests:
if test_data["data"]["status"] == "success":
continue
test_case = test_data["id"].replace(".test_toolbox.", ".test_toolbox:")
ids.add(test_case)
return ids
class GalaxyTestResults(object):
""" Class that combine the test-centric xunit output
with the Galaxy centric structured data output - and
abstracts away the difference (someday).
"""
def __init__(
self,
output_json_path,
output_xml_path,
output_html_path,
exit_code,
):
self.output_html_path = output_html_path
self.exit_code = exit_code
sd = StructuredData(output_json_path)
self.sd = sd
self.structured_data = sd.structured_data
self.structured_data_tests = sd.structured_data_tests
self.structured_data_by_id = sd.structured_data_by_id
if output_xml_path:
self.xunit_tree = parse_xunit_report(output_xml_path)
sd.merge_xunit(self._xunit_root)
else:
self.xunit_tree = ET.fromstring("<testsuite />")
self.sd.set_exit_code(exit_code)
self.sd.read_summary()
self.sd.update()
@property
def exit_code(self):
return self.sd.exit_code
@property
def has_details(self):
return self.sd.has_details
@property
def num_tests(self):
return self.sd.num_tests
@property
def num_problems(self):
return self.sd.num_problems
@property
def _xunit_root(self):
return self.xunit_tree.getroot()
@property
def all_tests_passed(self):
return self.sd.num_problems == 0
@property
def xunit_testcase_elements(self):
return xunit_t_elements_from_root(self._xunit_root)
def xunit_t_elements_from_root(xunit_root):
for testcase_el in find_cases(xunit_root):
yield testcase_el
def parse_xunit_report(xunit_report_path):
return ET.parse(xunit_report_path)
def find_cases(xunit_root):
return xunit_root.findall("testcase")
def case_id(testcase_el):
name_raw = testcase_el.attrib["name"]
if "TestForTool_" in name_raw:
raw_id = name_raw
else:
class_name = testcase_el.attrib["classname"]
raw_id = "{0}.{1}".format(class_name, name_raw)
name = None
num = None
if "TestForTool_" in raw_id:
tool_and_num = raw_id.split("TestForTool_")[-1]
if ".test_tool_" in tool_and_num:
name, num_str = tool_and_num.split(".test_tool_", 1)
num = _parse_num(num_str)
# Tempted to but something human friendly in here like
# num + 1 - but then it doesn't match HTML report.
else:
name = tool_and_num
else:
name = raw_id
return TestId(name, num, raw_id)
def _parse_num(num_str):
try:
num = int(num_str)
except ValueError:
num = None
return num
TestId = namedtuple("TestId", ["name", "num", "id"])
@property
def _label(self):
if self.num is not None:
return "{0}[{1}]".format(self.name, self.num)
else:
return self.id
TestId.label = _label