|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const cp = require('child_process'); |
| 5 | +const stdoutData = 'foo'; |
| 6 | +const stderrData = 'bar'; |
| 7 | +const expectedStdout = `${stdoutData}\n`; |
| 8 | +const expectedStderr = `${stderrData}\n`; |
| 9 | + |
| 10 | +if (process.argv[2] === 'child') { |
| 11 | + // The following console calls are part of the test. |
| 12 | + console.log(stdoutData); |
| 13 | + console.error(stderrData); |
| 14 | +} else { |
| 15 | + function run(options, callback) { |
| 16 | + const cmd = `${process.execPath} ${__filename} child`; |
| 17 | + |
| 18 | + cp.exec(cmd, options, common.mustCall((err, stdout, stderr) => { |
| 19 | + assert.ifError(err); |
| 20 | + callback(stdout, stderr); |
| 21 | + })); |
| 22 | + } |
| 23 | + |
| 24 | + // Test default encoding, which should be utf8. |
| 25 | + run({}, (stdout, stderr) => { |
| 26 | + assert.strictEqual(typeof stdout, 'string'); |
| 27 | + assert.strictEqual(typeof stderr, 'string'); |
| 28 | + assert.strictEqual(stdout, expectedStdout); |
| 29 | + assert.strictEqual(stderr, expectedStderr); |
| 30 | + }); |
| 31 | + |
| 32 | + // Test explicit utf8 encoding. |
| 33 | + run({ encoding: 'utf8' }, (stdout, stderr) => { |
| 34 | + assert.strictEqual(typeof stdout, 'string'); |
| 35 | + assert.strictEqual(typeof stderr, 'string'); |
| 36 | + assert.strictEqual(stdout, expectedStdout); |
| 37 | + assert.strictEqual(stderr, expectedStderr); |
| 38 | + }); |
| 39 | + |
| 40 | + // Test cases that result in buffer encodings. |
| 41 | + [undefined, null, 'buffer', 'invalid'].forEach((encoding) => { |
| 42 | + run({ encoding }, (stdout, stderr) => { |
| 43 | + assert(stdout instanceof Buffer); |
| 44 | + assert(stdout instanceof Buffer); |
| 45 | + assert.strictEqual(stdout.toString(), expectedStdout); |
| 46 | + assert.strictEqual(stderr.toString(), expectedStderr); |
| 47 | + }); |
| 48 | + }); |
| 49 | +} |
0 commit comments