diff options
author | killagu <killa123@126.com> | 2018-02-24 22:00:32 +0800 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-05-18 15:20:28 +0200 |
commit | 7d81f5d1435a208f34f0d3ca15034ff8eaf060ce (patch) | |
tree | 264cf850c46a26938129daa3cf2c1dd7548576bc /test | |
parent | 913a78936d660d6918d78a373d127d56cec6696b (diff) | |
download | android-node-v8-7d81f5d1435a208f34f0d3ca15034ff8eaf060ce.tar.gz android-node-v8-7d81f5d1435a208f34f0d3ca15034ff8eaf060ce.tar.bz2 android-node-v8-7d81f5d1435a208f34f0d3ca15034ff8eaf060ce.zip |
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>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-child-process-exec-maxBuffer.js | 22 | ||||
-rw-r--r-- | test/parallel/test-child-process-exec-std-encoding.js | 23 | ||||
-rw-r--r-- | test/parallel/test-stream-readable-setEncoding-null.js | 15 |
3 files changed, 60 insertions, 0 deletions
diff --git a/test/parallel/test-child-process-exec-maxBuffer.js b/test/parallel/test-child-process-exec-maxBuffer.js index 4a65ccf5be..94545e719b 100644 --- a/test/parallel/test-child-process-exec-maxBuffer.js +++ b/test/parallel/test-child-process-exec-maxBuffer.js @@ -41,3 +41,25 @@ const unicode = '中文测试'; // length = 4, byte length = 12 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'); +} diff --git a/test/parallel/test-child-process-exec-std-encoding.js b/test/parallel/test-child-process-exec-std-encoding.js new file mode 100644 index 0000000000..11cb66cf42 --- /dev/null +++ b/test/parallel/test-child-process-exec-std-encoding.js @@ -0,0 +1,23 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const cp = require('child_process'); +const stdoutData = 'foo'; +const stderrData = 'bar'; +const expectedStdout = `${stdoutData}\n`; +const expectedStderr = `${stderrData}\n`; + +if (process.argv[2] === 'child') { + // The following console calls are part of the test. + console.log(stdoutData); + console.error(stderrData); +} else { + const cmd = `"${process.execPath}" "${__filename}" child`; + const child = cp.exec(cmd, common.mustCall((err, stdout, stderr) => { + assert.ifError(err); + assert.strictEqual(stdout, expectedStdout); + assert.strictEqual(stderr, expectedStderr); + })); + child.stdout.setEncoding('utf-8'); + child.stderr.setEncoding('utf-8'); +} diff --git a/test/parallel/test-stream-readable-setEncoding-null.js b/test/parallel/test-stream-readable-setEncoding-null.js new file mode 100644 index 0000000000..b95b26bb79 --- /dev/null +++ b/test/parallel/test-stream-readable-setEncoding-null.js @@ -0,0 +1,15 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); +const { Readable } = require('stream'); + + +{ + const readable = new Readable({ encoding: 'hex' }); + assert.strictEqual(readable._readableState.encoding, 'hex'); + + readable.setEncoding(null); + + assert.strictEqual(readable._readableState.encoding, 'utf8'); +} |