dsh-flakefinder 0.4.23 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.en.md +4 -3
- package/README.md +4 -3
- package/cordis.patch.yml +10 -6
- package/docs/REFERENCE.en.md +4 -3
- package/docs/REFERENCE.zh.md +4 -3
- package/docs/USAGE.en.md +73 -22
- package/docs/USAGE.md +74 -40
- package/lib/adapters.d.ts +123 -0
- package/lib/adapters.js +377 -0
- package/lib/args.d.ts +2 -17
- package/lib/args.js +2 -112
- package/lib/classify.d.ts +61 -11
- package/lib/classify.js +561 -44
- package/lib/config.d.ts +26 -1
- package/lib/config.js +125 -4
- package/lib/index.d.ts +6 -4
- package/lib/index.js +46 -9
- package/lib/parse.d.ts +16 -6
- package/lib/parse.js +495 -28
- package/lib/runner.d.ts +29 -2
- package/lib/runner.js +221 -19
- package/lib/store.d.ts +87 -8
- package/lib/store.js +389 -35
- package/lib/tools.d.ts +4 -4
- package/lib/tools.js +691 -101
- package/lib/update-client.js +1 -1
- package/package.json +1 -1
package/lib/parse.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 测试报告解析:vitest / jest 的 JSON reporter
|
|
3
|
-
*
|
|
2
|
+
* 测试报告解析:vitest / jest 的 JSON reporter、pytest 的 JUnit XML、
|
|
3
|
+
* 与 node:test 的层级 TAP 13(含 YAML 诊断块)统一成一次运行的扁平用例列表。
|
|
4
4
|
*
|
|
5
5
|
* @module dsh-flakefinder/parse
|
|
6
6
|
*/
|
|
7
|
+
import { normalizeRelPath } from './config.js';
|
|
8
|
+
const ANSI_RE = /\x1b\[[0-9;]*[A-Za-z]/g;
|
|
9
|
+
const ERROR_CLASS_RE = /^([A-Z][A-Za-z0-9_.]*(?:Error|Exception|Failure))\b/;
|
|
10
|
+
const FILE_EXT_RE = /\.(?:[cm]?js|[cm]?ts|jsx|tsx)$/i;
|
|
11
|
+
function stripAnsi(text) {
|
|
12
|
+
return text.replace(ANSI_RE, '');
|
|
13
|
+
}
|
|
7
14
|
function asNumber(value) {
|
|
8
15
|
return typeof value === 'number' && Number.isFinite(value) ? value : null;
|
|
9
16
|
}
|
|
@@ -16,8 +23,57 @@ function normalizeStatus(value) {
|
|
|
16
23
|
return 'skipped';
|
|
17
24
|
return 'failed';
|
|
18
25
|
}
|
|
26
|
+
function inferErrorType(text) {
|
|
27
|
+
const cleaned = stripAnsi(text).trim();
|
|
28
|
+
if (cleaned === '')
|
|
29
|
+
return undefined;
|
|
30
|
+
const firstLine = cleaned.split(/\r?\n/)[0]?.trim() ?? '';
|
|
31
|
+
const cls = ERROR_CLASS_RE.exec(firstLine);
|
|
32
|
+
if (cls !== null && cls[1] !== 'Error')
|
|
33
|
+
return cls[1];
|
|
34
|
+
if (/timed?\s*out|\btimeout\b/i.test(firstLine))
|
|
35
|
+
return 'TimeoutError';
|
|
36
|
+
if (/^expect\(|\bassert(?:ion)?\b/i.test(firstLine))
|
|
37
|
+
return 'AssertionError';
|
|
38
|
+
if (cls !== null)
|
|
39
|
+
return cls[1];
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
function parseErrorText(raw, explicitType) {
|
|
43
|
+
const cleaned = stripAnsi(raw).replace(/\r\n/g, '\n').trim();
|
|
44
|
+
if (cleaned === '')
|
|
45
|
+
return undefined;
|
|
46
|
+
const lines = cleaned.split('\n');
|
|
47
|
+
const firstIdx = lines.findIndex(line => line.trim() !== '');
|
|
48
|
+
if (firstIdx === -1)
|
|
49
|
+
return undefined;
|
|
50
|
+
const firstLine = lines[firstIdx].trim();
|
|
51
|
+
const rest = lines.slice(firstIdx + 1).join('\n').trim();
|
|
52
|
+
const type = explicitType?.trim() || inferErrorType(firstLine);
|
|
53
|
+
return {
|
|
54
|
+
...(type !== undefined ? { type } : {}),
|
|
55
|
+
message: firstLine,
|
|
56
|
+
...(rest !== '' ? { stack: rest } : {}),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function uniqueId(baseId, seen, used) {
|
|
60
|
+
const count = (seen.get(baseId) ?? 0) + 1;
|
|
61
|
+
seen.set(baseId, count);
|
|
62
|
+
if (count === 1 && !used.has(baseId)) {
|
|
63
|
+
used.add(baseId);
|
|
64
|
+
return baseId;
|
|
65
|
+
}
|
|
66
|
+
let suffix = Math.max(count, 2);
|
|
67
|
+
let candidate = baseId + ' #' + String(suffix);
|
|
68
|
+
while (used.has(candidate)) {
|
|
69
|
+
suffix += 1;
|
|
70
|
+
candidate = baseId + ' #' + String(suffix);
|
|
71
|
+
}
|
|
72
|
+
used.add(candidate);
|
|
73
|
+
return candidate;
|
|
74
|
+
}
|
|
19
75
|
/** vitest 与 jest 的 JSON 结构高度接近,这里统一读取。 */
|
|
20
|
-
export function parseJsonReport(text, framework) {
|
|
76
|
+
export function parseJsonReport(text, framework, cwd) {
|
|
21
77
|
let root;
|
|
22
78
|
try {
|
|
23
79
|
root = JSON.parse(text);
|
|
@@ -28,8 +84,11 @@ export function parseJsonReport(text, framework) {
|
|
|
28
84
|
const obj = (root ?? {});
|
|
29
85
|
const suites = Array.isArray(obj.testResults) ? obj.testResults : [];
|
|
30
86
|
const tests = [];
|
|
87
|
+
const seenIds = new Map();
|
|
88
|
+
const usedIds = new Set();
|
|
31
89
|
for (const suite of suites) {
|
|
32
|
-
const
|
|
90
|
+
const rawFile = typeof suite.name === 'string' && suite.name !== '' ? suite.name : '(unknown)';
|
|
91
|
+
const file = rawFile === '(unknown)' ? rawFile : normalizeRelPath(rawFile, cwd);
|
|
33
92
|
const assertions = Array.isArray(suite.assertionResults) ? suite.assertionResults : [];
|
|
34
93
|
if (assertions.length > 0) {
|
|
35
94
|
for (const item of assertions) {
|
|
@@ -37,23 +96,36 @@ export function parseJsonReport(text, framework) {
|
|
|
37
96
|
? item.fullName
|
|
38
97
|
: (typeof item.title === 'string' ? item.title : '(unnamed)');
|
|
39
98
|
const status = normalizeStatus(item.status);
|
|
99
|
+
let error;
|
|
100
|
+
if (Array.isArray(item.failureMessages)) {
|
|
101
|
+
const messages = item.failureMessages
|
|
102
|
+
.filter((msg) => typeof msg === 'string' && msg.trim() !== '');
|
|
103
|
+
if (messages.length > 0) {
|
|
104
|
+
error = parseErrorText(messages.join('\n\n'));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
40
107
|
tests.push({
|
|
41
|
-
id: file + ' > ' + fullName,
|
|
108
|
+
id: uniqueId(file + ' > ' + fullName, seenIds, usedIds),
|
|
42
109
|
file,
|
|
43
110
|
name: fullName,
|
|
44
111
|
status,
|
|
45
112
|
durationMs: Math.max(0, asNumber(item.duration) ?? 0),
|
|
113
|
+
...(error !== undefined ? { error } : {}),
|
|
46
114
|
});
|
|
47
115
|
}
|
|
48
116
|
}
|
|
49
117
|
else {
|
|
50
118
|
const status = normalizeStatus(suite.status);
|
|
119
|
+
const error = typeof suite.message === 'string' && suite.message.trim() !== ''
|
|
120
|
+
? parseErrorText(suite.message)
|
|
121
|
+
: undefined;
|
|
51
122
|
tests.push({
|
|
52
|
-
id: file + ' > (suite)',
|
|
123
|
+
id: uniqueId(file + ' > (suite)', seenIds, usedIds),
|
|
53
124
|
file,
|
|
54
125
|
name: '(suite)',
|
|
55
126
|
status,
|
|
56
127
|
durationMs: 0,
|
|
128
|
+
...(error !== undefined ? { error } : {}),
|
|
57
129
|
});
|
|
58
130
|
}
|
|
59
131
|
}
|
|
@@ -73,8 +145,10 @@ function reportedCount(reported, fallback) {
|
|
|
73
145
|
return fallback;
|
|
74
146
|
}
|
|
75
147
|
function attrOf(tag, name) {
|
|
76
|
-
const m = new RegExp('(?:^|\\s)' + name + '
|
|
77
|
-
|
|
148
|
+
const m = new RegExp('(?:^|\\s)' + name + '\\s*=\\s*(?:"([^"]*)"|\'([^\']*)\')').exec(tag);
|
|
149
|
+
if (m === null)
|
|
150
|
+
return '';
|
|
151
|
+
return m[1] ?? m[2] ?? '';
|
|
78
152
|
}
|
|
79
153
|
/** 解码 XML 属性里的实体与数字字符引用。 */
|
|
80
154
|
function decodeXmlEntities(text) {
|
|
@@ -84,49 +158,442 @@ function decodeXmlEntities(text) {
|
|
|
84
158
|
if (key.startsWith('#')) {
|
|
85
159
|
const hex = key.startsWith('#x');
|
|
86
160
|
const code = parseInt(hex ? key.slice(2) : key.slice(1), hex ? 16 : 10);
|
|
87
|
-
return Number.isFinite(code) ? String.fromCodePoint(code) : raw;
|
|
161
|
+
return Number.isFinite(code) && code >= 0 && code <= 0x10FFFF ? String.fromCodePoint(code) : raw;
|
|
88
162
|
}
|
|
89
163
|
return named[key] ?? raw;
|
|
90
164
|
});
|
|
91
165
|
}
|
|
166
|
+
/** 解包 CDATA 段,并对 CDATA 之外的 XML 实体进行解码。 */
|
|
167
|
+
function decodeXmlText(raw) {
|
|
168
|
+
return raw.replace(/<!\[CDATA\[([\s\S]*?)\]\]>|([^<]+|<)/g, (_m, cdata, plain) => {
|
|
169
|
+
if (cdata !== undefined)
|
|
170
|
+
return cdata;
|
|
171
|
+
return decodeXmlEntities(plain ?? '');
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
function extractJunitError(body) {
|
|
175
|
+
const failMatch = /<(failure|error)\b([^>]*?)(?:\s*\/>|>([\s\S]*?)<\/\1\s*>)/i.exec(body);
|
|
176
|
+
if (failMatch === null)
|
|
177
|
+
return undefined;
|
|
178
|
+
const tagName = failMatch[1].toLowerCase();
|
|
179
|
+
const attrs = failMatch[2] ?? '';
|
|
180
|
+
const rawBody = failMatch[3] ?? '';
|
|
181
|
+
const attrType = decodeXmlEntities(attrOf(attrs, 'type')).trim() || undefined;
|
|
182
|
+
const attrMessage = decodeXmlEntities(attrOf(attrs, 'message')).trim();
|
|
183
|
+
let stackText = decodeXmlText(rawBody).trim();
|
|
184
|
+
if (stackText === '') {
|
|
185
|
+
const sysErr = /<system-err\b[^>]*>([\s\S]*?)<\/system-err\s*>/i.exec(body);
|
|
186
|
+
if (sysErr !== null) {
|
|
187
|
+
stackText = decodeXmlText(sysErr[1] ?? '').trim();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (attrMessage !== '') {
|
|
191
|
+
const type = attrType ?? inferErrorType(attrMessage) ?? inferErrorType(stackText);
|
|
192
|
+
return {
|
|
193
|
+
...(type !== undefined ? { type } : {}),
|
|
194
|
+
message: attrMessage,
|
|
195
|
+
...(stackText !== '' ? { stack: stackText } : {}),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
if (stackText !== '') {
|
|
199
|
+
return parseErrorText(stackText, attrType);
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
...(attrType !== undefined ? { type: attrType } : {}),
|
|
203
|
+
message: attrType ?? (tagName === 'error' ? 'Test error' : 'Test failed'),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
92
206
|
/** 解析 pytest 的 JUnit XML 报告。 */
|
|
93
|
-
export function parseJunitReport(text) {
|
|
207
|
+
export function parseJunitReport(text, cwd) {
|
|
208
|
+
if (!/<(?:testsuites|testsuite|testcase)\b/i.test(text)) {
|
|
209
|
+
throw new Error('pytest JUnit XML 报告格式无效或为空:未找到 testsuite/testcase 节点。');
|
|
210
|
+
}
|
|
211
|
+
const strippedCdata = text.replace(/<!\[CDATA\[[\s\S]*?\]\]>/g, '');
|
|
212
|
+
if (/<testsuite\b(?![^>]*\/>)/i.test(strippedCdata) && !/<\/testsuite\s*>/i.test(strippedCdata)) {
|
|
213
|
+
throw new Error('pytest JUnit XML 报告不完整或被截断:缺少 </testsuite> 闭合标签。');
|
|
214
|
+
}
|
|
215
|
+
if (/<testsuites\b(?![^>]*\/>)/i.test(strippedCdata) && !/<\/testsuites\s*>/i.test(strippedCdata)) {
|
|
216
|
+
throw new Error('pytest JUnit XML 报告不完整或被截断:缺少 </testsuites> 闭合标签。');
|
|
217
|
+
}
|
|
94
218
|
const tests = [];
|
|
219
|
+
const seenIds = new Map();
|
|
220
|
+
const usedIds = new Set();
|
|
95
221
|
// Match self-closing and paired nodes in one pass; separate regexes can let
|
|
96
222
|
// an empty node consume the closing tag of the following failed testcase.
|
|
97
223
|
const cases = /<testcase\b([^>]*?)(?:\s*\/>|>([\s\S]*?)<\/testcase\s*>)/gi;
|
|
98
224
|
for (const m of text.matchAll(cases)) {
|
|
99
225
|
const tag = m[1];
|
|
100
226
|
const body = m[2] ?? '';
|
|
101
|
-
const
|
|
227
|
+
const fileAttr = decodeXmlEntities(attrOf(tag, 'file')).trim();
|
|
228
|
+
const classname = decodeXmlEntities(attrOf(tag, 'classname')).trim();
|
|
229
|
+
const file = fileAttr !== ''
|
|
230
|
+
? normalizeRelPath(fileAttr, cwd)
|
|
231
|
+
: (classname !== '' ? classname : '(pytest)');
|
|
102
232
|
const name = decodeXmlEntities(attrOf(tag, 'name')) || '(unnamed)';
|
|
103
|
-
const
|
|
104
|
-
|
|
233
|
+
const error = extractJunitError(body);
|
|
234
|
+
const status = error !== undefined || /<(?:failure|error)\b/i.test(body)
|
|
235
|
+
? 'failed'
|
|
236
|
+
: /<skipped\b/i.test(body)
|
|
237
|
+
? 'skipped'
|
|
238
|
+
: 'passed';
|
|
239
|
+
tests.push({
|
|
240
|
+
id: uniqueId(file + ' > ' + name, seenIds, usedIds),
|
|
241
|
+
file,
|
|
242
|
+
name,
|
|
243
|
+
status,
|
|
244
|
+
durationMs: Math.max(0, Number(attrOf(tag, 'time')) || 0) * 1000,
|
|
245
|
+
...(status === 'failed' && error !== undefined ? { error } : {}),
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
const openedTestcases = (strippedCdata.match(/<testcase\b/gi) ?? []).length;
|
|
249
|
+
if (openedTestcases > tests.length) {
|
|
250
|
+
throw new Error('pytest JUnit XML 报告不完整或被截断:存在未闭合的 testcase 节点。');
|
|
105
251
|
}
|
|
106
252
|
const failed = tests.filter(item => item.status === 'failed').length;
|
|
107
253
|
const skipped = tests.filter(item => item.status === 'skipped').length;
|
|
108
254
|
return { framework: 'pytest', success: failed === 0, total: tests.length, passed: tests.length - failed - skipped, failed, skipped, tests };
|
|
109
255
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
256
|
+
function indentOf(line) {
|
|
257
|
+
return line.length - line.trimStart().length;
|
|
258
|
+
}
|
|
259
|
+
function parseYamlScalar(raw) {
|
|
260
|
+
const trimmed = raw.trim();
|
|
261
|
+
if (trimmed.length >= 2 && trimmed.startsWith("'") && trimmed.endsWith("'")) {
|
|
262
|
+
return trimmed
|
|
263
|
+
.slice(1, -1)
|
|
264
|
+
.replace(/''/g, "'")
|
|
265
|
+
.replace(/\\\\/g, '\\');
|
|
266
|
+
}
|
|
267
|
+
if (trimmed.length >= 2 && trimmed.startsWith('"') && trimmed.endsWith('"')) {
|
|
268
|
+
try {
|
|
269
|
+
const parsed = JSON.parse(trimmed);
|
|
270
|
+
if (typeof parsed === 'string')
|
|
271
|
+
return parsed;
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
// 回退到基础去引号。
|
|
275
|
+
}
|
|
276
|
+
return trimmed.slice(1, -1);
|
|
277
|
+
}
|
|
278
|
+
return trimmed;
|
|
279
|
+
}
|
|
280
|
+
function parseTapYamlBlock(lines, startIndex, caseIndent) {
|
|
281
|
+
const yaml = {};
|
|
282
|
+
if (startIndex >= lines.length)
|
|
283
|
+
return { yaml, nextIndex: startIndex };
|
|
284
|
+
const firstLine = lines[startIndex];
|
|
285
|
+
const openMatch = /^(\s+)---\s*$/.exec(firstLine);
|
|
286
|
+
if (openMatch === null || openMatch[1].length <= caseIndent) {
|
|
287
|
+
return { yaml, nextIndex: startIndex };
|
|
288
|
+
}
|
|
289
|
+
const yamlIndent = openMatch[1].length;
|
|
290
|
+
let i = startIndex + 1;
|
|
291
|
+
while (i < lines.length) {
|
|
292
|
+
const line = lines[i];
|
|
293
|
+
const trimmed = line.trim();
|
|
294
|
+
if (trimmed === '') {
|
|
295
|
+
i += 1;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
const lineIndent = indentOf(line);
|
|
299
|
+
if (trimmed === '...' && lineIndent <= yamlIndent) {
|
|
300
|
+
i += 1;
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
if (lineIndent <= caseIndent) {
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
const kv = /^(\s*)([A-Za-z_][A-Za-z0-9_-]*):\s*(.*)$/.exec(line);
|
|
307
|
+
if (kv === null || kv[1].length > yamlIndent + 1) {
|
|
308
|
+
i += 1;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const keyIndent = kv[1].length;
|
|
312
|
+
const key = kv[2];
|
|
313
|
+
const rest = kv[3].trim();
|
|
314
|
+
if (/^[|>][+-]?\d*$/.test(rest)) {
|
|
315
|
+
const folded = rest.startsWith('>');
|
|
316
|
+
const collected = [];
|
|
317
|
+
let j = i + 1;
|
|
318
|
+
while (j < lines.length) {
|
|
319
|
+
const sub = lines[j];
|
|
320
|
+
if (sub.trim() === '') {
|
|
321
|
+
collected.push('');
|
|
322
|
+
j += 1;
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
const subIndent = indentOf(sub);
|
|
326
|
+
if (subIndent <= keyIndent)
|
|
327
|
+
break;
|
|
328
|
+
collected.push(sub);
|
|
329
|
+
j += 1;
|
|
330
|
+
}
|
|
331
|
+
while (collected.length > 0 && collected[collected.length - 1] === '') {
|
|
332
|
+
collected.pop();
|
|
333
|
+
}
|
|
334
|
+
let minIndent = Infinity;
|
|
335
|
+
for (const row of collected) {
|
|
336
|
+
if (row.trim() !== '') {
|
|
337
|
+
const ind = indentOf(row);
|
|
338
|
+
if (ind < minIndent)
|
|
339
|
+
minIndent = ind;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const strip = Number.isFinite(minIndent) ? minIndent : 0;
|
|
343
|
+
const stripped = collected.map(row => (row.length >= strip ? row.slice(strip) : ''));
|
|
344
|
+
if (folded) {
|
|
345
|
+
const paragraphs = [];
|
|
346
|
+
let current = [];
|
|
347
|
+
for (const row of stripped) {
|
|
348
|
+
if (row === '') {
|
|
349
|
+
if (current.length > 0) {
|
|
350
|
+
paragraphs.push(current.join(' '));
|
|
351
|
+
current = [];
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
current.push(row);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (current.length > 0)
|
|
359
|
+
paragraphs.push(current.join(' '));
|
|
360
|
+
yaml[key] = paragraphs.join('\n\n');
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
yaml[key] = stripped.join('\n');
|
|
364
|
+
}
|
|
365
|
+
i = j;
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
if (rest === '') {
|
|
369
|
+
// 嵌套 YAML 对象(如 cause:),跳过其缩进子行。
|
|
370
|
+
let j = i + 1;
|
|
371
|
+
while (j < lines.length) {
|
|
372
|
+
const sub = lines[j];
|
|
373
|
+
if (sub.trim() === '') {
|
|
374
|
+
j += 1;
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
if (indentOf(sub) <= keyIndent)
|
|
378
|
+
break;
|
|
379
|
+
j += 1;
|
|
380
|
+
}
|
|
381
|
+
i = j;
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
yaml[key] = parseYamlScalar(rest);
|
|
385
|
+
i += 1;
|
|
386
|
+
}
|
|
387
|
+
return { yaml, nextIndex: i };
|
|
388
|
+
}
|
|
389
|
+
function buildTapError(yaml) {
|
|
390
|
+
const rawError = stripAnsi(yaml.error ?? '').trim();
|
|
391
|
+
const rawStack = stripAnsi(yaml.stack ?? '').trim();
|
|
392
|
+
const failureType = yaml.failureType?.trim() || undefined;
|
|
393
|
+
const explicitName = yaml.name?.trim() || undefined;
|
|
394
|
+
const code = yaml.code?.trim() || undefined;
|
|
395
|
+
const expected = yaml.expected?.trim();
|
|
396
|
+
const actual = yaml.actual?.trim();
|
|
397
|
+
const location = yaml.location?.trim();
|
|
398
|
+
if (rawError === '' &&
|
|
399
|
+
rawStack === '' &&
|
|
400
|
+
failureType === undefined &&
|
|
401
|
+
explicitName === undefined &&
|
|
402
|
+
code === undefined &&
|
|
403
|
+
expected === undefined &&
|
|
404
|
+
actual === undefined) {
|
|
405
|
+
return undefined;
|
|
406
|
+
}
|
|
407
|
+
let type = explicitName;
|
|
408
|
+
if (type === undefined && code === 'ERR_ASSERTION')
|
|
409
|
+
type = 'AssertionError';
|
|
410
|
+
if (type === undefined)
|
|
411
|
+
type = inferErrorType(rawError);
|
|
412
|
+
if (type === undefined && failureType !== undefined && /timeout/i.test(failureType))
|
|
413
|
+
type = 'TimeoutError';
|
|
414
|
+
if (type === undefined)
|
|
415
|
+
type = failureType;
|
|
416
|
+
let message = rawError;
|
|
417
|
+
if (message === '') {
|
|
418
|
+
message = rawStack.split(/\r?\n/)[0]?.trim() || failureType || code || 'Test failed';
|
|
419
|
+
}
|
|
420
|
+
const diffParts = [];
|
|
421
|
+
if (expected !== undefined && expected !== '')
|
|
422
|
+
diffParts.push('expected: ' + expected);
|
|
423
|
+
if (actual !== undefined && actual !== '')
|
|
424
|
+
diffParts.push('actual: ' + actual);
|
|
425
|
+
if (diffParts.length > 0 && !message.includes('expected:') && !message.includes('actual:')) {
|
|
426
|
+
message += ' (' + diffParts.join(', ') + ')';
|
|
427
|
+
}
|
|
428
|
+
const stack = rawStack !== '' ? rawStack : (location ? 'at ' + location : undefined);
|
|
429
|
+
return {
|
|
430
|
+
...(type !== undefined ? { type } : {}),
|
|
431
|
+
message,
|
|
432
|
+
...(stack !== undefined ? { stack } : {}),
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
function parseTapDuration(yaml) {
|
|
436
|
+
if (yaml.duration_ms === undefined)
|
|
437
|
+
return 0;
|
|
438
|
+
const num = Number(yaml.duration_ms);
|
|
439
|
+
return Number.isFinite(num) && num >= 0 ? num : 0;
|
|
440
|
+
}
|
|
441
|
+
/** 解析 node:test 的 TAP 13 输出:支持层级子测试(仅输出叶节点)与 YAML 诊断块。 */
|
|
442
|
+
export function parseTapReport(text, options) {
|
|
113
443
|
const lines = text.split(/\r?\n/);
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
444
|
+
const root = {
|
|
445
|
+
indent: -1,
|
|
446
|
+
name: '',
|
|
447
|
+
yaml: {},
|
|
448
|
+
children: [],
|
|
449
|
+
completed: false,
|
|
450
|
+
fromSubtestHeader: false,
|
|
451
|
+
};
|
|
452
|
+
const stack = [root];
|
|
453
|
+
const bailOuts = [];
|
|
454
|
+
let i = 0;
|
|
455
|
+
while (i < lines.length) {
|
|
456
|
+
const line = lines[i];
|
|
457
|
+
const trimmed = line.trim();
|
|
458
|
+
if (trimmed === '' || /^TAP version\b/i.test(trimmed) || /^\d+\.\.\d+(?:\s*#.*)?$/.test(trimmed)) {
|
|
459
|
+
i += 1;
|
|
118
460
|
continue;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
461
|
+
}
|
|
462
|
+
const bailMatch = /^Bail out!(?:\s+(.*))?$/i.exec(trimmed);
|
|
463
|
+
if (bailMatch !== null) {
|
|
464
|
+
bailOuts.push((bailMatch[1] ?? '').trim() || 'TAP Bail out!');
|
|
465
|
+
i += 1;
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
const indent = indentOf(line);
|
|
469
|
+
const subtestMatch = /^#\s*Subtest:\s*(.+?)\s*$/.exec(trimmed);
|
|
470
|
+
if (subtestMatch !== null) {
|
|
471
|
+
while (stack.length > 1 && stack[stack.length - 1].indent >= indent) {
|
|
472
|
+
stack.pop();
|
|
473
|
+
}
|
|
474
|
+
const node = {
|
|
475
|
+
indent,
|
|
476
|
+
name: subtestMatch[1],
|
|
477
|
+
yaml: {},
|
|
478
|
+
children: [],
|
|
479
|
+
completed: false,
|
|
480
|
+
fromSubtestHeader: true,
|
|
481
|
+
};
|
|
482
|
+
stack[stack.length - 1].children.push(node);
|
|
483
|
+
stack.push(node);
|
|
484
|
+
i += 1;
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
if (trimmed.startsWith('#')) {
|
|
488
|
+
i += 1;
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
const caseMatch = /^(not ok|ok)(?=\s|$)(?:\s+(\d+))?(?:\s+-\s*|\s+)?(.*)$/.exec(trimmed);
|
|
492
|
+
if (caseMatch === null) {
|
|
493
|
+
i += 1;
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
const failed = caseMatch[1] === 'not ok';
|
|
497
|
+
const rawDesc = (caseMatch[3] ?? '').trim();
|
|
498
|
+
const directiveMatch = /(?:^|\s)(?<!\\)#\s*(SKIP|TODO)\b.*$/i.exec(rawDesc);
|
|
499
|
+
const skipped = directiveMatch !== null;
|
|
500
|
+
const cleanName = (directiveMatch !== null ? rawDesc.slice(0, directiveMatch.index) : rawDesc)
|
|
501
|
+
.replace(/\\#/g, '#')
|
|
502
|
+
.trim() || '(unnamed)';
|
|
503
|
+
const status = skipped ? 'skipped' : failed ? 'failed' : 'passed';
|
|
504
|
+
while (stack.length > 1 && stack[stack.length - 1].indent > indent) {
|
|
505
|
+
stack.pop();
|
|
506
|
+
}
|
|
507
|
+
const top = stack[stack.length - 1];
|
|
508
|
+
let targetNode;
|
|
509
|
+
if (top.indent === indent && top.fromSubtestHeader && !top.completed) {
|
|
510
|
+
targetNode = top;
|
|
511
|
+
targetNode.status = status;
|
|
512
|
+
if (targetNode.name === '' && cleanName !== '(unnamed)')
|
|
513
|
+
targetNode.name = cleanName;
|
|
514
|
+
targetNode.completed = true;
|
|
515
|
+
stack.pop();
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
targetNode = {
|
|
519
|
+
indent,
|
|
520
|
+
name: cleanName,
|
|
521
|
+
status,
|
|
522
|
+
yaml: {},
|
|
523
|
+
children: [],
|
|
524
|
+
completed: true,
|
|
525
|
+
fromSubtestHeader: false,
|
|
526
|
+
};
|
|
527
|
+
top.children.push(targetNode);
|
|
528
|
+
}
|
|
529
|
+
const yamlResult = parseTapYamlBlock(lines, i + 1, indent);
|
|
530
|
+
targetNode.yaml = yamlResult.yaml;
|
|
531
|
+
i = yamlResult.nextIndex;
|
|
532
|
+
}
|
|
533
|
+
const defaultFile = options?.target !== undefined && options.target.trim() !== ''
|
|
534
|
+
? normalizeRelPath(options.target, options.cwd)
|
|
535
|
+
: '(node:test)';
|
|
536
|
+
const tests = [];
|
|
537
|
+
const seenIds = new Map();
|
|
538
|
+
const usedIds = new Set();
|
|
539
|
+
function emitLeaf(segments, status, yaml) {
|
|
540
|
+
let file = defaultFile;
|
|
541
|
+
let name;
|
|
542
|
+
if (segments.length >= 2 && FILE_EXT_RE.test(segments[0])) {
|
|
543
|
+
file = normalizeRelPath(segments[0], options?.cwd);
|
|
544
|
+
name = segments.slice(1).join(' > ');
|
|
545
|
+
}
|
|
546
|
+
else {
|
|
547
|
+
name = segments.join(' > ');
|
|
548
|
+
}
|
|
549
|
+
const error = status === 'failed' ? buildTapError(yaml) : undefined;
|
|
124
550
|
tests.push({
|
|
125
|
-
id:
|
|
126
|
-
file
|
|
551
|
+
id: uniqueId(file + ' > ' + name, seenIds, usedIds),
|
|
552
|
+
file,
|
|
127
553
|
name,
|
|
128
|
-
status
|
|
554
|
+
status,
|
|
555
|
+
durationMs: parseTapDuration(yaml),
|
|
556
|
+
...(error !== undefined ? { error } : {}),
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
function hasCompletedDescendant(node) {
|
|
560
|
+
for (const child of node.children) {
|
|
561
|
+
if (child.completed || hasCompletedDescendant(child))
|
|
562
|
+
return true;
|
|
563
|
+
}
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
function walk(nodes, prefix) {
|
|
567
|
+
let anyFailed = false;
|
|
568
|
+
for (const node of nodes) {
|
|
569
|
+
const pathSegments = [...prefix, node.name || '(unnamed)'];
|
|
570
|
+
if (hasCompletedDescendant(node)) {
|
|
571
|
+
const childFailed = walk(node.children, pathSegments);
|
|
572
|
+
if (childFailed)
|
|
573
|
+
anyFailed = true;
|
|
574
|
+
// 容器本身因 hookFailed 等原因失败但子测试全过时,补记一条容器失败以免被当成稳定通过。
|
|
575
|
+
if (node.completed && node.status === 'failed' && !childFailed && node.yaml.failureType !== 'subtestsFailed') {
|
|
576
|
+
emitLeaf([...pathSegments, '(hook)'], 'failed', node.yaml);
|
|
577
|
+
anyFailed = true;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
else if (node.completed && node.status !== undefined) {
|
|
581
|
+
emitLeaf(pathSegments, node.status, node.yaml);
|
|
582
|
+
if (node.status === 'failed')
|
|
583
|
+
anyFailed = true;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
return anyFailed;
|
|
587
|
+
}
|
|
588
|
+
walk(root.children, []);
|
|
589
|
+
for (const bailMsg of bailOuts) {
|
|
590
|
+
tests.push({
|
|
591
|
+
id: uniqueId(defaultFile + ' > (bail out)', seenIds, usedIds),
|
|
592
|
+
file: defaultFile,
|
|
593
|
+
name: '(bail out)',
|
|
594
|
+
status: 'failed',
|
|
129
595
|
durationMs: 0,
|
|
596
|
+
error: { type: 'BailOutError', message: bailMsg },
|
|
130
597
|
});
|
|
131
598
|
}
|
|
132
599
|
const failed = tests.filter(item => item.status === 'failed').length;
|
package/lib/runner.d.ts
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 进程执行层:复用 dsh-docker 同款 subprocess 包装,并按计划读取 JSON 报告。
|
|
2
|
+
* 进程执行层:复用 dsh-docker 同款 subprocess 包装,并按计划读取 JSON / JUnit / TAP 报告。
|
|
3
|
+
* 支持单轮超时错误区分、4MB 截断检测、以及受控并发与早停批次调度。
|
|
3
4
|
*
|
|
4
5
|
* @module dsh-flakefinder/runner
|
|
5
6
|
*/
|
|
6
7
|
import type { TestPlan } from './args.js';
|
|
8
|
+
import type { DetectMode } from './config.js';
|
|
7
9
|
import { type NormalizedRun } from './parse.js';
|
|
8
10
|
export interface RunResult {
|
|
9
11
|
exitCode: number | null;
|
|
10
12
|
signal: string | null;
|
|
11
13
|
stdout: string;
|
|
12
14
|
stderr: string;
|
|
15
|
+
truncated?: boolean;
|
|
13
16
|
}
|
|
14
17
|
export interface ProcessRunner {
|
|
15
18
|
run(argv: readonly string[], options?: {
|
|
@@ -27,11 +30,13 @@ export interface SubprocessHandleLike {
|
|
|
27
30
|
stdout?: {
|
|
28
31
|
readFrom(offset: number): {
|
|
29
32
|
text: string;
|
|
33
|
+
lossy?: boolean;
|
|
30
34
|
};
|
|
31
35
|
};
|
|
32
36
|
stderr?: {
|
|
33
37
|
readFrom(offset: number): {
|
|
34
38
|
text: string;
|
|
39
|
+
lossy?: boolean;
|
|
35
40
|
};
|
|
36
41
|
};
|
|
37
42
|
};
|
|
@@ -54,6 +59,13 @@ export interface SubprocessSpawnLike {
|
|
|
54
59
|
signal?: AbortSignal;
|
|
55
60
|
}): SubprocessHandleLike;
|
|
56
61
|
}
|
|
62
|
+
/** 单轮测试运行超时错误,与宿主 AbortSignal 取消严格区分。 */
|
|
63
|
+
export declare class FlakeTimeoutError extends Error {
|
|
64
|
+
readonly timeoutMs: number;
|
|
65
|
+
constructor(timeoutMs: number, message?: string);
|
|
66
|
+
}
|
|
67
|
+
/** 判断错误是否为单轮测试超时错误。 */
|
|
68
|
+
export declare function isTimeoutError(error: unknown): error is FlakeTimeoutError;
|
|
57
69
|
/** 用 DSH subprocess 服务构造 ProcessRunner。 */
|
|
58
70
|
export declare function createSubprocessRunner(spawn: SubprocessSpawnLike, graceMs: number, defaultTimeoutMs: number): ProcessRunner;
|
|
59
71
|
export interface FlakeRun {
|
|
@@ -62,7 +74,22 @@ export interface FlakeRun {
|
|
|
62
74
|
durationMs: number;
|
|
63
75
|
exitCode: number | null;
|
|
64
76
|
stderr: string;
|
|
77
|
+
timedOut?: boolean;
|
|
78
|
+
truncated?: boolean;
|
|
79
|
+
seed?: number;
|
|
65
80
|
error?: string;
|
|
66
81
|
}
|
|
67
|
-
|
|
82
|
+
export interface BatchOptions {
|
|
83
|
+
timeoutMs: number;
|
|
84
|
+
cwd?: string;
|
|
85
|
+
signal?: AbortSignal;
|
|
86
|
+
concurrency?: number;
|
|
87
|
+
mode?: DetectMode;
|
|
88
|
+
}
|
|
89
|
+
/** 执行一个测试计划并读取报告;进程失败但报告存在时仍返回报告,单轮超时返回结构化超时记录。 */
|
|
68
90
|
export declare function executePlan(runner: ProcessRunner, plan: TestPlan, index: number, timeoutMs: number, cwd?: string, signal?: AbortSignal): Promise<FlakeRun>;
|
|
91
|
+
/**
|
|
92
|
+
* 批量执行多个测试计划:支持 1-4 路并发 worker 池,以及 verify-fix / until-flaky 早停模式。
|
|
93
|
+
* 返回结果始终按 1-based `index` 升序排列;宿主 signal 取消时立刻中止并抛出取消原因。
|
|
94
|
+
*/
|
|
95
|
+
export declare function executePlansBatch(runner: ProcessRunner, plans: readonly TestPlan[], options: BatchOptions): Promise<FlakeRun[]>;
|