Skip to content

Commit c42da04

Browse files
committed
Rewrite testrunner logic, make JSON config,
Move options logic into ./bin/nodeunit Move testrunner into ./lib/reporters /default
1 parent e314c2b commit c42da04

File tree

6 files changed

+63
-65
lines changed

6 files changed

+63
-65
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ clean:
3939
rm -rf $(BUILDDIR) stamp-build
4040

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

4444
.PHONY: test install uninstall build all

bin/nodeunit

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,64 @@
11
#!/usr/bin/env node
22

3-
var testrunner = require('../lib/testrunner'),
4-
fs = require('fs');
3+
var
4+
fs = require('fs'),
5+
sys = require('sys');
56

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

9-
// load package.json and read version number
10-
if (args.length === 1 && (args[0] === '-v' || args[0] === '--version')) {
11-
fs.readFile(__dirname + '/../package.json', function (err, content) {
12-
if (err) {
13-
throw err;
14-
}
15-
else {
16-
var pkg = JSON.parse(content);
17-
console.log(pkg.version);
18-
}
19-
});
20-
return;
21-
}
10+
var files = [];
11+
12+
var testrunner,
13+
config_file,
14+
config_param_found = false,
15+
reporter_file = 'default',
16+
reporter_param_found = false;
2217

23-
var options;
24-
// check for --config argument
25-
if (args[0] === '--config' || args[0].slice(0, 9) === "--config=") {
26-
var config_file;
27-
if (args[0] === '--config') {
28-
config_file = args[1];
29-
args = args.slice(2);
18+
// a very basic pseudo --options parser
19+
args.forEach(function (arg) {
20+
if (arg.slice(0, 9) === "--config=") {
21+
config_file = arg.slice(9);
22+
} else if (arg === '--config') {
23+
config_param_found = true;
24+
} else if (config_param_found) {
25+
config_file = arg;
26+
config_param_found = false;
27+
} else if (arg === '--reporter') {
28+
reporter_param_found = true;
29+
} else if (reporter_param_found) {
30+
reporter_file = arg;
31+
reporter_param_found = false;
32+
} else if ((arg === '-v') || (arg === '--version')) {
33+
var content = fs.readFileSync(__dirname + '/../package.json', 'utf8');
34+
var pkg = JSON.parse(content);
35+
console.log(pkg.version);
36+
process.exit(0);
3037
} else {
31-
config_file = args[0].slice(9);
32-
args = args.slice(1);
38+
files.push(arg);
3339
}
40+
});
3441

35-
eval(fs.readFileSync(config_file, 'utf8'));
36-
if (typeof options === 'undefined') {
37-
sys.puts("Error: there's no `options` variable in the config file.");
38-
process.exit(1);
42+
// load default options
43+
var content = fs.readFileSync(__dirname + '/nodeunit.json', 'utf8');
44+
var options = JSON.parse(content);
45+
46+
if (config_file) {
47+
content = fs.readFileSync(config_file, 'utf8');
48+
var custom_options = JSON.parse(content);
49+
50+
for (var option in custom_options) {
51+
if (typeof option === 'string') {
52+
options[option] = custom_options[option];
53+
}
3954
}
4055
}
4156

42-
testrunner.run(args, options);
57+
try {
58+
testrunner = require('../lib/reporters/' + reporter_file);
59+
} catch (e) {
60+
testrunner = require(reporter_file);
61+
}
62+
63+
testrunner.run(files, options);
4364

bin/nodeunit.cfg

Lines changed: 0 additions & 18 deletions
This file was deleted.

bin/nodeunit.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"error_prefix": "\u001B[31m",
3+
"error_suffix": "\u001B[39m",
4+
"ok_prefix": "\u001B[32m",
5+
"ok_suffix": "\u001B[39m",
6+
"bold_prefix": "\u001B[1m",
7+
"bold_suffix": "\u001B[22m",
8+
"assertion_prefix": "\u001B[35m",
9+
"assertion_suffix": "\u001B[39m"
10+
}

lib/nodeunit.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ var async = require('../deps/async'),
1818
* Export sub-modules.
1919
*/
2020

21-
exports.testrunner = require('./testrunner');
2221
exports.types = types;
2322
exports.utils = utils;
2423

lib/testrunner.js renamed to lib/reporters/default.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,19 @@
88
* Module dependencies
99
*/
1010

11-
var nodeunit = require('./nodeunit'),
11+
var nodeunit = require('../nodeunit'),
1212
sys = require('sys'),
1313
path = require('path'),
1414
fs = require('fs');
1515

16-
/**
17-
* Default options
18-
*/
19-
20-
eval(fs.readFileSync(__dirname + '/../bin/nodeunit.cfg', 'utf8'));
21-
var default_options = options;
22-
2316
/**
2417
* Run all tests within each module, reporting the results to the command-line.
2518
*
2619
* @param {Array} files
2720
* @api public
2821
*/
2922

30-
exports.run = function (files, custom_options) {
31-
32-
var options = default_options;
33-
for (option_name in custom_options) {
34-
if (typeof option_name === 'string') {
35-
options[option_name] = custom_options[option_name];
36-
}
37-
}
23+
exports.run = function (files, options) {
3824

3925
var error = function (str) {
4026
return options.error_prefix + str + options.error_suffix;

0 commit comments

Comments
 (0)