From 02bdbe9fd832ff9b3dc5ab9ecca4160407c31fcc Mon Sep 17 00:00:00 2001 From: Perry Mitchell Date: Sat, 17 Oct 2015 16:13:34 +0300 Subject: [PATCH 1/3] Added recursive flag --- bin/nodeunit | 3 +++ lib/nodeunit.js | 2 +- lib/reporters/default.js | 1 + lib/utils.js | 39 +++++++++++++++++++++++++++++++++------ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/bin/nodeunit b/bin/nodeunit index 7495920b8..0c0a9dbf9 100755 --- a/bin/nodeunit +++ b/bin/nodeunit @@ -29,6 +29,7 @@ var usage = "Usage: nodeunit [options] testmodule1.js testfolder [...] \n" + " --config FILE the path to a JSON file with options\n" + " --reporter FILE optional path to a reporter file to customize the output\n" + " --list-reporters list available build-in reporters\n" + + " -r recursively run tests in sub-directories\n" + " -t testName, specify a test to run\n" + " -f fullTestName, specify a specific test to run. fullTestName is built so: \"outerGroup - .. - innerGroup - testName\"\n" + " -h, --help display this help and exit\n" + @@ -63,6 +64,8 @@ args.forEach(function (arg) { } else if (reporter_param_found) { reporter_file = arg; reporter_param_found = false; + } else if (arg === '-r') { + options.recursive = true; } else if (arg === '-t') { testspec_param_found = true; } else if (testspec_param_found) { diff --git a/lib/nodeunit.js b/lib/nodeunit.js index 7d33c7b3f..45f9108f2 100644 --- a/lib/nodeunit.js +++ b/lib/nodeunit.js @@ -79,7 +79,7 @@ exports.runFiles = function (paths, opt) { exports.done() options.done(types.assertionList(all_assertions, end - start)); }); - }); + }, options.recursive); }; diff --git a/lib/reporters/default.js b/lib/reporters/default.js index 0de3f230d..4a13e5a37 100644 --- a/lib/reporters/default.js +++ b/lib/reporters/default.js @@ -72,6 +72,7 @@ exports.run = function (files, options, callback) { var opts = { testspec: options.testspec, testFullSpec: options.testFullSpec, + recursive: options.recursive, moduleStart: function (name) { console.log('\n' + bold(name)); }, diff --git a/lib/utils.js b/lib/utils.js index b1bc5c3ae..aba4acd84 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -12,7 +12,8 @@ var async = require('../deps/async'), fs = require('fs'), util = require('util'), Script = require('vm').Script, - http = require('http'); + http = require('http'), + path = require('path'); /** @@ -52,10 +53,11 @@ extensionPattern = new RegExp('\\.(?:' + extensions.join('|') + ')$'); * * @param {Array} paths * @param {Function} callback + * @param {Boolean=} recursive * @api public */ - -exports.modulePaths = function (paths, callback) { +exports.modulePaths = function modulePaths(paths, callback, recursive) { + recursive = (recursive === true); async.concatSeries(paths, function (p, cb) { fs.stat(p, function (err, stats) { if (err) { @@ -82,10 +84,35 @@ exports.modulePaths = function (paths, callback) { return [p, mod_name].join('/'); }); - // sort filenames here, because Array.map changes order - fullpaths.sort(); + if (recursive) { + // get all sub directories + var directories = + files + .map(function(filename) { + // resolve path first + return path.resolve(p, filename); + }) + .filter(function(filename) { + // fetch only directories + return (fs.statSync(filename).isDirectory()); + }); + + // recursively call modulePaths() with sub directories + modulePaths(directories, function(err, files) { + if (!err) { + cb(null, fullpaths.concat(files)) + } else { + cb(err); + } + }, recursive); + } else { + // sort filenames here, because Array.map changes order + fullpaths.sort(); + + // finish + cb(null, fullpaths); + } - cb(null, fullpaths); }); } }); From 493e211979638ea2ee46874083f586cb7084ecc7 Mon Sep 17 00:00:00 2001 From: Perry Mitchell Date: Sat, 17 Oct 2015 16:16:37 +0300 Subject: [PATCH 2/3] Sorting for recursive call --- lib/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index aba4acd84..c6d1d0f75 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -100,7 +100,7 @@ exports.modulePaths = function modulePaths(paths, callback, recursive) { // recursively call modulePaths() with sub directories modulePaths(directories, function(err, files) { if (!err) { - cb(null, fullpaths.concat(files)) + cb(null, fullpaths.concat(files).sort()) } else { cb(err); } From 82e1463d9c62d1d0ad7212432f7f2179fcb97c21 Mon Sep 17 00:00:00 2001 From: Perry Mitchell Date: Tue, 30 Aug 2016 22:27:13 +0300 Subject: [PATCH 3/3] Test recursive flag --- test/fixtures/dir/example_test_sub.js | 4 ++++ test/test-cli.js | 23 +++++++++++++++++++++++ test/test-runfiles.js | 6 +++--- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/dir/example_test_sub.js diff --git a/test/fixtures/dir/example_test_sub.js b/test/fixtures/dir/example_test_sub.js new file mode 100644 index 000000000..e744234e0 --- /dev/null +++ b/test/fixtures/dir/example_test_sub.js @@ -0,0 +1,4 @@ +exports['example test sub'] = function (test) { + test.ok(true); + test.done(); +}; diff --git a/test/test-cli.js b/test/test-cli.js index 8799eb8b3..ba914c58f 100644 --- a/test/test-cli.js +++ b/test/test-cli.js @@ -3,6 +3,7 @@ var exec = require('child_process').exec, var bin = path.resolve(__dirname, '../bin/nodeunit'); var testfile_fullpath = path.resolve(__dirname, './fixtures/example_test.js'); +var fixtures_path = path.resolve(__dirname, './fixtures'); exports['run test suite using absolute path'] = function (test) { exec(bin + ' ' + testfile_fullpath, function (err, stdout, stderr) { @@ -14,3 +15,25 @@ exports['run test suite using absolute path'] = function (test) { test.done(); }); }; + +exports['runs only top-level suites without recursive flag'] = function (test) { + exec(bin + ' ' + fixtures_path, function (err, stdout, stderr) { + if (err) { + return test.done(err); + } + test.ok(/example test/.test(stdout)); + test.ok(!/example test sub/.test(stdout)); + test.done(); + }); +}; + +exports['runs top + nested suites with recursive flag'] = function (test) { + exec(bin + ' ' + fixtures_path + ' -r', function (err, stdout, stderr) { + if (err) { + return test.done(err); + } + test.ok(/example test/.test(stdout)); + test.ok(/example test sub/.test(stdout)); + test.done(); + }); +}; diff --git a/test/test-runfiles.js b/test/test-runfiles.js index 19e28f40f..5722f5888 100644 --- a/test/test-runfiles.js +++ b/test/test-runfiles.js @@ -19,7 +19,7 @@ var setup = function (fn) { exports.testRunFiles = setup(function (test) { - test.expect(28); + test.expect(33); var runModule_copy = nodeunit.runModule; var runModule_calls = []; @@ -43,7 +43,7 @@ exports.testRunFiles = setup(function (test) { }, done: function (assertions) { test.equals(assertions.failures(), 0, 'failures'); - test.equals(assertions.length, 4, 'length'); + test.equals(assertions.length, 5, 'length'); test.ok(typeof assertions.duration === "number"); var called_with = function (name) { @@ -55,7 +55,7 @@ exports.testRunFiles = setup(function (test) { test.ok(called_with('mock_module2'), 'mock_module2 ran'); test.ok(called_with('mock_module3'), 'mock_module3 ran'); test.ok(called_with('mock_module4'), 'mock_module4 ran'); - test.equals(runModule_calls.length, 4); + test.equals(runModule_calls.length, 5); nodeunit.runModule = runModule_copy; test.done();