1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const cp = require('child_process'); 5const fs = require('fs'); 6const tmpdir = require('../common/tmpdir'); 7 8// This test is only relevant on Windows. 9if (!common.isWindows) 10 common.skip('Windows specific test.'); 11 12// This test ensures that child_process.exec can work with any shells. 13 14tmpdir.refresh(); 15const tmpPath = `${tmpdir.path}\\path with spaces`; 16fs.mkdirSync(tmpPath); 17 18const test = (shell) => { 19 cp.exec('echo foo bar', { shell: shell }, 20 common.mustSucceed((stdout, stderror) => { 21 assert.ok(!stderror); 22 assert.ok(stdout.includes('foo') && stdout.includes('bar')); 23 })); 24}; 25const testCopy = (shellName, shellPath) => { 26 // Symlink the executable to a path with spaces, to ensure there are no issues 27 // related to quoting of argv0 28 const copyPath = `${tmpPath}\\${shellName}`; 29 fs.symlinkSync(shellPath, copyPath); 30 test(copyPath); 31}; 32 33const system32 = `${process.env.SystemRoot}\\System32`; 34 35// Test CMD 36test(true); 37test('cmd'); 38testCopy('cmd.exe', `${system32}\\cmd.exe`); 39test('cmd.exe'); 40test('CMD'); 41 42// Test PowerShell 43test('powershell'); 44testCopy('powershell.exe', 45 `${system32}\\WindowsPowerShell\\v1.0\\powershell.exe`); 46fs.writeFile(`${tmpPath}\\test file`, 'Test', common.mustSucceed(() => { 47 cp.exec(`Get-ChildItem "${tmpPath}" | Select-Object -Property Name`, 48 { shell: 'PowerShell' }, 49 common.mustSucceed((stdout, stderror) => { 50 assert.ok(!stderror); 51 assert.ok(stdout.includes( 52 'test file')); 53 })); 54})); 55 56// Test Bash (from WSL and Git), if available 57cp.exec('where bash', common.mustCall((error, stdout) => { 58 if (error) { 59 return; 60 } 61 const lines = stdout.trim().split(/[\r\n]+/g); 62 for (let i = 0; i < lines.length; ++i) { 63 const bashPath = lines[i].trim(); 64 test(bashPath); 65 testCopy(`bash_${i}.exe`, bashPath); 66 } 67})); 68