2
2
'use strict' ;
3
3
const assert = require ( 'assert' ) ;
4
4
const path = require ( 'path' ) ;
5
+ const { access } = require ( 'node:fs/promises' ) ;
5
6
6
7
const noop = ( ) => { } ;
7
8
@@ -75,9 +76,41 @@ exports.mustNotCall = function (msg) {
75
76
} ;
76
77
} ;
77
78
78
- exports . runTest = async function ( test , buildType , buildPathRoot = process . env . BUILD_PATH || '' ) {
79
- buildType = buildType || process . config . target_defaults . default_configuration || 'Release' ;
79
+ const buildTypes = {
80
+ Release : 'Release' ,
81
+ Debug : 'Debug'
82
+ } ;
83
+
84
+ async function checkBuildType ( buildType ) {
85
+ try {
86
+ await access ( path . join ( path . resolve ( './test/build' ) , buildType ) ) ;
87
+ return true ;
88
+ } catch {
89
+ return false ;
90
+ }
91
+ }
92
+
93
+ async function whichBuildType ( ) {
94
+ let buildType = 'Release' ;
95
+ const envBuildType = process . env . NODE_API_BUILD_CONFIG ;
96
+ if ( envBuildType ) {
97
+ if ( Object . values ( buildTypes ) . includes ( envBuildType ) ) {
98
+ if ( await checkBuildType ( envBuildType ) ) {
99
+ buildType = envBuildType ;
100
+ } else {
101
+ throw new Error ( `The ${ envBuildType } build doesn't exists.` ) ;
102
+ }
103
+ } else {
104
+ throw new Error ( 'Invalid value for NODE_API_BUILD_CONFIG environment variable. It should be set to Release or Debug.' ) ;
105
+ }
106
+ }
107
+ return buildType ;
108
+ }
109
+
110
+ exports . whichBuildType = whichBuildType ;
80
111
112
+ exports . runTest = async function ( test , buildType , buildPathRoot = process . env . BUILD_PATH || '' ) {
113
+ buildType = buildType || await whichBuildType ( ) ;
81
114
const bindings = [
82
115
path . join ( buildPathRoot , `../build/${ buildType } /binding.node` ) ,
83
116
path . join ( buildPathRoot , `../build/${ buildType } /binding_noexcept.node` ) ,
@@ -92,7 +125,7 @@ exports.runTest = async function (test, buildType, buildPathRoot = process.env.B
92
125
} ;
93
126
94
127
exports . runTestWithBindingPath = async function ( test , buildType , buildPathRoot = process . env . BUILD_PATH || '' ) {
95
- buildType = buildType || process . config . target_defaults . default_configuration || 'Release' ;
128
+ buildType = buildType || await whichBuildType ( ) ;
96
129
97
130
const bindings = [
98
131
path . join ( buildPathRoot , `../build/${ buildType } /binding.node` ) ,
@@ -107,7 +140,7 @@ exports.runTestWithBindingPath = async function (test, buildType, buildPathRoot
107
140
} ;
108
141
109
142
exports . runTestWithBuildType = async function ( test , buildType ) {
110
- buildType = buildType || process . config . target_defaults . default_configuration || 'Release' ;
143
+ buildType = buildType || await whichBuildType ( ) ;
111
144
112
145
await Promise . resolve ( test ( buildType ) )
113
146
. finally ( exports . mustCall ( ) ) ;
0 commit comments