node/test/parallel/test-child-process-exec-maxBuffer.js
killagu 7d81f5d143
child_process: fix exec set stdout.setEncoding
cp.exec decide to use `_stdout`(_stdout is string) or
`Buffer.concat(_stdout)`(_stdout is buffer array) by options.encoding.
but std(out|err) encoding can be changed. If encoding is changed to
not null, `_stdout` will become string, and `Buffer.concat(_stdout)`
will throw TypeError. This patch will fix it, use
options.encoding and `std(out|err)._readableState.encoding`.

PR-URL: https://github.com/nodejs/node/pull/18976
Fixes: https://github.com/nodejs/node/issues/18969
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2018-05-18 15:20:28 +02:00

66 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
function checkFactory(streamName) {
return common.mustCall((err) => {
assert.strictEqual(err.message, `${streamName} maxBuffer length exceeded`);
assert(err instanceof RangeError);
assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER');
});
}
{
const cmd = `"${process.execPath}" -e "console.log('hello world');"`;
const options = { maxBuffer: Infinity };
cp.exec(cmd, options, common.mustCall((err, stdout, stderr) => {
assert.ifError(err);
assert.strictEqual(stdout.trim(), 'hello world');
assert.strictEqual(stderr, '');
}));
}
{
const cmd = 'echo "hello world"';
cp.exec(cmd, { maxBuffer: 5 }, checkFactory('stdout'));
}
const unicode = '中文测试'; // length = 4, byte length = 12
{
const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`;
cp.exec(cmd, { maxBuffer: 10 }, checkFactory('stdout'));
}
{
const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;
cp.exec(cmd, { maxBuffer: 10 }, checkFactory('stderr'));
}
{
const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`;
const child = cp.exec(
cmd,
{ encoding: null, maxBuffer: 10 },
checkFactory('stdout'));
child.stdout.setEncoding('utf-8');
}
{
const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;
const child = cp.exec(
cmd,
{ encoding: null, maxBuffer: 10 },
checkFactory('stderr'));
child.stderr.setEncoding('utf-8');
}