node/test/parallel/test-child-process-execfile.js
jvelezpo 6f8de31cde
test: verify the shell option works properly on execFile
Useful for executing in a shell because it accepts arguments as
an array instead of a string as exec does.
Depending on the circumstances,
that can prove to be useful if the arguments are already prepared.

PR-URL: https://github.com/nodejs/node/pull/18384
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2018-02-04 16:38:34 +01:00

50 lines
1.4 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const execFile = require('child_process').execFile;
const { getSystemErrorName } = require('util');
const fixtures = require('../common/fixtures');
const fixture = fixtures.path('exit.js');
const execOpts = { encoding: 'utf8', shell: true };
{
execFile(
process.execPath,
[fixture, 42],
common.mustCall((e) => {
// Check that arguments are included in message
assert.strictEqual(e.message.trim(),
`Command failed: ${process.execPath} ${fixture} 42`);
assert.strictEqual(e.code, 42);
})
);
}
{
// Verify that negative exit codes can be translated to UV error names.
const errorString = `Error: Command failed: ${process.execPath}`;
const code = -1;
const callback = common.mustCall((err, stdout, stderr) => {
assert.strictEqual(err.toString().trim(), errorString);
assert.strictEqual(err.code, getSystemErrorName(code));
assert.strictEqual(err.killed, true);
assert.strictEqual(err.signal, null);
assert.strictEqual(err.cmd, process.execPath);
assert.strictEqual(stdout.trim(), '');
assert.strictEqual(stderr.trim(), '');
});
const child = execFile(process.execPath, callback);
child.kill();
child.emit('close', code, null);
}
{
// Verify the shell option works properly
execFile(process.execPath, [fixture, 0], execOpts, common.mustCall((err) => {
assert.ifError(err);
}));
}