Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ Writing a Test Reporter
---------------------

Nodeunit exports runTest(fn, options), runModule(mod, options) and
runFiles(paths, options). You'll most likely want to run test suites from
runFiles(paths, recursive, options). You'll most likely want to run test suites from
files, which can be done using the latter function. The _options_ argument can
contain callbacks which run during testing. Nodeunit provides the following
callbacks:
Expand Down
3 changes: 3 additions & 0 deletions bin/nodeunit
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var usage = "Usage: nodeunit [options] testmodule1.js testfolder [...] \n" +
" --reporter FILE optional path to a reporter file to customize the output\n" +
" --list-reporters list available build-in reporters\n" +
" -t name, specify a test to run\n" +
" -r, --recursive run tests from directories recursively" +
" -h, --help display this help and exit\n" +
" -v, --version output version information and exit";

Expand Down Expand Up @@ -80,6 +81,8 @@ args.forEach(function (arg) {
console.log(' * ' + reporter_file + (reporter.info ? ': ' + reporter.info : ''));
});
process.exit(0);
} else if ((arg === '-r') || (arg === '--recursive')) {
options.recursive = true;
} else if ((arg === '-v') || (arg === '--version')) {
var content = fs.readFileSync(__dirname + '/../package.json', 'utf8');
var pkg = JSON.parse(content);
Expand Down
3 changes: 2 additions & 1 deletion bin/nodeunit.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"bold_prefix": "\u001B[1m",
"bold_suffix": "\u001B[22m",
"assertion_prefix": "\u001B[35m",
"assertion_suffix": "\u001B[39m"
"assertion_suffix": "\u001B[39m",
"recursive": false
}
1 change: 0 additions & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ exports.runModule = function (name, mod, opt, callback) {

// TODO: add proper unit tests for this function
exports.runModules = function (modules, opt) {
var all_assertions = [];
var options = types.options(opt);
var start = new Date().getTime();

Expand Down
10 changes: 5 additions & 5 deletions lib/nodeunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ exports.testrunner = {

for (var k in core) {
exports[k] = core[k];
};
}


/**
Expand All @@ -55,11 +55,12 @@ for (var k in core) {
* sub-directories.
*
* @param {Array} paths
* @param {Boolean} recursive
* @param {Object} opt
* @api public
*/

exports.runFiles = function (paths, opt) {
exports.runFiles = function (paths, recursive, opt) {
var all_assertions = [];
var options = types.options(opt);
var start = new Date().getTime();
Expand All @@ -68,19 +69,18 @@ exports.runFiles = function (paths, opt) {
return options.done(types.assertionList(all_assertions));
}

utils.modulePaths(paths, function (err, files) {
utils.modulePaths(paths, recursive, function (err, files) {
if (err) throw err;
async.concatSeries(files, function (file, cb) {
var name = path.basename(file);
exports.runModule(name, require(file), options, cb);
},
function (err, all_assertions) {
var end = new Date().getTime();
exports.done()
exports.done();
options.done(types.assertionList(all_assertions, end - start));
});
});

};

/* Export all prototypes from events.EventEmitter */
Expand Down
19 changes: 10 additions & 9 deletions lib/reporters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ exports.run = function (files, options, callback) {
}
});

var opts = {
var opts = {
testspec: options.testspec,
moduleStart: function (name) {
console.log('\n' + bold(name));
Expand Down Expand Up @@ -119,12 +119,13 @@ exports.run = function (files, options, callback) {
tracker.put(name);
}
};
if (files && files.length) {
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
nodeunit.runFiles(paths, opts);
} else {
nodeunit.runModules(files,opts);
}

if (files && files.length) {
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
nodeunit.runFiles(paths, options.recursive, opts);
} else {
nodeunit.runModules(files, opts);
}
};
2 changes: 1 addition & 1 deletion lib/reporters/eclipse.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ exports.run = function (files, options, callback) {
}
});

nodeunit.runFiles(paths, {
nodeunit.runFiles(paths, options.recursive, {
testspec: undefined,
moduleStart: function (name) {
console.log('\n' + name);
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ exports.run = function (files, options, callback) {
console.log('</style>');
console.log('</head>');
console.log('<body>');
nodeunit.runFiles(paths, {
nodeunit.runFiles(paths, options.recursive, {
testspec: options.testspec,
moduleStart: function (name) {
console.log('<h2>' + name + '</h2>');
Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/junit.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ exports.run = function (files, opts, callback) {
return path.join(process.cwd(), p);
});

var modules = {}
var modules = {};
var curModule;

nodeunit.runFiles(paths, {
nodeunit.runFiles(paths, options.recursive, {
testspec: opts.testspec,
moduleStart: function (name) {
curModule = {
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/machineout.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ exports.run = function (files, options, callback) {
}
});

nodeunit.runFiles(paths, {
nodeunit.runFiles(paths, options.recursive, {
testspec: options.testspec,
moduleStart: function (name) {},
testDone: function (name, assertions) {
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ exports.run = function (files, options, callback) {
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
nodeunit.runFiles(paths, opts);
nodeunit.runFiles(paths, options.recursive, opts);
} else {
nodeunit.runModules(files,opts);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ exports.run = function (files, options) {
console.log(txt);
};

nodeunit.runFiles(paths, {
nodeunit.runFiles(paths, options.recursive, {
testspec: options.testspec,
moduleStart: function (name) {
console.log('\n' + bold(name));
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/skip_passed.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ exports.run = function (files, options, callback) {
return path.join(process.cwd(), p);
});

nodeunit.runFiles(paths, {
nodeunit.runFiles(paths, options.recursive, {
testspec: options.testspec,
moduleStart: function (name) {
console.log('\n' + bold(name));
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exports.run = function (files, options) {
var output = new TapProducer();
output.pipe(process.stdout);

nodeunit.runFiles(paths, {
nodeunit.runFiles(paths, options.recursive, {
testStart: function (name) {
output.write(name.toString());
},
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/verbose.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ exports.run = function (files, options) {
}
});

nodeunit.runFiles(paths, {
nodeunit.runFiles(paths, options.recursive, {
testspec: options.testspec,
moduleStart: function (name) {
console.log('\n' + bold(name));
Expand Down
110 changes: 84 additions & 26 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var async = require('../deps/async'),
fs = require('fs'),
util = require('util'),
Script = process.binding('evals').Script,
http = require('http');
http = require('http'),
path = require('path');


/**
Expand All @@ -32,54 +33,111 @@ catch (e) {

/**
* Finds all modules at each path in an array, If a path is a directory, it
* returns all supported file types inside it. This only reads 1 level deep in
* returns all supported file types inside it.
*
* With recursive=false this function only reads 1 level deep in
* the directory and does not recurse through sub-directories.
*
* The extension (.js, .coffee etc) is stripped from the filenames so they can
* simply be require()'ed.
*
* @param {Array} paths
* @param {Boolean} recursive
* @param {Function} callback
* @api public
*/

exports.modulePaths = function (paths, callback) {
async.concat(paths, function (p, cb) {
fs.stat(p, function (err, stats) {
exports.modulePaths = function (paths, recursive, callback) {
async.concatSeries(paths, function (path, cb) {
fs.stat(path, function (err, stats) {
if (err) {
return cb(err);
cb(err);
return;
}
if (stats.isFile()) {
return cb(null, [p]);
cb(null, [path]);
return;
}
if (stats.isDirectory()) {
fs.readdir(p, function (err, files) {
exports.readDirMaybeRecursive(path, recursive, function (err, files) {
if (err) {
return cb(err);
cb(err);
return;
}

// filter out any filenames with unsupported extensions
var modules = files.filter(function (filename) {
return extensionPattern.exec(filename);
});

// remove extension from module name and prepend the
// directory path
var fullpaths = modules.map(function (filename) {
var mod_name = filename.replace(extensionPattern, '');
return [p, mod_name].join('/');
});

// sort filenames here, because Array.map changes order
fullpaths.sort();

cb(null, fullpaths);
cb(null, files);
});
return;
}
cb(null, []);
});
}, function (err, files) {
if (err) {
callback(err);
return;
}

// filter out any filenames with unsupported extensions
var modules = files.filter(function (filename) {
return extensionPattern.exec(filename);
});

// remove extension from module name
var paths = modules.map(function (filename) {
return filename.replace(extensionPattern, '');
});
}, callback);

// sort filenames here, because Array.map changes order
paths.sort();
callback(null, paths);
});
};


/**
* Finds all files in directory, maybe recursively
*
* @param {Array} dir
* @param {Boolean} recursive
* @param {Function} callback
* @api private
*/

exports.readDirMaybeRecursive = function(dir, recursive, callback) {
fs.readdir(dir, function (err, dirEntities) {
if (err) {
return callback(err);
}

async.concatSeries(dirEntities, function (entityName, cb) {
var entityFullName = path.join(dir, entityName);
fs.stat(entityFullName, function (err, stats) {
if (err) {
cb(err);
return;
}
if (stats.isFile()) {
cb(null, [entityFullName]);
return;
}
if (stats.isDirectory() && recursive) {
exports.readDirMaybeRecursive(entityFullName, recursive, function (err, files) {
if (err) {
cb(err);
return;
}

cb(null, files);
});
return;
}
cb(null, []);
});
}, callback);
});
};


/**
* Evaluates JavaScript files in a sandbox, returning the context. The first
* argument can either be a single filename or an array of filenames. If
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file added test/fixtures/nested_dirs/nd.js
Empty file.
8 changes: 5 additions & 3 deletions test/test-runfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ exports.testRunFiles = setup(function (test) {
[__dirname + '/fixtures/mock_module1.js',
__dirname + '/fixtures/mock_module2.js',
__dirname + '/fixtures/dir'],
false,
opts
);
});

exports.testRunFilesEmpty = function (test) {
test.expect(3);
nodeunit.runFiles([], {
nodeunit.runFiles([], false, {
moduleStart: function () {
test.ok(false, 'should not be called');
},
Expand Down Expand Up @@ -115,7 +116,7 @@ exports.testEmptyDir = function (test) {
}

// runFiles on empty directory:
nodeunit.runFiles([dir2], {
nodeunit.runFiles([dir2], false, {
moduleStart: function () {
test.ok(false, 'should not be called');
},
Expand Down Expand Up @@ -207,7 +208,8 @@ if (CoffeeScript) {
};

nodeunit.runFiles(
[__dirname + 'fixtures/coffee/mock_coffee_module.coffee'],
[__dirname + '/fixtures/coffee/mock_coffee_module.coffee'],
false,
opts
);
};
Expand Down
4 changes: 1 addition & 3 deletions test/test-sandbox.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var nodeunit = require('../lib/nodeunit');
var sandbox = require('../lib/utils').sandbox;
var testCase = nodeunit.testCase;

exports.testSimpleSandbox = function (test) {
var raw_jscode1 = sandbox(__dirname + '/fixtures/raw_jscode1.js');
Expand All @@ -26,6 +24,6 @@ exports.testSandboxMultiple = function (test) {
__dirname + '/fixtures/raw_jscode3.js',
__dirname + '/fixtures/raw_jscode3.js'
]);
test.equal(raw_jscode3.t, 3, 'two files loaded');
test.equal(raw_jscode3.t, 3, 'three files loaded');
test.done();
};
Loading