1'use strict'; 2// Flags: --expose-internals 3const common = require('../common'); 4const assert = require('assert'); 5const cp = require('child_process'); 6 7if (process.argv[2] === 'child') { 8 // Since maxBuffer is 0, this should trigger an error. 9 console.log('foo'); 10} else { 11 const internalCp = require('internal/child_process'); 12 13 // Monkey patch ChildProcess#kill() to kill the process and then throw. 14 const kill = internalCp.ChildProcess.prototype.kill; 15 16 internalCp.ChildProcess.prototype.kill = function() { 17 kill.apply(this, arguments); 18 throw new Error('mock error'); 19 }; 20 21 const cmd = `"${process.execPath}" "${__filename}" child`; 22 const options = { maxBuffer: 0, killSignal: 'SIGKILL' }; 23 24 const child = cp.exec(cmd, options, common.mustCall((err, stdout, stderr) => { 25 // Verify that if ChildProcess#kill() throws, the error is reported. 26 assert.strictEqual(err.message, 'mock error', err); 27 assert.strictEqual(stdout, ''); 28 assert.strictEqual(stderr, ''); 29 assert.strictEqual(child.killed, true); 30 })); 31} 32