11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst cp = require('child_process');
51cb0ef41Sopenharmony_ciconst fs = require('fs');
61cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// This test is only relevant on Windows.
91cb0ef41Sopenharmony_ciif (!common.isWindows)
101cb0ef41Sopenharmony_ci  common.skip('Windows specific test.');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// This test ensures that child_process.exec can work with any shells.
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_citmpdir.refresh();
151cb0ef41Sopenharmony_ciconst tmpPath = `${tmpdir.path}\\path with spaces`;
161cb0ef41Sopenharmony_cifs.mkdirSync(tmpPath);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst test = (shell) => {
191cb0ef41Sopenharmony_ci  cp.exec('echo foo bar', { shell: shell },
201cb0ef41Sopenharmony_ci          common.mustSucceed((stdout, stderror) => {
211cb0ef41Sopenharmony_ci            assert.ok(!stderror);
221cb0ef41Sopenharmony_ci            assert.ok(stdout.includes('foo') && stdout.includes('bar'));
231cb0ef41Sopenharmony_ci          }));
241cb0ef41Sopenharmony_ci};
251cb0ef41Sopenharmony_ciconst testCopy = (shellName, shellPath) => {
261cb0ef41Sopenharmony_ci  // Symlink the executable to a path with spaces, to ensure there are no issues
271cb0ef41Sopenharmony_ci  // related to quoting of argv0
281cb0ef41Sopenharmony_ci  const copyPath = `${tmpPath}\\${shellName}`;
291cb0ef41Sopenharmony_ci  fs.symlinkSync(shellPath, copyPath);
301cb0ef41Sopenharmony_ci  test(copyPath);
311cb0ef41Sopenharmony_ci};
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst system32 = `${process.env.SystemRoot}\\System32`;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci// Test CMD
361cb0ef41Sopenharmony_citest(true);
371cb0ef41Sopenharmony_citest('cmd');
381cb0ef41Sopenharmony_citestCopy('cmd.exe', `${system32}\\cmd.exe`);
391cb0ef41Sopenharmony_citest('cmd.exe');
401cb0ef41Sopenharmony_citest('CMD');
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// Test PowerShell
431cb0ef41Sopenharmony_citest('powershell');
441cb0ef41Sopenharmony_citestCopy('powershell.exe',
451cb0ef41Sopenharmony_ci         `${system32}\\WindowsPowerShell\\v1.0\\powershell.exe`);
461cb0ef41Sopenharmony_cifs.writeFile(`${tmpPath}\\test file`, 'Test', common.mustSucceed(() => {
471cb0ef41Sopenharmony_ci  cp.exec(`Get-ChildItem "${tmpPath}" | Select-Object -Property Name`,
481cb0ef41Sopenharmony_ci          { shell: 'PowerShell' },
491cb0ef41Sopenharmony_ci          common.mustSucceed((stdout, stderror) => {
501cb0ef41Sopenharmony_ci            assert.ok(!stderror);
511cb0ef41Sopenharmony_ci            assert.ok(stdout.includes(
521cb0ef41Sopenharmony_ci              'test file'));
531cb0ef41Sopenharmony_ci          }));
541cb0ef41Sopenharmony_ci}));
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci// Test Bash (from WSL and Git), if available
571cb0ef41Sopenharmony_cicp.exec('where bash', common.mustCall((error, stdout) => {
581cb0ef41Sopenharmony_ci  if (error) {
591cb0ef41Sopenharmony_ci    return;
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci  const lines = stdout.trim().split(/[\r\n]+/g);
621cb0ef41Sopenharmony_ci  for (let i = 0; i < lines.length; ++i) {
631cb0ef41Sopenharmony_ci    const bashPath = lines[i].trim();
641cb0ef41Sopenharmony_ci    test(bashPath);
651cb0ef41Sopenharmony_ci    testCopy(`bash_${i}.exe`, bashPath);
661cb0ef41Sopenharmony_ci  }
671cb0ef41Sopenharmony_ci}));
68