Skip to content

Commit 41284fb

Browse files
cjihrigMylesBorins
authored andcommitted
test: cover thrown errors from exec() kill
This commit adds code coverage for the scenario where exec() kills a child process, but the call to ChildProcess#kill() throws an exception. PR-URL: #11038 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent 7347860 commit 41284fb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
// Flags: --expose_internals
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const cp = require('child_process');
6+
const internalCp = require('internal/child_process');
7+
8+
if (process.argv[2] === 'child') {
9+
// Keep the process alive and printing to stdout.
10+
setInterval(() => { console.log('foo'); }, 1);
11+
} else {
12+
// Monkey patch ChildProcess#kill() to kill the process and then throw.
13+
const kill = internalCp.ChildProcess.prototype.kill;
14+
15+
internalCp.ChildProcess.prototype.kill = function() {
16+
kill.apply(this, arguments);
17+
throw new Error('mock error');
18+
};
19+
20+
const cmd = `${process.execPath} ${__filename} child`;
21+
const options = { maxBuffer: 0 };
22+
const child = cp.exec(cmd, options, common.mustCall((err, stdout, stderr) => {
23+
// Verify that if ChildProcess#kill() throws, the error is reported.
24+
assert(/^Error: mock error$/.test(err));
25+
assert.strictEqual(stdout, '');
26+
assert.strictEqual(stderr, '');
27+
assert.strictEqual(child.killed, true);
28+
}));
29+
}

0 commit comments

Comments
 (0)