From 2f3bce71d1b80d5d89c392fa9bb84258d215fa20 Mon Sep 17 00:00:00 2001 From: Artur Adib Date: Tue, 27 Mar 2012 18:51:02 -0400 Subject: [PATCH] echo() overrides silent() --- shell.js | 5 +++-- test/echo.js | 32 ++++++++++++++++++++++++++++---- test/exec.js | 7 +++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/shell.js b/shell.js index 4fc5d3f..d7d840d 100644 --- a/shell.js +++ b/shell.js @@ -777,7 +777,7 @@ exports.which = wrap('which', _which); //@ like `.to()`. function _echo(options) { var messages = [].slice.call(arguments, 1); - log.apply(this, messages); + console.log.apply(this, messages); return ShellString(messages.join(' ')); }; exports.echo = wrap('echo', _echo); @@ -868,7 +868,8 @@ exports.error = function() { //@ silent(silentState); // restore old silent state //@ ``` //@ -//@ Suppresses all output if `state = true`. Returns state if no arguments given. +//@ Suppresses all command output if `state = true`, except for `echo()` calls. +//@ Returns state if no arguments given. exports.silent = function(_state) { if (typeof _state !== 'boolean') return state.silent; diff --git a/test/echo.js b/test/echo.js index 3822684..a3e425f 100644 --- a/test/echo.js +++ b/test/echo.js @@ -2,7 +2,8 @@ var shell = require('..'); var assert = require('assert'), path = require('path'), - fs = require('fs'); + fs = require('fs'), + child = require('child_process'); // Node shims for < v0.7 fs.existsSync = fs.existsSync || path.existsSync; @@ -20,7 +21,30 @@ shell.mkdir('tmp') // Valids // -assert.equal(shell.echo('hello world'), 'hello world'); -assert.equal(shell.error(), null); -shell.exit(123); +// From here on we use child.exec() to intercept the stdout + + +// simple test with defaults +shell.mkdir('-p', 'tmp'); +var file = 'tmp/tempscript'+Math.random()+'.js', + script = 'require(\'../../global.js\'); echo(111);'; +script.to(file); +child.exec('node '+file, function(err, stdout, stderr) { + assert.ok(stdout === '111\n' || stdout === '111\nundefined\n'); // 'undefined' for v0.4 + + // simple test with silent(true) + shell.mkdir('-p', 'tmp'); + var file = 'tmp/tempscript'+Math.random()+'.js', + script = 'require(\'../../global.js\'); silent(true); echo(555);'; + script.to(file); + child.exec('node '+file, function(err, stdout, stderr) { + assert.ok(stdout === '555\n' || stdout === '555\nundefined\n'); // 'undefined' for v0.4 + + theEnd(); + }); +}); + +function theEnd() { + shell.exit(123); +} diff --git a/test/exec.js b/test/exec.js index 9568d76..7194a32 100644 --- a/test/exec.js +++ b/test/exec.js @@ -76,6 +76,13 @@ shell.exec('node -e \"console.log(5678);\"', {async:true}, function(code, output assert.ok(output === '5678\n' || output === '5678\nundefined\n'); // 'undefined' for v0.4 asyncFlags[0] = true; + + // Most of the following code doesn't really belong here since it tests the sync version. + // However there seems to be a race condition with the stdout returned by child.exec() + // that makes the tests fail intermittently. So we're keeping them here in a chain + // to avoid this race issue + + // // check if stdout is proxied with default silent options (i.e. silent = false) //