11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 251cb0ef41Sopenharmony_citmpdir.refresh(); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciconst assert = require('assert'); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciconst { execFileSync, execSync, spawnSync } = require('child_process'); 301cb0ef41Sopenharmony_ciconst { getSystemErrorName } = require('util'); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciconst TIMER = 200; 331cb0ef41Sopenharmony_cilet SLEEP = 2000; 341cb0ef41Sopenharmony_ciif (common.isWindows) { 351cb0ef41Sopenharmony_ci // Some of the windows machines in the CI need more time to launch 361cb0ef41Sopenharmony_ci // and receive output from child processes. 371cb0ef41Sopenharmony_ci // https://github.com/nodejs/build/issues/3014 381cb0ef41Sopenharmony_ci SLEEP = 10000; 391cb0ef41Sopenharmony_ci} 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ciconst execOpts = { encoding: 'utf8', shell: true }; 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci// Verify that stderr is not accessed when a bad shell is used 441cb0ef41Sopenharmony_ciassert.throws( 451cb0ef41Sopenharmony_ci function() { execSync('exit -1', { shell: 'bad_shell' }); }, 461cb0ef41Sopenharmony_ci /spawnSync bad_shell ENOENT/ 471cb0ef41Sopenharmony_ci); 481cb0ef41Sopenharmony_ciassert.throws( 491cb0ef41Sopenharmony_ci function() { execFileSync('exit -1', { shell: 'bad_shell' }); }, 501cb0ef41Sopenharmony_ci /spawnSync bad_shell ENOENT/ 511cb0ef41Sopenharmony_ci); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_cilet caught = false; 541cb0ef41Sopenharmony_cilet ret, err; 551cb0ef41Sopenharmony_ciconst start = Date.now(); 561cb0ef41Sopenharmony_citry { 571cb0ef41Sopenharmony_ci const cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`; 581cb0ef41Sopenharmony_ci ret = execSync(cmd, { timeout: TIMER }); 591cb0ef41Sopenharmony_ci} catch (e) { 601cb0ef41Sopenharmony_ci caught = true; 611cb0ef41Sopenharmony_ci assert.strictEqual(getSystemErrorName(e.errno), 'ETIMEDOUT'); 621cb0ef41Sopenharmony_ci err = e; 631cb0ef41Sopenharmony_ci} finally { 641cb0ef41Sopenharmony_ci assert.strictEqual(ret, undefined, 651cb0ef41Sopenharmony_ci `should not have a return value, received ${ret}`); 661cb0ef41Sopenharmony_ci assert.ok(caught, 'execSync should throw'); 671cb0ef41Sopenharmony_ci const end = Date.now() - start; 681cb0ef41Sopenharmony_ci assert(end < SLEEP); 691cb0ef41Sopenharmony_ci assert(err.status > 128 || err.signal, `status: ${err.status}, signal: ${err.signal}`); 701cb0ef41Sopenharmony_ci} 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ciassert.throws(function() { 731cb0ef41Sopenharmony_ci execSync('iamabadcommand'); 741cb0ef41Sopenharmony_ci}, /Command failed: iamabadcommand/); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ciconst msg = 'foobar'; 771cb0ef41Sopenharmony_ciconst msgBuf = Buffer.from(`${msg}\n`); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci// console.log ends every line with just '\n', even on Windows. 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ciconst cmd = `"${process.execPath}" -e "console.log('${msg}');"`; 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci{ 841cb0ef41Sopenharmony_ci const ret = execSync(cmd); 851cb0ef41Sopenharmony_ci assert.strictEqual(ret.length, msgBuf.length); 861cb0ef41Sopenharmony_ci assert.deepStrictEqual(ret, msgBuf); 871cb0ef41Sopenharmony_ci} 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci{ 901cb0ef41Sopenharmony_ci const ret = execSync(cmd, { encoding: 'utf8' }); 911cb0ef41Sopenharmony_ci assert.strictEqual(ret, `${msg}\n`); 921cb0ef41Sopenharmony_ci} 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ciconst args = [ 951cb0ef41Sopenharmony_ci '-e', 961cb0ef41Sopenharmony_ci `console.log("${msg}");`, 971cb0ef41Sopenharmony_ci]; 981cb0ef41Sopenharmony_ci{ 991cb0ef41Sopenharmony_ci const ret = execFileSync(process.execPath, args); 1001cb0ef41Sopenharmony_ci assert.deepStrictEqual(ret, msgBuf); 1011cb0ef41Sopenharmony_ci} 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci{ 1041cb0ef41Sopenharmony_ci const ret = execFileSync(process.execPath, args, { encoding: 'utf8' }); 1051cb0ef41Sopenharmony_ci assert.strictEqual(ret, `${msg}\n`); 1061cb0ef41Sopenharmony_ci} 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci// Verify that the cwd option works. 1091cb0ef41Sopenharmony_ci// See https://github.com/nodejs/node-v0.x-archive/issues/7824. 1101cb0ef41Sopenharmony_ci{ 1111cb0ef41Sopenharmony_ci const cwd = tmpdir.path; 1121cb0ef41Sopenharmony_ci const cmd = common.isWindows ? 'echo %cd%' : 'pwd'; 1131cb0ef41Sopenharmony_ci const response = execSync(cmd, { cwd }); 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci assert.strictEqual(response.toString().trim(), cwd); 1161cb0ef41Sopenharmony_ci} 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci// Verify that stderr is not accessed when stdio = 'ignore'. 1191cb0ef41Sopenharmony_ci// See https://github.com/nodejs/node-v0.x-archive/issues/7966. 1201cb0ef41Sopenharmony_ci{ 1211cb0ef41Sopenharmony_ci assert.throws(function() { 1221cb0ef41Sopenharmony_ci execSync('exit -1', { stdio: 'ignore' }); 1231cb0ef41Sopenharmony_ci }, /Command failed: exit -1/); 1241cb0ef41Sopenharmony_ci} 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci// Verify the execFileSync() behavior when the child exits with a non-zero code. 1271cb0ef41Sopenharmony_ci{ 1281cb0ef41Sopenharmony_ci const args = ['-e', 'process.exit(1)']; 1291cb0ef41Sopenharmony_ci const spawnSyncResult = spawnSync(process.execPath, args); 1301cb0ef41Sopenharmony_ci const spawnSyncKeys = Object.keys(spawnSyncResult).sort(); 1311cb0ef41Sopenharmony_ci assert.deepStrictEqual(spawnSyncKeys, [ 1321cb0ef41Sopenharmony_ci 'output', 1331cb0ef41Sopenharmony_ci 'pid', 1341cb0ef41Sopenharmony_ci 'signal', 1351cb0ef41Sopenharmony_ci 'status', 1361cb0ef41Sopenharmony_ci 'stderr', 1371cb0ef41Sopenharmony_ci 'stdout', 1381cb0ef41Sopenharmony_ci ]); 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci assert.throws(() => { 1411cb0ef41Sopenharmony_ci execFileSync(process.execPath, args); 1421cb0ef41Sopenharmony_ci }, (err) => { 1431cb0ef41Sopenharmony_ci const msg = `Command failed: ${process.execPath} ${args.join(' ')}`; 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci assert(err instanceof Error); 1461cb0ef41Sopenharmony_ci assert.strictEqual(err.message, msg); 1471cb0ef41Sopenharmony_ci assert.strictEqual(err.status, 1); 1481cb0ef41Sopenharmony_ci assert.strictEqual(typeof err.pid, 'number'); 1491cb0ef41Sopenharmony_ci spawnSyncKeys 1501cb0ef41Sopenharmony_ci .filter((key) => key !== 'pid') 1511cb0ef41Sopenharmony_ci .forEach((key) => { 1521cb0ef41Sopenharmony_ci assert.deepStrictEqual(err[key], spawnSyncResult[key]); 1531cb0ef41Sopenharmony_ci }); 1541cb0ef41Sopenharmony_ci return true; 1551cb0ef41Sopenharmony_ci }); 1561cb0ef41Sopenharmony_ci} 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci// Verify the shell option works properly 1591cb0ef41Sopenharmony_ciexecFileSync(process.execPath, [], execOpts); 160