Skip to content

Commit

Permalink
test: refactor remove repeated execution index.js
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RaisinTen committed Dec 25, 2020
1 parent 1bdc758 commit c472036
Showing 1 changed file with 45 additions and 45 deletions.
90 changes: 45 additions & 45 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
'use strict';

let napiVersion = Number(process.versions.napi);
if (process.env.NAPI_VERSION) {
// we need this so that we don't try run tests that rely
// on methods that are not available in the NAPI_VERSION
// specified
napiVersion = process.env.NAPI_VERSION;
}
console.log('napiVersion:' + napiVersion);

const majorNodeVersion = process.versions.node.split('.')[0];

if (typeof global.gc !== 'function') {
// Construct the correct (version-dependent) command-line args.
let args = ['--expose-gc', '--no-concurrent-array-buffer-freeing'];
if (majorNodeVersion >= 14) {
args.push('--no-concurrent-array-buffer-sweeping');
}
args.push(__filename);

const child = require('./napi_child').spawnSync(process.argv[0], args, {
stdio: 'inherit',
});

if (child.signal) {
console.error(`Tests aborted with ${child.signal}`);
process.exitCode = 1;
} else {
process.exitCode = child.status;
}
process.exit(process.exitCode);
}

process.config.target_defaults.default_configuration =
require('fs')
.readdirSync(require('path').join(__dirname, 'build'))
Expand Down Expand Up @@ -68,17 +100,6 @@ let testModules = [
'version_management'
];

let napiVersion = Number(process.versions.napi)
if (process.env.NAPI_VERSION) {
// we need this so that we don't try run tests that rely
// on methods that are not available in the NAPI_VERSION
// specified
napiVersion = process.env.NAPI_VERSION;
}
console.log('napiVersion:' + napiVersion);

const majorNodeVersion = process.versions.node.split('.')[0]

if (napiVersion < 3) {
testModules.splice(testModules.indexOf('callbackscope'), 1);
testModules.splice(testModules.indexOf('version_management'), 1);
Expand Down Expand Up @@ -110,40 +131,19 @@ if (majorNodeVersion < 12) {
testModules.splice(testModules.indexOf('objectwrap_worker_thread'), 1);
}

if (typeof global.gc === 'function') {
(async function() {
console.log(`Testing with N-API Version '${napiVersion}'.`);
(async function() {
console.log(`Testing with N-API Version '${napiVersion}'.`);

console.log('Starting test suite\n');
console.log('Starting test suite\n');

// Requiring each module runs tests in the module.
for (const name of testModules) {
console.log(`Running test '${name}'`);
await require('./' + name);
};
// Requiring each module runs tests in the module.
for (const name of testModules) {
console.log(`Running test '${name}'`);
await require('./' + name);
};

console.log('\nAll tests passed!');
})().catch((error) => {
console.log(error);
process.exit(1);
});
} else {
// Construct the correct (version-dependent) command-line args.
let args = ['--expose-gc', '--no-concurrent-array-buffer-freeing'];
if (majorNodeVersion >= 14) {
args.push('--no-concurrent-array-buffer-sweeping');
}
args.push(__filename);

const child = require('./napi_child').spawnSync(process.argv[0], args, {
stdio: 'inherit',
});

if (child.signal) {
console.error(`Tests aborted with ${child.signal}`);
process.exitCode = 1;
} else {
process.exitCode = child.status;
}
process.exit(process.exitCode);
}
console.log('\nAll tests passed!');
})().catch((error) => {
console.log(error);
process.exit(1);
});

0 comments on commit c472036

Please sign in to comment.