Permalink
Browse files

Finished up TestTask, added comments

  • Loading branch information...
mde
mde committed Jun 19, 2012
1 parent 50973b2 commit 3c0c63531f794049a5cc844230900a6ceb440793
Showing with 43 additions and 11 deletions.
  1. +43 −11 lib/test_task.js
View
@@ -21,49 +21,81 @@ var path = require('path')
, exec = require('child_process').exec
, currDir = process.cwd();
-var TestTask = function (directory) {
+var TestTask = function (name, directory) {
this.directory = directory;
- namespace('test', function () {
+ desc('Run the tests for ' + name);
+ task('test', ['test:run']);
+ namespace('test', function () {
task('run', function () {
+ // Grab all the JS files in the specified directory
var jsFiles = jake.readdirR(directory).filter(function (f) {
return /\.js$/.test(f);
});
+
+ // Create a namespace for all the testing tasks to live in
namespace('testExec', function () {
+ // Each test will be a prereq for the dummy top-level task
var prereqs = []
+ // Continuation to pass to the async tests, wrapping `continune`
, next = function () {
complete();
}
- , createAction = function (a) {
- return a(next);
+ // Used as the action for the defined task for each test.
+ , createAction = function (n, a) {
+ // A wrapped function that passes in the `next` function
+ // for any tasks that run asynchronously
+ return function () {
+ console.log('Running ' + n);
+ // 'this' will be the task when action is run
+ return a.call(this, next);
+ }
};
+ // Pull in each test-file, and iterate over any exported
+ // test-functions. Register each test-function as a prereq
+ // task
jsFiles.forEach(function (file) {
var exp = require(path.join(currDir, file))
, name
- , action;
+ , action
+ , isAsync;
+
for (var p in exp) {
name = p;
action = exp[p];
+ // If the test-function is defined with a continuation
+ // param, flag the task as async
+ isAsync = !!action.length;
+ // Add the name of this test to the list of prereqs
+ // for the dummy top-level task
prereqs.push(name);
- task(name, createAction(action), {
- async: !!action.length
+ // Define the actual task with the name, the wrapped action,
+ // and the correc async-flag
+ task(name, createAction(name, action), {
+ async: isAsync
});
}
});
+ // Create the dummy top-level task. When calling a task internally
+ // with `invoke` that is async (or has async prereqs), have to listen
+ // for the 'complete' event to know when it's done
task('__top__', prereqs);
- jake.Task['testExec:__top__'].invoke();
+ var t = jake.Task['testExec:__top__'];
+ t.addListener('complete', function () {
+ console.log('All tests ran successfully');
+ complete();
+ });
+ t.invoke(); // Do the thing!
});
- });
+ }, {async: true});
});
-
-
};
jake.TestTask = TestTask;

0 comments on commit 3c0c635

Please sign in to comment.