-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
child_process: spawn ignores options in case args is undefined
spawn method ignores 3-d argument 'options' in case the second one 'args' equals to 'undefined'. Fixes: #24912 PR-URL: #24913 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
- Loading branch information
1 parent
3f281b2
commit a193a0f
Showing
4 changed files
with
103 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
// This test confirms that `undefined`, `null`, and `[]` | ||
// can be used as a placeholder for the second argument (`args`) of `spawn()`. | ||
// Previously, there was a bug where using `undefined` for the second argument | ||
// caused the third argument (`options`) to be ignored. | ||
// See https://github.com/nodejs/node/issues/24912. | ||
|
||
const assert = require('assert'); | ||
const { spawn } = require('child_process'); | ||
|
||
const common = require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
|
||
const command = common.isWindows ? 'cd' : 'pwd'; | ||
const options = { cwd: tmpdir.path }; | ||
|
||
if (common.isWindows) { | ||
// This test is not the case for Windows based systems | ||
// unless the `shell` options equals to `true` | ||
|
||
options.shell = true; | ||
} | ||
|
||
const testCases = [ | ||
undefined, | ||
null, | ||
[], | ||
]; | ||
|
||
const expectedResult = tmpdir.path.trim().toLowerCase(); | ||
|
||
(async () => { | ||
const results = await Promise.all( | ||
testCases.map((testCase) => { | ||
return new Promise((resolve) => { | ||
const subprocess = spawn(command, testCase, options); | ||
|
||
let accumulatedData = Buffer.alloc(0); | ||
|
||
subprocess.stdout.on('data', common.mustCall((data) => { | ||
accumulatedData = Buffer.concat([accumulatedData, data]); | ||
})); | ||
|
||
subprocess.stdout.on('end', () => { | ||
resolve(accumulatedData.toString().trim().toLowerCase()); | ||
}); | ||
}); | ||
}) | ||
); | ||
|
||
assert.deepStrictEqual([...new Set(results)], [expectedResult]); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
// This test confirms that `undefined`, `null`, and `[]` can be used | ||
// as a placeholder for the second argument (`args`) of `spawnSync()`. | ||
// Previously, there was a bug where using `undefined` for the second argument | ||
// caused the third argument (`options`) to be ignored. | ||
// See https://github.com/nodejs/node/issues/24912. | ||
|
||
const assert = require('assert'); | ||
const { spawnSync } = require('child_process'); | ||
|
||
const common = require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
|
||
const command = common.isWindows ? 'cd' : 'pwd'; | ||
const options = { cwd: tmpdir.path }; | ||
|
||
if (common.isWindows) { | ||
// This test is not the case for Windows based systems | ||
// unless the `shell` options equals to `true` | ||
|
||
options.shell = true; | ||
} | ||
|
||
const testCases = [ | ||
undefined, | ||
null, | ||
[], | ||
]; | ||
|
||
const expectedResult = tmpdir.path.trim().toLowerCase(); | ||
|
||
const results = testCases.map((testCase) => { | ||
const { stdout, stderr } = spawnSync( | ||
command, | ||
testCase, | ||
options | ||
); | ||
|
||
assert.deepStrictEqual(stderr, Buffer.alloc(0)); | ||
|
||
return stdout.toString().trim().toLowerCase(); | ||
}); | ||
|
||
assert.deepStrictEqual([...new Set(results)], [expectedResult]); |