Skip to content

Commit

Permalink
Rewrite testrunner logic, make JSON config,
Browse files Browse the repository at this point in the history
Move options logic into ./bin/nodeunit
Move testrunner into ./lib/reporters /default
  • Loading branch information
Sannis committed Sep 29, 2010
1 parent e314c2b commit c42da04
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ clean:
rm -rf $(BUILDDIR) stamp-build

lint:
nodelint --config nodelint.cfg ./index.js ./bin/nodeunit ./lib/*.js ./test/*.js
nodelint --config nodelint.cfg ./index.js ./bin/nodeunit{,.json} ./lib/*.js ./test/*.js

.PHONY: test install uninstall build all
79 changes: 50 additions & 29 deletions bin/nodeunit
Original file line number Diff line number Diff line change
@@ -1,43 +1,64 @@
#!/usr/bin/env node

var testrunner = require('../lib/testrunner'),
fs = require('fs');
var
fs = require('fs'),
sys = require('sys');

require.paths.push(process.cwd());
var args = process.ARGV.slice(2);

// load package.json and read version number
if (args.length === 1 && (args[0] === '-v' || args[0] === '--version')) {
fs.readFile(__dirname + '/../package.json', function (err, content) {
if (err) {
throw err;
}
else {
var pkg = JSON.parse(content);
console.log(pkg.version);
}
});
return;
}
var files = [];

var testrunner,
config_file,
config_param_found = false,
reporter_file = 'default',
reporter_param_found = false;

var options;
// check for --config argument
if (args[0] === '--config' || args[0].slice(0, 9) === "--config=") {
var config_file;
if (args[0] === '--config') {
config_file = args[1];
args = args.slice(2);
// a very basic pseudo --options parser
args.forEach(function (arg) {
if (arg.slice(0, 9) === "--config=") {
config_file = arg.slice(9);
} else if (arg === '--config') {
config_param_found = true;
} else if (config_param_found) {
config_file = arg;
config_param_found = false;
} else if (arg === '--reporter') {
reporter_param_found = true;
} else if (reporter_param_found) {
reporter_file = arg;
reporter_param_found = false;
} else if ((arg === '-v') || (arg === '--version')) {
var content = fs.readFileSync(__dirname + '/../package.json', 'utf8');
var pkg = JSON.parse(content);
console.log(pkg.version);
process.exit(0);
} else {
config_file = args[0].slice(9);
args = args.slice(1);
files.push(arg);
}
});

eval(fs.readFileSync(config_file, 'utf8'));
if (typeof options === 'undefined') {
sys.puts("Error: there's no `options` variable in the config file.");
process.exit(1);
// load default options
var content = fs.readFileSync(__dirname + '/nodeunit.json', 'utf8');
var options = JSON.parse(content);

if (config_file) {
content = fs.readFileSync(config_file, 'utf8');
var custom_options = JSON.parse(content);

for (var option in custom_options) {
if (typeof option === 'string') {
options[option] = custom_options[option];
}
}
}

testrunner.run(args, options);
try {
testrunner = require('../lib/reporters/' + reporter_file);
} catch (e) {
testrunner = require(reporter_file);
}

testrunner.run(files, options);

18 changes: 0 additions & 18 deletions bin/nodeunit.cfg

This file was deleted.

10 changes: 10 additions & 0 deletions bin/nodeunit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"error_prefix": "\u001B[31m",
"error_suffix": "\u001B[39m",
"ok_prefix": "\u001B[32m",
"ok_suffix": "\u001B[39m",
"bold_prefix": "\u001B[1m",
"bold_suffix": "\u001B[22m",
"assertion_prefix": "\u001B[35m",
"assertion_suffix": "\u001B[39m"
}
1 change: 0 additions & 1 deletion lib/nodeunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var async = require('../deps/async'),
* Export sub-modules.
*/

exports.testrunner = require('./testrunner');
exports.types = types;
exports.utils = utils;

Expand Down
18 changes: 2 additions & 16 deletions lib/testrunner.js → lib/reporters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,19 @@
* Module dependencies
*/

var nodeunit = require('./nodeunit'),
var nodeunit = require('../nodeunit'),
sys = require('sys'),
path = require('path'),
fs = require('fs');

/**
* Default options
*/

eval(fs.readFileSync(__dirname + '/../bin/nodeunit.cfg', 'utf8'));
var default_options = options;

/**
* Run all tests within each module, reporting the results to the command-line.
*
* @param {Array} files
* @api public
*/

exports.run = function (files, custom_options) {

var options = default_options;
for (option_name in custom_options) {
if (typeof option_name === 'string') {
options[option_name] = custom_options[option_name];
}
}
exports.run = function (files, options) {

var error = function (str) {
return options.error_prefix + str + options.error_suffix;
Expand Down

0 comments on commit c42da04

Please sign in to comment.