From 28a9e4c5fae18e0c46e5d4931a9dd4eff41a8c82 Mon Sep 17 00:00:00 2001 From: Greg Perkins Date: Sat, 2 Feb 2013 18:32:04 -0800 Subject: [PATCH] Harden paths for tests --- test/test.js | 58 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/test/test.js b/test/test.js index 7f2a020..382b161 100644 --- a/test/test.js +++ b/test/test.js @@ -1,18 +1,35 @@ '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(); }); @@ -20,12 +37,9 @@ exports['grunt'] = { // 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(); }); @@ -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(); }); @@ -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(); });