11import assert from "node:assert" ;
2- import { spawn } from "node:child_process" ;
32import fs from "node:fs" ;
43import path from "node:path" ;
54
5+ import { spawnTest } from "./child_process.js" ;
6+
67assert (
78 typeof import . meta. dirname === "string" ,
89 "Expecting a recent Node.js runtime API version" ,
910) ;
1011
1112const ROOT_PATH = path . resolve ( import . meta. dirname , ".." , ".." ) ;
1213const TESTS_ROOT_PATH = path . join ( ROOT_PATH , "tests" ) ;
13- const FEATURES_MODULE_PATH = path . join (
14- ROOT_PATH ,
15- "implementors" ,
16- "node" ,
17- "features.js" ,
18- ) ;
19- const ASSERT_MODULE_PATH = path . join (
20- ROOT_PATH ,
21- "implementors" ,
22- "node" ,
23- "assert.js" ,
24- ) ;
25- const LOAD_ADDON_MODULE_PATH = path . join (
26- ROOT_PATH ,
27- "implementors" ,
28- "node" ,
29- "load-addon.js" ,
30- ) ;
31- const GC_MODULE_PATH = path . join ( ROOT_PATH , "implementors" , "node" , "gc.js" ) ;
32- const MUST_CALL_MODULE_PATH = path . join (
33- ROOT_PATH ,
34- "implementors" ,
35- "node" ,
36- "must-call.js" ,
37- ) ;
38- const CHILD_PROCESS_MODULE_PATH = path . join (
39- ROOT_PATH ,
40- "implementors" ,
41- "node" ,
42- "child_process.js" ,
43- ) ;
4414
4515export function listDirectoryEntries ( dir : string ) {
4616 const entries = fs . readdirSync ( dir , { withFileTypes : true } ) ;
@@ -61,63 +31,26 @@ export function listDirectoryEntries(dir: string) {
6131 return { directories, files } ;
6232}
6333
64- export function runFileInSubprocess (
65- cwd : string ,
66- filePath : string ,
67- ) : Promise < void > {
68- return new Promise ( ( resolve , reject ) => {
69- const child = spawn (
70- process . execPath ,
71- [
72- // Using file scheme prefix when to enable imports on Windows
73- "--expose-gc" ,
74- "--import" ,
75- "file://" + FEATURES_MODULE_PATH ,
76- "--import" ,
77- "file://" + ASSERT_MODULE_PATH ,
78- "--import" ,
79- "file://" + LOAD_ADDON_MODULE_PATH ,
80- "--import" ,
81- "file://" + GC_MODULE_PATH ,
82- "--import" ,
83- "file://" + MUST_CALL_MODULE_PATH ,
84- "--import" ,
85- "file://" + CHILD_PROCESS_MODULE_PATH ,
86- filePath ,
87- ] ,
88- { cwd } ,
89- ) ;
90-
91- let stderrOutput = "" ;
92- child . stderr . setEncoding ( "utf8" ) ;
93- child . stderr . on ( "data" , ( chunk ) => {
94- stderrOutput += chunk ;
95- } ) ;
96-
97- child . stdout . pipe ( process . stdout ) ;
98-
99- child . on ( "error" , reject ) ;
100-
101- child . on ( "close" , ( code , signal ) => {
102- if ( code === 0 ) {
103- resolve ( ) ;
104- return ;
105- }
106-
107- const reason =
108- code !== null ? `exit code ${ code } ` : `signal ${ signal ?? "unknown" } ` ;
109- const trimmedStderr = stderrOutput . trim ( ) ;
110- const stderrSuffix = trimmedStderr
111- ? `\n--- stderr ---\n${ trimmedStderr } \n--- end stderr ---`
112- : "" ;
113- reject (
114- new Error (
115- `Test file ${ path . relative (
116- TESTS_ROOT_PATH ,
117- filePath ,
118- ) } failed (${ reason } )${ stderrSuffix } `,
119- ) ,
120- ) ;
121- } ) ;
34+ export function runFileInSubprocess ( cwd : string , filePath : string ) : void {
35+ const { status, signal, stdout, stderr } = spawnTest ( filePath , {
36+ cwd,
37+ nodeFlags : [ "--expose-gc" ] ,
12238 } ) ;
39+
40+ if ( stdout ) process . stdout . write ( stdout ) ;
41+
42+ if ( status === 0 ) return ;
43+
44+ const reason =
45+ status !== null ? `exit code ${ status } ` : `signal ${ signal ?? "unknown" } ` ;
46+ const trimmedStderr = stderr . trim ( ) ;
47+ const stderrSuffix = trimmedStderr
48+ ? `\n--- stderr ---\n${ trimmedStderr } \n--- end stderr ---`
49+ : "" ;
50+ throw new Error (
51+ `Test file ${ path . relative (
52+ TESTS_ROOT_PATH ,
53+ path . join ( cwd , filePath ) ,
54+ ) } failed (${ reason } )${ stderrSuffix } `,
55+ ) ;
12356}
0 commit comments