-
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: fix switches for alternative shells on Windows
On Windows, normalizeSpawnArguments set "/d /s /c" for any shells. It cause exec and other methods are limited to cmd.exe as a shell. Powershell and git-bash are often used instead of cmd.exe, and they can recieve "-c" switch like unix shells. So normalizeSpawnArguments is changed to set "/d /s /c" for cmd.exe, and "-c" for others. Fixes: #21905 PR-URL: #21943 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: João Reis <reis@janeasystems.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
- Loading branch information
1 parent
3209679
commit af883e1
Showing
4 changed files
with
86 additions
and
11 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
68 changes: 68 additions & 0 deletions
68
test/parallel/test-child-process-exec-any-shells-windows.js
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,68 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const cp = require('child_process'); | ||
const fs = require('fs'); | ||
const tmpdir = require('../common/tmpdir'); | ||
|
||
// This test is only relevant on Windows. | ||
if (!common.isWindows) | ||
common.skip('Windows specific test.'); | ||
|
||
// This test ensures that child_process.exec can work with any shells. | ||
|
||
tmpdir.refresh(); | ||
const tmpPath = `${tmpdir.path}\\path with spaces`; | ||
fs.mkdirSync(tmpPath); | ||
|
||
const test = (shell) => { | ||
cp.exec('echo foo bar', { shell: shell }, | ||
common.mustCall((error, stdout, stderror) => { | ||
assert.ok(!error && !stderror); | ||
assert.ok(stdout.includes('foo') && stdout.includes('bar')); | ||
})); | ||
}; | ||
const testCopy = (shellName, shellPath) => { | ||
// Copy the executable to a path with spaces, to ensure there are no issues | ||
// related to quoting of argv0 | ||
const copyPath = `${tmpPath}\\${shellName}`; | ||
fs.copyFileSync(shellPath, copyPath); | ||
test(copyPath); | ||
}; | ||
|
||
const system32 = `${process.env.SystemRoot}\\System32`; | ||
|
||
// Test CMD | ||
test(true); | ||
test('cmd'); | ||
testCopy('cmd.exe', `${system32}\\cmd.exe`); | ||
test('cmd.exe'); | ||
test('CMD'); | ||
|
||
// Test PowerShell | ||
test('powershell'); | ||
testCopy('powershell.exe', | ||
`${system32}\\WindowsPowerShell\\v1.0\\powershell.exe`); | ||
fs.writeFile(`${tmpPath}\\test file`, 'Test', common.mustCall((err) => { | ||
assert.ifError(err); | ||
cp.exec(`Get-ChildItem "${tmpPath}" | Select-Object -Property Name`, | ||
{ shell: 'PowerShell' }, | ||
common.mustCall((error, stdout, stderror) => { | ||
assert.ok(!error && !stderror); | ||
assert.ok(stdout.includes( | ||
'test file')); | ||
})); | ||
})); | ||
|
||
// Test Bash (from WSL and Git), if available | ||
cp.exec('where bash', common.mustCall((error, stdout) => { | ||
if (error) { | ||
return; | ||
} | ||
const lines = stdout.trim().split(/[\r\n]+/g); | ||
for (let i = 0; i < lines.length; ++i) { | ||
const bashPath = lines[i].trim(); | ||
test(bashPath); | ||
testCopy(`bash_${i}.exe`, bashPath); | ||
} | ||
})); |
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