Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added test case to check the addon building process. #808

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
}]
],
'include_dirs': ["<!@(node -p \"require('../').include\")"],
'include_dirs': ["<!(node -p \"require('../').include_dir\")"],
'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ]
}
2 changes: 1 addition & 1 deletion doc/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ To use **N-API** in a native module:
2. Reference this package's include directory and gyp file in `binding.gyp`:

```gyp
'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],
'include_dirs': ["<!(node -p \"require('node-addon-api').include_dir\")"],
```

3. Decide whether the package will enable C++ exceptions in the N-API wrapper.
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const path = require('path');

const include = path.relative('.', __dirname);
const include_dir = path.relative('.', __dirname);

module.exports = {
include: include,
gyp: path.join(include, 'node_api.gyp:nothing'),
include: `"${__dirname}"`, // deprecated, can be removed as part of 4.0.0
include_dir,
gyp: path.join(include_dir, 'node_api.gyp:nothing'),
isNodeApiBuiltin: true,
needsFlag: false
};
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',
},
},
],
}
10 changes: 10 additions & 0 deletions test/addon_build/tpl/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'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
8 changes: 4 additions & 4 deletions tools/conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ if (disable != "--disable" && dir != "--disable") {
[ /[ ]*"nan": *"[^"]+"(,|)[\n\r]/g, '' ]
],
'binding.gyp': [
[ /([ ]*)'include_dirs': \[/g, '$1\'include_dirs\': [\n$1 \'<!@(node -p "require(\\\'node-addon-api\\\').include")\',' ],
[ /([ ]*)"include_dirs": \[/g, '$1"include_dirs": [\n$1 "<!@(node -p \\"require(\'node-addon-api\').include\\")",' ],
[ /([ ]*)'include_dirs': \[/g, '$1\'include_dirs\': [\n$1 \'<!(node -p "require(\\\'node-addon-api\\\').include_dir")\',' ],
[ /([ ]*)"include_dirs": \[/g, '$1"include_dirs": [\n$1 "<!(node -p \\"require(\'node-addon-api\').include_dir\\")",' ],
[ /[ ]*("|')<!\(node -e ("|'|\\"|\\')require\(("|'|\\"|\\')nan("|'|\\"|\\')\)("|'|\\"|\\')\)("|')(,|)[\r\n]/g, '' ],
[ /([ ]*)("|')target_name("|'): ("|')(.+?)("|'),/g, '$1$2target_name$2: $4$5$6,\n $2cflags!$2: [ $2-fno-exceptions$2 ],\n $2cflags_cc!$2: [ $2-fno-exceptions$2 ],\n $2xcode_settings$2: { $2GCC_ENABLE_CPP_EXCEPTIONS$2: $2YES$2,\n $2CLANG_CXX_LIBRARY$2: $2libc++$2,\n $2MACOSX_DEPLOYMENT_TARGET$2: $210.7$2,\n },\n $2msvs_settings$2: {\n $2VCCLCompilerTool$2: { $2ExceptionHandling$2: 1 },\n },' ],
]
Expand All @@ -35,8 +35,8 @@ if (disable != "--disable" && dir != "--disable") {
[ /[ ]*"nan": *"[^"]+"(,|)[\n\r]/g, '' ]
],
'binding.gyp': [
[ /([ ]*)'include_dirs': \[/g, '$1\'include_dirs\': [\n$1 \'<!@(node -p "require(\\\'node-addon-api\\\').include")\',' ],
[ /([ ]*)"include_dirs": \[/g, '$1"include_dirs": [\n$1 "<!@(node -p \'require(\\\"node-addon-api\\\").include\')",' ],
[ /([ ]*)'include_dirs': \[/g, '$1\'include_dirs\': [\n$1 \'<!(node -p "require(\\\'node-addon-api\\\').include_dir")\',' ],
[ /([ ]*)"include_dirs": \[/g, '$1"include_dirs": [\n$1 "<!(node -p \'require(\\\"node-addon-api\\\").include_dir\')",' ],
[ /[ ]*("|')<!\(node -e ("|'|\\"|\\')require\(("|'|\\"|\\')nan("|'|\\"|\\')\)("|'|\\"|\\')\)("|')(,|)[\r\n]/g, '' ],
[ /([ ]*)("|')target_name("|'): ("|')(.+?)("|'),/g, '$1$2target_name$2: $4$5$6,\n $2cflags!$2: [ $2-fno-exceptions$2 ],\n $2cflags_cc!$2: [ $2-fno-exceptions$2 ],\n $2defines$2: [ $2NAPI_DISABLE_CPP_EXCEPTIONS$2 ],\n $2conditions$2: [\n [\'OS==\"win\"\', { $2defines$2: [ $2_HAS_EXCEPTIONS=1$2 ] }]\n ]' ],
]
Expand Down