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'); 24 25const assert = require('assert'); 26 27const spawnSync = require('child_process').spawnSync; 28 29const msgOut = 'this is stdout'; 30const msgErr = 'this is stderr'; 31 32// This is actually not os.EOL? 33const msgOutBuf = Buffer.from(`${msgOut}\n`); 34const msgErrBuf = Buffer.from(`${msgErr}\n`); 35 36const args = [ 37 '-e', 38 `console.log("${msgOut}"); console.error("${msgErr}");`, 39]; 40 41let ret; 42 43 44function checkSpawnSyncRet(ret) { 45 assert.strictEqual(ret.status, 0); 46 assert.strictEqual(ret.error, undefined); 47} 48 49function verifyBufOutput(ret) { 50 checkSpawnSyncRet(ret); 51 assert.deepStrictEqual(ret.stdout, msgOutBuf); 52 assert.deepStrictEqual(ret.stderr, msgErrBuf); 53} 54 55if (process.argv.includes('spawnchild')) { 56 switch (process.argv[3]) { 57 case '1': 58 ret = spawnSync(process.execPath, args, { stdio: 'inherit' }); 59 checkSpawnSyncRet(ret); 60 break; 61 case '2': 62 ret = spawnSync(process.execPath, args, { 63 stdio: ['inherit', 'inherit', 'inherit'] 64 }); 65 checkSpawnSyncRet(ret); 66 break; 67 } 68 process.exit(0); 69 return; 70} 71 72verifyBufOutput(spawnSync(process.execPath, [__filename, 'spawnchild', 1])); 73verifyBufOutput(spawnSync(process.execPath, [__filename, 'spawnchild', 2])); 74 75let options = { 76 input: 1234 77}; 78 79assert.throws( 80 () => spawnSync('cat', [], options), 81 { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' }); 82 83options = { 84 input: 'hello world' 85}; 86 87ret = spawnSync('cat', [], options); 88 89checkSpawnSyncRet(ret); 90assert.strictEqual(ret.stdout.toString('utf8'), options.input); 91assert.strictEqual(ret.stderr.toString('utf8'), ''); 92 93options = { 94 input: Buffer.from('hello world') 95}; 96 97ret = spawnSync('cat', [], options); 98 99checkSpawnSyncRet(ret); 100assert.deepStrictEqual(ret.stdout, options.input); 101assert.deepStrictEqual(ret.stderr, Buffer.from('')); 102 103// common.getArrayBufferViews expects a buffer 104// with length an multiple of 8 105const msgBuf = Buffer.from('hello world'.repeat(8)); 106for (const arrayBufferView of common.getArrayBufferViews(msgBuf)) { 107 options = { 108 input: arrayBufferView 109 }; 110 111 ret = spawnSync('cat', [], options); 112 113 checkSpawnSyncRet(ret); 114 115 assert.deepStrictEqual(ret.stdout, msgBuf); 116 assert.deepStrictEqual(ret.stderr, Buffer.from('')); 117} 118 119verifyBufOutput(spawnSync(process.execPath, args)); 120 121ret = spawnSync(process.execPath, args, { encoding: 'utf8' }); 122 123checkSpawnSyncRet(ret); 124assert.strictEqual(ret.stdout, `${msgOut}\n`); 125assert.strictEqual(ret.stderr, `${msgErr}\n`); 126