Skip to content

Commit

Permalink
test: add stdio checks to cp-exec-maxBuffer
Browse files Browse the repository at this point in the history
Expands this test case to check what happens to stdout/stderr when
maxBuffer is exceeded.

Also changes how cases are checked so that assertion stacks are
tracable to their test case, aka 'make it actually debuggable'.

PR-URL: nodejs#24951
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
Fishrock123 committed Jan 23, 2019
1 parent b1a4e41 commit 5f86682
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions test/parallel/test-child-process-exec-maxBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ 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');
});
function runChecks(err, stdio, streamName, expected) {
assert.strictEqual(err.message, `${streamName} maxBuffer length exceeded`);
assert(err instanceof RangeError);
assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER');
assert.deepStrictEqual(stdio[streamName], expected);
}

{
Expand All @@ -25,21 +24,39 @@ function checkFactory(streamName) {
{
const cmd = 'echo "hello world"';

cp.exec(cmd, { maxBuffer: 5 }, checkFactory('stdout'));
cp.exec(
cmd,
{ maxBuffer: 5 },
common.mustCall((err, stdout, stderr) => {
runChecks(err, { stdout, stderr }, 'stdout', '');
})
);
}

const unicode = '中文测试'; // length = 4, byte length = 12

{
const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`;

cp.exec(cmd, { maxBuffer: 10 }, checkFactory('stdout'));
cp.exec(
cmd,
{ maxBuffer: 10 },
common.mustCall((err, stdout, stderr) => {
runChecks(err, { stdout, stderr }, 'stdout', '');
})
);
}

{
const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;

cp.exec(cmd, { maxBuffer: 10 }, checkFactory('stderr'));
cp.exec(
cmd,
{ maxBuffer: 3 },
common.mustCall((err, stdout, stderr) => {
runChecks(err, { stdout, stderr }, 'stderr', '');
})
);
}

{
Expand All @@ -48,7 +65,10 @@ const unicode = '中文测试'; // length = 4, byte length = 12
const child = cp.exec(
cmd,
{ encoding: null, maxBuffer: 10 },
checkFactory('stdout'));
common.mustCall((err, stdout, stderr) => {
runChecks(err, { stdout, stderr }, 'stdout', '');
})
);

child.stdout.setEncoding('utf-8');
}
Expand All @@ -58,8 +78,11 @@ const unicode = '中文测试'; // length = 4, byte length = 12

const child = cp.exec(
cmd,
{ encoding: null, maxBuffer: 10 },
checkFactory('stderr'));
{ encoding: null, maxBuffer: 3 },
common.mustCall((err, stdout, stderr) => {
runChecks(err, { stdout, stderr }, 'stderr', '');
})
);

child.stderr.setEncoding('utf-8');
}

0 comments on commit 5f86682

Please sign in to comment.