1- import { spawn } from "node:child_process" ;
2- import { promises as fs } from "node:fs" ;
31import path from "node:path" ;
42import { test , type TestContext } from "node:test" ;
53
4+ import { listDirectoryEntries , runFileInSubprocess } from "./tests.ts" ;
5+
66const ROOT_PATH = path . resolve ( import . meta. dirname , ".." , ".." ) ;
77const TESTS_ROOT_PATH = path . join ( ROOT_PATH , "tests" ) ;
88
@@ -19,79 +19,11 @@ const LOAD_ADDON_MODULE_PATH = path.join(
1919 "load-addon.js"
2020) ;
2121
22- async function listDirectoryEntries ( dir : string ) {
23- const entries = await fs . readdir ( dir , { withFileTypes : true } ) ;
24- const directories : string [ ] = [ ] ;
25- const files : string [ ] = [ ] ;
26-
27- for ( const entry of entries ) {
28- if ( entry . isDirectory ( ) ) {
29- directories . push ( entry . name ) ;
30- } else if ( entry . isFile ( ) && entry . name . endsWith ( ".js" ) ) {
31- files . push ( entry . name ) ;
32- }
33- }
34-
35- directories . sort ( ) ;
36- files . sort ( ) ;
37-
38- return { directories, files } ;
39- }
40-
41- function runFileInSubprocess ( cwd : string , filePath : string ) : Promise < void > {
42- return new Promise ( ( resolve , reject ) => {
43- const child = spawn (
44- process . execPath ,
45- [
46- // Using file scheme prefix when to enable imports on Windows
47- "--import" ,
48- "file://" + ASSERT_MODULE_PATH ,
49- "--import" ,
50- "file://" + LOAD_ADDON_MODULE_PATH ,
51- filePath ,
52- ] ,
53- { cwd }
54- ) ;
55-
56- let stderrOutput = "" ;
57- child . stderr . setEncoding ( "utf8" ) ;
58- child . stderr . on ( "data" , ( chunk ) => {
59- stderrOutput += chunk ;
60- } ) ;
61-
62- child . stdout . pipe ( process . stdout ) ;
63-
64- child . on ( "error" , reject ) ;
65-
66- child . on ( "close" , ( code , signal ) => {
67- if ( code === 0 ) {
68- resolve ( ) ;
69- return ;
70- }
71-
72- const reason =
73- code !== null ? `exit code ${ code } ` : `signal ${ signal ?? "unknown" } ` ;
74- const trimmedStderr = stderrOutput . trim ( ) ;
75- const stderrSuffix = trimmedStderr
76- ? `\n--- stderr ---\n${ trimmedStderr } \n--- end stderr ---`
77- : "" ;
78- reject (
79- new Error (
80- `Test file ${ path . relative (
81- TESTS_ROOT_PATH ,
82- filePath
83- ) } failed (${ reason } )${ stderrSuffix } `
84- )
85- ) ;
86- } ) ;
87- } ) ;
88- }
89-
9022async function populateSuite (
9123 testContext : TestContext ,
9224 dir : string
9325) : Promise < void > {
94- const { directories, files } = await listDirectoryEntries ( dir ) ;
26+ const { directories, files } = listDirectoryEntries ( dir ) ;
9527
9628 for ( const file of files ) {
9729 await testContext . test ( file , ( ) => runFileInSubprocess ( dir , file ) ) ;
0 commit comments