Files
reverse-engineering-anything/tests/fixtures/darwinHelperToolchain.ts
T
morluto a2acbe3939 test(process): consolidate Darwin helper cache publication and cancellation (#12898)
test(process): verify shared helper publication and cancellation at ownership boundary
2026-11-20 17:54:30 +00:00

70 lines
2.6 KiB
TypeScript

import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
/**
* Fake xcrun: answers toolchain lookups from fixture files and, given
* `swiftc … -o OUT`, writes a helper that reads every requested PID.
*/
export const writeFakeXcrun = async (
directory: string,
blocking = false,
stalledLookups = false,
) => {
const executable = join(directory, "fake-xcrun");
const swiftc = join(directory, "toolchain", "swiftc");
const sdk = join(directory, "sdk");
const countPath = join(directory, "compile-count");
const rootsPath = join(directory, "compile-roots");
const lookupsPath = join(directory, "stalled-lookups");
await mkdir(join(directory, "toolchain"), { recursive: true });
await mkdir(sdk, { recursive: true });
await writeFile(swiftc, "compiler v1");
await writeFile(join(sdk, "SDKSettings.json"), '{"Version":"1"}');
const helper = `#!/usr/bin/env node
const pids = process.argv.slice(2).filter((value) => value !== "--identities");
process.stdout.write(JSON.stringify({ results: pids.map((pid) => ({ pid: Number(pid), state: "readable", run_id: "fixture" })) }));
`;
await writeFile(
executable,
`#!/usr/bin/env node
const fs = process.getBuiltinModule("node:fs");
const path = process.getBuiltinModule("node:path");
const args = process.argv.slice(2);
const compile = () => {
const outputIndex = args.indexOf("-o");
if (outputIndex < 0) process.exit(64);
const output = args[outputIndex + 1];
fs.appendFileSync(${JSON.stringify(countPath)}, "x");
fs.appendFileSync(${JSON.stringify(rootsPath)}, path.dirname(output) + "\\n");
if (${String(blocking)}) return setInterval(() => {}, 1000);
fs.writeFileSync(output, ${JSON.stringify(helper)});
fs.chmodSync(output, 0o755);
};
if (args[0] === "swiftc") compile();
else if (${String(stalledLookups)}) {
fs.appendFileSync(${JSON.stringify(lookupsPath)}, JSON.stringify(args) + "\\n");
setInterval(() => {}, 1000);
}
else if (args[0] === "--find") process.stdout.write(${JSON.stringify(swiftc)} + "\\n");
else if (args[0] === "--show-sdk-path") process.stdout.write(${JSON.stringify(sdk)} + "\\n");
else process.exit(64);
`,
);
await chmod(executable, 0o755);
return {
executable,
swiftc,
sdk,
compilations: async () =>
(await readFile(countPath, "utf8").catch(() => "")).length,
stalledLookups: async () =>
(await readFile(lookupsPath, "utf8").catch(() => ""))
.split("\n")
.filter((line) => line !== ""),
roots: async () =>
(await readFile(rootsPath, "utf8").catch(() => ""))
.split("\n")
.filter((line) => line !== ""),
};
};