Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 244 lines (216 sloc) 7.854 kB
#!/usr/bin/node
var dokimon = require('./dokimon'),
fs = require('fs'),
i = 0,
args = {},
scripts = [],
COMMAND = {
RUN : '-r',
RUN_SPECIFIED : '-rs',
LIST : '-l',
HELP : '-help',
SHOW : '-s',
HELP_SHORT : '-h',
ARG_CONFIG : '-config',
ARG_HOST : '-host',
ARG_VERBOSE : '-verbose',
ARG_TEST_DIR : '-testdir'
},
availableArgs = [
COMMAND.ARG_CONFIG,
COMMAND.ARG_HOST,
COMMAND.ARG_TEST_DIR,
COMMAND.ARG_VERBOSE
],
argsRequiringValue = [
COMMAND.ARG_CONFIG,
COMMAND.ARG_HOST,
COMMAND.ARG_TEST_DIR
],
arraySearch = function(find, arr) {
for(var i=0; i < arr.length; i++) {
if(arr[i] == find)
return i;
}
return false;
},
fixPath = function(path) {
var fl = path.substr(0,1);
if(fl == '.' || fl == '/')
return path;
return process.cwd()+'/'+path;
},
command = process.argv.length > 2 ? process.argv[2]:COMMAND.HELP;
/**
* Let's pollute the global namespace with our configuration
* @global
* @var {Object} dokimonConfig
*/
dokimonConfig = {};
/*
* Separate requested scripts from arguments
*/
if(process.argv.length > 3) {
for(i=3; i < process.argv.length; i++) {
// argument
if(process.argv[i].substring(0,1) == '-') {
var argVal = true;
// requires val
if(arraySearch(process.argv[i], argsRequiringValue) !== false) {
if(process.argv[i+1] == undefined) {
console.log('Value for argument '+process.argv[i]+' missing');
process.exit();
}
argVal = process.argv[i+1];
}
else if(arraySearch(process.argv[i], availableArgs) === false) {
console.log('Unknown argument '+process.argv[i]);
process.exit(0);
}
args[process.argv[i]] = argVal;
if(argVal !== true) // value given from command line
i++;
}
// script
else
scripts.push(process.argv[i]);
}
}
/*
* Load configuration and override configuration variables with command line args
*/
if(command != COMMAND.HELP && command != COMMAND.HELP_SHORT) {
if(args[COMMAND.ARG_CONFIG] == undefined)
args[COMMAND.ARG_CONFIG] = './config.json';
args[COMMAND.ARG_CONFIG] = fixPath(args[COMMAND.ARG_CONFIG]);
dokimonConfig = JSON.parse(fs.readFileSync(args[COMMAND.ARG_CONFIG]));
for(i = 0; i < argsRequiringValue.length; i++) {
if(args[argsRequiringValue[i]] != undefined)
dokimonConfig[ argsRequiringValue[i].substr(1, argsRequiringValue[i].length) ] = args[argsRequiringValue[i]];
}
if(args[COMMAND.ARG_VERBOSE] != undefined)
dokimonConfig.verbose = true;
var testManager = new dokimon.TestManager(dokimonConfig);
}
/*
* RUN TESTS
*/
if(command == COMMAND.RUN || command == COMMAND.RUN_SPECIFIED) {
var collectedTests = [];
// Run specified test
if(command == COMMAND.RUN_SPECIFIED) {
if(process.argv.length < 5)
console.log('To few arguments, dokimon %s myscript MyTestFunction', COMMAND.RUN_SPECIFIED);
else {
var testFileName = testManager.scriptNameToPath(process.argv[3]);
console.log('# Will run '+testFileName+'::'+process.argv[4]);
var t = require(fixPath(testFileName));
if(t instanceof dokimon.Test)
t = [t];
testManager.setTests(t);
testManager.runTest(process.argv[4]);
}
}
// Run all scripts
else if(scripts.length == 0) {
console.log('##-> RUNNING ALL TESTS');
testManager.loadScriptsInDir(dokimonConfig.testdir).forEach(function(script) {
console.log('# Will run '+script);
var t = require(fixPath(dokimonConfig.testdir +'/'+ script + dokimon.TEST_EXTENSION));
if(t instanceof dokimon.Test)
t = [t];
for(var j=0; j < t.length; j++)
collectedTests.push( t[j] );
});
testManager.setTests(collectedTests);
testManager.run();
}
// Run tests in specified scripts
else {
for(i=0; i < scripts.length; i++) {
try {
var testFileName = testManager.scriptNameToPath(scripts[i]);
console.log('# Will run '+testFileName);
var t = require(fixPath(testFileName));
if(!(t instanceof dokimon.Test)) {
for(var j=0; j < t.length; j++)
collectedTests.push( t[j] );
}
else
collectedTests.push(t);
}
catch(e) {
if(e.message.indexOf('No such file or directory') > -1) {
console.log(scripts[i] +' not found');
if(i > 0) {
console.log("Did you maybe mean to use "+COMMAND.RUN_SPECIFIED+" instead of "+COMMAND.RUN+"? \n" +
"example: dokimon "+COMMAND.RUN_SPECIFIED+" "+scripts[i-1]+' '+scripts[i]);
}
process.exit(0);
}
else
throw e;
}
}
if(collectedTests.length == 0)
console.log('No tests exist!');
else {
testManager.setTests(collectedTests);
testManager.run();
}
}
}
/*
* LIST AVAILABLE TEST SCRIPTS
*/
else if(command == COMMAND.LIST) {
testManager.loadScriptsInDir(dokimonConfig.testdir).forEach(function(script) {
console.log(script);
var t = require(fixPath(dokimonConfig.testdir+'/'+script+dokimon.TEST_EXTENSION));
if(t instanceof dokimon.Test)
t = [t];
for(var j=0; j < t.length; j++)
console.log(" - "+t[j].name)
});
}
/*
* SHOW TESTS IN SCRIPT
*/
else if(command == COMMAND.SHOW) {
if(process.argv.length < 4) {
console.log('To few arguments, dokimon '+COMMAND.SHOW+' myscript');
}
else {
var t = require(fixPath(testManager.scriptNameToPath(process.argv[3])));
if(t instanceof dokimon.Test)
t = [t];
for(var j=0; j < t.length; j++)
console.log(" - "+t[j].name);
}
}
/*
* SHOW HELP INFO
*/
else if(command == COMMAND.HELP_SHORT || command == COMMAND.HELP) {
console.log('More help available at https://github.com/victorjonsson/nodejs-dokimon/');
console.log("Usage:\ndokimon [command] [script | scripts] [arguments]");
console.log('');
console.log('Commands:');
console.log('-r\tRun all scripts or one specified script (e.g. "dokimon -r" or "dokimon -r myscript")');
console.log('-rs\tRun a specific test (e.g. "dokimon -rs myscript sometest")');
console.log('-l\tList all available dokimon scripts and their containing tests');
console.log('-s\tList all tests in the specified dokimon script (e.g. "dokimon -s myscript")');
console.log('');
console.log('Optional arguments:');
console.log('-verbose\tGet a more verbose output when running tests');
console.log('-host \tOverride the host defined in config.json (e.g. "dokimon -r -host dev.mysite.com")');
console.log('-testdir\tUse another test directory than the one defined in config.json (e.g. "dokimon -r -testdir tests/production/")');
console.log('-config \tUse another config file than the one that is automatically loaded by dokimon (e.g. "dokimon -r -config stageserver.conf.json")');
console.log('');
}
/*
* COMMAND NOT FOUND
*/
else {
console.log("Command does not exist, try "+COMMAND.HELP);
}
Jump to Line
Something went wrong with that request. Please try again.