codemod 1.18.7 → 2.0.0-alpha.2
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/README.md +57 -2
- package/codemod +17 -30
- package/package.json +78 -10
- package/register.d.mts +11 -0
- package/register.mjs +60 -0
- package/runtime/addon.mjs +59 -0
- package/runtime/ast-grep.mjs +93 -0
- package/runtime/context.mjs +77 -0
- package/runtime/hooks.mjs +9 -0
- package/runtime/host.d.mts +120 -0
- package/runtime/host.mjs +390 -0
- package/runtime/llm.mjs +11 -0
- package/runtime/metrics.mjs +45 -0
- package/runtime/modules.mjs +19 -0
- package/runtime/orchestrator.mjs +657 -0
- package/runtime/runtime.mjs +48 -0
- package/runtime/workflow.mjs +135 -0
- package/testing/index.d.mts +203 -0
- package/testing/index.mjs +638 -0
- package/types/index.d.ts +8 -0
- package/types/langs/angular.d.ts +5 -0
- package/types/langs/c.d.ts +5 -0
- package/types/langs/c_sharp.d.ts +5 -0
- package/types/langs/cpp.d.ts +5 -0
- package/types/langs/css.d.ts +5 -0
- package/types/langs/elixir.d.ts +5 -0
- package/types/langs/go.d.ts +5 -0
- package/types/langs/html.d.ts +5 -0
- package/types/langs/java.d.ts +5 -0
- package/types/langs/javascript.d.ts +5 -0
- package/types/langs/json.d.ts +5 -0
- package/types/langs/kotlin.d.ts +5 -0
- package/types/langs/php.d.ts +5 -0
- package/types/langs/python.d.ts +5 -0
- package/types/langs/ruby.d.ts +5 -0
- package/types/langs/rust.d.ts +5 -0
- package/types/langs/scala.d.ts +5 -0
- package/types/langs/toml.d.ts +5 -0
- package/types/langs/tsx.d.ts +5 -0
- package/types/langs/typescript.d.ts +5 -0
- package/types/langs/xml.d.ts +5 -0
- package/types/langs/yaml.d.ts +5 -0
- package/types/llm.d.ts +6 -0
- package/types/llrt/abort.d.ts +69 -0
- package/types/llrt/assert.d.ts +39 -0
- package/types/llrt/async_hooks.d.ts +210 -0
- package/types/llrt/buffer.d.ts +1186 -0
- package/types/llrt/child_process.d.ts +447 -0
- package/types/llrt/console.d.ts +210 -0
- package/types/llrt/crypto.d.ts +854 -0
- package/types/llrt/dgram.d.ts +381 -0
- package/types/llrt/dns.d.ts +124 -0
- package/types/llrt/dom-events.d.ts +51 -0
- package/types/llrt/events.d.ts +245 -0
- package/types/llrt/exceptions.d.ts +26 -0
- package/types/llrt/fetch.d.ts +332 -0
- package/types/llrt/fs/promises.d.ts +554 -0
- package/types/llrt/fs.d.ts +360 -0
- package/types/llrt/globals.d.ts +23 -0
- package/types/llrt/https.d.ts +27 -0
- package/types/llrt/index.d.ts +33 -0
- package/types/llrt/module.d.ts +285 -0
- package/types/llrt/navigator.d.ts +9 -0
- package/types/llrt/net.d.ts +488 -0
- package/types/llrt/os.d.ts +293 -0
- package/types/llrt/path.d.ts +150 -0
- package/types/llrt/perf_hooks.d.ts +23 -0
- package/types/llrt/process.d.ts +502 -0
- package/types/llrt/stream/web.d.ts +526 -0
- package/types/llrt/stream.d.ts +366 -0
- package/types/llrt/string_decoder.d.ts +63 -0
- package/types/llrt/timers.d.ts +72 -0
- package/types/llrt/timezone.d.ts +101 -0
- package/types/llrt/tty.d.ts +11 -0
- package/types/llrt/url.d.ts +774 -0
- package/types/llrt/util.d.ts +240 -0
- package/types/llrt/zlib.d.ts +177 -0
- package/types/main.d.ts +5 -0
- package/types/metrics.d.ts +5 -0
- package/types/modules/ast-grep.d.ts +634 -0
- package/types/modules/langs.d.ts +87 -0
- package/types/modules/llm.d.ts +23 -0
- package/types/modules/metrics.d.ts +107 -0
- package/types/modules/runtime.d.ts +66 -0
- package/types/modules/workflow.d.ts +750 -0
- package/types/runtime.d.ts +6 -0
- package/types/workflow.d.ts +5 -0
- package/vite.d.mts +24 -0
- package/vite.mjs +26 -0
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
// `codemod/testing`: run workflows from any test runner (node:test, Vitest,
|
|
2
|
+
// Jest, Bun) and compare the files they leave.
|
|
3
|
+
//
|
|
4
|
+
// Each run copies its target into a temporary directory first. TypeScript
|
|
5
|
+
// workflows run in this process (`codemod/host`), so breakpoints, coverage
|
|
6
|
+
// and module mocks work in the workflow's code. With `host: "quickjs"`, or
|
|
7
|
+
// for YAML workflows, the run goes to the installed codemod CLI instead
|
|
8
|
+
// (`codemod workflow harness`), in the engine's own sandboxed runtime.
|
|
9
|
+
// Failures are `AssertionError`s with `actual` and `expected`, which every
|
|
10
|
+
// runner knows how to show.
|
|
11
|
+
|
|
12
|
+
import { spawn } from "node:child_process";
|
|
13
|
+
import { AssertionError } from "node:assert";
|
|
14
|
+
import {
|
|
15
|
+
cpSync,
|
|
16
|
+
existsSync,
|
|
17
|
+
mkdirSync,
|
|
18
|
+
mkdtempSync,
|
|
19
|
+
readdirSync,
|
|
20
|
+
readFileSync,
|
|
21
|
+
rmSync,
|
|
22
|
+
statSync,
|
|
23
|
+
writeFileSync,
|
|
24
|
+
} from "node:fs";
|
|
25
|
+
import { createRequire } from "node:module";
|
|
26
|
+
import { tmpdir } from "node:os";
|
|
27
|
+
import { basename, dirname, extname, join, relative, resolve, sep } from "node:path";
|
|
28
|
+
import { fileURLToPath } from "node:url";
|
|
29
|
+
|
|
30
|
+
const require = createRequire(import.meta.url);
|
|
31
|
+
|
|
32
|
+
// ------------------------------------------------------------ running ---
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Run a workflow on a copy of `target` and return what happened.
|
|
36
|
+
* Resolves for every outcome (check `run.status`); rejects only when the
|
|
37
|
+
* workflow cannot be run at all (it does not build, the CLI is missing) or
|
|
38
|
+
* an `until` / `given` label matches no step.
|
|
39
|
+
*/
|
|
40
|
+
export async function runWorkflow(workflow, options) {
|
|
41
|
+
if (!options || options.target === undefined) {
|
|
42
|
+
throw new TypeError("runWorkflow(workflow, { target }): target is required");
|
|
43
|
+
}
|
|
44
|
+
const dir = mkdtempSync(join(tmpdir(), "codemod-test-"));
|
|
45
|
+
try {
|
|
46
|
+
materialize(options.target, dir);
|
|
47
|
+
const before = readTree(dir);
|
|
48
|
+
const result = (await inProcess(workflow, options))
|
|
49
|
+
? await runInProcess(workflow, options, dir)
|
|
50
|
+
: await harness(
|
|
51
|
+
{
|
|
52
|
+
workflow: toPath(workflow),
|
|
53
|
+
workflowName: options.workflowName,
|
|
54
|
+
target: dir,
|
|
55
|
+
params: options.params ?? {},
|
|
56
|
+
until: options.until,
|
|
57
|
+
given: options.given ?? {},
|
|
58
|
+
approve: options.approve ?? true,
|
|
59
|
+
capabilities: options.capabilities ?? [],
|
|
60
|
+
},
|
|
61
|
+
options.signal,
|
|
62
|
+
);
|
|
63
|
+
const files = readTree(dir);
|
|
64
|
+
checkLabels(result, options);
|
|
65
|
+
return new WorkflowRun(result, dir, before, files);
|
|
66
|
+
} finally {
|
|
67
|
+
if (!options.keep) rmSync(dir, { recursive: true, force: true });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Whether this run goes to `codemod/host` rather than the CLI. */
|
|
72
|
+
async function inProcess(workflow, options) {
|
|
73
|
+
const host = options.host ?? "node";
|
|
74
|
+
if (host === "quickjs") return false;
|
|
75
|
+
if (host !== "node") throw new TypeError(`host must be "node" or "quickjs", not ${host}`);
|
|
76
|
+
if (workflow && typeof workflow === "object" && !isUrl(workflow)) return true;
|
|
77
|
+
const { addon } = await import("../runtime/addon.mjs");
|
|
78
|
+
let path;
|
|
79
|
+
try {
|
|
80
|
+
path = addon().resolveWorkflow(toPath(workflow), options.workflowName ?? null);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
// Without the addon, fall back to the CLI unless Node was asked for.
|
|
83
|
+
if (options.host === undefined && /native addon/.test(error.message)) return false;
|
|
84
|
+
throw new Error(`cannot run ${toPath(workflow)}: ${error.message}`);
|
|
85
|
+
}
|
|
86
|
+
// YAML workflows run in the engine.
|
|
87
|
+
return /\.m?[jt]s$/.test(path);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function runInProcess(workflow, options, dir) {
|
|
91
|
+
const { runWorkflow: run } = await import("../runtime/host.mjs");
|
|
92
|
+
let report;
|
|
93
|
+
try {
|
|
94
|
+
report = await run(isPath(workflow) ? toPath(workflow) : workflow, {
|
|
95
|
+
target: dir,
|
|
96
|
+
params: options.params ?? {},
|
|
97
|
+
until: options.until,
|
|
98
|
+
given: options.until !== undefined || options.given ? (options.given ?? {}) : undefined,
|
|
99
|
+
approve: options.approve ?? true,
|
|
100
|
+
capabilities: options.capabilities ?? [],
|
|
101
|
+
workflowName: options.workflowName,
|
|
102
|
+
path: options.path === undefined ? undefined : toPath(options.path),
|
|
103
|
+
signal: options.signal,
|
|
104
|
+
captureConsole: true,
|
|
105
|
+
});
|
|
106
|
+
} catch (error) {
|
|
107
|
+
throw new Error(`cannot run ${describeWorkflow(workflow, options)}: ${error.message}`, {
|
|
108
|
+
cause: error,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return { ...report, logs: logsOf(report.events) };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function describeWorkflow(workflow, options) {
|
|
115
|
+
if (isPath(workflow)) return toPath(workflow);
|
|
116
|
+
return options.path === undefined ? "the workflow" : toPath(options.path);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Log lines from the engine's events, with the label of the step that wrote each. */
|
|
120
|
+
function logsOf(events) {
|
|
121
|
+
const labels = new Map();
|
|
122
|
+
const lines = [];
|
|
123
|
+
for (const event of events) {
|
|
124
|
+
if (event.event === "step_started") labels.set(event.position, event.label);
|
|
125
|
+
else if (event.event === "step_log") {
|
|
126
|
+
lines.push({ step: labels.get(event.position) ?? null, line: event.line });
|
|
127
|
+
} else if (event.event === "run_log") lines.push({ step: null, line: event.line });
|
|
128
|
+
}
|
|
129
|
+
return lines;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
class WorkflowRun {
|
|
133
|
+
constructor(result, dir, before, files) {
|
|
134
|
+
this.status = result.status;
|
|
135
|
+
this.output = result.output;
|
|
136
|
+
this.error = result.error;
|
|
137
|
+
this.awaiting = result.awaiting;
|
|
138
|
+
this.stoppedAt = result.stoppedAt;
|
|
139
|
+
this.dir = dir;
|
|
140
|
+
this.files = Object.freeze(files);
|
|
141
|
+
this.changes = Object.freeze(changes(before, files));
|
|
142
|
+
this.steps = Object.freeze(result.steps.map((step) => Object.freeze(step)));
|
|
143
|
+
this.logs = Object.freeze(result.logs.map((log) => Object.freeze(log)));
|
|
144
|
+
this.metrics = result.metrics;
|
|
145
|
+
Object.defineProperty(this, "before", { value: before, enumerable: false });
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Whether the run finished, or stopped at its `until` step. */
|
|
149
|
+
get ok() {
|
|
150
|
+
return this.status === "succeeded" || this.status === "stopped";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The first step with this label. Throws when there is none. */
|
|
154
|
+
step(label) {
|
|
155
|
+
const found = this.steps.find((step) => step.label === label);
|
|
156
|
+
if (!found) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
`no step is labeled ${JSON.stringify(label)}; the run's steps are: ${labels(this.steps)}`,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
return found;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** A unified diff of every file the run changed. */
|
|
165
|
+
diff() {
|
|
166
|
+
return Object.keys(this.changes)
|
|
167
|
+
.sort()
|
|
168
|
+
.map((path) => unifiedDiff(path, this.before[path] ?? "", this.changes[path] ?? ""))
|
|
169
|
+
.join("");
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function checkLabels(result, options) {
|
|
174
|
+
const ran = result.steps;
|
|
175
|
+
if (options.until !== undefined && result.status === "succeeded") {
|
|
176
|
+
throw new Error(
|
|
177
|
+
`until: no step is labeled ${JSON.stringify(options.until)}; the run's steps are: ${labels(ran)}`,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
if (result.status !== "succeeded") return;
|
|
181
|
+
const unused = Object.keys(options.given ?? {}).filter(
|
|
182
|
+
(label) => !ran.some((step) => step.given && step.label === label),
|
|
183
|
+
);
|
|
184
|
+
if (unused.length) {
|
|
185
|
+
throw new Error(
|
|
186
|
+
`given: no step is labeled ${unused.map((l) => JSON.stringify(l)).join(", ")}; the run's steps are: ${labels(ran)}`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function labels(steps) {
|
|
192
|
+
return steps.length ? steps.map((step) => JSON.stringify(step.label)).join(", ") : "(none)";
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function harness(spec, signal) {
|
|
196
|
+
const binary = codemodBinary();
|
|
197
|
+
return new Promise((resolvePromise, reject) => {
|
|
198
|
+
const child = spawn(binary, ["workflow", "harness"], {
|
|
199
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
200
|
+
env: { ...process.env, NO_COLOR: "1", DISABLE_ANALYTICS: "true" },
|
|
201
|
+
signal,
|
|
202
|
+
});
|
|
203
|
+
let stdout = "";
|
|
204
|
+
let stderr = "";
|
|
205
|
+
child.stdout.setEncoding("utf8").on("data", (chunk) => (stdout += chunk));
|
|
206
|
+
child.stderr.setEncoding("utf8").on("data", (chunk) => (stderr += chunk));
|
|
207
|
+
child.on("error", (error) => {
|
|
208
|
+
// Aborting stops the CLI; the run then ended as canceled, as it does
|
|
209
|
+
// when Node.js runs the workflow.
|
|
210
|
+
if (signal?.aborted) {
|
|
211
|
+
resolvePromise({
|
|
212
|
+
status: "canceled",
|
|
213
|
+
output: null,
|
|
214
|
+
error: null,
|
|
215
|
+
awaiting: null,
|
|
216
|
+
stoppedAt: null,
|
|
217
|
+
steps: [],
|
|
218
|
+
logs: [],
|
|
219
|
+
metrics: {},
|
|
220
|
+
});
|
|
221
|
+
} else {
|
|
222
|
+
reject(error);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
child.on("close", (code) => {
|
|
226
|
+
const line = stdout.trim().split("\n").pop();
|
|
227
|
+
if (code === 0 && line) {
|
|
228
|
+
try {
|
|
229
|
+
resolvePromise(JSON.parse(line));
|
|
230
|
+
return;
|
|
231
|
+
} catch {
|
|
232
|
+
// fall through to the error below
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const detail = (stderr.trim() || stdout.trim() || `exit code ${code}`).replace(
|
|
236
|
+
/^Error:\s*/,
|
|
237
|
+
"",
|
|
238
|
+
);
|
|
239
|
+
reject(new Error(`cannot run ${spec.workflow}: ${detail}`));
|
|
240
|
+
});
|
|
241
|
+
child.stdin.end(JSON.stringify(spec));
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
let binaryPath;
|
|
246
|
+
|
|
247
|
+
/** The codemod CLI binary: `CODEMOD_BINARY`, or the one this package installed. */
|
|
248
|
+
function codemodBinary() {
|
|
249
|
+
if (process.env.CODEMOD_BINARY) return process.env.CODEMOD_BINARY;
|
|
250
|
+
if (binaryPath) return binaryPath;
|
|
251
|
+
const { findBinaryPath } = require("../codemod");
|
|
252
|
+
const found = findBinaryPath();
|
|
253
|
+
if (!found.binaryPath) {
|
|
254
|
+
throw new Error(
|
|
255
|
+
`cannot find the codemod binary${found.pkgName ? ` (expected the ${found.pkgName} package)` : ""}; reinstall codemod with optional dependencies, or set CODEMOD_BINARY`,
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
binaryPath = found.binaryPath;
|
|
259
|
+
return binaryPath;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// ------------------------------------------------------------ fixtures ---
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* The fixtures in `dir`: each subdirectory with `input/` and `expected/`, or
|
|
266
|
+
* `input.<ext>` and `expected.<ext>`, and optionally `params.json`. Groups of
|
|
267
|
+
* fixtures may be nested.
|
|
268
|
+
*/
|
|
269
|
+
export function fixtures(dir) {
|
|
270
|
+
const root = toPath(dir);
|
|
271
|
+
if (!existsSync(root) || !statSync(root).isDirectory()) {
|
|
272
|
+
throw new Error(`no fixtures directory at ${root}`);
|
|
273
|
+
}
|
|
274
|
+
const found = [];
|
|
275
|
+
// The fixtures directory may itself hold `input/` and `expected/`.
|
|
276
|
+
if (isDir(join(root, "input"))) {
|
|
277
|
+
found.push(
|
|
278
|
+
fixture(basename(root), root, join(root, "input"), join(root, "expected"), readParams(root)),
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
collect(root, root, found);
|
|
282
|
+
return found;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function isDir(path) {
|
|
286
|
+
return existsSync(path) && statSync(path).isDirectory();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function collect(root, dir, found) {
|
|
290
|
+
// A fixture's own input/ and expected/ are not fixtures.
|
|
291
|
+
const own = isDir(join(dir, "input")) ? new Set(["input", "expected"]) : new Set();
|
|
292
|
+
const subdirs = readdirSync(dir, { withFileTypes: true })
|
|
293
|
+
.filter((entry) => entry.isDirectory() && !own.has(entry.name))
|
|
294
|
+
.map((entry) => join(dir, entry.name))
|
|
295
|
+
.sort();
|
|
296
|
+
for (const sub of subdirs) {
|
|
297
|
+
const name = relative(root, sub).split(sep).join("/");
|
|
298
|
+
const params = readParams(sub);
|
|
299
|
+
if (isDir(join(sub, "input"))) {
|
|
300
|
+
found.push(fixture(name, sub, join(sub, "input"), join(sub, "expected"), params));
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
const inputs = readdirSync(sub).filter(
|
|
304
|
+
(file) => basename(file, extname(file)) === "input" && statSync(join(sub, file)).isFile(),
|
|
305
|
+
);
|
|
306
|
+
if (inputs.length > 1) {
|
|
307
|
+
throw new Error(
|
|
308
|
+
`${sub} has more than one input file; use input/ and expected/ directories instead`,
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
if (inputs.length === 1) {
|
|
312
|
+
const input = join(sub, inputs[0]);
|
|
313
|
+
found.push(fixture(name, sub, input, join(sub, `expected${extname(input)}`), params));
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
collect(root, sub, found);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function fixture(name, dir, input, expected, params) {
|
|
321
|
+
return Object.freeze({ name, dir, input, expected, params });
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function readParams(dir) {
|
|
325
|
+
const path = join(dir, "params.json");
|
|
326
|
+
if (!existsSync(path)) return {};
|
|
327
|
+
const params = JSON.parse(readFileSync(path, "utf8"));
|
|
328
|
+
if (!params || typeof params !== "object" || Array.isArray(params)) {
|
|
329
|
+
throw new Error(`${path} must hold a JSON object`);
|
|
330
|
+
}
|
|
331
|
+
return params;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// ----------------------------------------------------------- assertions ---
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Assert that a run left the expected files. `actual` is a run, a directory,
|
|
338
|
+
* or files by path; `expected` is a directory, a single file, or files by
|
|
339
|
+
* path. With `CODEMOD_UPDATE_FIXTURES=1`, an expected directory or file is
|
|
340
|
+
* rewritten to match instead.
|
|
341
|
+
*/
|
|
342
|
+
export function assertTree(actual, expected, options = {}) {
|
|
343
|
+
if (actual instanceof WorkflowRun && !actual.ok) {
|
|
344
|
+
throw new AssertionError({
|
|
345
|
+
message: describeFailure(actual),
|
|
346
|
+
actual: actual.status,
|
|
347
|
+
expected: "succeeded",
|
|
348
|
+
operator: "assertTree",
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
const actualTree = actual instanceof WorkflowRun ? actual.files : treeOf(actual);
|
|
352
|
+
const expectedPath = isPath(expected) ? toPath(expected) : null;
|
|
353
|
+
const single =
|
|
354
|
+
expectedPath !== null && existsSync(expectedPath) && statSync(expectedPath).isFile();
|
|
355
|
+
|
|
356
|
+
if (expectedPath !== null && updating()) {
|
|
357
|
+
// A file (`expected.ts`) when one exists or the name has an extension;
|
|
358
|
+
// otherwise a directory.
|
|
359
|
+
const asFile = single || (!existsSync(expectedPath) && extname(expectedPath) !== "");
|
|
360
|
+
if (asFile) {
|
|
361
|
+
writeFileSync(expectedPath, soleFile(actualTree, expectedPath)[1]);
|
|
362
|
+
} else {
|
|
363
|
+
rmSync(expectedPath, { recursive: true, force: true });
|
|
364
|
+
writeTree(expectedPath, actualTree);
|
|
365
|
+
}
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (single) {
|
|
370
|
+
const [path, text] = soleFile(actualTree, expectedPath);
|
|
371
|
+
const want = normalize(readFileSync(expectedPath, "utf8"));
|
|
372
|
+
if (!same(text, want, options)) {
|
|
373
|
+
fail(
|
|
374
|
+
`${path} differs from ${expectedPath}`,
|
|
375
|
+
text,
|
|
376
|
+
want,
|
|
377
|
+
unifiedDiff(path, want, text),
|
|
378
|
+
options,
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const expectedTree = treeOf(expected);
|
|
385
|
+
const paths = [...new Set([...Object.keys(expectedTree), ...Object.keys(actualTree)])].sort();
|
|
386
|
+
const differing = paths.filter(
|
|
387
|
+
(path) =>
|
|
388
|
+
!(path in expectedTree) ||
|
|
389
|
+
!(path in actualTree) ||
|
|
390
|
+
!same(actualTree[path], expectedTree[path], options),
|
|
391
|
+
);
|
|
392
|
+
if (!differing.length) return;
|
|
393
|
+
|
|
394
|
+
const report = differing.map((path) => {
|
|
395
|
+
if (!(path in actualTree)) return `missing ${path}\n`;
|
|
396
|
+
if (!(path in expectedTree)) return `unexpected ${path}\n`;
|
|
397
|
+
return unifiedDiff(path, expectedTree[path], actualTree[path]);
|
|
398
|
+
});
|
|
399
|
+
const only = differing.length === 1 && differing[0] in actualTree && differing[0] in expectedTree;
|
|
400
|
+
const title = only
|
|
401
|
+
? `${differing[0]} differs from the expected output`
|
|
402
|
+
: `${differing.length} ${differing.length === 1 ? "file differs" : "files differ"} from the expected output`;
|
|
403
|
+
if (only) {
|
|
404
|
+
fail(title, actualTree[differing[0]], expectedTree[differing[0]], report.join(""), options);
|
|
405
|
+
}
|
|
406
|
+
fail(
|
|
407
|
+
title,
|
|
408
|
+
render(actualTree, differing),
|
|
409
|
+
render(expectedTree, differing),
|
|
410
|
+
report.join(""),
|
|
411
|
+
options,
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function fail(title, actual, expected, diff, options) {
|
|
416
|
+
const message = options.message ? `${options.message}\n${title}` : title;
|
|
417
|
+
throw new AssertionError({
|
|
418
|
+
message: `${message}\n\n${diff.trimEnd()}\n${updateHint()}`,
|
|
419
|
+
actual,
|
|
420
|
+
expected,
|
|
421
|
+
operator: "assertTree",
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function describeFailure(run) {
|
|
426
|
+
const what = {
|
|
427
|
+
failed: `the run failed: ${run.error}`,
|
|
428
|
+
canceled: "the run was canceled",
|
|
429
|
+
"awaiting-approval": `the run is waiting for an approval: ${run.awaiting}`,
|
|
430
|
+
}[run.status];
|
|
431
|
+
const logs = run.logs.length
|
|
432
|
+
? `\n\nlogs:\n${run.logs.map((log) => ` ${log.step ? `[${log.step}] ` : ""}${log.line}`).join("\n")}`
|
|
433
|
+
: "";
|
|
434
|
+
return `${what ?? `the run ended as ${run.status}`}${logs}`;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function updateHint() {
|
|
438
|
+
return "(set CODEMOD_UPDATE_FIXTURES=1 to update expected files from the run)";
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function updating() {
|
|
442
|
+
const value = process.env.CODEMOD_UPDATE_FIXTURES;
|
|
443
|
+
return value === "1" || value === "true";
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function same(a, b, options) {
|
|
447
|
+
if (options.ignoreWhitespace) return squash(a) === squash(b);
|
|
448
|
+
return a === b;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function squash(text) {
|
|
452
|
+
return text
|
|
453
|
+
.split("\n")
|
|
454
|
+
.map((line) => line.trimEnd())
|
|
455
|
+
.join("\n")
|
|
456
|
+
.trimEnd();
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function soleFile(tree, expectedPath) {
|
|
460
|
+
const entries = Object.entries(tree);
|
|
461
|
+
if (entries.length === 1) return entries[0];
|
|
462
|
+
throw new AssertionError({
|
|
463
|
+
message: `${expectedPath} is one file, but the run left ${entries.length} (${Object.keys(tree).join(", ") || "none"}); compare against a directory to test more than one file`,
|
|
464
|
+
actual: Object.keys(tree),
|
|
465
|
+
expected: [basename(expectedPath)],
|
|
466
|
+
operator: "assertTree",
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function render(tree, paths) {
|
|
471
|
+
return paths
|
|
472
|
+
.map((path) => (path in tree ? `// ${path}\n${tree[path]}` : `// ${path} (no file)\n`))
|
|
473
|
+
.join("\n");
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// --------------------------------------------------------------- trees ---
|
|
477
|
+
|
|
478
|
+
function treeOf(value) {
|
|
479
|
+
if (isPath(value)) return readTree(toPath(value));
|
|
480
|
+
if (value && typeof value === "object") {
|
|
481
|
+
const tree = {};
|
|
482
|
+
for (const [path, text] of Object.entries(value)) {
|
|
483
|
+
if (text === null) continue;
|
|
484
|
+
if (typeof text !== "string") throw new TypeError(`file ${path} must be a string`);
|
|
485
|
+
tree[path.replace(/^\.\//, "")] = normalize(text);
|
|
486
|
+
}
|
|
487
|
+
return tree;
|
|
488
|
+
}
|
|
489
|
+
throw new TypeError("expected a directory path, a URL, or files by path");
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function materialize(target, dir) {
|
|
493
|
+
if (isPath(target)) {
|
|
494
|
+
const path = toPath(target);
|
|
495
|
+
if (!existsSync(path)) throw new Error(`target ${path} does not exist`);
|
|
496
|
+
if (statSync(path).isFile()) cpSync(path, join(dir, basename(path)));
|
|
497
|
+
else cpSync(path, dir, { recursive: true });
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
writeTree(dir, treeOf(target));
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function writeTree(dir, tree) {
|
|
504
|
+
mkdirSync(dir, { recursive: true });
|
|
505
|
+
for (const [path, text] of Object.entries(tree)) {
|
|
506
|
+
const file = join(dir, path);
|
|
507
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
508
|
+
writeFileSync(file, text);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function readTree(dir) {
|
|
513
|
+
const tree = {};
|
|
514
|
+
if (!existsSync(dir)) return tree;
|
|
515
|
+
const walk = (current) => {
|
|
516
|
+
for (const entry of readdirSync(current, { withFileTypes: true })) {
|
|
517
|
+
const path = join(current, entry.name);
|
|
518
|
+
if (entry.isDirectory()) walk(path);
|
|
519
|
+
else tree[relative(dir, path).split(sep).join("/")] = normalize(readFileSync(path, "utf8"));
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
walk(dir);
|
|
523
|
+
return Object.fromEntries(Object.entries(tree).sort(([a], [b]) => (a < b ? -1 : 1)));
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function changes(before, after) {
|
|
527
|
+
const changed = {};
|
|
528
|
+
for (const [path, text] of Object.entries(after)) {
|
|
529
|
+
if (before[path] !== text) changed[path] = text;
|
|
530
|
+
}
|
|
531
|
+
for (const path of Object.keys(before)) {
|
|
532
|
+
if (!(path in after)) changed[path] = null;
|
|
533
|
+
}
|
|
534
|
+
return changed;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function normalize(text) {
|
|
538
|
+
return text.replace(/\r\n/g, "\n");
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function toPath(value) {
|
|
542
|
+
if (isUrl(value)) return fileURLToPath(value.href);
|
|
543
|
+
if (typeof value === "string" && value.startsWith("file:")) return fileURLToPath(value);
|
|
544
|
+
if (typeof value === "string") return resolve(value);
|
|
545
|
+
throw new TypeError("expected a path or a file URL");
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// Not `instanceof URL`: test environments may have their own URL class.
|
|
549
|
+
function isUrl(value) {
|
|
550
|
+
return value !== null && typeof value === "object" && typeof value.href === "string";
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function isPath(value) {
|
|
554
|
+
return typeof value === "string" || isUrl(value);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// ---------------------------------------------------------------- diff ---
|
|
558
|
+
|
|
559
|
+
/** A unified diff of two texts, by line, with three lines of context. */
|
|
560
|
+
function unifiedDiff(path, from, to) {
|
|
561
|
+
const a = splitLines(from);
|
|
562
|
+
const b = splitLines(to);
|
|
563
|
+
const ops = diffLines(a, b);
|
|
564
|
+
if (ops.every((op) => op[0] === " ")) return "";
|
|
565
|
+
const hunks = [];
|
|
566
|
+
const context = 3;
|
|
567
|
+
let i = 0;
|
|
568
|
+
while (i < ops.length) {
|
|
569
|
+
if (ops[i][0] === " ") {
|
|
570
|
+
i++;
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
let start = Math.max(0, i - context);
|
|
574
|
+
let end = i;
|
|
575
|
+
// Extend the hunk while changes are within 2 * context lines of each other.
|
|
576
|
+
while (end < ops.length) {
|
|
577
|
+
let next = end;
|
|
578
|
+
while (next < ops.length && ops[next][0] === " ") next++;
|
|
579
|
+
if (next >= ops.length || next - end > context * 2) break;
|
|
580
|
+
while (next < ops.length && ops[next][0] !== " ") next++;
|
|
581
|
+
end = next;
|
|
582
|
+
}
|
|
583
|
+
end = Math.min(ops.length, end + context);
|
|
584
|
+
hunks.push([start, end]);
|
|
585
|
+
i = end;
|
|
586
|
+
}
|
|
587
|
+
let out = `--- expected/${path}\n+++ actual/${path}\n`;
|
|
588
|
+
for (const [start, end] of hunks) {
|
|
589
|
+
let aLine = 1;
|
|
590
|
+
let bLine = 1;
|
|
591
|
+
for (let k = 0; k < start; k++) {
|
|
592
|
+
if (ops[k][0] !== "+") aLine++;
|
|
593
|
+
if (ops[k][0] !== "-") bLine++;
|
|
594
|
+
}
|
|
595
|
+
const slice = ops.slice(start, end);
|
|
596
|
+
const aCount = slice.filter((op) => op[0] !== "+").length;
|
|
597
|
+
const bCount = slice.filter((op) => op[0] !== "-").length;
|
|
598
|
+
out += `@@ -${aLine},${aCount} +${bLine},${bCount} @@\n`;
|
|
599
|
+
for (const [sign, line] of slice) out += `${sign}${line}\n`;
|
|
600
|
+
}
|
|
601
|
+
return out;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function splitLines(text) {
|
|
605
|
+
if (text === "") return [];
|
|
606
|
+
const lines = text.split("\n");
|
|
607
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
608
|
+
return lines;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/** Longest-common-subsequence line diff: a list of [" " | "-" | "+", line]. */
|
|
612
|
+
function diffLines(a, b) {
|
|
613
|
+
const n = a.length;
|
|
614
|
+
const m = b.length;
|
|
615
|
+
const table = Array.from({ length: n + 1 }, () => new Uint32Array(m + 1));
|
|
616
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
617
|
+
for (let j = m - 1; j >= 0; j--) {
|
|
618
|
+
table[i][j] =
|
|
619
|
+
a[i] === b[j] ? table[i + 1][j + 1] + 1 : Math.max(table[i + 1][j], table[i][j + 1]);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
const ops = [];
|
|
623
|
+
let i = 0;
|
|
624
|
+
let j = 0;
|
|
625
|
+
while (i < n && j < m) {
|
|
626
|
+
if (a[i] === b[j]) {
|
|
627
|
+
ops.push([" ", a[i++]]);
|
|
628
|
+
j++;
|
|
629
|
+
} else if (table[i + 1][j] >= table[i][j + 1]) {
|
|
630
|
+
ops.push(["-", a[i++]]);
|
|
631
|
+
} else {
|
|
632
|
+
ops.push(["+", b[j++]]);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
while (i < n) ops.push(["-", a[i++]]);
|
|
636
|
+
while (j < m) ops.push(["+", b[j++]]);
|
|
637
|
+
return ops;
|
|
638
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// oxlint-disable triple-slash-reference
|
|
2
|
+
/// <reference path="llrt/index.d.ts" />
|
|
3
|
+
/// <reference path="modules/ast-grep.d.ts" />
|
|
4
|
+
/// <reference path="modules/llm.d.ts" />
|
|
5
|
+
/// <reference path="modules/metrics.d.ts" />
|
|
6
|
+
/// <reference path="modules/runtime.d.ts" />
|
|
7
|
+
/// <reference path="modules/workflow.d.ts" />
|
|
8
|
+
/// <reference path="modules/langs.d.ts" />
|