Skip to content

Commit

Permalink
enable unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepak Rajamohan committed Jul 30, 2021
1 parent e02e8a4 commit 56dd4af
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,9 @@
"dev:incremental": "node test",
"doc": "doxygen doc/Doxyfile",
"lint": "node tools/clang-format",
"lint:fix": "node tools/clang-format --fix"
"lint:fix": "node tools/clang-format --fix",
"preunit": "node unit-test --filter=\"$npm_config_filter\"",
"unit": "node test"
},
"pre-commit": "lint",
"version": "4.0.0",
Expand Down
5 changes: 5 additions & 0 deletions unit-test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/build
/benchmark/build
/benchmark/src
/test/addon_build/addons
45 changes: 45 additions & 0 deletions unit-test/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
'target_defaults': {
'includes': ['common.gypi'],
'variables': {
'sources': "<!(node -p \"process.env['files_to_run'].split(',')\")",
'build_sources': [
'../test/name.cc',
'./generated/binding.cc'
]
},
},
'targets': [
{
"target_name": "generateBindingCC",
"type": "none",
"actions": [ {
"action_name": "generateBindingCC",
"message": "Generating binding cc file",
"outputs": ["generated/binding.cc"],
"conditions": [
[ "'true'=='true'", {
"inputs": [""],
"action": [
"node",
"generate-binding-cc.js",
"<!(node -p \"process.env['files_to_compile']\")"
]
} ]
]
} ]
},
{
'target_name': 'binding',
'includes': ['../except.gypi'],
'sources': ['>@(build_sources)'],
'dependencies': [ 'generateBindingCC' ]
},
{
'target_name': 'binding_noexcept',
'includes': ['../noexcept.gypi'],
'sources': ['>@(build_sources)'],
'dependencies': [ 'generateBindingCC' ]
},
],
}
21 changes: 21 additions & 0 deletions unit-test/common.gypi
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
'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']
}
}]
],
'include_dirs': ["<!(node -p \"require('../').include_dir\")", "./generated"],
'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ]
}
49 changes: 49 additions & 0 deletions unit-test/generate-binding-cc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const fs = require('fs');
const path = require('path');

const NapiVersionMap = require('./napi-version-map');


console.log('generate start', new Date())

const filesToRun = process.argv.slice(2);
console.log('files to compile: ', process.argv, filesToRun);

p = new Promise((resolve, reject) => {
/*setTimeout(() => {
r('done')
}, 8000)*/
const content = [];

const files = filesToRun[0].split(',');
files.forEach((file) => {

const configName = file.split('.cc')[0];

if (!NapiVersionMap[configName]) return;

const config = NapiVersionMap[configName];
console.log(config)
content.push("#include \"napi.h\"");
content.push("using namespace Napi;");
content.push("Object InitName(Env env);");
content.push("Object Init(Env env, Object exports) {");
content.push("exports.Set(\"name\", InitName(env));");
content.push("return exports;");
content.push("}");
});

resolve(content);
});

p.then((content) => {
const generatedFilePath = path.join(__dirname, 'generated', 'binding.cc' );

console.log( "generatedFilePath", generatedFilePath)

fs.writeFileSync( generatedFilePath , "" );

fs.writeFileSync( generatedFilePath, content.join('\r\n'), { flag: "a" } );

console.log('completed', new Date())
});
7 changes: 7 additions & 0 deletions unit-test/generated/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "napi.h"
using namespace Napi;
Object InitName(Env env);
Object Init(Env env, Object exports) {
exports.Set("name", InitName(env));
return exports;
}
23 changes: 23 additions & 0 deletions unit-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { spawn } = require("child_process");

process.argv.forEach(function (val, index, array) {
console.log(index + ': ' + val);
});

const ls = spawn("node-gyp", ["rebuild"], {cwd: __dirname, env: { ...process.env, files_to_run: '../test/name.cc,generated/binding.cc', files_to_compile: 'name.cc' }});

ls.stdout.on("data", data => {
console.log(`stdout: ${data}`);
});

ls.stderr.on("data", data => {
console.log(`stderr: ${data}`);
});

ls.on('error', (error) => {
console.log(`error: ${error.message}`);
});

ls.on("close", code => {
console.log(`child process exited with code ${code}`);
});
56 changes: 56 additions & 0 deletions unit-test/napi-version-map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"addon": {
"objectName": "Addon",
"versioning": "> 5"
},
"addon_data": {
"objectName": "AddonData",
"versioning": "> 5"
},
"arraybuffer": {
"objectName": "ArrayBuffer"
},
"asynccontext": {
"objectName": "AsyncContext"
},
"asyncprogressqueueworker": {
"objectName": "AsyncProgressQueueWorker",
"versioning": "> 3"
},
"asyncprogressworker": {
"objectName": "AsyncProgressWorker",
"versioning": "> 3"
},
"asyncworker-persistent": {
"objectName": "PersistentAsyncWorker"
},
"asyncworker": {
"objectName": "AsyncWorker"
},
"bigint": { "objectName": "bigint" },
"binding-swallowexcept": { "objectName": "binding-swallowexcept" },
"binding": { "objectName": "binding" },
"buffer": { "objectName": "buffer" },
"callbackscope": { "objectName": "callbackscope" },
"date": { "objectName": "date" },
"error": { "objectName": "error" },
"external": { "objectName": "external" },
"function": { "objectName": "function" },
"functionreference": { "objectName": "functionreference" },
"handlescope": { "objectName": "handlescope" },
"memory_management": { "objectName": "memory_management" },
"movable_callbacks": { "objectName": "movable_callbacks" },
"name": { "objectName": "Name" },
"objectreference": { "objectName": "objectreference" },
"objectwrap-removewrap": { "objectName": "objectwrap-removewrap" },
"objectwrap": { "objectName": "ObjectWrap" },
"objectwrap_constructor_exception": { "objectName": "objectwrap_constructor_exception" },
"objectwrap_multiple_inheritance": { "objectName": "objectwrap_multiple_inheritance" },
"promise": { "objectName": "promise" },
"reference": { "objectName": "reference" },
"run_script": { "objectName": "run_script" },
"symbol": { "objectName": "symbol" },
"thunking_manual": { "objectName": "thunking_manual" },
"typedarray": { "objectName": "typedarray" },
"version_management": { "objectName": "version_management" }
}
3 changes: 3 additions & 0 deletions unit-test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

require('./build/Release/binding.node');

0 comments on commit 56dd4af

Please sign in to comment.