Skip to content

Commit

Permalink
Get started on sorting out tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Dec 11, 2011
1 parent 6dab36c commit 434d81d
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 5 deletions.
12 changes: 12 additions & 0 deletions Jakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ var fs = require('fs')
, pkg = JSON.parse(fs.readFileSync('./package.json').toString())
, version = pkg.version

desc('Runs the Jake tests.');
task('test', function () {
cmds = [
'node ./tests/parseargs.js'
, 'node ./tests/task_base.js'
];
jake.exec(cmds, function () {
console.log('All tests passed.');
complete();
});
});

var t = new jake.PackageTask('jake', 'v' + version, function () {
var fileList = [
'Makefile'
Expand Down
4 changes: 2 additions & 2 deletions lib/jake.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ jake = new function () {
* @param {Array} args The list of command-line args passed after
* the task name
*/
this.run = function (name, includePrereqs, args) {
args = args || [];
this.run = function (name, includePrereqs, argsArg) {
args = argsArg ? Array.prototype.slice.call(argsArg) : [];
this.createTree(name, {isRoot: true, includePrereqs: includePrereqs, args: args});
_taskList.splice.apply(_taskList, [_taskIndex, 0].concat(_workingTaskList));
_workingTaskList = [];
Expand Down
56 changes: 55 additions & 1 deletion tests/Jakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
desc('The default task.');
task('default', function () {
console.log('default task');
});

desc('Accepts args and env vars.');
task('argsEnvVars', function () {
var res = {
args: arguments
, env: {
foo: process.env.foo
, baz: process.env.baz
}
};
console.log(JSON.stringify(res));
});

namespace('foo', function () {
desc('The foo:bar task.');
task('bar', function () {
if (arguments.length) {
console.log('foo:bar[' +
Array.prototype.join.call(arguments, ',') +
'] task');
}
else {
console.log('foo:bar task');
}
});

desc('The foo:baz task, calls foo:bar as a prerequisite.');
task('baz', ['foo:bar'], function () {
console.log('foo:baz task');
});

desc('The foo:qux task, calls foo:bar with args as a prerequisite.');
task('qux', ['foo:bar[asdf,qwer]'], function () {
console.log('foo:qux task');
});

desc('The foo:qux task, calls foo:bar with cmdline args as a prerequisite.');
task('qux', ['foo:bar[asdf,qwer]'], function () {
console.log('foo:qux task');
});

desc('The foo:frang task, calls foo:bar with passed args as a prerequisite.');
task('frang', function () {
jake.Task['foo:bar'].invoke('zxcv', 'uiop');
console.log('foo:frang task');
});

});

/*
var sys = require('sys'),
fs = require('fs')
proc = require('child_process');
Expand Down Expand Up @@ -166,6 +220,6 @@ task('testExec', function () {
};
jake.exec(cmds, callback, {stdout: true});
}, true);

*/


33 changes: 33 additions & 0 deletions tests/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var exec = require('child_process').exec;

var helpers = new (function () {
this.exec = function (cmd, callback) {
exec(cmd, function (err, stdout, stderr) {
var out = helpers.trim(stdout);
if (err) {
throw err;
}
if (stderr) {
callback(stderr);
}
else if (out) {
callback(out);
}
});
};

this.trim = function (s) {
var str = s || '';
return str.replace(/^\s*|\s*$/g, '');
};

this.parse = function (s) {
var str = s || '';
str = helpers.trim(str);
str = str.replace(/'/g, '"');
return JSON.parse(str);
};

})();

module.exports = helpers;
4 changes: 2 additions & 2 deletions tests/parseargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ assert.equal(undefined, p.opts.jakefile);
p.parse(z('-f zoobie -t howdy'));
assert.equal('zoobie', p.opts.jakefile);
assert.equal(true, p.opts.trace);
assert.equal('howdy', p.taskName);
assert.equal('howdy', p.taskNames[0]);

// -f expects a value, -t does not (foo=bar is env var)
p.parse(z('-f zoobie -t foo=bar'));
Expand All @@ -79,7 +79,7 @@ p.parse(z('-f zoobie -t howdy foo=bar'));
assert.equal('zoobie', p.opts.jakefile);
assert.equal(true, p.opts.trace);
assert.equal('bar', p.envVars.foo);
assert.equal('howdy', p.taskName);
assert.equal('howdy', p.taskNames[0]);

// -t does not expect a value, -f does (throw howdy away)
p.parse(z('-t howdy -f zoobie'));
Expand Down
74 changes: 74 additions & 0 deletions tests/task_base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var assert = require('assert')
, h = require('./helpers');

process.chdir('./tests');

var tests = new (function () {
this.testDefault = function () {
h.exec('../bin/cli.js', function (out) {
assert.equal('default task', out);
});

h.exec('../bin/cli.js default', function (out) {
assert.equal('default task', out);
});
};

this.testWithArgs = function () {
h.exec('../bin/cli.js argsEnvVars[foo,bar]', function (out) {
var parsed = h.parse(out)
, args = parsed.args;
assert.equal(args[0], 'foo');
assert.equal(args[1], 'bar');
});
};

this.testWithEnvVars = function () {
h.exec('../bin/cli.js argsEnvVars foo=bar baz=qux', function (out) {
var parsed = h.parse(out)
, env = parsed.env;
assert.equal(env.foo, 'bar');
assert.equal(env.baz, 'qux');
});
};

this.testWithArgsAndEnvVars = function () {
h.exec('../bin/cli.js argsEnvVars[foo,bar] foo=bar baz=qux', function (out) {
var parsed = h.parse(out)
, args = parsed.args
, env = parsed.env;
assert.equal(args[0], 'foo');
assert.equal(args[1], 'bar');
assert.equal(env.foo, 'bar');
assert.equal(env.baz, 'qux');
});
};

this.testPrereq = function () {
h.exec('../bin/cli.js foo:baz', function (out) {
assert.equal('foo:bar task\nfoo:baz task', out);
});
};

this.testPrereqWithCmdlineArgs = function () {
h.exec('../bin/cli.js foo:qux', function (out) {
assert.equal('foo:bar[asdf,qwer] task\nfoo:qux task', out);
});
};

this.testPrereqWithArgsViaInvoke = function () {
h.exec('../bin/cli.js foo:frang', function (out) {
assert.equal('foo:bar[zxcv,uiop] task\nfoo:frang task', out);
});
};

})();

for (var p in tests) {
if (typeof tests[p] == 'function') {
tests[p]();
}
}

process.chdir('../');

0 comments on commit 434d81d

Please sign in to comment.