Skip to content

Commit

Permalink
Harden paths for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gregrperkins committed Feb 3, 2013
1 parent 88d2cf5 commit 28a9e4c
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
'use strict';

var grunt = require('grunt');

var spawn = require('child_process').spawn;
var path = require('path');

var grunt = require('grunt');
var gruntfileFixture = path.join('test', 'fixtures', 'Gruntfile.js');
var gruntBin = path.resolve('node_modules', '.bin', 'grunt');

// Descent with modification from grunt.util.spawn
var gruntSpawn = function(task, cb) {
var args = ['--gruntfile', gruntfileFixture].concat([task]);
var child = spawn(gruntBin, args);
var stdout = '';
var stderr = '';
if (child.stdout) {
child.stdout.on('data', function(buf) { stdout += buf; });
}
if (child.stderr) {
child.stderr.on('data', function(buf) { stderr += buf; });
}
child.on('close', function(code) {
cb(stdout, code, stderr);
});
};


exports['grunt'] = {
// Just testing that subprocessing grunt works
'can spawn properly': function(test) {
test.expect(1);
grunt.util.spawn({
grunt: true,
args: ['--gruntfile', gruntfileFixture, 'okay'],
}, function(err, result, code) {
gruntSpawn('okay', function(stdout, code) {
test.equal(code, 0, 'should have no error code');
test.done();
});
},
// Show that we get errors when we throw stuff
'returns errors to calling process': function(test) {
test.expect(2);
grunt.util.spawn({
grunt: true,
args: ['--gruntfile', gruntfileFixture, 'fail'],
}, function(err, result, code) {
gruntSpawn('fail', function(stdout, code) {
test.notEqual(code, 0, 'should return error code on assertion failure');
var outHasFail = /Fatal error: broken/.test(result.stdout);
var outHasFail = /Fatal error: broken/.test(stdout);
test.ok(outHasFail, 'stdout should contain output indicating failure.');
test.done();
});
Expand All @@ -36,13 +50,10 @@ exports['grunt-mocha-hack'] = {
// Show that we can spawn the grunt-mocha-hack and get success
'can spawn properly': function(test) {
test.expect(3);
grunt.util.spawn({
grunt: true,
args: ['--gruntfile', gruntfileFixture, 'mocha-hack:one'],
}, function(err, result, code) {
console.log(result.stdout);
test.ok(/# tests 3/.test(result.stdout), 'should have run 3 tests');
test.ok(/# fail 0/.test(result.stdout), 'should have failed 0 tests');
gruntSpawn('mocha-hack:one', function(stdout, code) {
// console.log(stdout);
test.ok(/# tests 3/.test(stdout), 'should have run 3 tests');
test.ok(/# fail 0/.test(stdout), 'should have failed 0 tests');
test.equal(code, 0, 'should have no error code');
test.done();
});
Expand All @@ -51,13 +62,10 @@ exports['grunt-mocha-hack'] = {
// Instead, we should get a complete and meaningful printout.
'gets errors properly': function(test) {
test.expect(3);
grunt.util.spawn({
grunt: true,
args: ['--gruntfile', gruntfileFixture, 'mocha-hack:two'],
}, function(err, result, code) {
console.log(result.stdout);
test.ok(/# tests 3/.test(result.stdout), 'should have run 3 tests');
test.ok(/# fail 3/.test(result.stdout), 'should have failed 3 tests');
gruntSpawn('mocha-hack:two', function(stdout, code) {
// console.log(stdout);
test.ok(/# tests 3/.test(stdout), 'should have run 3 tests');
test.ok(/# fail 3/.test(stdout), 'should have failed 3 tests');
test.equal(code, 3, 'should have error code - task failure');
test.done();
});
Expand Down

0 comments on commit 28a9e4c

Please sign in to comment.