|
1 | 1 | // Tsdown config tests protect package artifact build contracts. |
2 | | -import { spawnSync } from "node:child_process"; |
| 2 | +import { execFile } from "node:child_process"; |
3 | 3 | import { createHash } from "node:crypto"; |
4 | 4 | import fs from "node:fs"; |
5 | 5 | import { createRequire } from "node:module"; |
@@ -137,59 +137,82 @@ describe("tsdown config", () => { |
137 | 137 | relocatedRoot, |
138 | 138 | worker ? "output/dist/native" : "dist/native", |
139 | 139 | ); |
140 | | - const probe = ( |
| 140 | + const probe = async ( |
141 | 141 | name: string, |
142 | 142 | mode: string, |
143 | 143 | outcome: string, |
144 | 144 | override: NodeJS.ProcessEnv = {}, |
145 | 145 | ) => { |
146 | 146 | const rootDir = path.join(relocatedRoot, name); |
147 | 147 | 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 | + }, |
174 | 181 | }, |
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(); |
178 | 186 | expect(result.status, `${name}\n${result.stdout}\n${result.stderr}`).toBe(0); |
179 | 187 | }; |
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 | + ]); |
185 | 206 | const assets = nativeAssetInventory(nativeSource); |
186 | 207 | expect(assets).toHaveLength(7); |
187 | 208 | expect(nativeAssetInventory(nativeOutput)).toEqual(assets); |
188 | 209 | 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 | + ]); |
193 | 216 | } finally { |
194 | 217 | for (const bundle of bundles) { |
195 | 218 | await bundle[Symbol.asyncDispose](); |
|
0 commit comments