11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst cp = require('child_process');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Verify that a shell is, in fact, executed
71cb0ef41Sopenharmony_ciconst doesNotExist = cp.spawn('does-not-exist', { shell: true });
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciassert.notStrictEqual(doesNotExist.spawnfile, 'does-not-exist');
101cb0ef41Sopenharmony_cidoesNotExist.on('error', common.mustNotCall());
111cb0ef41Sopenharmony_cidoesNotExist.on('exit', common.mustCall((code, signal) => {
121cb0ef41Sopenharmony_ci  assert.strictEqual(signal, null);
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  if (common.isWindows)
151cb0ef41Sopenharmony_ci    assert.strictEqual(code, 1);    // Exit code of cmd.exe
161cb0ef41Sopenharmony_ci  else
171cb0ef41Sopenharmony_ci    assert.strictEqual(code, 127);  // Exit code of /bin/sh
181cb0ef41Sopenharmony_ci}));
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci// Verify that passing arguments works
211cb0ef41Sopenharmony_ciconst echo = cp.spawn('echo', ['foo'], {
221cb0ef41Sopenharmony_ci  encoding: 'utf8',
231cb0ef41Sopenharmony_ci  shell: true
241cb0ef41Sopenharmony_ci});
251cb0ef41Sopenharmony_cilet echoOutput = '';
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciassert.strictEqual(echo.spawnargs[echo.spawnargs.length - 1].replace(/"/g, ''),
281cb0ef41Sopenharmony_ci                   'echo foo');
291cb0ef41Sopenharmony_ciecho.stdout.on('data', (data) => {
301cb0ef41Sopenharmony_ci  echoOutput += data;
311cb0ef41Sopenharmony_ci});
321cb0ef41Sopenharmony_ciecho.on('close', common.mustCall((code, signal) => {
331cb0ef41Sopenharmony_ci  assert.strictEqual(echoOutput.trim(), 'foo');
341cb0ef41Sopenharmony_ci}));
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci// Verify that shell features can be used
371cb0ef41Sopenharmony_ciconst cmd = 'echo bar | cat';
381cb0ef41Sopenharmony_ciconst command = cp.spawn(cmd, {
391cb0ef41Sopenharmony_ci  encoding: 'utf8',
401cb0ef41Sopenharmony_ci  shell: true
411cb0ef41Sopenharmony_ci});
421cb0ef41Sopenharmony_cilet commandOutput = '';
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cicommand.stdout.on('data', (data) => {
451cb0ef41Sopenharmony_ci  commandOutput += data;
461cb0ef41Sopenharmony_ci});
471cb0ef41Sopenharmony_cicommand.on('close', common.mustCall((code, signal) => {
481cb0ef41Sopenharmony_ci  assert.strictEqual(commandOutput.trim(), 'bar');
491cb0ef41Sopenharmony_ci}));
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci// Verify that the environment is properly inherited
521cb0ef41Sopenharmony_ciconst env = cp.spawn(`"${process.execPath}" -pe process.env.BAZ`, {
531cb0ef41Sopenharmony_ci  env: { ...process.env, BAZ: 'buzz' },
541cb0ef41Sopenharmony_ci  encoding: 'utf8',
551cb0ef41Sopenharmony_ci  shell: true
561cb0ef41Sopenharmony_ci});
571cb0ef41Sopenharmony_cilet envOutput = '';
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_cienv.stdout.on('data', (data) => {
601cb0ef41Sopenharmony_ci  envOutput += data;
611cb0ef41Sopenharmony_ci});
621cb0ef41Sopenharmony_cienv.on('close', common.mustCall((code, signal) => {
631cb0ef41Sopenharmony_ci  assert.strictEqual(envOutput.trim(), 'buzz');
641cb0ef41Sopenharmony_ci}));
65