-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytest-parser.js
More file actions
290 lines (240 loc) · 8.05 KB
/
Copy pathpytest-parser.js
File metadata and controls
290 lines (240 loc) · 8.05 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
function buildRawOutput(stdout = '', stderr = '') {
if (stdout && stderr) {
return `${stdout}\n${stderr}`;
}
return stdout || stderr || '';
}
function extractPytestSummary(output) {
const lines = output
.split(/\r?\n/)
.map((line) => line.trim())
.filter(Boolean);
const summaryLine = [...lines].reverse().find((line) => (
/^=+/.test(line) &&
/=+$/.test(line) &&
(/\bin [\d.]+s\b/.test(line) || /\bno tests ran\b/.test(line))
));
if (!summaryLine) {
return '';
}
return summaryLine.replace(/^=+\s*/, '').replace(/\s*=+$/, '');
}
function extractPytestCount(summary, labelPattern) {
const match = summary.match(new RegExp(`(\\d+) ${labelPattern}\\b`));
return match ? parseInt(match[1], 10) : 0;
}
function normalizePytestTarget(target = '') {
const trimmed = target.trim();
if (!trimmed) {
return '';
}
const segments = trimmed.split('::').filter(Boolean);
if (segments.length > 1) {
return segments[segments.length - 1].trim();
}
const pathSegments = trimmed.split(/[\\/]/).filter(Boolean);
return pathSegments[pathSegments.length - 1] || trimmed;
}
function extractPytestShortSummaryTarget(output, prefix) {
const line = output
.split(/\r?\n/)
.map((entry) => entry.trim())
.find((entry) => entry.startsWith(`${prefix} `));
return line ? normalizePytestTarget(line.slice(prefix.length + 1).trim()) : '';
}
function extractPytestErrorMessage(output, stderr = '') {
const combined = buildRawOutput(output, stderr);
const patterns = [
/^\s*E\s+([A-Za-z_.]+(?:Error|Exception): .+)$/m,
/^([A-Za-z_.]+(?:Error|Exception): .+)$/m,
/^(ImportError while importing test module .+)$/m,
/^\s*E\s+(.+)$/m,
];
for (const pattern of patterns) {
const match = combined.match(pattern);
if (match) {
return match[1].trim();
}
}
return 'Pytest error during collection or execution';
}
function extractPytestFailureSection(stdout = '') {
const failureSectionMatch = stdout.match(
/={10,}\s+FAILURES\s+={10,}\n([\s\S]*?)(?=\n={10,}\s+(?:short test summary info|ERRORS)\s+={10,}|\n={10,}\s+\d+ .+? in [\d.]+s\s+={10,}|$)/i
);
return failureSectionMatch ? failureSectionMatch[1] : '';
}
function extractPytestFailureBlocks(stdout = '') {
const failureSection = extractPytestFailureSection(stdout);
if (!failureSection) {
return [];
}
const blockRegex = /_{5,}\s*(.*?)\s*_{5,}\n([\s\S]*?)(?=\n_{5,}\s*.*?\s*_{5,}\n|$)/g;
const blocks = [];
for (const match of failureSection.matchAll(blockRegex)) {
blocks.push({
title: normalizePytestTarget(match[1] || ''),
body: match[2] || '',
});
}
return blocks;
}
function findPytestComparison(expression) {
const operators = [' is not ', ' is ', '=='];
const openingBrackets = new Set(['(', '[', '{']);
const closingBrackets = new Set([')', ']', '}']);
let quote = '';
let escaped = false;
let depth = 0;
let comparison = null;
for (let index = 0; index < expression.length; index += 1) {
const character = expression[index];
if (quote) {
if (escaped) {
escaped = false;
} else if (character === '\\') {
escaped = true;
} else if (character === quote) {
quote = '';
}
continue;
}
if (character === "'" || character === '"') {
quote = character;
continue;
}
if (openingBrackets.has(character)) {
depth += 1;
continue;
}
if (closingBrackets.has(character)) {
depth = Math.max(0, depth - 1);
continue;
}
const operator = operators.find((candidate) => expression.startsWith(candidate, index));
if (operator && (!comparison || depth < comparison.depth)) {
comparison = { index, operator, depth };
}
}
return comparison;
}
function extractPytestAssertionDetails(body = '') {
const sourceLineMatch = body.match(/^\s*>\s*(.+)$/m);
const errorLines = [...body.matchAll(/^\s*E\s+(.+)$/gm)]
.map((match) => match[1].trim())
.filter((line) => line && !/^\+\s+where\b/.test(line));
const technicalLine = errorLines.find((line) => (
/^(?:AssertionError:\s+)?assert\s+/.test(line)
)) || '';
const expressionMatch = technicalLine.match(/^(?:AssertionError:\s+)?assert\s+(.+)$/);
if (!expressionMatch) {
return null;
}
const expression = expressionMatch[1].trim();
const comparison = findPytestComparison(expression);
if (!comparison) {
return null;
}
const receivedSide = expression.slice(0, comparison.index);
const expectedSide = expression.slice(comparison.index + comparison.operator.length);
return {
assertionLine: sourceLineMatch ? sourceLineMatch[1].trim() : '',
technicalLine,
expected: expectedSide.trim(),
received: receivedSide.trim(),
};
}
function extractPytestAssertionMessage(body = '') {
const errorLines = [...body.matchAll(/^\s*E\s+(.+)$/gm)]
.map((match) => match[1].trim())
.filter((line) => line && !/^\+\s+where\b/.test(line));
return errorLines.find((line) => /^AssertionError:/i.test(line)) || '';
}
function extractPytestFailureDetail(block, rawout) {
const sourceLineMatch = block.body.match(/^\s*>\s*(.+)$/m);
const errorLines = [...block.body.matchAll(/^\s*E\s+(.+)$/gm)]
.map((match) => match[1].trim())
.filter((line) => line && !/^\+\s+where\b/.test(line));
const technicalLine = errorLines[errorLines.length - 1] || '';
const assertionDetails = extractPytestAssertionDetails(block.body);
const assertionMessage = extractPytestAssertionMessage(block.body);
if (assertionDetails) {
return {
test_case: block.title || 'pytest assertion failure',
expected: assertionDetails.expected,
received: assertionDetails.received,
error_message: assertionMessage || `Assertion failed: ${assertionDetails.assertionLine || assertionDetails.technicalLine}`,
diagnostic: block.body.trim(),
rawout,
};
}
return {
test_case: block.title || 'pytest failure',
expected: '',
received: '',
error_message: assertionMessage || technicalLine || (sourceLineMatch ? `Assertion failed: ${sourceLineMatch[1].trim()}` : 'Pytest reported a failure'),
diagnostic: block.body.trim(),
rawout,
};
}
function parsePytestOutput(stdout = '', stderr = '', exitCode = null) {
const summary = extractPytestSummary(stdout);
const rawout = buildRawOutput(stdout, stderr);
const passed_tests = extractPytestCount(summary, 'passed');
const failed_tests = extractPytestCount(summary, 'failed');
const errors = extractPytestCount(summary, 'error(?:s)?');
const no_tests_collected = exitCode === 5 || /\bno tests ran\b/.test(summary);
const failures = [];
const failureBlocks = extractPytestFailureBlocks(stdout);
failureBlocks.forEach((block) => {
failures.push(extractPytestFailureDetail(block, rawout));
});
if (failed_tests > 0 && failures.length === 0) {
failures.push({
test_case: extractPytestShortSummaryTarget(stdout, 'FAILED') || 'pytest assertion failure',
expected: '',
received: '',
error_message: extractPytestErrorMessage(stdout, stderr),
rawout,
});
}
let runtime_error = '';
if (errors > 0) {
runtime_error = extractPytestErrorMessage(stdout, stderr);
failures.push({
test_case: extractPytestShortSummaryTarget(stdout, 'ERROR') || 'pytest collection/execution',
expected: '',
received: '',
error_message: runtime_error,
rawout,
});
} else if (no_tests_collected) {
runtime_error = 'Pytest did not collect any tests';
failures.push({
test_case: 'pytest collection',
expected: 'at least 1 collected test',
received: '0 collected tests',
error_message: runtime_error,
rawout,
});
}
return {
tests_run: passed_tests + failed_tests,
passed: passed_tests,
failed: failed_tests,
errors,
no_tests_collected,
exit_code: exitCode,
failure_details: failures,
runtime_error,
};
}
module.exports = {
buildRawOutput,
extractPytestSummary,
extractPytestCount,
extractPytestShortSummaryTarget,
extractPytestErrorMessage,
extractPytestFailureBlocks,
parsePytestOutput,
};