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 Oct 15, 2021
1 parent 079ce39 commit defc460
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 14 deletions.
6 changes: 6 additions & 0 deletions unit-test/binding-file-template.js
Original file line number Diff line number Diff line change
@@ -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 = [];
Expand Down
14 changes: 14 additions & 0 deletions unit-test/exceptions.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
5 changes: 3 additions & 2 deletions unit-test/generate-binding-cc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(); };
Expand Down
32 changes: 22 additions & 10 deletions unit-test/injectTestParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
Expand All @@ -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}`;
};

/**
Expand All @@ -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;
};

Expand Down
14 changes: 14 additions & 0 deletions unit-test/listOfTestModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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' ||
Expand Down
7 changes: 7 additions & 0 deletions unit-test/matchModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
7 changes: 5 additions & 2 deletions unit-test/spawnTask.js
Original file line number Diff line number Diff line change
@@ -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}`);
Expand Down
5 changes: 5 additions & 0 deletions unit-test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '../');
Expand Down

0 comments on commit defc460

Please sign in to comment.