From 528dcde225088a41b8218c72367b73aba15c3869 Mon Sep 17 00:00:00 2001 From: Deepak Rajamohan Date: Sat, 11 Sep 2021 00:48:29 -0700 Subject: [PATCH] unit test --- test/common/index.js | 2 - test/index.js | 2 - unit-test/config.js | 40 ++++++++++++++++ unit-test/generate-binding-cc.js | 78 ++++++++++++++------------------ unit-test/generated/binding.cc | 4 -- unit-test/injectTestParams.js | 4 +- unit-test/test.js | 58 ++++++++---------------- 7 files changed, 95 insertions(+), 93 deletions(-) create mode 100644 unit-test/config.js diff --git a/test/common/index.js b/test/common/index.js index 67577a3f7..02e084ee5 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -84,8 +84,6 @@ exports.runTest = async function(test, buildType, buildPathRoot = process.env.RE path.join(buildPathRoot, `../build/${buildType}/binding_noexcept.node`), ].map(it => require.resolve(it)); - console.log('BINDINGS ... ', bindings) - for (const item of bindings) { await Promise.resolve(test(require(item))) .finally(exports.mustCall()); diff --git a/test/index.js b/test/index.js index 5eb589177..7cad377b1 100644 --- a/test/index.js +++ b/test/index.js @@ -84,8 +84,6 @@ function loadTestModules(currentDirectory = getCurrentDir(), pre = '') { loadTestModules(); -testModules.push('objectwrap') - process.config.target_defaults.default_configuration = fs .readdirSync(path.join(__dirname, 'build')) diff --git a/unit-test/config.js b/unit-test/config.js new file mode 100644 index 000000000..2ae3dd4a9 --- /dev/null +++ b/unit-test/config.js @@ -0,0 +1,40 @@ +const testFunctionsMapV5 = { + "addon": { "objectName": "Addon" }, + "addon_data": { "objectName": "AddonData" } +}; + +const testFunctionsMapV3 = { + "asyncprogressqueueworker": { "objectName": "AsyncProgressQueueWorker" }, + "asyncprogressworker": { "objectName": "AsyncProgressWorker" }, +}; + +const testFunctionsMap = { + "arraybuffer": { "objectName": "ArrayBuffer" }, + "asynccontext": { "objectName": "AsyncContext" }, + "asyncworker-persistent": { "objectName": "PersistentAsyncWorker" }, + "asyncworker": { "objectName": "AsyncWorker" }, + "bigint": { "objectName": "BigInt" }, + "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": "MemoryManagement" }, + "movable_callbacks": { "objectName": "MovableCallbacks" }, + "name": { "objectName": "Name" }, + "objectreference": { "objectName": "ObjectReference" }, + "objectwrap-removewrap": { "objectName": "ObjectWrapRemoveWrap" }, + "objectwrap": { "objectName": "ObjectWrap" }, + "objectwrap_constructor_exception": { "objectName": "ObjectWrapConstructorException" }, + "objectwrap_multiple_inheritance": { "objectName": "ObjectWrapMultipleInheritance" }, + "promise": { "objectName": "Promise" }, + "reference": { "objectName": "Reference" }, + "run_script": { "objectName": "RunScript" }, + "symbol": { "objectName": "Symbol" }, + "thunking_manual": { "objectName": "ThunkingManual" }, + "typedarray": { "objectName": "TypedArray" }, + "version_management": { "objectName": "VersionManagement" } +}; diff --git a/unit-test/generate-binding-cc.js b/unit-test/generate-binding-cc.js index 377e4776b..c2e622bf5 100644 --- a/unit-test/generate-binding-cc.js +++ b/unit-test/generate-binding-cc.js @@ -1,51 +1,41 @@ const fs = require('fs'); const path = require('path'); +const filesToRun = process.argv.slice(2); -const testFunctionsMapV5 = { - "addon": { "objectName": "Addon" }, - "addon_data": { "objectName": "AddonData" } -}; - -const testFunctionsMapV3 = { - "asyncprogressqueueworker": { "objectName": "AsyncProgressQueueWorker" }, - "asyncprogressworker": { "objectName": "AsyncProgressWorker" }, -}; +console.log('files to compile: ', process.argv, filesToRun); +const buildFiles = {}; +const buidDirs = {}; -const testFunctionsMap = { - "arraybuffer": { "objectName": "ArrayBuffer" }, - "asynccontext": { "objectName": "AsyncContext" }, - "asyncworker-persistent": { "objectName": "PersistentAsyncWorker" }, - "asyncworker": { "objectName": "AsyncWorker" }, - "bigint": { "objectName": "BigInt" }, - "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": "MemoryManagement" }, - "movable_callbacks": { "objectName": "MovableCallbacks" }, - "name": { "objectName": "Name" }, - "objectreference": { "objectName": "ObjectReference" }, - "objectwrap-removewrap": { "objectName": "ObjectWrapRemoveWrap" }, - "objectwrap": { "objectName": "ObjectWrap" }, - "objectwrap_constructor_exception": { "objectName": "ObjectWrapConstructorException" }, - "objectwrap_multiple_inheritance": { "objectName": "ObjectWrapMultipleInheritance" }, - "promise": { "objectName": "Promise" }, - "reference": { "objectName": "Reference" }, - "run_script": { "objectName": "RunScript" }, - "symbol": { "objectName": "Symbol" }, - "thunking_manual": { "objectName": "ThunkingManual" }, - "typedarray": { "objectName": "TypedArray" }, - "version_management": { "objectName": "VersionManagement" } -}; +function getExportName(fileName) { + const str = fileName.replace(/(\_\w)/g, (k) => k[1].toUpperCase()); + return str.charAt(0).toUpperCase() + str.substring(1); +} -console.log('generate start', new Date()) +function listOfTestModules(currentDirectory = __dirname + '/../test', pre = '') { + fs.readdirSync(currentDirectory).forEach((file) => { + if (file === 'binding.cc' || + file === 'binding.gyp' || + file === 'build' || + file === 'common' || + file === 'thunking_manual.cc' || + file === 'addon_build' || + file[0] === '.') { + return; + } + const absoluteFilepath = path.join(currentDirectory, file); + const fileName = file.toLowerCase().replace('.cc', ''); + if (fs.statSync(absoluteFilepath).isDirectory()) { + buidDirs[fileName] = {} + listOfTestModules(absoluteFilepath, pre + file + '/'); + } else { + if (!file.toLowerCase().endsWith('.cc')) return; + buildFiles[fileName] = { objectName: getExportName(fileName) }; + } + }); +} +listOfTestModules(); -const filesToRun = process.argv.slice(2); -console.log('files to compile: ', process.argv, filesToRun); +console.log('testBinaries', buildFiles) p = new Promise((resolve, reject) => { /*setTimeout(() => { @@ -57,7 +47,7 @@ p = new Promise((resolve, reject) => { filesToRun.forEach((file) => { const configName = file.split('.cc')[0]; - const config = testFunctionsMap[configName] || testFunctionsMapV5[configName] || testFunctionsMapV3[configName]; + const config = buidDirs[configName]; if (!config) { console.log('not found', file, configName); return @@ -68,7 +58,6 @@ p = new Promise((resolve, reject) => { }); content.push("#include \"napi.h\""); - content.push("#include \"hello.h\""); content.push("using namespace Napi;"); //content.push("Object InitName(Env env);"); @@ -79,7 +68,6 @@ p = new Promise((resolve, reject) => { //content.push("exports.Set(\"name\", InitName(env));"); exports.forEach(exp => content.push(exp)); - content.push("exports.Set(Napi::String::New(env, \"HelloWorld\"), Napi::Function::New(env, HelloWorld));") content.push("return exports;"); content.push("}"); content.push("NODE_API_MODULE(addon, Init);") diff --git a/unit-test/generated/binding.cc b/unit-test/generated/binding.cc index 83f0d1791..7792c838b 100644 --- a/unit-test/generated/binding.cc +++ b/unit-test/generated/binding.cc @@ -1,10 +1,6 @@ #include "napi.h" -#include "hello.h" using namespace Napi; -Object InitFunction(Env env); Object Init(Env env, Object exports) { -exports.Set("function", InitFunction(env)); -exports.Set(Napi::String::New(env, "HelloWorld"), Napi::Function::New(env, HelloWorld)); return exports; } NODE_API_MODULE(addon, Init); \ No newline at end of file diff --git a/unit-test/injectTestParams.js b/unit-test/injectTestParams.js index 87d290561..22ccd59df 100644 --- a/unit-test/injectTestParams.js +++ b/unit-test/injectTestParams.js @@ -1,7 +1,7 @@ module.exports.filesToCompile = function () { const filterCondition = process.env.filter; - let files_to_compile = './generated/binding.cc ../test/hello.cc'; + let files_to_compile = './generated/binding.cc'; let files = filterCondition.split(' ').length ? filterCondition.split(' ') : [filterCondition]; let addedFiles = ''; files.forEach((file) => addedFiles = `${addedFiles} ../test/${file}.cc`); @@ -10,7 +10,7 @@ module.exports.filesToCompile = function () { module.exports.filesForBinding = function () { const filterCondition = process.env.filter; - let files_to_compile = 'hello.cc'; + let files_to_compile = ''; let files = filterCondition.split(' ').length ? filterCondition.split(' ') : [filterCondition]; let addedFiles = ''; files.forEach((file) => addedFiles = `${addedFiles} ${file}.cc`); diff --git a/unit-test/test.js b/unit-test/test.js index 707dbc72b..1297a50fc 100644 --- a/unit-test/test.js +++ b/unit-test/test.js @@ -1,47 +1,29 @@ 'use strict'; const path = require('path'); -const addon = require('./build/Release/binding.node'); -const assert = require("assert"); const { spawn } = require("child_process"); -process.argv.forEach(function (val, index, array) { - console.log(index + ': ' + val); -}); +const executeTests = async function () { + const childProcess = spawn("node", ["test"], {cwd: path.join(__dirname, '../'), env: { ...process.env, REL_BUILD_PATH: path.join(__dirname, './build') }}); -const ls = spawn("node", ["test"], {cwd: path.join(__dirname, '../'), env: { ...process.env, REL_BUILD_PATH: path.join(__dirname, './build') }}); + childProcess.stdout.on("data", data => { + console.log(`${data}`); + }); -ls.stdout.on("data", data => { - console.log(`stdout: ${data}`); -}); + childProcess.stderr.on("data", data => { + console.log(`error: ${data}`); + }); -ls.stderr.on("data", data => { - console.log(`stderr: ${data}`); -}); + return new Promise((resolve, reject) => { + childProcess.on('error', (error) => { + console.log(`error: ${error.message}`); + reject(error); + }); -ls.on('error', (error) => { - console.log(`error: ${error.message}`); -}); + childProcess.on("close", code => { + console.log(`child process exited with code ${code}`); + resolve(); + }); + }); +} -ls.on("close", code => { - console.log(`child process exited with code ${code}`); -}); - -/** - * - * Hello world test - * - */ - const HelloWorld = addon.HelloWorld; - - assert(HelloWorld, "The expected function is undefined"); - - function testBasic() - { - const result = HelloWorld("hello"); - assert.strictEqual(result, "world", "Unexpected value returned"); - } - - assert.doesNotThrow(testBasic, undefined, "testBasic threw an expection"); - - console.log("Tests passed- everything looks OK!"); - \ No newline at end of file +executeTests();