1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const tmpdir = require('../common/tmpdir'); 25tmpdir.refresh(); 26 27const assert = require('assert'); 28 29const { execFileSync, execSync, spawnSync } = require('child_process'); 30 31const TIMER = 200; 32const SLEEP = 2000; 33 34const execOpts = { encoding: 'utf8', shell: true }; 35 36// Verify that stderr is not accessed when a bad shell is used 37assert.throws( 38 function() { execSync('exit -1', { shell: 'bad_shell' }); }, 39 /spawnSync bad_shell ENOENT/ 40); 41assert.throws( 42 function() { execFileSync('exit -1', { shell: 'bad_shell' }); }, 43 /spawnSync bad_shell ENOENT/ 44); 45 46let caught = false; 47let ret, err; 48const start = Date.now(); 49try { 50 const cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`; 51 ret = execSync(cmd, { timeout: TIMER }); 52} catch (e) { 53 caught = true; 54 assert.strictEqual(e.errno, 'ETIMEDOUT'); 55 err = e; 56} finally { 57 assert.strictEqual(ret, undefined, 58 `should not have a return value, received ${ret}`); 59 assert.ok(caught, 'execSync should throw'); 60 const end = Date.now() - start; 61 assert(end < SLEEP); 62 assert(err.status > 128 || err.signal); 63} 64 65assert.throws(function() { 66 execSync('iamabadcommand'); 67}, /Command failed: iamabadcommand/); 68 69const msg = 'foobar'; 70const msgBuf = Buffer.from(`${msg}\n`); 71 72// console.log ends every line with just '\n', even on Windows. 73 74const cmd = `"${process.execPath}" -e "console.log('${msg}');"`; 75 76{ 77 const ret = execSync(cmd); 78 assert.strictEqual(ret.length, msgBuf.length); 79 assert.deepStrictEqual(ret, msgBuf); 80} 81 82{ 83 const ret = execSync(cmd, { encoding: 'utf8' }); 84 assert.strictEqual(ret, `${msg}\n`); 85} 86 87const args = [ 88 '-e', 89 `console.log("${msg}");` 90]; 91{ 92 const ret = execFileSync(process.execPath, args); 93 assert.deepStrictEqual(ret, msgBuf); 94} 95 96{ 97 const ret = execFileSync(process.execPath, args, { encoding: 'utf8' }); 98 assert.strictEqual(ret, `${msg}\n`); 99} 100 101// Verify that the cwd option works. 102// See https://github.com/nodejs/node-v0.x-archive/issues/7824. 103{ 104 const cwd = tmpdir.path; 105 const cmd = common.isWindows ? 'echo %cd%' : 'pwd'; 106 const response = execSync(cmd, { cwd }); 107 108 assert.strictEqual(response.toString().trim(), cwd); 109} 110 111// Verify that stderr is not accessed when stdio = 'ignore'. 112// See https://github.com/nodejs/node-v0.x-archive/issues/7966. 113{ 114 assert.throws(function() { 115 execSync('exit -1', { stdio: 'ignore' }); 116 }, /Command failed: exit -1/); 117} 118 119// Verify the execFileSync() behavior when the child exits with a non-zero code. 120{ 121 const args = ['-e', 'process.exit(1)']; 122 const spawnSyncResult = spawnSync(process.execPath, args); 123 const spawnSyncKeys = Object.keys(spawnSyncResult).sort(); 124 assert.deepStrictEqual(spawnSyncKeys, [ 125 'output', 126 'pid', 127 'signal', 128 'status', 129 'stderr', 130 'stdout' 131 ]); 132 133 assert.throws(() => { 134 execFileSync(process.execPath, args); 135 }, (err) => { 136 const msg = `Command failed: ${process.execPath} ${args.join(' ')}`; 137 138 assert(err instanceof Error); 139 assert.strictEqual(err.message, msg); 140 assert.strictEqual(err.status, 1); 141 assert.strictEqual(typeof err.pid, 'number'); 142 spawnSyncKeys 143 .filter((key) => key !== 'pid') 144 .forEach((key) => { 145 assert.deepStrictEqual(err[key], spawnSyncResult[key]); 146 }); 147 return true; 148 }); 149} 150 151// Verify the shell option works properly 152execFileSync(process.execPath, [], execOpts); 153