22'use strict' ;
33const assert = require ( 'assert' ) ;
44const path = require ( 'path' ) ;
5+ const { access } = require ( 'node:fs/promises' ) ;
56
67const noop = ( ) => { } ;
78
@@ -75,9 +76,41 @@ exports.mustNotCall = function (msg) {
7576 } ;
7677} ;
7778
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 ;
80111
112+ exports . runTest = async function ( test , buildType , buildPathRoot = process . env . BUILD_PATH || '' ) {
113+ buildType = buildType || await whichBuildType ( ) ;
81114 const bindings = [
82115 path . join ( buildPathRoot , `../build/${ buildType } /binding.node` ) ,
83116 path . join ( buildPathRoot , `../build/${ buildType } /binding_noexcept.node` ) ,
@@ -92,7 +125,7 @@ exports.runTest = async function (test, buildType, buildPathRoot = process.env.B
92125} ;
93126
94127exports . 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 ( ) ;
96129
97130 const bindings = [
98131 path . join ( buildPathRoot , `../build/${ buildType } /binding.node` ) ,
@@ -107,7 +140,7 @@ exports.runTestWithBindingPath = async function (test, buildType, buildPathRoot
107140} ;
108141
109142exports . runTestWithBuildType = async function ( test , buildType ) {
110- buildType = buildType || process . config . target_defaults . default_configuration || 'Release' ;
143+ buildType = buildType || await whichBuildType ( ) ;
111144
112145 await Promise . resolve ( test ( buildType ) )
113146 . finally ( exports . mustCall ( ) ) ;
0 commit comments