Skip to content

Commit

Permalink
test: run test suites with helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Apr 23, 2021
1 parent 63f4643 commit f19912f
Show file tree
Hide file tree
Showing 67 changed files with 160 additions and 252 deletions.
5 changes: 2 additions & 3 deletions test/addon.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));
module.exports = require('./common').runTest(test);

function test(binding) {
assert.strictEqual(binding.addon.increment(), 43);
Expand Down
9 changes: 2 additions & 7 deletions test/addon_data.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');
const { spawn } = require('child_process');
const readline = require('readline');
const path = require('path');

module.exports =
test(path.resolve(__dirname, `./build/${buildType}/binding.node`))
.then(() =>
test(path.resolve(__dirname,
`./build/${buildType}/binding_noexcept.node`)));
module.exports = require('./common').runTestWithBindingPath(test);

// Make sure the instance data finalizer is called at process exit. If the hint
// is non-zero, it will be printed out by the child process.
Expand Down
5 changes: 2 additions & 3 deletions test/arraybuffer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');
const testUtil = require('./testUtil');

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = require('./common').runTest(test);

function test(binding) {
return testUtil.runGCTests([
Expand Down
34 changes: 18 additions & 16 deletions test/asynccontext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');
const common = require('./common');

Expand All @@ -17,8 +17,7 @@ function checkAsyncHooks() {
return false;
}

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));
module.exports = common.runTest(test);

function installAsyncHooksForTest() {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -53,21 +52,24 @@ function installAsyncHooksForTest() {
}

function test(binding) {
binding.asynccontext.makeCallback(common.mustCall(), { foo: 'foo' });
if (!checkAsyncHooks())
if (!checkAsyncHooks()) {
return;
}

const hooks = installAsyncHooksForTest();
const triggerAsyncId = async_hooks.executionAsyncId();
hooks.then(actual => {
assert.deepStrictEqual(actual, [
{ eventName: 'init',
type: 'async_context_test',
triggerAsyncId: triggerAsyncId,
resource: { foo: 'foo' } },
{ eventName: 'before' },
{ eventName: 'after' },
{ eventName: 'destroy' }
]);
}).catch(common.mustNotCall());
const interval = setInterval(() => {}, 10);
binding.asynccontext.makeCallback(common.mustCall(), { foo: 'foo' });
return hooks.then(actual => {
assert.deepStrictEqual(actual, [
{ eventName: 'init',
type: 'async_context_test',
triggerAsyncId: triggerAsyncId,
resource: { foo: 'foo' } },
{ eventName: 'before' },
{ eventName: 'after' },
{ eventName: 'destroy' }
]);
}).catch(common.mustNotCall())
.finally(() => clearInterval(interval));
}
6 changes: 2 additions & 4 deletions test/asyncprogressqueueworker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const common = require('./common')
const assert = require('assert');
const os = require('os');

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = common.runTest(test);

async function test({ asyncprogressqueueworker }) {
await success(asyncprogressqueueworker);
Expand Down
5 changes: 2 additions & 3 deletions test/asyncprogressworker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const common = require('./common')
const assert = require('assert');

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = common.runTest(test);

async function test({ asyncprogressworker }) {
await success(asyncprogressworker);
Expand Down
5 changes: 2 additions & 3 deletions test/asyncworker-nocallback.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const common = require('./common');

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = common.runTest(test);

async function test(binding) {
await binding.asyncworker.doWorkNoCallback(true, {})
Expand Down
13 changes: 5 additions & 8 deletions test/asyncworker-persistent.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');
const common = require('./common');
const binding = require(`./build/${buildType}/binding.node`);
const noexceptBinding = require(`./build/${buildType}/binding_noexcept.node`);

function test(binding, succeed) {
return new Promise((resolve) =>
Expand All @@ -21,7 +18,7 @@ function test(binding, succeed) {
}));
}

module.exports = test(binding.persistentasyncworker, false)
.then(() => test(binding.persistentasyncworker, true))
.then(() => test(noexceptBinding.persistentasyncworker, false))
.then(() => test(noexceptBinding.persistentasyncworker, true));
module.exports = require('./common').runTest(async binding => {
await test(binding.persistentasyncworker, false);
await test(binding.persistentasyncworker, true);
});
4 changes: 1 addition & 3 deletions test/asyncworker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
const common = require('./common');

Expand All @@ -17,8 +16,7 @@ function checkAsyncHooks() {
return false;
}

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = common.runTest(test);

function installAsyncHooksForTest() {
return new Promise((resolve, reject) => {
Expand Down
4 changes: 1 addition & 3 deletions test/basic_types/array.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));
module.exports = require('../common').runTest(test);

function test(binding) {

Expand Down
5 changes: 2 additions & 3 deletions test/basic_types/boolean.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));
module.exports = require('../common').runTest(test);

function test(binding) {
const bool1 = binding.basic_types_boolean.createBoolean(true);
Expand Down
4 changes: 1 addition & 3 deletions test/basic_types/number.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));
module.exports = require('../common').runTest(test);

function test(binding) {
const MIN_INT32 = -2147483648;
Expand Down
4 changes: 1 addition & 3 deletions test/basic_types/value.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));
module.exports = require('../common').runTest(test);

function test(binding) {
const externalValue = binding.basic_types_value.createExternal();
Expand Down
4 changes: 1 addition & 3 deletions test/bigint.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));
module.exports = require('./common').runTest(test);

function test(binding) {
const {
Expand Down
5 changes: 2 additions & 3 deletions test/buffer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');
const testUtil = require('./testUtil');
const safeBuffer = require('safe-buffer');

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = require('./common').runTest(test);

function test(binding) {
return testUtil.runGCTests([
Expand Down
5 changes: 1 addition & 4 deletions test/callbackscope.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
const common = require('./common');

// we only check async hooks on 8.x an higher were
// they are closer to working properly
Expand All @@ -17,8 +15,7 @@ function checkAsyncHooks() {
return false;
}

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));
module.exports = require('./common').runTest(test);

function test(binding) {
if (!checkAsyncHooks())
Expand Down
32 changes: 32 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,35 @@ exports.mustNotCall = function(msg) {
assert.fail(msg || 'function should not have been called');
};
};

exports.runTest = async function(test, buildType) {
buildType = buildType ?? process.config.target_defaults.default_configuration;

const bindings = [
`../build/${buildType}/binding.node`,
`../build/${buildType}/binding_noexcept.node`,
].map(it => require.resolve(it));

// TODO(legendecas): investigate strange CHECK failures in Node.js core
// - src/callback.cc
// - InternalCallbackScope::Close
// - CHECK_EQ(env_->execution_async_id(), 0);
//
// for (const item of bindings) {
// await test(require(item));
// }
return bindings.map(item => test(require(item)));
}

exports.runTestWithBindingPath = async function(test, buildType) {
buildType = buildType ?? process.config.target_defaults.default_configuration;

const bindings = [
`../build/${buildType}/binding.node`,
`../build/${buildType}/binding_noexcept.node`,
].map(it => require.resolve(it));

for (const item of bindings) {
await test(item);
}
}
5 changes: 1 addition & 4 deletions test/dataview/dataview.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict';

const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));
module.exports = require('../common').runTest(test);

function test(binding) {
function testDataViewCreation(factory, arrayBuffer, offset, length) {
Expand Down
4 changes: 1 addition & 3 deletions test/dataview/dataview_read_write.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));
module.exports = require('../common').runTest(test);

function test(binding) {
function expected(type, value) {
Expand Down
4 changes: 1 addition & 3 deletions test/date.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));
module.exports = require('./common').runTest(test);

function test(binding) {
const {
Expand Down
5 changes: 2 additions & 3 deletions test/error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');

if (process.argv[2] === 'fatal') {
Expand All @@ -8,8 +8,7 @@ if (process.argv[2] === 'fatal') {
return;
}

test(`./build/${buildType}/binding.node`);
test(`./build/${buildType}/binding_noexcept.node`);
module.exports = require('./common').runTestWithBindingPath(test);

function test(bindingPath) {
const binding = require(bindingPath);
Expand Down
6 changes: 2 additions & 4 deletions test/external.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');
const { spawnSync } = require('child_process');
const testUtil = require('./testUtil');
Expand Down Expand Up @@ -40,9 +40,7 @@ if (process.argv.length === 3) {
return;
}

module.exports = test(require.resolve(`./build/${buildType}/binding.node`))
.then(() =>
test(require.resolve(`./build/${buildType}/binding_noexcept.node`)));
module.exports = require('./common').runTestWithBindingPath(test);

function test(bindingPath) {
const binding = require(bindingPath);
Expand Down
10 changes: 5 additions & 5 deletions test/function.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');

test(require(`./build/${buildType}/binding.node`).function.plain);
test(require(`./build/${buildType}/binding_noexcept.node`).function.plain);
test(require(`./build/${buildType}/binding.node`).function.templated);
test(require(`./build/${buildType}/binding_noexcept.node`).function.templated);
module.exports = require('./common').runTest(binding => {
test(binding.function.plain);
test(binding.function.templated);
});

function test(binding) {
assert.strictEqual(binding.emptyConstructor(true), true);
Expand Down
Loading

0 comments on commit f19912f

Please sign in to comment.