0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-child-process-exec-abortcontroller-promisified.js
Antoine du Hamel 1fcb128771
test: do not assume process.execPath contains no spaces
We had a bunch of tests that would fail if run from an executable that
contains any char that should be escaped when run from a shell.

PR-URL: https://github.com/nodejs/node/pull/55028
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2024-09-22 13:03:30 +00:00

89 lines
2.2 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const exec = require('child_process').exec;
const { promisify } = require('util');
const execPromisifed = promisify(exec);
const invalidArgTypeError = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
};
const waitCommand = common.isWindows ?
// `"` is forbidden for Windows paths, no need for escaping.
`"${process.execPath}" -e "setInterval(()=>{}, 99)"` :
'sleep 2m';
{
const ac = new AbortController();
const signal = ac.signal;
const promise = execPromisifed(waitCommand, { signal });
assert.rejects(promise, {
name: 'AbortError',
cause: new DOMException('This operation was aborted', 'AbortError'),
}).then(common.mustCall());
ac.abort();
}
{
const err = new Error('boom');
const ac = new AbortController();
const signal = ac.signal;
const promise = execPromisifed(waitCommand, { signal });
assert.rejects(promise, {
name: 'AbortError',
cause: err
}).then(common.mustCall());
ac.abort(err);
}
{
const ac = new AbortController();
const signal = ac.signal;
const promise = execPromisifed(waitCommand, { signal });
assert.rejects(promise, {
name: 'AbortError',
cause: 'boom'
}).then(common.mustCall());
ac.abort('boom');
}
{
assert.throws(() => {
execPromisifed(waitCommand, { signal: {} });
}, invalidArgTypeError);
}
{
function signal() {}
assert.throws(() => {
execPromisifed(waitCommand, { signal });
}, invalidArgTypeError);
}
{
const signal = AbortSignal.abort(); // Abort in advance
const promise = execPromisifed(waitCommand, { signal });
assert.rejects(promise, { name: 'AbortError' })
.then(common.mustCall());
}
{
const err = new Error('boom');
const signal = AbortSignal.abort(err); // Abort in advance
const promise = execPromisifed(waitCommand, { signal });
assert.rejects(promise, { name: 'AbortError', cause: err })
.then(common.mustCall());
}
{
const signal = AbortSignal.abort('boom'); // Abort in advance
const promise = execPromisifed(waitCommand, { signal });
assert.rejects(promise, { name: 'AbortError', cause: 'boom' })
.then(common.mustCall());
}