Skip to content

Commit

Permalink
test: added tests to check the build process
Browse files Browse the repository at this point in the history
Refs: nodejs/node-addon-api#766
PR-URL: nodejs/node-addon-api#808
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
John French committed Sep 10, 2020
1 parent d44b882 commit 7176886
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/build
/benchmark/build
/benchmark/src
/test/addon_build/addons
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
50 changes: 50 additions & 0 deletions test/addon_build/index.js
Original file line number Diff line number Diff line change
@@ -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);
}
})()
1 change: 1 addition & 0 deletions test/addon_build/tpl/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
17 changes: 17 additions & 0 deletions test/addon_build/tpl/addon.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <napi.h>

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::Value>();
}

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)
62 changes: 62 additions & 0 deletions test/addon_build/tpl/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
'target_defaults': {
'include_dirs': [
"<!(node -p \"require('node-addon-api').include_dir\")"
],
'variables': {
'NAPI_VERSION%': "<!(node -p \"process.versions.napi\")",
'disable_deprecated': "<!(node -p \"process.env['npm_config_disable_deprecated']\")"
},
'conditions': [
['NAPI_VERSION!=""', { 'defines': ['NAPI_VERSION=<@(NAPI_VERSION)'] } ],
['disable_deprecated=="true"', {
'defines': ['NODE_ADDON_API_DISABLE_DEPRECATED']
}],
['OS=="mac"', {
'cflags+': ['-fvisibility=hidden'],
'xcode_settings': {
'OTHER_CFLAGS': ['-fvisibility=hidden']
}
}]
],
'sources': [
'addon.cc',
],
},
'targets': [
{
'target_name': 'addon',
'defines': [ 'NAPI_CPP_EXCEPTIONS' ],
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'msvs_settings': {
'VCCLCompilerTool': {
'ExceptionHandling': 1,
'EnablePREfast': 'true',
},
},
'xcode_settings': {
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
},
},
{
'target_name': 'addon_noexcept',
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
'cflags': [ '-fno-exceptions' ],
'cflags_cc': [ '-fno-exceptions' ],
'msvs_settings': {
'VCCLCompilerTool': {
'ExceptionHandling': 0,
'EnablePREfast': 'true',
},
},
'xcode_settings': {
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
'GCC_ENABLE_CPP_EXCEPTIONS': 'NO',
},
},
],
}
9 changes: 9 additions & 0 deletions test/addon_build/tpl/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

const except = require('bindings')('addon')
const noexcept = require('bindings')('addon_noexcept')

module.exports = {
except,
noexcept
}
11 changes: 11 additions & 0 deletions test/addon_build/tpl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "addon",
"version": "0.0.0",
"description": "Node.js addon",
"private": true,
"scripts": {},
"dependencies": {
"node-addon-api": "file:../../../../"
},
"gypfile": true
}
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ process.config.target_defaults.default_configuration =
// FIXME: We might need a way to load test modules automatically without
// explicit declaration as follows.
let testModules = [
'addon_build',
'addon',
'addon_data',
'arraybuffer',
Expand Down

0 comments on commit 7176886

Please sign in to comment.