Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Feature/fix child process type checking #8454

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/api/process.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ emulation with `process.kill()`, and `child_process.kill()`:

## process.stdout

A `Writable Stream` to `stdout`.
A `Writable Stream` to `stdout` (on fd `1`).

Example: the definition of `console.log`

Expand Down Expand Up @@ -150,7 +150,7 @@ See [the tty docs](tty.html#tty_tty) for more information.

## process.stderr

A writable stream to stderr.
A writable stream to stderr (on fd `2`).

`process.stderr` and `process.stdout` are unlike other streams in Node in
that writes to them are usually blocking.
Expand All @@ -164,7 +164,7 @@ that writes to them are usually blocking.

## process.stdin

A `Readable Stream` for stdin.
A `Readable Stream` for stdin (on fd `0`).

Example of opening standard input and listening for both events:

Expand Down
32 changes: 22 additions & 10 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ exports.fork = function(modulePath /*, args, options*/) {
if (Array.isArray(arguments[1])) {
args = arguments[1];
options = util._extend({}, arguments[2]);
} else if (arguments[1] && typeof arguments[1] !== 'object') {
throw new TypeError('Incorrect value of args option');
} else {
args = [];
options = util._extend({}, arguments[1]);
Expand Down Expand Up @@ -591,7 +593,7 @@ exports.exec = function(command /*, options, callback */) {


exports.execFile = function(file /* args, options, callback */) {
var args, optionArg, callback;
var args = [], optionArg, callback;
var options = {
encoding: 'utf8',
timeout: 0,
Expand All @@ -601,18 +603,28 @@ exports.execFile = function(file /* args, options, callback */) {
env: null
};

// Parse the parameters.
// Parse the optional positional parameters.
var pos = 1;
if (pos < arguments.length && Array.isArray(arguments[pos])) {
args = arguments[pos++];
}
else if(pos < arguments.length && arguments[pos] == null) {
pos++;
}

if (typeof arguments[arguments.length - 1] === 'function') {
callback = arguments[arguments.length - 1];
if (pos < arguments.length && typeof arguments[pos] === 'object') {
options = util._extend(options, arguments[pos++]);
}
else if(pos < arguments.length && arguments[pos] == null) {
pos++;
}

if (Array.isArray(arguments[1])) {
args = arguments[1];
options = util._extend(options, arguments[2]);
} else {
args = [];
options = util._extend(options, arguments[1]);
if (pos < arguments.length && typeof arguments[pos] === 'function') {
callback = arguments[pos++];
}

if (pos === 1 && arguments.length > 1) {
throw new TypeError('Incorrect value of args option');
}

var child = spawn(file, args, {
Expand Down
115 changes: 88 additions & 27 deletions test/simple/test-child-process-spawn-typeerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,91 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var spawn = require('child_process').spawn,
assert = require('assert'),
windows = (process.platform === 'win32'),
cmd = (windows) ? 'dir' : 'ls',
invalidcmd = (windows) ? 'ls' : 'dir',
errors = 0;

try {
// Ensure this throws a TypeError
var child = spawn(invalidcmd, 'this is not an array');

child.on('error', function (err) {
errors++;
});

} catch (e) {
assert.equal(e instanceof TypeError, true);
}

// verify that args argument is optional
assert.doesNotThrow(function() {
spawn(cmd, {});
});

process.on('exit', function() {
assert.equal(errors, 0);
});
var assert = require('assert');
var child_process = require('child_process');
var spawn = child_process.spawn;
var fork = child_process.fork;
var execFile = child_process.execFile;
var cmd = (process.platform === 'win32') ? 'rundll32' : 'ls';
var empty = require('../common').fixturesDir + '/empty.js';

// Argument types for combinatorics
var a=[], o={}, c=(function callback(){}), s='string', u=undefined, n=null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even before my changes, I think this test was a bit overkill. It intentionally ran a bad command and passed in bad arguments to make sure that the TypeError was thrown, and the invalid command error wasn't encountered. A simple assert.throws() like you've done would have been simpler. I'm fine with getting rid of this code.

Would you mind removing the the errors variable and the process.on('exit', ...)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at all, I will do.


// function spawn(file=f [,args=a] [, options=o]) has valid combinations:
// (f)
// (f, a)
// (f, a, o)
// (f, o)
assert.doesNotThrow(function() { spawn(cmd); });
assert.doesNotThrow(function() { spawn(cmd, a); });
assert.doesNotThrow(function() { spawn(cmd, a, o); });
assert.doesNotThrow(function() { spawn(cmd, o); });

// Variants of undefined as explicit 'no argument' at a position
assert.doesNotThrow(function() { execFile(empty, u, o); });
assert.doesNotThrow(function() { execFile(empty, a, u); });
assert.doesNotThrow(function() { execFile(empty, n, o); });
assert.doesNotThrow(function() { execFile(empty, a, n); });

assert.throws(function() { spawn(cmd, s); }, TypeError);
assert.doesNotThrow(function() { spawn(cmd, a, s); }, TypeError);


// verify that execFile has same argument parsing behaviour as spawn
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal opinion is that these tests should go in a different file. I'm not sure that future developers would think to look in test-child-process-spawn-typeerror for execFile() tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to rename it, so that when some future developer changes the signature of execFile they will think, "hey, I should change spawn, too!". OK?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a fine idea to me. I don't have the ultimate say, but I think it makes sense.

//
// function execFile(file=f [,args=a] [, options=o] [, callback=c]) has valid
// combinations:
// (f)
// (f, a)
// (f, a, o)
// (f, a, o, c)
// (f, a, c)
// (f, o)
// (f, o, c)
// (f, c)
assert.doesNotThrow(function() { execFile(cmd); });
assert.doesNotThrow(function() { execFile(cmd, a); });
assert.doesNotThrow(function() { execFile(cmd, a, o); });
assert.doesNotThrow(function() { execFile(cmd, a, o, c); });
assert.doesNotThrow(function() { execFile(cmd, a, c); });
assert.doesNotThrow(function() { execFile(cmd, o); });
assert.doesNotThrow(function() { execFile(cmd, o, c); });
assert.doesNotThrow(function() { execFile(cmd, c); });

// Variants of undefined as explicit 'no argument' at a position
assert.doesNotThrow(function() { execFile(cmd, u, o, c); });
assert.doesNotThrow(function() { execFile(cmd, a, u, c); });
assert.doesNotThrow(function() { execFile(cmd, a, o, u); });
assert.doesNotThrow(function() { execFile(cmd, n, o, c); });
assert.doesNotThrow(function() { execFile(cmd, a, n, c); });
assert.doesNotThrow(function() { execFile(cmd, a, o, n); });

// string is invalid in arg position (this may seem strange, but is
// consistent across node API, cf. `net.createServer('not options', 'not
// callback')`
assert.throws(function() { execFile(cmd, s, o, c); }, TypeError);
assert.doesNotThrow(function() { execFile(cmd, a, s, c); });
assert.doesNotThrow(function() { execFile(cmd, a, o, s); });


// verify that fork has same argument parsing behaviour as spawn
//
// function fork(file=f [,args=a] [, options=o]) has valid combinations:
// (f)
// (f, a)
// (f, a, o)
// (f, o)
assert.doesNotThrow(function() { fork(empty); });
assert.doesNotThrow(function() { fork(empty, a); });
assert.doesNotThrow(function() { fork(empty, a, o); });
assert.doesNotThrow(function() { fork(empty, o); });

// Variants of undefined as explicit 'no argument' at a position
assert.doesNotThrow(function() { execFile(empty, u, o); });
assert.doesNotThrow(function() { execFile(empty, a, u); });
assert.doesNotThrow(function() { execFile(empty, n, o); });
assert.doesNotThrow(function() { execFile(empty, a, n); });

assert.throws(function() { fork(empty, s); }, TypeError);
assert.doesNotThrow(function() { fork(empty, a, s); }, TypeError);