1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const cp = require('child_process'); 5 6// Verify that a shell is, in fact, executed 7const doesNotExist = cp.spawn('does-not-exist', { shell: true }); 8 9assert.notStrictEqual(doesNotExist.spawnfile, 'does-not-exist'); 10doesNotExist.on('error', common.mustNotCall()); 11doesNotExist.on('exit', common.mustCall((code, signal) => { 12 assert.strictEqual(signal, null); 13 14 if (common.isWindows) 15 assert.strictEqual(code, 1); // Exit code of cmd.exe 16 else 17 assert.strictEqual(code, 127); // Exit code of /bin/sh 18})); 19 20// Verify that passing arguments works 21const echo = cp.spawn('echo', ['foo'], { 22 encoding: 'utf8', 23 shell: true 24}); 25let echoOutput = ''; 26 27assert.strictEqual(echo.spawnargs[echo.spawnargs.length - 1].replace(/"/g, ''), 28 'echo foo'); 29echo.stdout.on('data', (data) => { 30 echoOutput += data; 31}); 32echo.on('close', common.mustCall((code, signal) => { 33 assert.strictEqual(echoOutput.trim(), 'foo'); 34})); 35 36// Verify that shell features can be used 37const cmd = 'echo bar | cat'; 38const command = cp.spawn(cmd, { 39 encoding: 'utf8', 40 shell: true 41}); 42let commandOutput = ''; 43 44command.stdout.on('data', (data) => { 45 commandOutput += data; 46}); 47command.on('close', common.mustCall((code, signal) => { 48 assert.strictEqual(commandOutput.trim(), 'bar'); 49})); 50 51// Verify that the environment is properly inherited 52const env = cp.spawn(`"${process.execPath}" -pe process.env.BAZ`, { 53 env: { ...process.env, BAZ: 'buzz' }, 54 encoding: 'utf8', 55 shell: true 56}); 57let envOutput = ''; 58 59env.stdout.on('data', (data) => { 60 envOutput += data; 61}); 62env.on('close', common.mustCall((code, signal) => { 63 assert.strictEqual(envOutput.trim(), 'buzz'); 64})); 65