mirror of
https://github.com/morluto/rea.git
synced 2026-11-14 18:52:48 +00:00
test: strengthen regression oracles and cleanup ownership (#8152)
* test: strengthen suite oracles and cleanup ownership * test: reuse setup across independent scenarios (#8153) * Potential fix for pull request finding 'CodeQL / Improper code sanitization' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
1ddee1089b
commit
675d4076de
98 files changed
+1373
-1034
No files matched your search
@@ -14,6 +14,28 @@ import {
|
||||
|
||||
const realSetTimeout = globalThis.setTimeout;
|
||||
|
||||
const inlineJavaScriptCharMap: Record<string, string> = {
|
||||
"<": "\\u003C",
|
||||
">": "\\u003E",
|
||||
"/": "\\u002F",
|
||||
"\\": "\\\\",
|
||||
"\b": "\\b",
|
||||
"\f": "\\f",
|
||||
"\n": "\\n",
|
||||
"\r": "\\r",
|
||||
"\t": "\\t",
|
||||
"\0": "\\0",
|
||||
"\u2028": "\\u2028",
|
||||
"\u2029": "\\u2029",
|
||||
};
|
||||
|
||||
function escapeUnsafeCharsForInlineJavaScript(value: string): string {
|
||||
return value.replace(
|
||||
/[<>/\\\b\f\n\r\t\0\u2028\u2029]/g,
|
||||
(character) => inlineJavaScriptCharMap[character] ?? character,
|
||||
);
|
||||
}
|
||||
|
||||
const waitForFile = async (path: string): Promise<boolean> => {
|
||||
for (let attempt = 0; attempt < 500; attempt += 1) {
|
||||
if (
|
||||
@@ -297,30 +319,34 @@ describe("execFileOutput cancellation lifecycle", () => {
|
||||
async () => {
|
||||
const directory = await createTestTempDirectory("rea-exec-double-stop-");
|
||||
const ready = join(directory, "ready");
|
||||
const interrupted = join(directory, "interrupted");
|
||||
const script = [
|
||||
'const fs = require("node:fs");',
|
||||
'for (const name of ["SIGINT", "SIGTERM"]) process.on(name, () => {});',
|
||||
`fs.writeFileSync(${JSON.stringify(ready)}, "");`,
|
||||
`process.on("SIGINT", () => fs.writeFileSync(${escapeUnsafeCharsForInlineJavaScript(JSON.stringify(interrupted))}, ""));`,
|
||||
`fs.writeFileSync(${escapeUnsafeCharsForInlineJavaScript(JSON.stringify(ready))}, "");`,
|
||||
"setInterval(() => {}, 1000);",
|
||||
].join("\n");
|
||||
const controller = new AbortController();
|
||||
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
||||
const execution = execFileOutput(process.execPath, ["-e", script], {
|
||||
signal: controller.signal,
|
||||
timeout: 10_000,
|
||||
stopSignal: "SIGINT",
|
||||
});
|
||||
const startedAt = Date.now();
|
||||
expect(await waitForFile(ready)).toBe(true);
|
||||
const remainingBeforeDeadline = Math.max(
|
||||
0,
|
||||
9_000 - (Date.now() - startedAt),
|
||||
);
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(resolve, remainingBeforeDeadline),
|
||||
);
|
||||
controller.abort();
|
||||
|
||||
await expect(execution).rejects.toMatchObject({ name: "AbortError" });
|
||||
}).catch((cause: unknown) => cause);
|
||||
try {
|
||||
expect(await waitForFile(ready)).toBe(true);
|
||||
await vi.advanceTimersByTimeAsync(9_000);
|
||||
controller.abort();
|
||||
expect(await waitForFile(interrupted)).toBe(true);
|
||||
await vi.advanceTimersByTimeAsync(1_000);
|
||||
await vi.advanceTimersByTimeAsync(4_000);
|
||||
expect(await execution).toMatchObject({ name: "AbortError" });
|
||||
} finally {
|
||||
controller.abort();
|
||||
await vi.runAllTimersAsync();
|
||||
vi.useRealTimers();
|
||||
await execution;
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
@@ -391,17 +417,22 @@ describe.skipIf(process.platform === "win32")(
|
||||
`fs.writeFileSync(${JSON.stringify(ready)}, "");`,
|
||||
"setInterval(() => {}, 1000);",
|
||||
].join("\n");
|
||||
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
||||
const execution = execFileOutput(process.execPath, ["-e", script], {
|
||||
timeout: 10_000,
|
||||
stopSignal: "SIGINT",
|
||||
}).catch((cause: unknown) => cause);
|
||||
try {
|
||||
const execution = execFileOutput(process.execPath, ["-e", script], {
|
||||
timeout: 10_000,
|
||||
stopSignal: "SIGINT",
|
||||
});
|
||||
expect(await waitForFile(ready)).toBe(true);
|
||||
await expect(execution).rejects.toMatchObject({
|
||||
await vi.advanceTimersByTimeAsync(10_000);
|
||||
expect(await execution).toMatchObject({
|
||||
code: "ETIMEDOUT",
|
||||
signal: null,
|
||||
});
|
||||
} finally {
|
||||
await vi.runAllTimersAsync();
|
||||
vi.useRealTimers();
|
||||
await execution;
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user