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/lib/config.d.ts CHANGED
@@ -1,14 +1,22 @@
1
1
  /**
2
- * dsh-flakefinder 配置解析:重复次数、超时、审批策略与存储路径。
2
+ * dsh-flakefinder 配置解析:重复次数、并发、超时、审批策略与存储路径。
3
3
  *
4
4
  * @module dsh-flakefinder/config
5
5
  */
6
+ /** flaky_detect 执行模式。 */
7
+ export type DetectMode = 'full' | 'until-flaky' | 'verify-fix';
6
8
  /** 插件行配置(cordis.patch.yml 里的 config 段,可缺省)。 */
7
9
  export interface FlakeConfig {
8
10
  /** flaky_detect 默认重复次数(3-20)。 */
9
11
  defaultRuns?: number;
10
12
  /** 单次运行允许的最大重复次数(3-50,默认 20)。 */
11
13
  maxRuns?: number;
14
+ /** flaky_detect 默认并发数(1-4,默认 1)。 */
15
+ defaultConcurrency?: number;
16
+ /** 默认禁用覆盖率采集以加速复跑(默认 true)。 */
17
+ noCoverage?: boolean;
18
+ /** 隔离清单条目的过期提醒天数(1-365,默认 14)。 */
19
+ staleQuarantineDays?: number;
12
20
  /** 单次测试运行的超时(毫秒,默认 120000)。 */
13
21
  timeoutMs?: number;
14
22
  /** 超时后给测试进程的宽限时间(毫秒,默认 10000)。 */
@@ -26,6 +34,9 @@ export interface FlakeConfig {
26
34
  export interface ResolvedFlakeConfig {
27
35
  defaultRuns: number;
28
36
  maxRuns: number;
37
+ defaultConcurrency: number;
38
+ noCoverage: boolean;
39
+ staleQuarantineDays: number;
29
40
  timeoutMs: number;
30
41
  graceMs: number;
31
42
  writeApproval: boolean;
@@ -33,11 +44,16 @@ export interface ResolvedFlakeConfig {
33
44
  quarantineFile: string;
34
45
  quarantineFileRelative?: string;
35
46
  pythonPath: string;
47
+ pythonPathExplicit?: boolean;
36
48
  }
37
49
  /** 解析并校验配置,非法值抛出中文错误。 */
38
50
  export declare function resolveConfig(config: FlakeConfig | undefined | null, cwd?: string): ResolvedFlakeConfig;
39
51
  /** 解析并钳制 flaky_detect 的 runs 参数。 */
40
52
  export declare function resolveRuns(raw: unknown, cfg: ResolvedFlakeConfig): number;
53
+ /** 解析并校验 flaky_detect 的并发数(1-4)。 */
54
+ export declare function resolveConcurrency(raw: unknown, cfg?: Pick<ResolvedFlakeConfig, 'defaultConcurrency'>): number;
55
+ /** 解析 flaky_detect 的执行模式;默认 full。 */
56
+ export declare function readDetectMode(raw: unknown): DetectMode;
41
57
  /** 校验测试目标:非空且不能以 - 开头,避免被测试框架解析成选项。 */
42
58
  export declare function assertTarget(value: string): string;
43
59
  /** 从参数里取字符串;不识别类型时返回 undefined。 */
@@ -46,3 +62,12 @@ export declare function optionalString(args: Record<string, unknown>, key: strin
46
62
  export declare function requiredString(args: Record<string, unknown>, key: string, label: string): string;
47
63
  /** 从参数里取整数;缺失用默认值,非法抛中文错误。 */
48
64
  export declare function optionalInteger(args: Record<string, unknown>, key: string, label: string, lo: number, hi: number, fallback: number): number;
65
+ /** 从参数里取布尔值;缺失用默认值,非法类型抛中文错误。 */
66
+ export declare function optionalBoolean(args: Record<string, unknown>, key: string, fallback?: boolean): boolean;
67
+ /** 从参数里取随机种子(0-2147483647 整数);缺失返回 undefined,非法抛中文错误。 */
68
+ export declare function optionalSeed(args: Record<string, unknown>, key: string): number | undefined;
69
+ /**
70
+ * 将测试文件路径规范化为 POSIX 相对路径(使用 `/`、去掉前导 `./`);
71
+ * 若提供 `cwd` 且路径位于 `cwd` 之内,转为相对 `cwd` 的路径。
72
+ */
73
+ export declare function normalizeRelPath(filePath: string, cwd?: string): string;
package/lib/config.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * dsh-flakefinder 配置解析:重复次数、超时、审批策略与存储路径。
2
+ * dsh-flakefinder 配置解析:重复次数、并发、超时、审批策略与存储路径。
3
3
  *
4
4
  * @module dsh-flakefinder/config
5
5
  */
@@ -7,6 +7,11 @@ import { homedir } from 'node:os';
7
7
  import path from 'node:path';
8
8
  const MIN_RUNS = 3;
9
9
  const MAX_RUNS = 50;
10
+ const MIN_CONCURRENCY = 1;
11
+ const MAX_CONCURRENCY = 4;
12
+ const MIN_STALE_DAYS = 1;
13
+ const MAX_STALE_DAYS = 365;
14
+ const MAX_SEED = 2147483647;
10
15
  /** DSH_HOME 缺省时退回用户目录下的 .dsh。 */
11
16
  function defaultDataDir() {
12
17
  const home = process.env.DSH_HOME?.trim();
@@ -25,6 +30,15 @@ export function resolveConfig(config, cwd = process.cwd()) {
25
30
  }
26
31
  if (defaultRuns > maxRuns)
27
32
  defaultRuns = maxRuns;
33
+ let defaultConcurrency = 1;
34
+ if (cfg.defaultConcurrency !== undefined) {
35
+ defaultConcurrency = readRuns('defaultConcurrency', cfg.defaultConcurrency, MIN_CONCURRENCY, MAX_CONCURRENCY);
36
+ }
37
+ const noCoverage = cfg.noCoverage !== false;
38
+ let staleQuarantineDays = 14;
39
+ if (cfg.staleQuarantineDays !== undefined) {
40
+ staleQuarantineDays = readRuns('staleQuarantineDays', cfg.staleQuarantineDays, MIN_STALE_DAYS, MAX_STALE_DAYS);
41
+ }
28
42
  let timeoutMs = 120000;
29
43
  if (cfg.timeoutMs !== undefined) {
30
44
  if (typeof cfg.timeoutMs !== 'number' || !Number.isFinite(cfg.timeoutMs) || cfg.timeoutMs <= 0) {
@@ -46,11 +60,28 @@ export function resolveConfig(config, cwd = process.cwd()) {
46
60
  const quarantineFile = typeof cfg.quarantineFile === 'string' && cfg.quarantineFile.trim() !== ''
47
61
  ? path.resolve(cwd, cfg.quarantineFile.trim())
48
62
  : path.join(cwd, '.flakefinder.json');
49
- const pythonPath = typeof cfg.pythonPath === 'string' && cfg.pythonPath.trim() !== ''
63
+ const explicitConfigPython = typeof cfg.pythonPath === 'string' && cfg.pythonPath.trim() !== ''
50
64
  ? cfg.pythonPath.trim()
51
- : (process.env.DSH_FLAKEFINDER_PYTHON?.trim() || (process.platform === 'win32' ? 'python' : 'python3'));
65
+ : undefined;
66
+ const explicitEnvPython = process.env.DSH_FLAKEFINDER_PYTHON?.trim() || undefined;
67
+ const pythonPathExplicit = explicitConfigPython !== undefined || explicitEnvPython !== undefined;
68
+ const pythonPath = explicitConfigPython ?? explicitEnvPython ?? (process.platform === 'win32' ? 'python' : 'python3');
52
69
  const relativeQuarantine = typeof cfg.quarantineFile === 'string' && cfg.quarantineFile.trim() ? cfg.quarantineFile.trim() : '.flakefinder.json';
53
- return { defaultRuns, maxRuns, timeoutMs, graceMs, writeApproval, dataDir, quarantineFile, pythonPath, ...(path.isAbsolute(relativeQuarantine) ? {} : { quarantineFileRelative: relativeQuarantine }) };
70
+ return {
71
+ defaultRuns,
72
+ maxRuns,
73
+ defaultConcurrency,
74
+ noCoverage,
75
+ staleQuarantineDays,
76
+ timeoutMs,
77
+ graceMs,
78
+ writeApproval,
79
+ dataDir,
80
+ quarantineFile,
81
+ pythonPath,
82
+ pythonPathExplicit,
83
+ ...(path.isAbsolute(relativeQuarantine) ? {} : { quarantineFileRelative: relativeQuarantine }),
84
+ };
54
85
  }
55
86
  function readRuns(label, value, lo, hi) {
56
87
  if (typeof value !== 'number' || !Number.isFinite(value))
@@ -71,6 +102,27 @@ export function resolveRuns(raw, cfg) {
71
102
  throw new Error('runs 必须在 3-' + String(cfg.maxRuns) + ' 之间。');
72
103
  return runs;
73
104
  }
105
+ /** 解析并校验 flaky_detect 的并发数(1-4)。 */
106
+ export function resolveConcurrency(raw, cfg) {
107
+ if (raw === undefined || raw === null)
108
+ return cfg?.defaultConcurrency ?? 1;
109
+ if (typeof raw !== 'number' || !Number.isFinite(raw)) {
110
+ throw new Error('concurrency 必须是数字(' + String(MIN_CONCURRENCY) + '-' + String(MAX_CONCURRENCY) + ')。');
111
+ }
112
+ const value = Math.round(raw);
113
+ if (value < MIN_CONCURRENCY || value > MAX_CONCURRENCY) {
114
+ throw new Error('concurrency 必须在 ' + String(MIN_CONCURRENCY) + '-' + String(MAX_CONCURRENCY) + ' 之间。');
115
+ }
116
+ return value;
117
+ }
118
+ /** 解析 flaky_detect 的执行模式;默认 full。 */
119
+ export function readDetectMode(raw) {
120
+ if (raw === undefined || raw === null)
121
+ return 'full';
122
+ if (raw === 'full' || raw === 'until-flaky' || raw === 'verify-fix')
123
+ return raw;
124
+ throw new Error('mode 只支持 full / until-flaky / verify-fix,收到:' + String(raw));
125
+ }
74
126
  /** 校验测试目标:非空且不能以 - 开头,避免被测试框架解析成选项。 */
75
127
  export function assertTarget(value) {
76
128
  const trimmed = value.trim();
@@ -104,3 +156,72 @@ export function optionalInteger(args, key, label, lo, hi, fallback) {
104
156
  throw new Error(label + '(参数 ' + key + ')必须在 ' + String(lo) + '-' + String(hi) + ' 之间。');
105
157
  return rounded;
106
158
  }
159
+ /** 从参数里取布尔值;缺失用默认值,非法类型抛中文错误。 */
160
+ export function optionalBoolean(args, key, fallback = false) {
161
+ const value = args[key];
162
+ if (value === undefined || value === null)
163
+ return fallback;
164
+ if (typeof value === 'boolean')
165
+ return value;
166
+ throw new Error('参数 ' + key + ' 必须是布尔值。');
167
+ }
168
+ /** 从参数里取随机种子(0-2147483647 整数);缺失返回 undefined,非法抛中文错误。 */
169
+ export function optionalSeed(args, key) {
170
+ const value = args[key];
171
+ if (value === undefined || value === null)
172
+ return undefined;
173
+ if (typeof value !== 'number' || !Number.isFinite(value) || !Number.isInteger(value)) {
174
+ throw new Error('随机种子(参数 ' + key + ')必须是整数。');
175
+ }
176
+ if (value < 0 || value > MAX_SEED) {
177
+ throw new Error('随机种子(参数 ' + key + ')必须在 0-' + String(MAX_SEED) + ' 之间。');
178
+ }
179
+ return value;
180
+ }
181
+ const WIN_ABS_RE = /^[A-Za-z]:[\\/]/;
182
+ /**
183
+ * 将测试文件路径规范化为 POSIX 相对路径(使用 `/`、去掉前导 `./`);
184
+ * 若提供 `cwd` 且路径位于 `cwd` 之内,转为相对 `cwd` 的路径。
185
+ */
186
+ export function normalizeRelPath(filePath, cwd) {
187
+ const trimmed = filePath.trim();
188
+ if (trimmed === '')
189
+ return '';
190
+ const normFile = trimmed.replace(/\\/g, '/');
191
+ if (typeof cwd === 'string' && cwd.trim() !== '') {
192
+ const trimmedCwd = cwd.trim();
193
+ const normCwd = trimmedCwd.replace(/\\/g, '/');
194
+ if (WIN_ABS_RE.test(normFile) && WIN_ABS_RE.test(normCwd)) {
195
+ const resolvedCwd = path.win32.resolve(trimmedCwd).replace(/\\/g, '/').replace(/\/+$/, '');
196
+ const resolvedFile = path.win32.resolve(trimmed).replace(/\\/g, '/');
197
+ if (resolvedFile.toLowerCase().startsWith(resolvedCwd.toLowerCase() + '/')) {
198
+ return resolvedFile.slice(resolvedCwd.length + 1).replace(/^(?:\.\/)+/, '');
199
+ }
200
+ if (resolvedFile.toLowerCase() === resolvedCwd.toLowerCase()) {
201
+ return '.';
202
+ }
203
+ return resolvedFile;
204
+ }
205
+ if (normFile.startsWith('/') && normCwd.startsWith('/')) {
206
+ const resolvedCwd = path.posix.resolve(normCwd).replace(/\/+$/, '');
207
+ const resolvedFile = path.posix.resolve(normFile);
208
+ if (resolvedCwd === '') {
209
+ return resolvedFile.slice(1).replace(/^(?:\.\/)+/, '');
210
+ }
211
+ if (resolvedFile.startsWith(resolvedCwd + '/')) {
212
+ return resolvedFile.slice(resolvedCwd.length + 1).replace(/^(?:\.\/)+/, '');
213
+ }
214
+ if (resolvedFile === resolvedCwd) {
215
+ return '.';
216
+ }
217
+ return resolvedFile;
218
+ }
219
+ if (path.isAbsolute(trimmed)) {
220
+ const rel = path.relative(path.resolve(trimmedCwd), path.resolve(trimmed)).replace(/\\/g, '/');
221
+ if (rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel)) {
222
+ return rel.replace(/^(?:\.\/)+/, '');
223
+ }
224
+ }
225
+ }
226
+ return normFile.replace(/^(?:\.\/)+/, '');
227
+ }
package/lib/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  * dsh-flakefinder —— 测试稳定性工具插件(node 半身,配置走 cordis.patch.yml)。
3
3
  *
4
- * 插件导出 apply(ctx, config):注册五个面向模型的工具(flaky_detect / flaky_history /
5
- * flaky_quarantine / flaky_clear / flaky_report)。测试进程走 DSH 官方 subprocess 服务
6
- * (argv 数组、无 shell),隔离清单写操作默认走宿主审批门。仅依赖文件锁库。
4
+ * 插件导出 apply(ctx, config):注册六个面向模型的工具(flaky_detect / flaky_history /
5
+ * flaky_quarantine / flaky_clear / flaky_report / flaky_health)。测试进程走 DSH 官方
6
+ * subprocess 服务(argv 数组、无 shell),隔离清单写操作默认走宿主审批门。
7
+ * 运行时依赖 proper-lockfile 与 @stardustlc/dsh-updates。
7
8
  *
8
9
  * @module dsh-flakefinder
9
10
  */
@@ -31,9 +32,10 @@ export interface FlakePluginContext extends ApprovalContext {
31
32
  }): (() => void) | void;
32
33
  }
33
34
  /**
34
- * 插件入口:解析配置、封装执行器与存储、注册五个工具。
35
+ * 插件入口:解析配置、封装执行器与存储、注册六个工具。
35
36
  */
36
37
  export declare function apply(ctx: FlakePluginContext, config?: FlakeConfig | null): void;
38
+ export * from './adapters.js';
37
39
  export * from './args.js';
38
40
  export * from './classify.js';
39
41
  export * from './config.js';
package/lib/index.js CHANGED
@@ -2,9 +2,10 @@ import { registerUpdates } from '@stardustlc/dsh-updates';
2
2
  /**
3
3
  * dsh-flakefinder —— 测试稳定性工具插件(node 半身,配置走 cordis.patch.yml)。
4
4
  *
5
- * 插件导出 apply(ctx, config):注册五个面向模型的工具(flaky_detect / flaky_history /
6
- * flaky_quarantine / flaky_clear / flaky_report)。测试进程走 DSH 官方 subprocess 服务
7
- * (argv 数组、无 shell),隔离清单写操作默认走宿主审批门。仅依赖文件锁库。
5
+ * 插件导出 apply(ctx, config):注册六个面向模型的工具(flaky_detect / flaky_history /
6
+ * flaky_quarantine / flaky_clear / flaky_report / flaky_health)。测试进程走 DSH 官方
7
+ * subprocess 服务(argv 数组、无 shell),隔离清单写操作默认走宿主审批门。
8
+ * 运行时依赖 proper-lockfile 与 @stardustlc/dsh-updates。
8
9
  *
9
10
  * @module dsh-flakefinder
10
11
  */
@@ -16,8 +17,47 @@ import { approvalPolicy, approvalProblem } from './approval.js';
16
17
  /** cordis 服务注入:apply 里要用 ctx.subprocess 与 ctx.tools。 */
17
18
  export const name = 'flakefinder';
18
19
  export const inject = ['subprocess', 'tools'];
20
+ function parsePreExecuteArgs(rawArgs) {
21
+ if (typeof rawArgs === 'object' && rawArgs !== null)
22
+ return rawArgs;
23
+ if (typeof rawArgs === 'string' && rawArgs.trim() !== '') {
24
+ try {
25
+ const parsed = JSON.parse(rawArgs);
26
+ if (typeof parsed === 'object' && parsed !== null)
27
+ return parsed;
28
+ }
29
+ catch {
30
+ // 参数不是合法 JSON 时退回默认审批说明。
31
+ }
32
+ }
33
+ return {};
34
+ }
35
+ function formatApprovalReason(toolName, rawArgs) {
36
+ const args = parsePreExecuteArgs(rawArgs);
37
+ const tests = Array.isArray(args.tests)
38
+ ? args.tests.filter((item) => typeof item === 'string' && item.trim() !== '').map(item => item.trim())
39
+ : [];
40
+ if (tests.length === 0) {
41
+ return toolName === 'flaky_quarantine'
42
+ ? '把测试用例写入 .flakefinder.json 隔离清单'
43
+ : '从 .flakefinder.json 隔离清单移除测试用例';
44
+ }
45
+ const list = tests.map(item => ' - ' + item).join('\n');
46
+ if (toolName === 'flaky_quarantine') {
47
+ const meta = [];
48
+ if (typeof args.reason === 'string' && args.reason.trim() !== '') {
49
+ meta.push('原因:' + args.reason.trim());
50
+ }
51
+ if (typeof args.issue === 'string' && args.issue.trim() !== '') {
52
+ meta.push('关联:' + args.issue.trim());
53
+ }
54
+ const suffix = meta.length > 0 ? '\n' + meta.join(';') : '';
55
+ return '把以下 ' + String(tests.length) + ' 条测试用例写入 .flakefinder.json 隔离清单:\n' + list + suffix;
56
+ }
57
+ return '从 .flakefinder.json 隔离清单移除以下 ' + String(tests.length) + ' 条测试用例:\n' + list;
58
+ }
19
59
  /**
20
- * 插件入口:解析配置、封装执行器与存储、注册五个工具。
60
+ * 插件入口:解析配置、封装执行器与存储、注册六个工具。
21
61
  */
22
62
  export function apply(ctx, config) {
23
63
  registerUpdates(ctx, { name: "dsh-flakefinder" });
@@ -52,11 +92,7 @@ export function apply(ctx, config) {
52
92
  const problem = approvalProblem(approvalPolicy(ctx, exec), 'writeApproval');
53
93
  if (problem)
54
94
  return { kind: 'deny', reason: problem };
55
- if (exec.name === 'flaky_quarantine')
56
- return { kind: 'ask', reason: '把测试用例写入 .flakefinder.json 隔离清单' };
57
- if (exec.name === 'flaky_clear')
58
- return { kind: 'ask', reason: '从 .flakefinder.json 隔离清单移除测试用例' };
59
- return next();
95
+ return { kind: 'ask', reason: formatApprovalReason(exec.name, exec.arguments) };
60
96
  }, { prepend: true });
61
97
  }
62
98
  const teardown = () => {
@@ -70,6 +106,7 @@ export function apply(ctx, config) {
70
106
  ctx.on('dispose', teardown);
71
107
  }
72
108
  }
109
+ export * from './adapters.js';
73
110
  export * from './args.js';
74
111
  export * from './classify.js';
75
112
  export * from './config.js';
package/lib/parse.d.ts CHANGED
@@ -1,16 +1,22 @@
1
1
  /**
2
- * 测试报告解析:vitest / jest 的 JSON reporter 与 node:test 的 TAP 输出统一成
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
7
  export type TestStatus = 'passed' | 'failed' | 'skipped';
8
+ export interface TestErrorInfo {
9
+ type?: string;
10
+ message: string;
11
+ stack?: string;
12
+ }
8
13
  export interface NormalizedTest {
9
14
  id: string;
10
15
  file: string;
11
16
  name: string;
12
17
  status: TestStatus;
13
18
  durationMs: number;
19
+ error?: TestErrorInfo;
14
20
  }
15
21
  export interface NormalizedRun {
16
22
  framework: string;
@@ -21,9 +27,13 @@ export interface NormalizedRun {
21
27
  skipped: number;
22
28
  tests: NormalizedTest[];
23
29
  }
30
+ export interface TapParseOptions {
31
+ cwd?: string;
32
+ target?: string;
33
+ }
24
34
  /** vitest 与 jest 的 JSON 结构高度接近,这里统一读取。 */
25
- export declare function parseJsonReport(text: string, framework: string): NormalizedRun;
35
+ export declare function parseJsonReport(text: string, framework: string, cwd?: string): NormalizedRun;
26
36
  /** 解析 pytest 的 JUnit XML 报告。 */
27
- export declare function parseJunitReport(text: string): NormalizedRun;
28
- /** 解析 node:test 的 TAP 输出;子测试行忽略,只保留顶层用例。 */
29
- export declare function parseTapReport(text: string): NormalizedRun;
37
+ export declare function parseJunitReport(text: string, cwd?: string): NormalizedRun;
38
+ /** 解析 node:test 的 TAP 13 输出:支持层级子测试(仅输出叶节点)与 YAML 诊断块。 */
39
+ export declare function parseTapReport(text: string, options?: TapParseOptions): NormalizedRun;