-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
child-process: support any shells on Windows #21943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
b42be28
e9863e6
6f3bccc
6b9b090
1dcea34
c7163f8
a314340
ce7999a
097c905
a4122b7
27ba48f
f93c796
ade01d9
5a2c130
e2192a3
7a10600
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -478,10 +478,10 @@ function normalizeSpawnArguments(file, args, options) { | |
| // '/d /s /c' is used only for cmd.exe. | ||
| if (/^(?:.*\\)?cmd(?:\.exe)?$/i.test(file)) { | ||
| args = ['/d', '/s', '/c', `"${command}"`]; | ||
| options.windowsVerbatimArguments = true; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } else { | ||
| args = ['-c', command]; | ||
| } | ||
| options.windowsVerbatimArguments = true; | ||
| } else { | ||
| if (typeof options.shell === 'string') | ||
| file = options.shell; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,35 +2,63 @@ | |
| 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 }, (error, stdout, stderror) => { | ||
| assert.ok(!error && !stderror); | ||
| assert.ok(stdout.includes('foo') && stdout.includes('bar')); | ||
| }); | ||
| 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'); | ||
|
|
||
| cp.exec('where bash', (error, stdout) => { | ||
| if (error) { | ||
| return; | ||
| } | ||
| test(stdout.split(/[\r\n]+/g)[0].trim()); | ||
| }); | ||
|
|
||
| // Test PowerShell | ||
| test('powershell'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also include a more complex PowerShell test (with quotes and pipes)? Something like cp.exec(`Get-ChildItem "${__dirname}" | Select-Object -Property Name`,
{ shell: 'PowerShell' }, (error, stdout, stderror) => {
assert.ok(!error && !stderror);
assert.ok(stdout.includes('test-child-process-exec-any-shells-windows.js'));
}); |
||
| testCopy('powershell.exe', | ||
| `${system32}\\WindowsPowerShell\\v1.0\\powershell.exe`); | ||
| cp.exec(`Get-ChildItem "${__dirname}" | Select-Object -Property Name`, | ||
| { shell: 'PowerShell' }, (error, stdout, stderror) => { | ||
| { shell: 'PowerShell' }, common.mustCall((error, stdout, stderror) => { | ||
| assert.ok(!error && !stderror); | ||
| assert.ok(stdout.includes( | ||
| 'test-child-process-exec-any-shells-windows.js')); | ||
| }); | ||
| })); | ||
|
|
||
| // 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); | ||
| } | ||
| })); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A late comment, I think that
\bmight be a simpler test for the prefix:also IMHO no need to
?:the groups since we just.testThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P.S. I would call my comment a "nit" (that is a non blocking style change)