Skip to content

Commit 86a0b22

Browse files
authored
test: overlap native fs-safe probes within asset phases (#132168)
1 parent 486d075 commit 86a0b22

1 file changed

Lines changed: 63 additions & 40 deletions

File tree

‎test/scripts/tsdown-config.test.ts‎

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Tsdown config tests protect package artifact build contracts.
2-
import { spawnSync } from "node:child_process";
2+
import { execFile } from "node:child_process";
33
import { createHash } from "node:crypto";
44
import fs from "node:fs";
55
import { createRequire } from "node:module";
@@ -137,59 +137,82 @@ describe("tsdown config", () => {
137137
relocatedRoot,
138138
worker ? "output/dist/native" : "dist/native",
139139
);
140-
const probe = (
140+
const probe = async (
141141
name: string,
142142
mode: string,
143143
outcome: string,
144144
override: NodeJS.ProcessEnv = {},
145145
) => {
146146
const rootDir = path.join(relocatedRoot, name);
147147
fs.mkdirSync(rootDir);
148-
const result = spawnSync(
149-
process.execPath,
150-
[
151-
"--input-type=module",
152-
"--eval",
153-
FS_SAFE_CALLER_PROBE,
154-
entry,
155-
observer,
156-
rootDir,
157-
mode,
158-
outcome,
159-
],
160-
{
161-
cwd: relocatedRoot,
162-
encoding: "utf8",
163-
timeout: 30_000,
164-
env: {
165-
PATH: process.env.PATH,
166-
SystemRoot: process.env.SystemRoot,
167-
WINDIR: process.env.WINDIR,
168-
HOME: temporaryRoot,
169-
USERPROFILE: temporaryRoot,
170-
TMPDIR: temporaryRoot,
171-
TMP: temporaryRoot,
172-
TEMP: temporaryRoot,
173-
...override,
148+
const result = await new Promise<{
149+
error: Error | null;
150+
status: number | null;
151+
stdout: string;
152+
stderr: string;
153+
}>((resolve) => {
154+
const child = execFile(
155+
process.execPath,
156+
[
157+
"--input-type=module",
158+
"--eval",
159+
FS_SAFE_CALLER_PROBE,
160+
entry,
161+
observer,
162+
rootDir,
163+
mode,
164+
outcome,
165+
],
166+
{
167+
cwd: relocatedRoot,
168+
encoding: "utf8",
169+
timeout: 30_000,
170+
env: {
171+
PATH: process.env.PATH,
172+
SystemRoot: process.env.SystemRoot,
173+
WINDIR: process.env.WINDIR,
174+
HOME: temporaryRoot,
175+
USERPROFILE: temporaryRoot,
176+
TMPDIR: temporaryRoot,
177+
TMP: temporaryRoot,
178+
TEMP: temporaryRoot,
179+
...override,
180+
},
174181
},
175-
},
176-
);
177-
expect(result.error, name).toBeUndefined();
182+
(error, stdout, stderr) => resolve({ error, status: child.exitCode, stdout, stderr }),
183+
);
184+
});
185+
expect(result.error, name).toBeNull();
178186
expect(result.status, `${name}\n${result.stdout}\n${result.stderr}`).toBe(0);
179187
};
180-
for (const key of ["FS_SAFE_NATIVE_MODE", "OPENCLAW_FS_SAFE_NATIVE_MODE"]) {
181-
probe(key, "require", "native", { [key]: "require" });
182-
}
183-
probe("shared-config", "configured", "native");
184-
probe("default", "off", "fallback");
188+
const joinProbes = async (probes: Promise<void>[]) => {
189+
// Every child must close before assets are removed or bundles disposed,
190+
// including when a sibling probe fails.
191+
const results = await Promise.allSettled(probes);
192+
const failures = results.flatMap((result) =>
193+
result.status === "rejected" ? [result.reason] : [],
194+
);
195+
if (failures.length) {
196+
throw new AggregateError(failures, "Native fs-safe probes failed");
197+
}
198+
};
199+
await joinProbes([
200+
...["FS_SAFE_NATIVE_MODE", "OPENCLAW_FS_SAFE_NATIVE_MODE"].map((key) =>
201+
probe(key, "require", "native", { [key]: "require" }),
202+
),
203+
probe("shared-config", "configured", "native"),
204+
probe("default", "off", "fallback"),
205+
]);
185206
const assets = nativeAssetInventory(nativeSource);
186207
expect(assets).toHaveLength(7);
187208
expect(nativeAssetInventory(nativeOutput)).toEqual(assets);
188209
fs.rmSync(nativeOutput, { recursive: true });
189-
probe("missing", "require", "missing", { FS_SAFE_NATIVE_MODE: "require" });
190-
for (const mode of ["off", "auto"]) {
191-
probe(mode, mode, "fallback", { FS_SAFE_NATIVE_MODE: mode });
192-
}
210+
await joinProbes([
211+
probe("missing", "require", "missing", { FS_SAFE_NATIVE_MODE: "require" }),
212+
...["off", "auto"].map((mode) =>
213+
probe(mode, mode, "fallback", { FS_SAFE_NATIVE_MODE: mode }),
214+
),
215+
]);
193216
} finally {
194217
for (const bundle of bundles) {
195218
await bundle[Symbol.asyncDispose]();

0 commit comments

Comments
 (0)