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 (
32- ROOT_PATH ,
33- "implementors" ,
34- "node" ,
35- "gc.js"
36- ) ;
37- const MUST_CALL_MODULE_PATH = path . join (
38- ROOT_PATH ,
39- "implementors" ,
40- "node" ,
41- "must-call.js"
42- ) ;
4314
4415export function listDirectoryEntries ( dir : string ) {
4516 const entries = fs . readdirSync ( dir , { withFileTypes : true } ) ;
@@ -60,61 +31,26 @@ export function listDirectoryEntries(dir: string) {
6031 return { directories, files } ;
6132}
6233
63- export function runFileInSubprocess (
64- cwd : string ,
65- filePath : string
66- ) : Promise < void > {
67- return new Promise ( ( resolve , reject ) => {
68- const child = spawn (
69- process . execPath ,
70- [
71- // Using file scheme prefix when to enable imports on Windows
72- "--expose-gc" ,
73- "--import" ,
74- "file://" + FEATURES_MODULE_PATH ,
75- "--import" ,
76- "file://" + ASSERT_MODULE_PATH ,
77- "--import" ,
78- "file://" + LOAD_ADDON_MODULE_PATH ,
79- "--import" ,
80- "file://" + GC_MODULE_PATH ,
81- "--import" ,
82- "file://" + MUST_CALL_MODULE_PATH ,
83- filePath ,
84- ] ,
85- { cwd }
86- ) ;
87-
88- let stderrOutput = "" ;
89- child . stderr . setEncoding ( "utf8" ) ;
90- child . stderr . on ( "data" , ( chunk ) => {
91- stderrOutput += chunk ;
92- } ) ;
93-
94- child . stdout . pipe ( process . stdout ) ;
95-
96- child . on ( "error" , reject ) ;
97-
98- child . on ( "close" , ( code , signal ) => {
99- if ( code === 0 ) {
100- resolve ( ) ;
101- return ;
102- }
103-
104- const reason =
105- code !== null ? `exit code ${ code } ` : `signal ${ signal ?? "unknown" } ` ;
106- const trimmedStderr = stderrOutput . trim ( ) ;
107- const stderrSuffix = trimmedStderr
108- ? `\n--- stderr ---\n${ trimmedStderr } \n--- end stderr ---`
109- : "" ;
110- reject (
111- new Error (
112- `Test file ${ path . relative (
113- TESTS_ROOT_PATH ,
114- filePath
115- ) } failed (${ reason } )${ stderrSuffix } `
116- )
117- ) ;
118- } ) ;
34+ export function runFileInSubprocess ( cwd : string , filePath : string ) : void {
35+ const { status, signal, stdout, stderr } = spawnTest ( filePath , {
36+ cwd,
37+ nodeFlags : [ "--expose-gc" ] ,
11938 } ) ;
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+ filePath
54+ ) } failed (${ reason } )${ stderrSuffix } `
55+ ) ;
12056}
0 commit comments