@@ -5,6 +5,8 @@ process.config.target_defaults.default_configuration =
5
5
. readdirSync ( require ( 'path' ) . join ( __dirname , 'build' ) )
6
6
. filter ( ( item ) => ( item === 'Debug' || item === 'Release' ) ) [ 0 ] ;
7
7
8
+ const RUN_TEST_CMD = 'run-test' ;
9
+
8
10
// FIXME: We might need a way to load test modules automatically without
9
11
// explicit declaration as follows.
10
12
let testModules = [
@@ -75,7 +77,6 @@ if (process.env.NAPI_VERSION) {
75
77
// specified
76
78
napiVersion = process . env . NAPI_VERSION ;
77
79
}
78
- console . log ( 'napiVersion:' + napiVersion ) ;
79
80
80
81
const majorNodeVersion = process . versions . node . split ( '.' ) [ 0 ]
81
82
@@ -110,40 +111,65 @@ if (majorNodeVersion < 12) {
110
111
testModules . splice ( testModules . indexOf ( 'objectwrap_worker_thread' ) , 1 ) ;
111
112
}
112
113
113
- if ( typeof global . gc === 'function' ) {
114
- ( async function ( ) {
115
- console . log ( `Testing with N-API Version '${ napiVersion } '.` ) ;
116
-
117
- console . log ( 'Starting test suite\n' ) ;
118
-
119
- // Requiring each module runs tests in the module.
120
- for ( const name of testModules ) {
121
- console . log ( `Running test '${ name } '` ) ;
114
+ main ( process . argv . slice ( 2 ) ) . catch ( err => {
115
+ console . error ( err ) ;
116
+ process . exit ( 1 ) ;
117
+ } ) ;
118
+ async function main ( argv ) {
119
+ const cmd = argv [ 0 ] ;
120
+ if ( cmd === RUN_TEST_CMD ) {
121
+ const name = argv [ 1 ] ;
122
+ if ( name == null || name === '' ) {
123
+ console . log ( 'command run-test has to be run against a test case name.' ) ;
124
+ return process . exit ( 1 ) ;
125
+ }
126
+ // Requiring each module runs tests in the module.
122
127
await require ( './' + name ) ;
123
- } ;
128
+ return ;
129
+ }
124
130
125
- console . log ( '\nAll tests passed!' ) ;
126
- } ) ( ) . catch ( ( error ) => {
127
- console . log ( error ) ;
128
- process . exit ( 1 ) ;
129
- } ) ;
130
- } else {
131
+ runTestSuiteInBatch ( argv ) ;
132
+ }
133
+
134
+ function runTestSuiteInBatch ( argv ) {
135
+ const failFast = argv . includes ( '--fail-fast' ) ;
136
+ console . log ( `Testing with N-API Version '${ napiVersion } '.` ) ;
137
+ console . log ( 'Starting test suite\n' ) ;
131
138
// Construct the correct (version-dependent) command-line args.
132
139
let args = [ '--expose-gc' , '--no-concurrent-array-buffer-freeing' ] ;
133
140
if ( majorNodeVersion >= 14 ) {
134
141
args . push ( '--no-concurrent-array-buffer-sweeping' ) ;
135
142
}
136
- args . push ( __filename ) ;
143
+ args . push ( __filename , RUN_TEST_CMD ) ;
137
144
138
- const child = require ( './napi_child' ) . spawnSync ( process . argv [ 0 ] , args , {
139
- stdio : 'inherit' ,
140
- } ) ;
145
+ let failedModules = [ ] ;
146
+ for ( const name of testModules ) {
147
+ console . log ( `Running test '${ name } '` ) ;
148
+ const child = require ( './napi_child' ) . spawnSync ( process . execPath , [ ...args , name ] , {
149
+ stdio : 'inherit' ,
150
+ } ) ;
141
151
142
- if ( child . signal ) {
143
- console . error ( `Tests aborted with ${ child . signal } ` ) ;
144
- process . exitCode = 1 ;
152
+ let failed = false ;
153
+ if ( child . signal ) {
154
+ console . error ( `Test '${ name } ' aborted with ${ child . signal } ` ) ;
155
+ failed = true ;
156
+ } else if ( child . status !== 0 ) {
157
+ console . error ( `Test '${ name } ' exited with code ${ child . status } ` ) ;
158
+ failed = true ;
159
+ }
160
+
161
+ if ( failed ) {
162
+ failedModules . push ( name ) ;
163
+ }
164
+ if ( failed && failFast ) {
165
+ process . exit ( 1 ) ;
166
+ }
167
+ }
168
+
169
+ if ( failedModules . length === 0 ) {
170
+ console . log ( '\nAll tests passed!' ) ;
145
171
} else {
146
- process . exitCode = child . status ;
172
+ console . log ( '\nTests failed with: \n %s' , failedModules . join ( '\n ' ) ) ;
173
+ process . exit ( 1 ) ;
147
174
}
148
- process . exit ( process . exitCode ) ;
149
175
}
0 commit comments