Browse files

Get started on sorting out tests.

  • Loading branch information...
1 parent 6dab36c commit 434d81d77a1fd24880f56f70b2764b6abf8697c1 @mde mde committed Dec 10, 2011
Showing with 178 additions and 5 deletions.
  1. +12 −0 Jakefile
  2. +2 −2 lib/jake.js
  3. +55 −1 tests/Jakefile
  4. +33 −0 tests/helpers.js
  5. +2 −2 tests/parseargs.js
  6. +74 −0 tests/task_base.js
View
12 Jakefile
@@ -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'
View
4 lib/jake.js
@@ -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 = [];
View
56 tests/Jakefile
@@ -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');
@@ -166,6 +220,6 @@ task('testExec', function () {
};
jake.exec(cmds, callback, {stdout: true});
}, true);
-
+*/
View
33 tests/helpers.js
@@ -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;
View
4 tests/parseargs.js
@@ -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'));
@@ -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'));
View
74 tests/task_base.js
@@ -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.