diff --git a/.gitignore b/.gitignore index c10f4dffc..c5b8bc871 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /build /benchmark/build /benchmark/src +/test/addon_build/addons diff --git a/package.json b/package.json index 64a44f32a..0ec1d6ed6 100644 --- a/package.json +++ b/package.json @@ -252,6 +252,7 @@ "description": "Node.js API (N-API)", "devDependencies": { "benchmark": "^2.1.4", + "fs-extra": "^9.0.1", "bindings": "^1.5.0", "safe-buffer": "^5.1.1" }, diff --git a/test/addon_build/index.js b/test/addon_build/index.js new file mode 100644 index 000000000..0d0c6ea2f --- /dev/null +++ b/test/addon_build/index.js @@ -0,0 +1,50 @@ +'use strict'; + +const { promisify } = require('util'); +const exec = promisify(require('child_process').exec); +const { copy, remove } = require('fs-extra'); +const path = require('path'); +const assert = require('assert') + +const ADDONS_FOLDER = path.join(__dirname, 'addons'); + +const addons = [ + 'echo addon', + 'echo-addon' +] + +async function beforeAll(addons) { + console.log(' >Preparing native addons to build') + for (const addon of addons) { + await remove(path.join(ADDONS_FOLDER, addon)); + await copy(path.join(__dirname, 'tpl'), path.join(ADDONS_FOLDER, addon)); + } +} + +async function test(addon) { + console.log(` >Building addon: '${addon}'`); + const { stderr, stdout } = await exec('npm install', { + cwd: path.join(ADDONS_FOLDER, addon) + }) + console.log(` >Runting test for: '${addon}'`); + // Disabled the checks on stderr and stdout because of this issuue on npm: + // Stop using process.umask(): https://github.com/npm/cli/issues/1103 + // We should enable the following checks again after the resolution of + // the reported issue. + // assert.strictEqual(stderr, ''); + // assert.ok(stderr.length === 0); + // assert.ok(stdout.length > 0); + const binding = require(`${ADDONS_FOLDER}/${addon}`); + assert.strictEqual(binding.except.echo('except'), 'except'); + assert.strictEqual(binding.except.echo(101), 101); + assert.strictEqual(binding.noexcept.echo('noexcept'), 'noexcept'); + assert.strictEqual(binding.noexcept.echo(103), 103); +} + + +module.exports = (async function() { + await beforeAll(addons); + for (const addon of addons) { + await test(addon); + } +})() diff --git a/test/addon_build/tpl/.npmrc b/test/addon_build/tpl/.npmrc new file mode 100644 index 000000000..9cf949503 --- /dev/null +++ b/test/addon_build/tpl/.npmrc @@ -0,0 +1 @@ +package-lock=false \ No newline at end of file diff --git a/test/addon_build/tpl/addon.cc b/test/addon_build/tpl/addon.cc new file mode 100644 index 000000000..1a86799c4 --- /dev/null +++ b/test/addon_build/tpl/addon.cc @@ -0,0 +1,17 @@ +#include + +Napi::Value Echo(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + if (info.Length() != 1) { + Napi::TypeError::New(env, "Wrong number of arguments. One argument expected.") + .ThrowAsJavaScriptException(); + } + return info[0].As(); +} + +Napi::Object Init(Napi::Env env, Napi::Object exports) { + exports.Set(Napi::String::New(env, "echo"), Napi::Function::New(env, Echo)); + return exports; +} + +NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init) diff --git a/test/addon_build/tpl/binding.gyp b/test/addon_build/tpl/binding.gyp new file mode 100644 index 000000000..aa26f1acb --- /dev/null +++ b/test/addon_build/tpl/binding.gyp @@ -0,0 +1,62 @@ +{ + 'target_defaults': { + 'include_dirs': [ + "