Skip to content

Commit 2fa739e

Browse files
nfischerariporad
authored andcommitted
fix: null is no longer confused for an object
This is an issue in the case of `echo(which('fakecmd'))`. This used to succeed in v0.6 but was broken during the ShellString refactor.
1 parent 7f802ed commit 2fa739e

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

src/common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ function wrap(cmd, fn, options) {
268268
if (options && options.notUnix) {
269269
retValue = fn.apply(this, args);
270270
} else {
271-
if (typeof args[0] === 'object' && args[0].constructor.name === 'Object') {
271+
if (args[0] instanceof Object && args[0].constructor.name === 'Object') {
272272
args = args; // object count as options
273273
} else if (args.length === 0 || typeof args[0] !== 'string' || args[0].length <= 1 || args[0][0] !== '-') {
274274
args.unshift(''); // only add dummy option if '-option' not already present
@@ -284,7 +284,7 @@ function wrap(cmd, fn, options) {
284284
}, []);
285285
// Convert ShellStrings to regular strings
286286
args = args.map(function(arg) {
287-
if (arg.constructor.name === 'String') {
287+
if (arg instanceof Object && arg.constructor.name === 'String') {
288288
return arg.toString();
289289
} else
290290
return arg;

test/echo.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,26 @@ var file = 'tmp/tempscript'+Math.random()+'.js',
2222
script = 'require(\'../../global.js\'); echo("-asdf", "111");'; // test '-' bug (see issue #20)
2323
shell.ShellString(script).to(file);
2424
child.exec(JSON.stringify(process.execPath)+' '+file, function(err, stdout) {
25-
assert.ok(stdout === '-asdf 111\n' || stdout === '-asdf 111\nundefined\n'); // 'undefined' for v0.4
25+
assert.equal(stdout, '-asdf 111\n');
2626

27-
// simple test with silent(true)
28-
shell.mkdir('-p', 'tmp');
29-
var file = 'tmp/tempscript'+Math.random()+'.js',
30-
script = 'require(\'../../global.js\'); config.silent=true; echo(555);';
27+
// using null as an explicit argument doesn't crash the function
28+
file = 'tmp/tempscript'+Math.random()+'.js';
29+
script = 'require(\'../../global.js\'); echo(null);';
3130
shell.ShellString(script).to(file);
32-
child.exec(JSON.stringify(process.execPath)+' '+file, function(err, stdout) {
33-
assert.ok(stdout === '555\n' || stdout === '555\nundefined\n'); // 'undefined' for v0.4
34-
35-
theEnd();
31+
child.exec(JSON.stringify(process.execPath)+' '+file, function(err, stdout, stderr) {
32+
assert.equal(stdout, 'null\n');
33+
assert.equal(stderr, '');
34+
35+
// simple test with silent(true)
36+
shell.mkdir('-p', 'tmp');
37+
var file = 'tmp/tempscript'+Math.random()+'.js',
38+
script = 'require(\'../../global.js\'); config.silent=true; echo(555);';
39+
shell.ShellString(script).to(file);
40+
child.exec(JSON.stringify(process.execPath)+' '+file, function(err, stdout) {
41+
assert.equal(stdout, '555\n');
42+
43+
theEnd();
44+
});
3645
});
3746
});
3847

0 commit comments

Comments
 (0)