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'); 30const { getSystemErrorName } = require('util'); 31 32const TIMER = 200; 33const SLEEP = 2000; 34 35const execOpts = { encoding: 'utf8', shell: true }; 36 37// Verify that stderr is not accessed when a bad shell is used 38assert.throws( 39 function() { execSync('exit -1', { shell: 'bad_shell' }); }, 40 /spawnSync bad_shell ENOENT/ 41); 42assert.throws( 43 function() { execFileSync('exit -1', { shell: 'bad_shell' }); }, 44 /spawnSync bad_shell ENOENT/ 45); 46 47let caught = false; 48let ret, err; 49const start = Date.now(); 50try { 51 const cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`; 52 ret = execSync(cmd, { timeout: TIMER }); 53} catch (e) { 54 caught = true; 55 assert.strictEqual(getSystemErrorName(e.errno), 'ETIMEDOUT'); 56 err = e; 57} finally { 58 assert.strictEqual(ret, undefined, 59 `should not have a return value, received ${ret}`); 60 assert.ok(caught, 'execSync should throw'); 61 const end = Date.now() - start; 62 assert(end < SLEEP); 63 assert(err.status > 128 || err.signal); 64} 65 66assert.throws(function() { 67 execSync('iamabadcommand'); 68}, /Command failed: iamabadcommand/); 69 70const msg = 'foobar'; 71const msgBuf = Buffer.from(`${msg}\n`); 72 73// console.log ends every line with just '\n', even on Windows. 74 75const cmd = `"${process.execPath}" -e "console.log('${msg}');"`; 76 77{ 78 const ret = execSync(cmd); 79 assert.strictEqual(ret.length, msgBuf.length); 80 assert.deepStrictEqual(ret, msgBuf); 81} 82 83{ 84 const ret = execSync(cmd, { encoding: 'utf8' }); 85 assert.strictEqual(ret, `${msg}\n`); 86} 87 88const args = [ 89 '-e', 90 `console.log("${msg}");`, 91]; 92{ 93 const ret = execFileSync(process.execPath, args); 94 assert.deepStrictEqual(ret, msgBuf); 95} 96 97{ 98 const ret = execFileSync(process.execPath, args, { encoding: 'utf8' }); 99 assert.strictEqual(ret, `${msg}\n`); 100} 101 102// Verify that the cwd option works. 103// See https://github.com/nodejs/node-v0.x-archive/issues/7824. 104{ 105 const cwd = tmpdir.path; 106 const cmd = common.isWindows ? 'echo %cd%' : 'pwd'; 107 const response = execSync(cmd, { cwd }); 108 109 assert.strictEqual(response.toString().trim(), cwd); 110} 111 112// Verify that stderr is not accessed when stdio = 'ignore'. 113// See https://github.com/nodejs/node-v0.x-archive/issues/7966. 114{ 115 assert.throws(function() { 116 execSync('exit -1', { stdio: 'ignore' }); 117 }, /Command failed: exit -1/); 118} 119 120// Verify the execFileSync() behavior when the child exits with a non-zero code. 121{ 122 const args = ['-e', 'process.exit(1)']; 123 const spawnSyncResult = spawnSync(process.execPath, args); 124 const spawnSyncKeys = Object.keys(spawnSyncResult).sort(); 125 assert.deepStrictEqual(spawnSyncKeys, [ 126 'output', 127 'pid', 128 'signal', 129 'status', 130 'stderr', 131 'stdout', 132 ]); 133 134 assert.throws(() => { 135 execFileSync(process.execPath, args); 136 }, (err) => { 137 const msg = `Command failed: ${process.execPath} ${args.join(' ')}`; 138 139 assert(err instanceof Error); 140 assert.strictEqual(err.message, msg); 141 assert.strictEqual(err.status, 1); 142 assert.strictEqual(typeof err.pid, 'number'); 143 spawnSyncKeys 144 .filter((key) => key !== 'pid') 145 .forEach((key) => { 146 assert.deepStrictEqual(err[key], spawnSyncResult[key]); 147 }); 148 return true; 149 }); 150} 151 152// Verify the shell option works properly 153execFileSync(process.execPath, [], execOpts); 154