Skip to content

Instantly share code, notes, and snippets.

@tinybeachthor
Last active December 5, 2017 01:26
Show Gist options
  • Select an option

  • Save tinybeachthor/47981b244f14e5a315be34d10d0a0ea3 to your computer and use it in GitHub Desktop.

Select an option

Save tinybeachthor/47981b244f14e5a315be34d10d0a0ea3 to your computer and use it in GitHub Desktop.
jUnit 4 standalone node.js compiler/runner
var util = require('util')
var exec = require('child_process').exec;
const fs = require('fs')
const path = require('path')
const paths = {
test: './test',
junit: './test/junit.jar',
hamcrest_core: './test/hamcrest-core.jar'
};
const compileTests = `javac -cp .:${paths.junit}:./bin ./test/**/*.java -d .`;
const runTests = `java -cp .:${paths.junit}:${paths.hamcrest_core}:./test:./bin org.junit.runner.JUnitCore`;
//compile tests
exec(compileTests, function (error, stdout, stderr) {
if (error) {
console.log(stderr);
return;
}
else
console.log(stdout);
//find all compiled tests
let testPackages = fromDir(paths.test, '.class').reduce(
(acc, val) => {
return acc += ' ' + classToPackage(val);
},
''
);
// console.log(runTests + testPackages);
//run tests
exec(runTests + testPackages, function (error, stdout, stderr) {
//console.log(error);
console.log(stdout);
console.log(stderr);
});
});
//HELPERS
function fromDir(startPath, filter) {
if (!fs.existsSync(startPath)) {
console.log("no dir ", startPath);
return [];
}
var output = [];
var files = fs.readdirSync(startPath);
for (var i = 0; i < files.length; i++) {
var filename = path.join(startPath, files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
output = output.concat(fromDir(filename, filter));
}
else if (filename.indexOf(filter) >= 0) {
output.push(filename);
};
};
return output.concat();
};
function classToPackage(filename) {
return filename.slice(0, filename.length - 6).replace(/\//g, '.');
}
@tinybeachthor
Copy link
Author

Assumes the following directory structure:

root/
  bin/
  src/
  test/
    test.js

and all tests in test.* package

tested with:
junit-4.12.jar
hamcrest-core-1.3.jar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment