Skip to content

Commit

Permalink
closes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
arturadib committed Sep 28, 2012
1 parent 37ae50c commit 99b9b75
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 63 deletions.
9 changes: 7 additions & 2 deletions README.md
Expand Up @@ -150,7 +150,7 @@ Returns the current directory.
Available options:

+ `-R`: recursive
+ `-A`: all files (include files beginning with `.`)
+ `-A`: all files (include files beginning with `.`, except for `.` and `..`)

Examples:

Expand Down Expand Up @@ -352,7 +352,7 @@ Object containing environment variables (both getter and setter). Shortcut to pr
### exec(command [, options] [, callback])
Available options (all `false` by default):

+ `async`: Asynchronous execution. Needs callback.
+ `async`: Asynchronous execution. Defaults to true if a callback is provided.
+ `silent`: Do not echo program output to console.

Examples:
Expand All @@ -364,6 +364,11 @@ var child = exec('some_long_running_process', {async:true});
child.stdout.on('data', function(data) {
/* ... do something with data ... */
});

exec('some_long_running_process', function(code, output) {
console.log('Exit code:', code);
console.log('Program output:', output);
});
```

Executes the given `command` _synchronously_, unless otherwise specified.
Expand Down
4 changes: 3 additions & 1 deletion scripts/run-tests.js
Expand Up @@ -6,8 +6,10 @@ var failed = false;
cd(__dirname + '/../test');
ls('*.js').forEach(function(file) {
echo('Running test:', file);
if (exec('node '+file).code !== 123) // 123 avoids false positives (e.g. premature exit)
if (exec('node '+file).code !== 123) { // 123 avoids false positives (e.g. premature exit)
failed = true;
echo('*** FAILED! (missing return code)');
}
});

if (failed) {
Expand Down
9 changes: 7 additions & 2 deletions shell.js
Expand Up @@ -866,7 +866,7 @@ exports.env = process.env;
//@ ### exec(command [, options] [, callback])
//@ Available options (all `false` by default):
//@
//@ + `async`: Asynchronous execution. Needs callback.
//@ + `async`: Asynchronous execution. Defaults to true if a callback is provided.
//@ + `silent`: Do not echo program output to console.
//@
//@ Examples:
Expand All @@ -878,6 +878,11 @@ exports.env = process.env;
//@ child.stdout.on('data', function(data) {
//@ /* ... do something with data ... */
//@ });
//@
//@ exec('some_long_running_process', function(code, output) {
//@ console.log('Exit code:', code);
//@ console.log('Program output:', output);
//@ });
//@ ```
//@
//@ Executes the given `command` _synchronously_, unless otherwise specified.
Expand All @@ -894,7 +899,7 @@ function _exec(command, options, callback) {

if (typeof options === 'function') {
callback = options;
options = {};
options = { async: true };
}

options = extend({
Expand Down
73 changes: 15 additions & 58 deletions test/exec.js
Expand Up @@ -65,72 +65,29 @@ shell.cd('../..');
// async
//

// no callback (no need for asyncFlags)
// no callback
var c = shell.exec('node -e \"console.log(1234)\"', {async:true});
assert.equal(shell.error(), null);
assert.ok('stdout' in c, 'async exec returns child process object');

var asyncFlags = [];

//
// callback as 2nd argument
//
asyncFlags[0] = false;
shell.exec('node -e \"console.log(5678);\"', {async:true}, function(code, output) {
shell.exec('node -e \"console.log(5678);\"', function(code, output) {
assert.equal(code, 0);
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 (stdout).
// 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

// STILL SUFFERING INTERMITTENT FAILURES - COMMENTING OUT UNTIL THIS GETS SORTED OUT

shell.exit(123);


// //
// // check if stdout is proxied with default silent options (i.e. silent = false)
// //
// asyncFlags[1] = false;
// shell.mkdir('-p', 'tmp');
// var file = 'tmp/tempscript'+Math.random()+'.js',
// script = 'require(\'../../global.js\'); exec(\'node -e \"console.log(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
// asyncFlags[1] = true;

// //
// // check if stdout is proxied when: silent(true), {silent:false}
// //
// asyncFlags[2] = false;
// shell.mkdir('-p', 'tmp');
// var file = 'tmp/tempscript'+Math.random()+'.js',
// script = 'require(\'../../global.js\'); silent(true); exec(\'node -e \"console.log(333);\"\', {silent:false})';
// script.to(file);
// child.exec('node '+file, function(err, stdout, stderr) {
// assert.ok(stdout === '333\n' || stdout === '333\nundefined\n'); // 'undefined' for v0.4
// asyncFlags[2] = true;

// //
// // check if stdout is proxied when: silent(true), {silent:false} - async
// //
// asyncFlags[3] = false;
// shell.mkdir('-p', 'tmp');
// var file = 'tmp/tempscript'+Math.random()+'.js',
// script = 'require(\'../../global.js\'); silent(true); exec(\'node -e \"console.log(222);\"\', {silent:false, async:true})';
// script.to(file);
// child.exec('node '+file, function(err, stdout, stderr) {
// assert.ok(stdout === '222\n' || stdout === '222\nundefined\n'); // 'undefined' for v0.4
// asyncFlags[3] = true;

// shell.exit(123);
// });
// });
// });

//
// callback as 3rd argument
//
shell.exec('node -e \"console.log(5566);\"', {async:true}, function(code, output) {
assert.equal(code, 0);
assert.ok(output === '5566\n' || output === '5566\nundefined\n'); // 'undefined' for v0.4

shell.exit(123);

});

});

assert.equal(shell.error(), null);

0 comments on commit 99b9b75

Please sign in to comment.