create-clis 0.1.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/LICENSE +21 -0
- package/README.md +113 -0
- package/README.zh-CN.md +113 -0
- package/assets/manifest.json +99 -0
- package/assets/template/.editorconfig +12 -0
- package/assets/template/.gitattributes +18 -0
- package/assets/template/.github/ISSUE_TEMPLATE/bug_report.yml +86 -0
- package/assets/template/.github/ISSUE_TEMPLATE/feature_request.yml +45 -0
- package/assets/template/.github/PULL_REQUEST_TEMPLATE.md +26 -0
- package/assets/template/.github/workflows/ci.yml +42 -0
- package/assets/template/.github/workflows/release.yml +65 -0
- package/assets/template/.gitignorerc.json +21 -0
- package/assets/template/.nvmrc +1 -0
- package/assets/template/.oxlintrc.json +52 -0
- package/assets/template/.prettierignore +11 -0
- package/assets/template/.prettierrc +11 -0
- package/assets/template/.releaserc.json +17 -0
- package/assets/template/.vscode/extensions.json +8 -0
- package/assets/template/.vscode/launch.json +13 -0
- package/assets/template/.vscode/settings.json +14 -0
- package/assets/template/AGENTS.md +25 -0
- package/assets/template/CODE_OF_CONDUCT.md +91 -0
- package/assets/template/CONTRIBUTING.md +85 -0
- package/assets/template/CONTRIBUTING.zh-CN.md +85 -0
- package/assets/template/LICENSE +21 -0
- package/assets/template/README.md +22 -0
- package/assets/template/README.zh-CN.md +22 -0
- package/assets/template/SECURITY.md +21 -0
- package/assets/template/_gitignore +409 -0
- package/assets/template/_npmrc +6 -0
- package/assets/template/bunfig.toml +16 -0
- package/assets/template/docs/README.md +30 -0
- package/assets/template/docs/adrs/README.md +7 -0
- package/assets/template/docs/designs/README.md +11 -0
- package/assets/template/docs/designs/tech-debt.md +22 -0
- package/assets/template/docs/development.md +59 -0
- package/assets/template/lefthook.yml +55 -0
- package/assets/template/package.json +38 -0
- package/assets/template/packages/{{NAME}}/CHANGELOG.md +3 -0
- package/assets/template/packages/{{NAME}}/LICENSE +21 -0
- package/assets/template/packages/{{NAME}}/README.md +33 -0
- package/assets/template/packages/{{NAME}}/README.zh-CN.md +33 -0
- package/assets/template/packages/{{NAME}}/docs/README.md +21 -0
- package/assets/template/packages/{{NAME}}/package.json +40 -0
- package/assets/template/packages/{{NAME}}/scripts/build.ts +82 -0
- package/assets/template/packages/{{NAME}}/scripts/verify-release.test.ts +214 -0
- package/assets/template/packages/{{NAME}}/scripts/verify-release.ts +152 -0
- package/assets/template/packages/{{NAME}}/src/index.ts +15 -0
- package/assets/template/packages/{{NAME}}/tsconfig.build.json +12 -0
- package/assets/template/packages/{{NAME}}/tsconfig.json +5 -0
- package/assets/template/packages/{{NAME}}-cli/CHANGELOG.md +3 -0
- package/assets/template/packages/{{NAME}}-cli/LICENSE +21 -0
- package/assets/template/packages/{{NAME}}-cli/README.md +35 -0
- package/assets/template/packages/{{NAME}}-cli/README.zh-CN.md +35 -0
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}} +30 -0
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.cmd +30 -0
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.d.mts +23 -0
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.mjs +212 -0
- package/assets/template/packages/{{NAME}}-cli/docs/README.md +21 -0
- package/assets/template/packages/{{NAME}}-cli/package.json +48 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/release-artifact.ts +141 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/release.fixtures.ts +70 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/verify-release.test.ts +444 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/verify-release.ts +72 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/write-dist-manifest.test.ts +126 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/write-dist-manifest.ts +81 -0
- package/assets/template/packages/{{NAME}}-cli/src/cli.ts +36 -0
- package/assets/template/packages/{{NAME}}-cli/tsconfig.json +4 -0
- package/assets/template/scripts/ci.ts +163 -0
- package/assets/template/scripts/install-git-hooks.mjs +122 -0
- package/assets/template/scripts/install-git-hooks.test.ts +418 -0
- package/assets/template/scripts/lib/git-env.ts +21 -0
- package/assets/template/scripts/lib/release-verify.ts +431 -0
- package/assets/template/scripts/lib/tmp-root.ts +17 -0
- package/assets/template/scripts/lib/workspace.test.ts +92 -0
- package/assets/template/scripts/lib/workspace.ts +100 -0
- package/assets/template/scripts/lint-doc-examples.test.ts +129 -0
- package/assets/template/scripts/lint-doc-examples.ts +249 -0
- package/assets/template/scripts/lint-doc-references.test.ts +158 -0
- package/assets/template/scripts/lint-doc-references.ts +308 -0
- package/assets/template/scripts/lint-doc-shared.ts +59 -0
- package/assets/template/scripts/lint-stray-backups.test.ts +53 -0
- package/assets/template/scripts/lint-stray-backups.ts +39 -0
- package/assets/template/scripts/npm-trust-guard.test.ts +253 -0
- package/assets/template/scripts/npm-trust-guard.ts +533 -0
- package/assets/template/scripts/safe-install.ts +39 -0
- package/assets/template/scripts/transcription/api-harness.ts +219 -0
- package/assets/template/scripts/transcription/compare.ts +421 -0
- package/assets/template/scripts/transcription/corpus.ts +208 -0
- package/assets/template/scripts/transcription/fixture.ts +290 -0
- package/assets/template/scripts/transcription/make-mutants.ts +425 -0
- package/assets/template/scripts/transcription/report.ts +86 -0
- package/assets/template/scripts/transcription/run-conformance.ts +344 -0
- package/assets/template/scripts/transcription/validate-coverage.test.ts +119 -0
- package/assets/template/scripts/transcription/validate-coverage.ts +144 -0
- package/assets/template/scripts/transcription/validate.ts +366 -0
- package/assets/template/tsconfig.json +46 -0
- package/assets/template/turbo.jsonc +60 -0
- package/bin/create-clis +35 -0
- package/bin/create-clis.cmd +32 -0
- package/bin/create-clis.mjs +219 -0
- package/dist/create-clis.js +87682 -0
- package/dist/manifest.json +7 -0
- package/package.json +57 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 转写契约套件 · 语料层。
|
|
3
|
+
*
|
|
4
|
+
* 职责: 语料字段类型 (与 docs/sweep/protocol/conformance/corpus.schema.json 对应) + $FIXTURE 变量替换
|
|
5
|
+
* 契约 + 目录加载。字段语义以 schema 为准; 手写校验 (schema 的物化子集) 在 validate.ts, 只为尽早
|
|
6
|
+
* 给出可读报错, 不复刻 schema 的全部约束。
|
|
7
|
+
*/
|
|
8
|
+
import { readFile, readdir } from 'node:fs/promises';
|
|
9
|
+
import { join } from 'node:path';
|
|
10
|
+
|
|
11
|
+
import { validateCase } from './validate.ts';
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// 语料类型 (与 corpus.schema.json 对应)
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
export interface ProjectSpec {
|
|
18
|
+
dir: string;
|
|
19
|
+
files?: number;
|
|
20
|
+
bytesPerFile?: number;
|
|
21
|
+
nested?: boolean;
|
|
22
|
+
git?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface SymlinkSpec {
|
|
26
|
+
at: string;
|
|
27
|
+
to: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface FixtureSpec {
|
|
31
|
+
projects?: ProjectSpec[];
|
|
32
|
+
symlinks?: SymlinkSpec[];
|
|
33
|
+
unreadable?: string[];
|
|
34
|
+
/** 只读目录 (chmod 0500): 可读可进入、不可写, 供删除失败 / 部分删除类用例 */
|
|
35
|
+
readonly?: string[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface SetupStep {
|
|
39
|
+
write: { path: string; text: string };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface RunSpec {
|
|
43
|
+
argv?: string[];
|
|
44
|
+
env?: Record<string, string>;
|
|
45
|
+
cwd?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type FsState = 'gone' | 'exists' | 'empty';
|
|
49
|
+
|
|
50
|
+
export interface FsExpectation {
|
|
51
|
+
path: string;
|
|
52
|
+
state: FsState;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ExpectSpec {
|
|
56
|
+
exitCode: number;
|
|
57
|
+
stdoutExact?: string;
|
|
58
|
+
stdoutContains?: string[];
|
|
59
|
+
/** 「不该出现」面: 剪枝 / 排除 / 诱饵类用例的禁含断言 */
|
|
60
|
+
stdoutMustNotContain?: string[];
|
|
61
|
+
stderrContains?: string[];
|
|
62
|
+
/** 「不该出现」面: 「正确行为是静默」类用例的 stderr 禁含断言 */
|
|
63
|
+
stderrMustNotContain?: string[];
|
|
64
|
+
fs?: FsExpectation[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** cli 用例 (缺省形态): 经 --target 的被测命令做黑盒验收 */
|
|
68
|
+
export interface CliCorpusCase {
|
|
69
|
+
id: string;
|
|
70
|
+
kind?: 'cli';
|
|
71
|
+
specRefs: string[];
|
|
72
|
+
fixture: FixtureSpec;
|
|
73
|
+
setup?: SetupStep[];
|
|
74
|
+
run: RunSpec;
|
|
75
|
+
expect: ExpectSpec;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** api 用例的单步调用指令 (形状与执行语义见 docs/sweep/protocol/conformance/api-harness-protocol.md) */
|
|
79
|
+
export interface ApiCallStep {
|
|
80
|
+
as?: string;
|
|
81
|
+
call: {
|
|
82
|
+
export?: string;
|
|
83
|
+
on?: string;
|
|
84
|
+
method?: string;
|
|
85
|
+
args?: unknown[];
|
|
86
|
+
};
|
|
87
|
+
collectEvents?: boolean;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** api 用例的断言族: 末步返回值 / 抛错码 / 事件流 / fs (fs 与 cli 面共用) */
|
|
91
|
+
export interface ApiExpectSpec {
|
|
92
|
+
result?: { exact?: unknown; subset?: unknown };
|
|
93
|
+
error?: { code: string; name?: string };
|
|
94
|
+
events?: { lastIs?: string; mustInclude?: string[] };
|
|
95
|
+
fs?: FsExpectation[];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** api 用例: 经被测方按协议提供的 harness 可执行做黑盒验收 (--api-target) */
|
|
99
|
+
export interface ApiCorpusCase {
|
|
100
|
+
id: string;
|
|
101
|
+
kind: 'api';
|
|
102
|
+
specRefs: string[];
|
|
103
|
+
fixture: FixtureSpec;
|
|
104
|
+
setup?: SetupStep[];
|
|
105
|
+
env?: Record<string, string>;
|
|
106
|
+
steps: ApiCallStep[];
|
|
107
|
+
expect: ApiExpectSpec;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export type CorpusCase = CliCorpusCase | ApiCorpusCase;
|
|
111
|
+
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
// 变量替换契约 ($FIXTURE)
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
|
|
116
|
+
/** 语料中的 fixture 根变量 (字符串字段内出现即替换为 realpath 形态的绝对路径) */
|
|
117
|
+
export const FIXTURE_VAR = '$FIXTURE';
|
|
118
|
+
|
|
119
|
+
/** 字符串字段的变量替换 (仅 $FIXTURE 一个变量, 保持最小面) */
|
|
120
|
+
export const applyVars = (text: string, root: string): string =>
|
|
121
|
+
text.split(FIXTURE_VAR).join(root);
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 递归对任意 JSON 值里的字符串做 $FIXTURE 替换 (保结构)。
|
|
125
|
+
* 两处消费: api 用例的 steps 指令在派发给 harness 前替换; result 期望值在比对前替换。
|
|
126
|
+
* ({ "$probe": ... } 标记对象可安全穿过: 探针名不含 $FIXTURE。)
|
|
127
|
+
*/
|
|
128
|
+
export function replaceVarsDeep(value: unknown, root: string): unknown {
|
|
129
|
+
if (typeof value === 'string') return applyVars(value, root);
|
|
130
|
+
if (Array.isArray(value)) {
|
|
131
|
+
return value.map((item) => replaceVarsDeep(item, root));
|
|
132
|
+
}
|
|
133
|
+
if (typeof value === 'object' && value !== null) {
|
|
134
|
+
return Object.fromEntries(
|
|
135
|
+
Object.entries(value).map(([key, item]) => [
|
|
136
|
+
key,
|
|
137
|
+
replaceVarsDeep(item, root),
|
|
138
|
+
]),
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 读语料目录下全部 .json (按文件名升序, 执行次序确定)。
|
|
146
|
+
* 任一文件非法即抛错 (语料缺陷属于 runner 级错误, 不降级为用例失败)。
|
|
147
|
+
*
|
|
148
|
+
* ### 数据追踪示例
|
|
149
|
+
* ```text
|
|
150
|
+
* Input(真实 Payload)
|
|
151
|
+
* dir = 'docs/sweep/protocol/conformance/corpus' (含 scan-nested-prune.json 与 delete-execute-ok.json)
|
|
152
|
+
*
|
|
153
|
+
* 步骤 1:取 .json 文件名并排序
|
|
154
|
+
* names = ['delete-execute-ok.json', 'scan-nested-prune.json']
|
|
155
|
+
*
|
|
156
|
+
* 步骤 2:逐文件 JSON 解析 + validateCase + 文件名/id 一致性 + id 唯一性
|
|
157
|
+
* 全部通过 → cases = [两条 case 对象]
|
|
158
|
+
*
|
|
159
|
+
* Output(数据契约)
|
|
160
|
+
* return 按文件名的 case 数组; 任一环节失败即 throw (调用方落退出码 2)
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export async function loadCorpus(dir: string): Promise<CorpusCase[]> {
|
|
164
|
+
let names: string[];
|
|
165
|
+
try {
|
|
166
|
+
names = (await readdir(dir))
|
|
167
|
+
.filter((name) => name.endsWith('.json'))
|
|
168
|
+
.sort();
|
|
169
|
+
} catch (error) {
|
|
170
|
+
throw new Error(`语料目录不可读: ${dir} (${(error as Error).message})`);
|
|
171
|
+
}
|
|
172
|
+
if (names.length === 0) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`语料目录没有 .json 用例: ${dir} (空语料按 runner 级错误处理, 退 2 不假绿; 约定见 corpus/README.md)`,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const cases: CorpusCase[] = [];
|
|
179
|
+
const seen = new Set<string>();
|
|
180
|
+
for (const name of names) {
|
|
181
|
+
const file = join(dir, name);
|
|
182
|
+
const text = await readFile(file, 'utf8');
|
|
183
|
+
let data: unknown;
|
|
184
|
+
try {
|
|
185
|
+
data = JSON.parse(text);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
throw new Error(
|
|
188
|
+
`语料非法 (${name}): JSON 解析失败: ${(error as Error).message}`,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
const problems = validateCase(data);
|
|
192
|
+
if (problems.length > 0) {
|
|
193
|
+
throw new Error(`语料非法 (${name}):\n - ${problems.join('\n - ')}`);
|
|
194
|
+
}
|
|
195
|
+
const caseSpec = data as CorpusCase;
|
|
196
|
+
if (`${caseSpec.id}.json` !== name) {
|
|
197
|
+
throw new Error(
|
|
198
|
+
`语料非法 (${name}): 文件名须与 id 一致 (期望 ${caseSpec.id}.json)`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
if (seen.has(caseSpec.id)) {
|
|
202
|
+
throw new Error(`语料非法 (${name}): id 重复: ${caseSpec.id}`);
|
|
203
|
+
}
|
|
204
|
+
seen.add(caseSpec.id);
|
|
205
|
+
cases.push(caseSpec);
|
|
206
|
+
}
|
|
207
|
+
return cases;
|
|
208
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 转写契约套件 · 现场层 (fixture 建树 / setup 预置 / 最小 env / 执行 / 清理)。
|
|
3
|
+
*
|
|
4
|
+
* 职责: 把语料声明的 fixture 物化成真实目录树, 以最小白名单 env 启动被测进程 (cli case 起被测
|
|
5
|
+
* 可执行; api case 起 harness 并按步骤指令喂 stdin), 跑完回收现场。
|
|
6
|
+
* 建树与体积口径语义与 src/fixtures.ts 同构 (被测的既有测试口径即验收口径)。
|
|
7
|
+
*/
|
|
8
|
+
import { spawnSync } from 'node:child_process';
|
|
9
|
+
import { chmod, mkdir, rm, symlink, writeFile } from 'node:fs/promises';
|
|
10
|
+
import { dirname, join, resolve } from 'node:path';
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
type ApiCorpusCase,
|
|
14
|
+
type CliCorpusCase,
|
|
15
|
+
type FixtureSpec,
|
|
16
|
+
type SetupStep,
|
|
17
|
+
applyVars,
|
|
18
|
+
replaceVarsDeep,
|
|
19
|
+
} from './corpus.ts';
|
|
20
|
+
|
|
21
|
+
/** fixture 内的家目录名 (被测进程的 HOME 指向此处, 见 buildEnv 注释) */
|
|
22
|
+
const HOME_DIR_NAME = 'home';
|
|
23
|
+
|
|
24
|
+
/** 默认散文件数与字节数 (与 src/fixtures.ts 同值, 保语料与既有测试口径一致) */
|
|
25
|
+
const DEFAULT_FILES = 2;
|
|
26
|
+
const DEFAULT_BYTES_PER_FILE = 256;
|
|
27
|
+
|
|
28
|
+
/** 嵌套 node_modules 与 .git 诱饵文件的小字节数 (与 src/fixtures.ts 同值) */
|
|
29
|
+
const BAIT_FILE_BYTES = 64;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 最小环境白名单 (不继承宿主环境: 防宿主语义变量泄漏进断言面, 保可移植; 被测工具自己的配置环境
|
|
33
|
+
* 变量形如 `<TOOL>_CONFIG`, 随宿主带进来会改掉断言面实际生效的配置)。
|
|
34
|
+
* - PATH / TMPDIR 族: 运行基础 (被测 spawn 子进程、取临时目录时用);
|
|
35
|
+
* - HOME / USERPROFILE: 家目录, 由本运行器改写指向 fixture 内的 home (见 buildEnv);
|
|
36
|
+
* - SystemRoot / PATHEXT / ComSpec: Windows 下子进程运行基础;
|
|
37
|
+
* - QUOTING_STYLE: 放行宿主设定 (不剔除)。du 仅在其 stdout 为终端时按该变量引用文件名, 本运行器
|
|
38
|
+
* 以管道捕获输出、恒非终端, 故它不进入断言面; 保留放行只为不改写宿主语义 (BSD du 无此变量,
|
|
39
|
+
* 设了无害)。形态依据 (2026-09-27 核实) 见 src/size-du.ts 文件头「输出形态」。
|
|
40
|
+
*/
|
|
41
|
+
const BASE_ENV_KEYS = [
|
|
42
|
+
'PATH',
|
|
43
|
+
'TMPDIR',
|
|
44
|
+
'TEMP',
|
|
45
|
+
'TMP',
|
|
46
|
+
'SystemRoot',
|
|
47
|
+
'PATHEXT',
|
|
48
|
+
'ComSpec',
|
|
49
|
+
'QUOTING_STYLE',
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
/** 一次运行期的固定上下文 (由调用方解析 CLI 后组装) */
|
|
53
|
+
export interface RunContext {
|
|
54
|
+
/** 被测命令 argv (由 --target 拆分而来) */
|
|
55
|
+
targetArgv: string[];
|
|
56
|
+
/** API harness 命令 argv (由 --api-target 拆分而来; api 用例用) */
|
|
57
|
+
apiTargetArgv: string[];
|
|
58
|
+
timeoutMs: number;
|
|
59
|
+
keep: boolean;
|
|
60
|
+
/** fixture 根基座 (调用方保证已存在) */
|
|
61
|
+
workBase: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface ExecOutcome {
|
|
65
|
+
status: number | null;
|
|
66
|
+
stdout: string;
|
|
67
|
+
stderr: string;
|
|
68
|
+
/** 超时守卫击中 (进程已被杀, 判失败) */
|
|
69
|
+
timedOut: boolean;
|
|
70
|
+
/** 非超时的执行级失败 (命令不存在 / cwd 不存在等) */
|
|
71
|
+
spawnError?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 按声明建工作区树。外部副作用:在 root 下创建目录与文件 (root 由调用方创建并保证为空)。
|
|
76
|
+
*/
|
|
77
|
+
export async function buildFixture(
|
|
78
|
+
root: string,
|
|
79
|
+
fixture: FixtureSpec,
|
|
80
|
+
): Promise<void> {
|
|
81
|
+
for (const project of fixture.projects ?? []) {
|
|
82
|
+
const nodeModules = join(root, project.dir, 'node_modules');
|
|
83
|
+
await mkdir(nodeModules, { recursive: true });
|
|
84
|
+
const files = project.files ?? DEFAULT_FILES;
|
|
85
|
+
const bytes = project.bytesPerFile ?? DEFAULT_BYTES_PER_FILE;
|
|
86
|
+
for (let index = 0; index < files; index += 1) {
|
|
87
|
+
await writeFile(join(nodeModules, `pkg-${index}.js`), 'x'.repeat(bytes));
|
|
88
|
+
}
|
|
89
|
+
if (project.nested === true) {
|
|
90
|
+
const inner = join(nodeModules, 'dep', 'node_modules');
|
|
91
|
+
await mkdir(inner, { recursive: true });
|
|
92
|
+
await writeFile(join(inner, 'inner.js'), 'x'.repeat(BAIT_FILE_BYTES));
|
|
93
|
+
}
|
|
94
|
+
if (project.git === true) {
|
|
95
|
+
const bait = join(root, project.dir, '.git', 'x', 'node_modules');
|
|
96
|
+
await mkdir(bait, { recursive: true });
|
|
97
|
+
await writeFile(join(bait, 'bait.js'), 'x'.repeat(BAIT_FILE_BYTES));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
for (const link of fixture.symlinks ?? []) {
|
|
101
|
+
await symlink(join(root, link.to), join(root, link.at));
|
|
102
|
+
}
|
|
103
|
+
for (const dir of fixture.unreadable ?? []) {
|
|
104
|
+
await mkdir(join(root, dir), { recursive: true });
|
|
105
|
+
await chmod(join(root, dir), 0o000);
|
|
106
|
+
}
|
|
107
|
+
for (const dir of fixture.readonly ?? []) {
|
|
108
|
+
// chmod 0500 (r-x): 内容可读可进入 (扫描与体积统计正常), 不可写 (删除条目 EACCES)
|
|
109
|
+
await chmod(join(root, dir), 0o500);
|
|
110
|
+
}
|
|
111
|
+
// 家目录: 被测进程的 HOME 指向此处 (存在但空)。放 fixture 内一举两得:
|
|
112
|
+
// 既让 home 防线与 ~ 缩写行为可控可测, 又不引入 root 之外的残留。
|
|
113
|
+
await mkdir(join(root, HOME_DIR_NAME), { recursive: true });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 应用 setup 预置态 (在 fixture 建树之后按序执行); 写文件时自动创建父目录。
|
|
118
|
+
* 外部副作用:在 root 下写入文件。
|
|
119
|
+
*/
|
|
120
|
+
export async function applySetup(
|
|
121
|
+
root: string,
|
|
122
|
+
steps: SetupStep[],
|
|
123
|
+
): Promise<void> {
|
|
124
|
+
for (const step of steps) {
|
|
125
|
+
const full = join(root, step.write.path);
|
|
126
|
+
await mkdir(dirname(full), { recursive: true });
|
|
127
|
+
await writeFile(full, applyVars(step.write.text, root));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 清理 fixture: 先把 chmod 000 的目录恢复可写 (否则 rm 无法穿透), 再整棵删除。
|
|
133
|
+
* 清理失败不掩盖: 打 stderr 警告 (不改变用例判定)。
|
|
134
|
+
*/
|
|
135
|
+
export async function cleanupFixture(
|
|
136
|
+
rawRoot: string,
|
|
137
|
+
fixture: FixtureSpec,
|
|
138
|
+
): Promise<void> {
|
|
139
|
+
for (const dir of fixture.unreadable ?? []) {
|
|
140
|
+
await chmod(join(rawRoot, dir), 0o700).catch(() => {});
|
|
141
|
+
}
|
|
142
|
+
for (const dir of fixture.readonly ?? []) {
|
|
143
|
+
await chmod(join(rawRoot, dir), 0o700).catch(() => {});
|
|
144
|
+
}
|
|
145
|
+
await rm(rawRoot, { recursive: true, force: true }).catch(
|
|
146
|
+
(error: unknown) => {
|
|
147
|
+
process.stderr.write(
|
|
148
|
+
`[Warning] fixture 清理失败 (${rawRoot}): ${String(error)}\n`,
|
|
149
|
+
);
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 组装被测进程环境: 白名单宿主变量 + fixture 内家目录 + case 增量 (后覆盖前)。
|
|
156
|
+
* 外部副作用:无 (纯构造)。
|
|
157
|
+
*/
|
|
158
|
+
export function buildEnv(
|
|
159
|
+
root: string,
|
|
160
|
+
extra: Record<string, string>,
|
|
161
|
+
): Record<string, string> {
|
|
162
|
+
const env: Record<string, string> = {};
|
|
163
|
+
for (const key of BASE_ENV_KEYS) {
|
|
164
|
+
const value = process.env[key];
|
|
165
|
+
if (value !== undefined) env[key] = value;
|
|
166
|
+
}
|
|
167
|
+
// HOME 改写为 fixture 内目录: 被测的 ~ 缩写与 home 防线都落在可控域内, 且不与 fixture 根的
|
|
168
|
+
// 绝对路径互为前缀 (否则清单里的路径会被缩成 ~ 形态, 语料的逐字节期望无法用 $FIXTURE 表达)。
|
|
169
|
+
const fakeHome = join(root, HOME_DIR_NAME);
|
|
170
|
+
env.HOME = fakeHome;
|
|
171
|
+
env.USERPROFILE = fakeHome;
|
|
172
|
+
for (const [key, value] of Object.entries(extra)) {
|
|
173
|
+
env[key] = applyVars(value, root);
|
|
174
|
+
}
|
|
175
|
+
return env;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 执行被测命令一次 (带超时守卫)。
|
|
180
|
+
* 外部副作用:启动子进程 (cwd 为 fixture 内目录, 环境为白名单), 可能改动 fixture 文件系统。
|
|
181
|
+
*
|
|
182
|
+
* ### 数据追踪示例
|
|
183
|
+
* ```text
|
|
184
|
+
* Input(真实 Payload)
|
|
185
|
+
* ctx.targetArgv = ['bun', '/abs/src/cli.ts']; caseSpec.run = { cwd: 'zone', argv: ['--json'] }
|
|
186
|
+
*
|
|
187
|
+
* 步骤 1:拼装 argv 与 cwd
|
|
188
|
+
* argv = ['bun', '/abs/src/cli.ts', '--json']; cwd = '<fixture>/zone'
|
|
189
|
+
*
|
|
190
|
+
* 步骤 2:白名单 env 启动子进程 (超时守卫 timeoutMs, 到期 SIGKILL)
|
|
191
|
+
* result = { status: 0, stdout: '…', stderr: '' }
|
|
192
|
+
*
|
|
193
|
+
* Output(数据契约)
|
|
194
|
+
* return { status, stdout, stderr, timedOut, spawnError? } (spawn 级失败只填 spawnError)
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
export function executeCase(
|
|
198
|
+
caseSpec: CliCorpusCase,
|
|
199
|
+
root: string,
|
|
200
|
+
ctx: RunContext,
|
|
201
|
+
): ExecOutcome {
|
|
202
|
+
const run = caseSpec.run;
|
|
203
|
+
const argv = [
|
|
204
|
+
...ctx.targetArgv,
|
|
205
|
+
...(run.argv ?? []).map((item) => applyVars(item, root)),
|
|
206
|
+
];
|
|
207
|
+
// 收窄 argv[0] 的类型 (noUncheckedIndexedAccess): --target 非空由 parseArgs 保证, 拆分必出元素,
|
|
208
|
+
// 此分支不引入 as / @ts-ignore, 把不可达态按「执行级失败」同口径返回。
|
|
209
|
+
const command = argv[0];
|
|
210
|
+
if (command === undefined) {
|
|
211
|
+
return {
|
|
212
|
+
status: null,
|
|
213
|
+
stdout: '',
|
|
214
|
+
stderr: '',
|
|
215
|
+
timedOut: false,
|
|
216
|
+
spawnError: '被测命令为空 (--target 未给出可执行命令)',
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
const cwd = resolve(root, run.cwd ?? '.');
|
|
220
|
+
const env = buildEnv(root, run.env ?? {});
|
|
221
|
+
|
|
222
|
+
const result = spawnSync(command, argv.slice(1), {
|
|
223
|
+
cwd,
|
|
224
|
+
env,
|
|
225
|
+
timeout: ctx.timeoutMs,
|
|
226
|
+
// 超时后硬杀: 被测若死在 readline 等 stdin 上, SIGTERM 可能被吞
|
|
227
|
+
killSignal: 'SIGKILL',
|
|
228
|
+
encoding: 'utf8',
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
const error = result.error as (Error & { code?: string }) | undefined;
|
|
232
|
+
const timedOut =
|
|
233
|
+
error !== undefined &&
|
|
234
|
+
(error.code === 'ETIMEDOUT' || /ETIMEDOUT/.test(error.message));
|
|
235
|
+
return {
|
|
236
|
+
status: result.status,
|
|
237
|
+
stdout: result.stdout ?? '',
|
|
238
|
+
stderr: result.stderr ?? '',
|
|
239
|
+
timedOut,
|
|
240
|
+
spawnError: error !== undefined && !timedOut ? error.message : undefined,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* 执行 api 用例的 harness 一次 (带超时守卫): 调用指令 JSON (steps 经 $FIXTURE 替换) 经 stdin
|
|
246
|
+
* 喂入, stdout 收结果 JSON。
|
|
247
|
+
* 外部副作用:启动子进程 (cwd 为 fixture 根, 环境为白名单 + case.env), 可能改动 fixture 文件系统。
|
|
248
|
+
*/
|
|
249
|
+
export function executeApiCase(
|
|
250
|
+
caseSpec: ApiCorpusCase,
|
|
251
|
+
root: string,
|
|
252
|
+
ctx: RunContext,
|
|
253
|
+
): ExecOutcome {
|
|
254
|
+
const argv = ctx.apiTargetArgv;
|
|
255
|
+
const command = argv[0];
|
|
256
|
+
if (command === undefined) {
|
|
257
|
+
return {
|
|
258
|
+
status: null,
|
|
259
|
+
stdout: '',
|
|
260
|
+
stderr: '',
|
|
261
|
+
timedOut: false,
|
|
262
|
+
spawnError: 'API harness 命令为空 (--api-target 未给出可执行命令)',
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
const instruction = JSON.stringify(
|
|
266
|
+
replaceVarsDeep({ steps: caseSpec.steps }, root),
|
|
267
|
+
);
|
|
268
|
+
const env = buildEnv(root, caseSpec.env ?? {});
|
|
269
|
+
|
|
270
|
+
const result = spawnSync(command, argv.slice(1), {
|
|
271
|
+
cwd: root,
|
|
272
|
+
env,
|
|
273
|
+
input: instruction,
|
|
274
|
+
timeout: ctx.timeoutMs,
|
|
275
|
+
killSignal: 'SIGKILL',
|
|
276
|
+
encoding: 'utf8',
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const error = result.error as (Error & { code?: string }) | undefined;
|
|
280
|
+
const timedOut =
|
|
281
|
+
error !== undefined &&
|
|
282
|
+
(error.code === 'ETIMEDOUT' || /ETIMEDOUT/.test(error.message));
|
|
283
|
+
return {
|
|
284
|
+
status: result.status,
|
|
285
|
+
stdout: result.stdout ?? '',
|
|
286
|
+
stderr: result.stderr ?? '',
|
|
287
|
+
timedOut,
|
|
288
|
+
spawnError: error !== undefined && !timedOut ? error.message : undefined,
|
|
289
|
+
};
|
|
290
|
+
}
|