Last active
December 5, 2017 01:26
-
-
Save tinybeachthor/47981b244f14e5a315be34d10d0a0ea3 to your computer and use it in GitHub Desktop.
jUnit 4 standalone node.js compiler/runner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, '.'); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assumes the following directory structure:
and all tests in
test.*packagetested with:
junit-4.12.jarhamcrest-core-1.3.jar