forked from ScaCap/action-surefire-report
-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.js
226 lines (178 loc) · 6.99 KB
/
utils.js
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
const glob = require('@actions/glob');
const core = require('@actions/core');
const fs = require('fs');
const parser = require('xml-js');
const resolveFileAndLine = (clazz, method, stacktrace) => {
const regexp = new RegExp(`${clazz.replace(/\./g, "\\.")}\\.${method}\\((.*?):(\\d*?)\\)`, 'g');
const matches = [...stacktrace.matchAll(regexp)];
if (!matches.length) return { filename: clazz.replace(/\./g, "/"), line: 1 };
const [lastMatch] = matches.slice(-1);
const filename = clazz.replace(/\./g, "/");
const line = parseInt(lastMatch[2]);
return { filename, line };
};
const partition = (items, maxSize) => {
let result = [[]];
let buckets = Math.ceil(items.length / maxSize);
items.forEach((value, index) => {
let bucket = index % buckets;
if (typeof result[bucket] === 'undefined') {
result[bucket] = [];
}
result[bucket].push(value);
});
return result;
}
const unique = (items, hashFn) => {
let hashes = new Map();
let result = [];
items.forEach((value) => {
const hash = hashFn(value);
if (!hashes.has(hash)) {
result.push(value);
hashes.set(hash, true);
}
});
return result;
};
const resolvePath = async filename => {
core.debug(`Resolving path for ${filename}`);
const globber = await glob.create(`**/${filename}.*`, { followSymbolicLinks: false });
const results = await globber.glob();
core.debug(`Matched files: ${results}`);
const searchPath = globber.getSearchPaths()[0];
const path = results.length ? results[0].slice(searchPath.length + 1) : filename;
core.debug(`Resolved path: ${path}`);
return path;
};
const formatMilliseconds = milliseconds => {
let hour, minute, seconds;
if (milliseconds < 1000) {
return `${milliseconds}ms`;
}
seconds = Math.floor(milliseconds / 1000);
minute = Math.floor(seconds / 60);
seconds = seconds % 60;
hour = Math.floor(minute / 60);
minute = minute % 60;
return [
{val: hour, unit: 'h'},
{val: minute, unit: 'm'},
{val: seconds, unit: 's'}
]
.filter(value => value.val > 0)
.map(value => `${value.val}${value.unit}`)
.join(' ');
}
const toArray = value => {
return Array.isArray(value) ? value : [value];
}
const extractValue = value => {
return typeof value._cdata !== 'undefined' ? value._cdata : value;
}
const className = value => {
return value.split(".").slice(-1)[0];
}
const getGroups = (groups, signature) => {
let result = [];
toArray(groups).forEach((element) => {
if (element.signatures.indexOf(signature) >= 0) {
result.push(element.name);
}
});
return result.length > 0 ? `[groups: ${result.join(", ")}]` : '';
}
const getMethodParams = (method) => {
let result = [];
if (typeof method.params !== "undefined") {
result = toArray(method.params.param)
.map(param => `${param._attributes.index}: ${extractValue(param.value)}`);
}
return result.length > 0 ? `(${result.join(", ")})` : '';
}
async function parseFile(file) {
core.debug(`Parsing file ${file}`);
let annotations = [];
const data = await fs.promises.readFile(file);
const report = JSON.parse(parser.xml2json(data, { compact: true }));
const results = report["testng-results"];
const total = parseInt(results._attributes.total) || 0;
const passed = parseInt(results._attributes.passed) || 0;
const failed = parseInt(results._attributes.failed) || 0;
const skipped = parseInt(results._attributes.skipped) || 0;
const ignored = parseInt(results._attributes.ignored) || 0;
const clazzes = toArray(results.suite)
.map(suite => toArray(suite.test))
.flat(1)
.map(test => toArray(test.class))
.flat(1);
const groups = toArray(results.suite)
.map(suite => toArray(suite.groups))
.flat(1)
.filter(group => Object.keys(group).length > 0)
.map(group => toArray(group.group))
.flat(1)
.map(group => ({ name: group._attributes.name, signatures: toArray(group.method).map(method => `${method._attributes.class}.${method._attributes.name}`) } ));
const durationMs = toArray(results.suite)
.map(suite => parseInt(suite._attributes["duration-ms"]) || 0)
.reduce((a, b) => a + b, 0);
for (const clazz of clazzes) {
const methods = toArray(clazz["test-method"]);
core.debug(`Found ${methods.length} methods in test class ${clazz._attributes.name}`);
for (const method of methods) {
if (method._attributes.status == "FAIL") {
const stackTrace = extractValue(method.exception["full-stacktrace"]).trim();
const message = extractValue(method.exception.message).trim();
const { filename, line } = resolveFileAndLine(
clazz._attributes.name,
method._attributes.name,
stackTrace
);
const path = await resolvePath(filename);
const matchedGroups = getGroups(groups, `${clazz._attributes.name}.${method._attributes.name}`);
const methodParams = getMethodParams(method);
const title = `${className(clazz._attributes.name)} > ${method._attributes.name}${methodParams}${matchedGroups ? ' ' + matchedGroups : ''}`;
core.info(`${path}:${line} | ${message.replace(/\n/g, ' ')}`);
annotations.push({
path,
start_line: line,
end_line: line,
start_column: 0,
end_column: 0,
annotation_level: 'failure',
title,
message,
raw_details: stackTrace
});
}
}
}
return { total, passed, failed, ignored, skipped, annotations, durationMs };
}
const parseTestReports = async (reportPaths, showSkipped) => {
const globber = await glob.create(reportPaths, { followSymbolicLinks: false });
let annotations = [];
let passed = 0;
let failed = 0;
let ignored = 0;
let skipped = 0;
let durationMs = 0;
let errors = [];
for await (const file of globber.globGenerator()) {
try {
const { passed: p, failed: f, ignored: i, skipped: s, annotations: a, durationMs: d } = await parseFile(file);
passed += p;
failed += f;
ignored += showSkipped ? i : 0;
skipped += showSkipped ? s : 0;
annotations = annotations.concat(a);
durationMs += d;
}
catch (e) {
errors.push({file: file, error: e});
}
}
const total = passed + failed + ignored + skipped;
return { total, passed, failed, ignored, skipped, annotations, durationMs, errors };
};
module.exports = { resolveFileAndLine, resolvePath, parseFile, parseTestReports, formatMilliseconds, partition, unique };