diff --git a/src/exec-child.js b/src/exec-child.js index eab86ed3..999618cb 100644 --- a/src/exec-child.js +++ b/src/exec-child.js @@ -16,19 +16,49 @@ var pipe = params.pipe; var stdoutFile = params.stdoutFile; var stderrFile = params.stderrFile; +function isMaxBufferError(err) { + var maxBufferErrorPattern = /^.*\bmaxBuffer\b.*exceeded.*$/; + if (err instanceof Error && err.message && + err.message.match(maxBufferErrorPattern)) { + // < v10 + // Error: stdout maxBuffer exceeded + return true; + } else if (err instanceof RangeError && err.message && + err.message.match(maxBufferErrorPattern)) { + // >= v10 + // RangeError [ERR_CHILD_PROCESS_STDIO_MAXBUFFER]: stdout maxBuffer length + // exceeded + // TODO(nfischer): remove when we add v10 CI (Github issue #856). + /* istanbul ignore next */ + return true; + } + return false; +} + +var stdoutStream = fs.createWriteStream(stdoutFile); +var stderrStream = fs.createWriteStream(stderrFile); + +function appendError(message, code) { + stderrStream.write(message); + process.exitCode = code; +} + var c = childProcess.exec(cmd, execOptions, function (err) { if (!err) { process.exitCode = 0; + } else if (isMaxBufferError(err)) { + appendError('maxBuffer exceeded', 1); + } else if (err.code === undefined && err.message) { + /* istanbul ignore next */ + appendError(err.message, 1); } else if (err.code === undefined) { - process.exitCode = 1; + /* istanbul ignore next */ + appendError('Unknown issue', 1); } else { process.exitCode = err.code; } }); -var stdoutStream = fs.createWriteStream(stdoutFile); -var stderrStream = fs.createWriteStream(stderrFile); - c.stdout.pipe(stdoutStream); c.stderr.pipe(stderrStream); c.stdout.pipe(process.stdout); diff --git a/test/exec.js b/test/exec.js index 18c9b1b5..2e09658a 100644 --- a/test/exec.js +++ b/test/exec.js @@ -133,8 +133,12 @@ test('set maxBuffer (very small)', t => { t.falsy(shell.error()); t.is(result.code, 0); t.is(result.stdout, '1234567890' + os.EOL); - shell.exec('echo 1234567890', { maxBuffer: 6 }); + const result2 = shell.exec('echo 1234567890', { maxBuffer: 6 }); t.truthy(shell.error()); + t.is(result2.code, 1); + t.is(result2.stdout, '1234567890' + os.EOL); + const maxBufferErrorPattern = /.*\bmaxBuffer\b.*\bexceeded\b.*/; + t.regex(result2.stderr, maxBufferErrorPattern); }); test('set timeout option', t => {