Skip to content

Commit 6562e6b

Browse files
NickNasomhdawson
authored andcommitted
test: added tests to check the build process
Refs: #766 PR-URL: #808 Reviewed-By: Michael Dawson <[email protected]>
1 parent a13b36c commit 6562e6b

File tree

9 files changed

+153
-0
lines changed

9 files changed

+153
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/build
33
/benchmark/build
44
/benchmark/src
5+
/test/addon_build/addons

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
"description": "Node.js API (N-API)",
253253
"devDependencies": {
254254
"benchmark": "^2.1.4",
255+
"fs-extra": "^9.0.1",
255256
"bindings": "^1.5.0",
256257
"safe-buffer": "^5.1.1"
257258
},

test/addon_build/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
const { promisify } = require('util');
4+
const exec = promisify(require('child_process').exec);
5+
const { copy, remove } = require('fs-extra');
6+
const path = require('path');
7+
const assert = require('assert')
8+
9+
const ADDONS_FOLDER = path.join(__dirname, 'addons');
10+
11+
const addons = [
12+
'echo addon',
13+
'echo-addon'
14+
]
15+
16+
async function beforeAll(addons) {
17+
console.log(' >Preparing native addons to build')
18+
for (const addon of addons) {
19+
await remove(path.join(ADDONS_FOLDER, addon));
20+
await copy(path.join(__dirname, 'tpl'), path.join(ADDONS_FOLDER, addon));
21+
}
22+
}
23+
24+
async function test(addon) {
25+
console.log(` >Building addon: '${addon}'`);
26+
const { stderr, stdout } = await exec('npm install', {
27+
cwd: path.join(ADDONS_FOLDER, addon)
28+
})
29+
console.log(` >Runting test for: '${addon}'`);
30+
// Disabled the checks on stderr and stdout because of this issuue on npm:
31+
// Stop using process.umask(): https://github.com/npm/cli/issues/1103
32+
// We should enable the following checks again after the resolution of
33+
// the reported issue.
34+
// assert.strictEqual(stderr, '');
35+
// assert.ok(stderr.length === 0);
36+
// assert.ok(stdout.length > 0);
37+
const binding = require(`${ADDONS_FOLDER}/${addon}`);
38+
assert.strictEqual(binding.except.echo('except'), 'except');
39+
assert.strictEqual(binding.except.echo(101), 101);
40+
assert.strictEqual(binding.noexcept.echo('noexcept'), 'noexcept');
41+
assert.strictEqual(binding.noexcept.echo(103), 103);
42+
}
43+
44+
45+
module.exports = (async function() {
46+
await beforeAll(addons);
47+
for (const addon of addons) {
48+
await test(addon);
49+
}
50+
})()

test/addon_build/tpl/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

test/addon_build/tpl/addon.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <napi.h>
2+
3+
Napi::Value Echo(const Napi::CallbackInfo& info) {
4+
Napi::Env env = info.Env();
5+
if (info.Length() != 1) {
6+
Napi::TypeError::New(env, "Wrong number of arguments. One argument expected.")
7+
.ThrowAsJavaScriptException();
8+
}
9+
return info[0].As<Napi::Value>();
10+
}
11+
12+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
13+
exports.Set(Napi::String::New(env, "echo"), Napi::Function::New(env, Echo));
14+
return exports;
15+
}
16+
17+
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)

test/addon_build/tpl/binding.gyp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
'target_defaults': {
3+
'include_dirs': [
4+
"<!(node -p \"require('node-addon-api').include_dir\")"
5+
],
6+
'variables': {
7+
'NAPI_VERSION%': "<!(node -p \"process.versions.napi\")",
8+
'disable_deprecated': "<!(node -p \"process.env['npm_config_disable_deprecated']\")"
9+
},
10+
'conditions': [
11+
['NAPI_VERSION!=""', { 'defines': ['NAPI_VERSION=<@(NAPI_VERSION)'] } ],
12+
['disable_deprecated=="true"', {
13+
'defines': ['NODE_ADDON_API_DISABLE_DEPRECATED']
14+
}],
15+
['OS=="mac"', {
16+
'cflags+': ['-fvisibility=hidden'],
17+
'xcode_settings': {
18+
'OTHER_CFLAGS': ['-fvisibility=hidden']
19+
}
20+
}]
21+
],
22+
'sources': [
23+
'addon.cc',
24+
],
25+
},
26+
'targets': [
27+
{
28+
'target_name': 'addon',
29+
'defines': [ 'NAPI_CPP_EXCEPTIONS' ],
30+
'cflags!': [ '-fno-exceptions' ],
31+
'cflags_cc!': [ '-fno-exceptions' ],
32+
'msvs_settings': {
33+
'VCCLCompilerTool': {
34+
'ExceptionHandling': 1,
35+
'EnablePREfast': 'true',
36+
},
37+
},
38+
'xcode_settings': {
39+
'CLANG_CXX_LIBRARY': 'libc++',
40+
'MACOSX_DEPLOYMENT_TARGET': '10.7',
41+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
42+
},
43+
},
44+
{
45+
'target_name': 'addon_noexcept',
46+
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
47+
'cflags': [ '-fno-exceptions' ],
48+
'cflags_cc': [ '-fno-exceptions' ],
49+
'msvs_settings': {
50+
'VCCLCompilerTool': {
51+
'ExceptionHandling': 0,
52+
'EnablePREfast': 'true',
53+
},
54+
},
55+
'xcode_settings': {
56+
'CLANG_CXX_LIBRARY': 'libc++',
57+
'MACOSX_DEPLOYMENT_TARGET': '10.7',
58+
'GCC_ENABLE_CPP_EXCEPTIONS': 'NO',
59+
},
60+
},
61+
],
62+
}

test/addon_build/tpl/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
const except = require('bindings')('addon')
4+
const noexcept = require('bindings')('addon_noexcept')
5+
6+
module.exports = {
7+
except,
8+
noexcept
9+
}

test/addon_build/tpl/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "addon",
3+
"version": "0.0.0",
4+
"description": "Node.js addon",
5+
"private": true,
6+
"scripts": {},
7+
"dependencies": {
8+
"node-addon-api": "file:../../../../"
9+
},
10+
"gypfile": true
11+
}

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ process.config.target_defaults.default_configuration =
88
// FIXME: We might need a way to load test modules automatically without
99
// explicit declaration as follows.
1010
let testModules = [
11+
'addon_build',
1112
'addon',
1213
'addon_data',
1314
'arraybuffer',

0 commit comments

Comments
 (0)