Skip to content

Commit 744705f

Browse files
RaisinTenmhdawson
authored andcommitted
test: refactor remove repeated execution index.js
Since the only job of the main thread is to supply the correct command-line args to the child thread, here is a refactor to do just that. The generation and execution of the `testModule` is done by the child thread. PR-URL: #839 Reviewed-By: Gabriel Schulhof <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: NickNaso <[email protected]>
1 parent db62e3c commit 744705f

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

test/index.js

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

3+
const majorNodeVersion = process.versions.node.split('.')[0];
4+
5+
if (typeof global.gc !== 'function') {
6+
// Construct the correct (version-dependent) command-line args.
7+
let args = ['--expose-gc', '--no-concurrent-array-buffer-freeing'];
8+
if (majorNodeVersion >= 14) {
9+
args.push('--no-concurrent-array-buffer-sweeping');
10+
}
11+
args.push(__filename);
12+
13+
const child = require('./napi_child').spawnSync(process.argv[0], args, {
14+
stdio: 'inherit',
15+
});
16+
17+
if (child.signal) {
18+
console.error(`Tests aborted with ${child.signal}`);
19+
process.exitCode = 1;
20+
} else {
21+
process.exitCode = child.status;
22+
}
23+
process.exit(process.exitCode);
24+
}
25+
326
process.config.target_defaults.default_configuration =
427
require('fs')
528
.readdirSync(require('path').join(__dirname, 'build'))
@@ -68,7 +91,7 @@ let testModules = [
6891
'version_management'
6992
];
7093

71-
let napiVersion = Number(process.versions.napi)
94+
let napiVersion = Number(process.versions.napi);
7295
if (process.env.NAPI_VERSION) {
7396
// we need this so that we don't try run tests that rely
7497
// on methods that are not available in the NAPI_VERSION
@@ -77,8 +100,6 @@ if (process.env.NAPI_VERSION) {
77100
}
78101
console.log('napiVersion:' + napiVersion);
79102

80-
const majorNodeVersion = process.versions.node.split('.')[0]
81-
82103
if (napiVersion < 3) {
83104
testModules.splice(testModules.indexOf('callbackscope'), 1);
84105
testModules.splice(testModules.indexOf('version_management'), 1);
@@ -110,40 +131,19 @@ if (majorNodeVersion < 12) {
110131
testModules.splice(testModules.indexOf('objectwrap_worker_thread'), 1);
111132
}
112133

113-
if (typeof global.gc === 'function') {
114-
(async function() {
115-
console.log(`Testing with N-API Version '${napiVersion}'.`);
134+
(async function() {
135+
console.log(`Testing with N-API Version '${napiVersion}'.`);
116136

117-
console.log('Starting test suite\n');
137+
console.log('Starting test suite\n');
118138

119-
// Requiring each module runs tests in the module.
120-
for (const name of testModules) {
121-
console.log(`Running test '${name}'`);
122-
await require('./' + name);
123-
};
139+
// Requiring each module runs tests in the module.
140+
for (const name of testModules) {
141+
console.log(`Running test '${name}'`);
142+
await require('./' + name);
143+
};
124144

125-
console.log('\nAll tests passed!');
126-
})().catch((error) => {
127-
console.log(error);
128-
process.exit(1);
129-
});
130-
} else {
131-
// Construct the correct (version-dependent) command-line args.
132-
let args = ['--expose-gc', '--no-concurrent-array-buffer-freeing'];
133-
if (majorNodeVersion >= 14) {
134-
args.push('--no-concurrent-array-buffer-sweeping');
135-
}
136-
args.push(__filename);
137-
138-
const child = require('./napi_child').spawnSync(process.argv[0], args, {
139-
stdio: 'inherit',
140-
});
141-
142-
if (child.signal) {
143-
console.error(`Tests aborted with ${child.signal}`);
144-
process.exitCode = 1;
145-
} else {
146-
process.exitCode = child.status;
147-
}
148-
process.exit(process.exitCode);
149-
}
145+
console.log('\nAll tests passed!');
146+
})().catch((error) => {
147+
console.log(error);
148+
process.exit(1);
149+
});

0 commit comments

Comments
 (0)