0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-child-process-promisified.js
Gil Tayar 8d7f07f379
child_process: promisify includes stdio in error
This converts the initial implementation of a promised exec that used
the customPromisifyArgs support in util.promisify with a custom
implementation. This is because exec and execFile, when there is an
error, still supply the stdout and stderr of the process, and yet
the promisified version with customPromisifyArgs does
not supply this ability.

I created a custom implementation and attached it to exec and execFile
using the util.promisify.custom key.

Fixes: https://github.com/nodejs/node/issues/13364
PR-URL: https://github.com/nodejs/node/pull/13388
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-06-09 21:46:09 +02:00

54 lines
1.4 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const child_process = require('child_process');
const { promisify } = require('util');
common.crashOnUnhandledRejection();
const exec = promisify(child_process.exec);
const execFile = promisify(child_process.execFile);
{
exec(`${process.execPath} -p 42`).then(common.mustCall((obj) => {
assert.deepStrictEqual(obj, { stdout: '42\n', stderr: '' });
}));
}
{
execFile(process.execPath, ['-p', '42']).then(common.mustCall((obj) => {
assert.deepStrictEqual(obj, { stdout: '42\n', stderr: '' });
}));
}
{
exec('doesntexist').catch(common.mustCall((err) => {
assert(err.message.includes('doesntexist'));
}));
}
{
execFile('doesntexist', ['-p', '42']).catch(common.mustCall((err) => {
assert(err.message.includes('doesntexist'));
}));
}
const failingCodeWithStdoutErr =
'console.log(42);console.error(43);process.exit(1)';
{
exec(`${process.execPath} -e "${failingCodeWithStdoutErr}"`)
.catch(common.mustCall((err) => {
assert.strictEqual(err.code, 1);
assert.strictEqual(err.stdout, '42\n');
assert.strictEqual(err.stderr, '43\n');
}));
}
{
execFile(process.execPath, ['-e', failingCodeWithStdoutErr])
.catch(common.mustCall((err) => {
assert.strictEqual(err.code, 1);
assert.strictEqual(err.stdout, '42\n');
assert.strictEqual(err.stderr, '43\n');
}));
}