diff --git a/unit-test/binding-file-template.js b/unit-test/binding-file-template.js index 341779857..66b2436a5 100644 --- a/unit-test/binding-file-template.js +++ b/unit-test/binding-file-template.js @@ -1,3 +1,9 @@ +/** + * + * @param bindingConfigurations + * + * This method acts as a template to generate the content of binding.cc file + */ module.exports.generateFileContent = function (bindingConfigurations) { const content = []; const inits = []; diff --git a/unit-test/exceptions.js b/unit-test/exceptions.js index 1e807f42e..006612664 100644 --- a/unit-test/exceptions.js +++ b/unit-test/exceptions.js @@ -1,3 +1,17 @@ +/** + * + * This file points out anamolies/exceptions in test files when generating the binding.cc file + * + * nouns: words in file names that are misspelled + * *NOTE: a 'constructor' property is explicitly added to override javascript object constructor + * + * exportNames: anamolies in init function names + * + * propertyNames: anamolies in exported property name of init functions + * + * skipBinding: skip including this file in binding.cc + * + */ module.exports = { nouns: { constructor: 'constructor', diff --git a/unit-test/generate-binding-cc.js b/unit-test/generate-binding-cc.js index fc1538777..d98e855c4 100644 --- a/unit-test/generate-binding-cc.js +++ b/unit-test/generate-binding-cc.js @@ -43,12 +43,13 @@ generateBindingConfigurations().then(generateFileContent).then(writeToBindingFil /** * * Test cases - * @fires only when run directly from terminal + * @fires only when run directly from terminal with TEST=true * * * + * */ -if (require.main === module) { +if (require.main === module && process.env.TEST === 'true') { const assert = require('assert'); const setArgsAndCall = (fn, filterCondition) => { process.argv = [null, null, ...filterCondition.split(' ')]; return fn(); }; diff --git a/unit-test/injectTestParams.js b/unit-test/injectTestParams.js index e61eab5af..1caf61a5a 100644 --- a/unit-test/injectTestParams.js +++ b/unit-test/injectTestParams.js @@ -4,6 +4,9 @@ const listOfTestModules = require('./listOfTestModules'); const buildDirs = listOfTestModules.dirs; const buildFiles = listOfTestModules.files; +if (!fs.existsSync('./generated/')) + fs.mkdirSync('./generated/'); + /** * * @returns : list of files to compile by node-gyp @@ -14,14 +17,18 @@ const buildFiles = listOfTestModules.files; * */ module.exports.filesToCompile = function () { - !fs.existsSync('./generated/') && fs.mkdirSync('./generated/', { recursive: true }); - const filterCondition = require('./matchModules').matchWildCards(process.env.filter || ''); - const files_to_compile = './generated/binding.cc test_helper.h'; - const conditions = filterCondition.split(' ').length ? filterCondition.split(' ') : [filterCondition]; + // match filter argument with available test modules + const matchedModules = require('./matchModules').matchWildCards(process.env.filter || ''); + + // standard list of files to compile + const addedFiles = './generated/binding.cc test_helper.h'; + + const filterConditions = matchedModules.split(' ').length ? matchedModules.split(' ') : [matchedModules]; const files = []; - for (const matchCondition of conditions) { + // generate a list of all files to compile + for (const matchCondition of filterConditions) { if (buildDirs[matchCondition.toLowerCase()]) { for (const file of buildDirs[matchCondition.toLowerCase()]) { const config = buildFiles[file]; @@ -35,12 +42,17 @@ module.exports.filesToCompile = function () { } } - let addedFiles = ''; + // generate a string of files to feed to the compiler + let files_to_compile = ''; files.forEach((file) => { - addedFiles = `${addedFiles} ../test/${file}.cc`; + files_to_compile = `${files_to_compile} ../test/${file}.cc`; }); - fs.writeFileSync(__dirname + '/generated/compilelist', `${files_to_compile} ${addedFiles}`); - return `${files_to_compile} ${addedFiles}`; + + // log list of compiled files + fs.writeFileSync(__dirname + '/generated/compilelist', `${addedFiles} ${files_to_compile}`.split(' ').join('\r\n')); + + // return file list + return `${addedFiles} ${files_to_compile}`; }; /** @@ -52,7 +64,7 @@ module.exports.filesToCompile = function () { */ module.exports.filesForBinding = function () { const filterCondition = require('./matchModules').matchWildCards(process.env.filter || ''); - fs.writeFileSync(__dirname + '/generated/bindingList', filterCondition); + fs.writeFileSync(__dirname + '/generated/bindingList', filterCondition.split(' ').join('\r\n')); return filterCondition; }; diff --git a/unit-test/listOfTestModules.js b/unit-test/listOfTestModules.js index 788aea81a..211e2862d 100644 --- a/unit-test/listOfTestModules.js +++ b/unit-test/listOfTestModules.js @@ -5,6 +5,15 @@ const exceptions = require('./exceptions'); const buildFiles = {}; const buidDirs = {}; + +/** + * + * @param fileName - expect to be in snake case , eg: this_is_a_test_file.cc + * @returns init function name in the file + * + * general format of init function name is camelCase version of the snake_case file name + * + */ function getExportObjectName (fileName) { fileName = fileName.split('_').map(token => exceptions.nouns[token] ? exceptions.nouns[token] : token).join('_'); const str = fileName.replace(/(\_\w)/g, (k) => k[1].toUpperCase()); @@ -22,6 +31,11 @@ function getExportPropertyName (fileName) { return fileName; } + +/** + * creates a list of test modules with expected init function names and corresponding export property names + * + */ function listOfTestModules (currentDirectory = __dirname + '/../test', pre = '') { fs.readdirSync(currentDirectory).forEach((file) => { if (file === 'binding.cc' || diff --git a/unit-test/matchModules.js b/unit-test/matchModules.js index 935370d92..531929380 100644 --- a/unit-test/matchModules.js +++ b/unit-test/matchModules.js @@ -11,6 +11,13 @@ function filterBy (wildcard, item) { return new RegExp('^' + wildcard.replace(/\*/g, '.*') + '$').test(item); } + +/** + * @param filterCondition + * + * matches all given wildcards with available test modules to generate an elaborate filter condition + * + */ function matchWildCards (filterCondition) { const conditions = filterCondition.split(' ').length ? filterCondition.split(' ') : [filterCondition]; const matches = []; diff --git a/unit-test/spawnTask.js b/unit-test/spawnTask.js index 20695fbe6..50dacd035 100644 --- a/unit-test/spawnTask.js +++ b/unit-test/spawnTask.js @@ -1,7 +1,10 @@ const { spawn } = require('child_process'); -module.exports.runChildProcess = async function (command, options) { - const childProcess = spawn('node', [command], options); +/* +* spawns a child process to run a given node.js script +*/ +module.exports.runChildProcess = async function (scriptName, options) { + const childProcess = spawn('node', [scriptName], options); childProcess.stdout.on('data', data => { console.log(`${data}`); diff --git a/unit-test/test.js b/unit-test/test.js index 9fa49174e..fafdb7934 100644 --- a/unit-test/test.js +++ b/unit-test/test.js @@ -2,6 +2,11 @@ const path = require('path'); const runChildProcess = require('./spawnTask').runChildProcess; +/* +* +* Execute tests with given filter conditions as a child process +* +*/ const executeTests = async function () { try { const workingDir = path.join(__dirname, '../');