Skip to content

Commit 1a39bfb

Browse files
cjihrigitaloacasas
authored andcommitted
test: increase coverage for exec() functions
This commit increases code coverage related to the stdout and stderr outputs of the child_process exec() functions. Previously, stdout was completely covered, but stderr was not. PR-URL: #10919 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 4b38744 commit 1a39bfb

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

test/parallel/test-child-process-exec-buffer.js

-24
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)