Browse files

File-task tests.

  • Loading branch information...
1 parent 433bf72 commit 80d9f0709dfc68ae2de8706a4c46a99cd6e2a39d @mde mde committed Dec 13, 2011
Showing with 122 additions and 8 deletions.
  1. +2 −1 Jakefile
  2. +40 −0 tests/Jakefile
  3. +31 −0 tests/file_task.js
  4. +25 −0 tests/helpers.js
  5. +24 −7 tests/task_base.js
View
3 Jakefile
@@ -7,11 +7,12 @@ task('test', function () {
cmds = [
'node ./tests/parseargs.js'
, 'node ./tests/task_base.js'
+ , 'node ./tests/file_task.js'
];
jake.exec(cmds, function () {
console.log('All tests passed.');
complete();
- });
+ }, {stdout: true});
});
var t = new jake.PackageTask('jake', 'v' + version, function () {
View
40 tests/Jakefile
@@ -1,8 +1,17 @@
+var exec = require('child_process').exec
+ , fs = require('fs');
+
desc('The default task.');
task('default', function () {
console.log('default task');
});
+desc('No action.');
+task({'noAction': ['default']});
+
+desc('No action, no prereqs.');
+task('noActionNoPrereqs');
+
desc('Accepts args and env vars.');
task('argsEnvVars', function () {
var res = {
@@ -90,6 +99,37 @@ namespace('hoge', function () {
});
+directory('foo');
+
+desc('File task, concatenating two files together');
+file({'foo/concat.txt': ['foo', 'foo/src1.txt', 'foo/src2.txt']}, function () {
+ console.log('foo/concat.txt task');
+ var data1 = fs.readFileSync('foo/src1.txt');
+ var data2 = fs.readFileSync('foo/src2.txt');
+ fs.writeFileSync('foo/concat.txt', data1 + data2);
+});
+
+desc('File task, async creation with child_process.exec');
+file('foo/src1.txt', function () {
+ fs.writeFile('foo/src1.txt', 'src1', function (err) {
+ console.log('foo/src1.txt task');
+ complete();
+ });
+}, {async: true});
+
+desc('File task, sync creation with writeFileSync');
+file({'foo/src2.txt': 'default'}, function () {
+ fs.writeFileSync('foo/src2.txt', 'src2');
+ console.log('foo/src2.txt task');
+});
+
+desc('File task, do not run unless the prereq file changes');
+file({'foo/src2-foo.txt': ['foo', 'foo/src2.txt']}, function () {
+ console.log('foo/src2-foo.txt task');
+ var data = fs.readFileSync('foo/src2.txt');
+ fs.writeFileSync('foo/src2-foo.txt', data);
+});
+
/*
var sys = require('sys'),
fs = require('fs')
View
31 tests/file_task.js
@@ -0,0 +1,31 @@
+var assert = require('assert')
+ , fs = require('fs')
+ , exec = require('child_process').exec
+ , h = require('./helpers');
+
+process.chdir('./tests');
+
+var tests = new (function () {
+ this.testConcatTwoFiles = function () {
+ h.exec('../bin/cli.js foo/concat.txt', function (out) {
+ var data;
+ assert.equal('default task\nfoo/src2.txt task\n' +
+ 'foo/concat.txt task\nfoo/src1.txt task', out);
+ data = fs.readFileSync(process.cwd() + '/foo/concat.txt');
+ exec('rm -fr ./foo', function (err, stdout, stderr) {
+ if (err) { throw err }
+ if (stderr || stdout) {
+ console.log (stderr || stdout);
+ }
+ h.next();
+ });
+ });
+ };
+
+})();
+
+h.run(tests, function () {
+ process.chdir('../');
+});
+
+
View
25 tests/helpers.js
@@ -1,6 +1,20 @@
var exec = require('child_process').exec;
var helpers = new (function () {
+ var _tests
+ , _names = []
+ , _name
+ , _callback
+ , _runner = function () {
+ if (!!(_name = _names.shift())) {
+ console.log('Running ' + _name);
+ _tests[_name]();
+ }
+ else {
+ _callback();
+ }
+ };
+
this.exec = function (cmd, callback) {
exec(cmd, function (err, stdout, stderr) {
var out = helpers.trim(stdout);
@@ -28,6 +42,17 @@ var helpers = new (function () {
return JSON.parse(str);
};
+ this.run = function (tests, callback) {
+ _tests = tests;
+ _names = Object.keys(tests);
+ _callback = callback;
+ _runner();
+ };
+
+ this.next = function () {
+ _runner();
+ };
+
})();
module.exports = helpers;
View
31 tests/task_base.js
@@ -12,6 +12,19 @@ var tests = new (function () {
h.exec('../bin/cli.js default', function (out) {
assert.equal('default task', out);
});
+ h.next();
+ };
+
+ this.testNoAction = function () {
+ h.exec('../bin/cli.js noAction', function (out) {
+ assert.equal('default task', out);
+ });
+ h.next();
+ };
+
+ this.testNoActionNoPrereqs = function () {
+ h.exec('../bin/cli.js noActionNoPrereqs', function () {});
+ h.next();
};
this.testWithArgs = function () {
@@ -21,6 +34,7 @@ var tests = new (function () {
assert.equal(args[0], 'foo');
assert.equal(args[1], 'bar');
});
+ h.next();
};
this.testWithEnvVars = function () {
@@ -30,6 +44,7 @@ var tests = new (function () {
assert.equal(env.foo, 'bar');
assert.equal(env.baz, 'qux');
});
+ h.next();
};
this.testWithArgsAndEnvVars = function () {
@@ -42,45 +57,47 @@ var tests = new (function () {
assert.equal(env.foo, 'bar');
assert.equal(env.baz, 'qux');
});
+ h.next();
};
this.testPrereq = function () {
h.exec('../bin/cli.js foo:baz', function (out) {
assert.equal('foo:bar task\nfoo:baz task', out);
});
+ h.next();
};
this.testPrereqWithCmdlineArgs = function () {
h.exec('../bin/cli.js foo:qux', function (out) {
assert.equal('foo:bar[asdf,qwer] task\nfoo:qux task', out);
});
+ h.next();
};
this.testPrereqWithArgsViaInvoke = function () {
h.exec('../bin/cli.js foo:frang[zxcv,uiop]', function (out) {
assert.equal('foo:bar[zxcv,uiop] task\nfoo:frang task', out);
});
+ h.next();
};
this.testPrereqOrdering = function () {
h.exec('../bin/cli.js hoge:fuga', function (out) {
assert.equal('hoge:hoge task\nhoge:piyo task\nhoge:fuga task', out);
});
+ h.next();
};
this.testAsync = function () {
h.exec('../bin/cli.js bar:bar', function (out) {
assert.equal('bar:foo task\nbar:bar task', out);
});
+ h.next();
};
})();
-for (var p in tests) {
- if (typeof tests[p] == 'function') {
- tests[p]();
- }
-}
-
-process.chdir('../');
+h.run(tests, function () {
+ process.chdir('../');
+});

0 comments on commit 80d9f07

Please sign in to comment.