Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow nodeunit to run tests in a dir and recursively inside its child dirs #54

Open
wants to merge 1 commit into from
@aurium

I'm using nodeunit in a project and i'm very happy with it, but my collection of test modules is going to be enough big to convince me to organize then in sub-directories. I start to do this, but i discover that "nodeunit test" command will not to run tests inside "test/helpers" or "test/models", so i made a patch to make util.modulePaths() to get it recursively to nodeunit.runFiles().

I don't see why not to work recursively, so i think that is the better default action.

I also made it with synchronous fileSystem methods because the testing action is not paralleled and the asynchronous code to do this is crazily more complex. :-)

@caolan
Owner

I think this is a good idea, but should include 2 changes:

  1. This should be made an option (-r perhaps) since we might break existing test suites by running fixtures directories etc.
  2. Nodeunit makes use of the async library which will help with the complexity of reading directories asynchronously, so we should remove the sync call - I can help with this

Hopefully these changes would be acceptable?
When I get some time to work on this I can make these changes and merge this in.

Thanks again for your contribution :)

@aurium

Thank you!
And i see, i will learn when i see the result code with your adaptation. :-)

@Sannis

I hope I will help with this issue in next week.

Maybe we can use nopt to parse arguments? It installs with npm, so it does not be a problem for node users.

@Sannis Sannis referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@mreinstein
Collaborator

@aurium I think this is a good patch. Can you please update your patch and resubmit? It doesn't merge cleanly.

@achingbrain

It would be rather good if something like this made it in. I frequently find myself arranging tests in subdirectories that mirrors the code they are testing and then being unable to run all the tests in one go without having some sort of test aggregator.

@groe

+1

@ZxBud

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 27, 2011
  1. @aurium
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 22 deletions.
  1. +34 −22 lib/utils.js
View
56 lib/utils.js
@@ -12,7 +12,8 @@ var async = require('../deps/async'),
fs = require('fs'),
sys = require('sys'),
Script = process.binding('evals').Script,
- http = require('http');
+ http = require('http'),
+ path = require('path');
/**
@@ -53,33 +54,44 @@ exports.modulePaths = function (paths, callback) {
return cb(null, [p]);
}
if (stats.isDirectory()) {
- fs.readdir(p, function (err, files) {
- if (err) {
- return cb(err);
- }
-
- // 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);
+ var files = exports.readDirRecursive(p);
+
+ // 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);
}
});
}, callback);
};
+exports.readDirRecursive = function(dir) {
+ var list = [];
+ fs.readdirSync(dir).forEach(function(entityName) {
+ var entityPath = path.join(dir, entityName);
+ if ( fs.statSync(entityPath).isDirectory() ) {
+ exports.readDirRecursive(entityPath).forEach(function(file) {
+ list.push(path.join(entityName, file));
+ });
+ } else {
+ list.push(entityName);
+ }
+ });
+ return list;
+};
+
/**
* Evaluates JavaScript files in a sandbox, returning the context. The first
* argument can either be a single filename or an array of filenames. If
Something went wrong with that request. Please try again.