From 8f666b0a50a9a31331b6d91f00b19bfe71d8dc96 Mon Sep 17 00:00:00 2001 From: Kenvin Davies Date: Fri, 4 Sep 2020 02:41:14 +0200 Subject: [PATCH] test: added tests to check the build process Refs: https://github.com/nodejs/node-addon-api/pull/766 PR-URL: https://github.com/nodejs/node-addon-api/pull/808 Reviewed-By: Michael Dawson --- .gitignore | 1 + package.json | 1 + test/addon_build/index.js | 50 +++++++++++++++++++++++++ test/addon_build/tpl/.npmrc | 1 + test/addon_build/tpl/addon.cc | 17 +++++++++ test/addon_build/tpl/binding.gyp | 62 +++++++++++++++++++++++++++++++ test/addon_build/tpl/index.js | 9 +++++ test/addon_build/tpl/package.json | 11 ++++++ test/index.js | 1 + 9 files changed, 153 insertions(+) create mode 100644 test/addon_build/index.js create mode 100644 test/addon_build/tpl/.npmrc create mode 100644 test/addon_build/tpl/addon.cc create mode 100644 test/addon_build/tpl/binding.gyp create mode 100644 test/addon_build/tpl/index.js create mode 100644 test/addon_build/tpl/package.json diff --git a/.gitignore b/.gitignore index c10f4df..c5b8bc8 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 64a44f3..0ec1d6e 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 0000000..0d0c6ea --- /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 0000000..9cf9495 --- /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 0000000..1a86799 --- /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 0000000..aa26f1a --- /dev/null +++ b/test/addon_build/tpl/binding.gyp @@ -0,0 +1,62 @@ +{ + 'target_defaults': { + 'include_dirs': [ + "