Skip to content

Commit 10ad762

Browse files
NickNasomhdawson
authored andcommitted
test: removed the usage of default_configuration.
PR-URL: #1226 Reviewed-By: Michael Dawson <[email protected] Reviewed-By: Chengzhong Wu <[email protected]>
1 parent e9db2ad commit 10ad762

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@
405405
"benchmark": "node benchmark",
406406
"pretest": "node-gyp rebuild -C test",
407407
"test": "node test",
408+
"test:debug": "node-gyp rebuild -C test --debug && NODE_API_BUILD_CONFIG=Debug node ./test/index.js",
408409
"predev": "node-gyp rebuild -C test --debug",
409410
"dev": "node test",
410411
"predev:incremental": "node-gyp configure build -C test --debug",

test/common/index.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict';
33
const assert = require('assert');
44
const path = require('path');
5+
const { access } = require('node:fs/promises');
56

67
const 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

94127
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();
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

109142
exports.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());

test/error_terminating_environment.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
2-
const buildType = process.config.target_defaults.default_configuration;
2+
33
const assert = require('assert');
4+
const { whichBuildType } = require('./common');
45

56
// These tests ensure that Error types can be used in a terminating
67
// environment without triggering any fatal errors.
@@ -63,11 +64,16 @@ if (process.argv[2] === 'runInChildProcess') {
6364
assert.fail('This should not be reachable');
6465
}
6566

66-
test(`./build/${buildType}/binding.node`, true);
67-
test(`./build/${buildType}/binding_noexcept.node`, true);
68-
test(`./build/${buildType}/binding_swallowexcept.node`, false);
69-
test(`./build/${buildType}/binding_swallowexcept_noexcept.node`, false);
70-
test(`./build/${buildType}/binding_custom_namespace.node`, true);
67+
wrapTest();
68+
69+
async function wrapTest () {
70+
const buildType = await whichBuildType();
71+
test(`./build/${buildType}/binding.node`, true);
72+
test(`./build/${buildType}/binding_noexcept.node`, true);
73+
test(`./build/${buildType}/binding_swallowexcept.node`, false);
74+
test(`./build/${buildType}/binding_swallowexcept_noexcept.node`, false);
75+
test(`./build/${buildType}/binding_custom_namespace.node`, true);
76+
}
7177

7278
function test (bindingPath, processShouldAbort) {
7379
const numberOfTestCases = 5;

test/maybe/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict';
22

3-
const buildType = process.config.target_defaults.default_configuration;
43
const assert = require('assert');
4+
const { whichBuildType } = require('../common');
55

66
const napiChild = require('../napi_child');
77

8-
module.exports = test(require(`../build/${buildType}/binding_noexcept_maybe.node`).maybe_check);
8+
module.exports = async function wrapTest () {
9+
const buildType = await whichBuildType();
10+
test(require(`../build/${buildType}/binding_noexcept_maybe.node`).maybe_check);
11+
};
912

1013
function test (binding) {
1114
if (process.argv.includes('child')) {

test/objectwrap_worker_thread.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict';
22
const path = require('path');
33
const { Worker, isMainThread } = require('worker_threads');
4+
const { runTestWithBuildType, whichBuildType } = require('./common');
45

5-
module.exports = require('./common').runTestWithBuildType(test);
6+
module.exports = runTestWithBuildType(test);
67

7-
async function test (buildType) {
8+
async function test () {
89
if (isMainThread) {
9-
const buildType = process.config.target_defaults.default_configuration;
10+
const buildType = await whichBuildType();
1011
const worker = new Worker(__filename, { workerData: buildType });
1112
return new Promise((resolve, reject) => {
1213
worker.on('exit', () => {

0 commit comments

Comments
 (0)