node.js
This file
Path
test/methods/node.js
307 lines · 16.3 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308import assert from 'node:assert/strict';import path from 'node:path';import process, {version} from 'node:process';import {pathToFileURL} from 'node:url';import test from 'node:test';import getNode from 'get-node';import {assertThrows, assertRejects} from '../helpers/assert.js';import {execa, execaSync, execaNode} from '../../index.js';import {FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';import {identity, fullStdio} from '../helpers/stdio.js';import {foobarString} from '../helpers/input.js';import {getDenoNodePath} from '../helpers/file-path.js';
process.chdir(FIXTURES_DIRECTORY);
const runWithNodeOption = (file, commandArguments, options) => Array.isArray(commandArguments) ? execa(file, commandArguments, {...options, node: true}) : execa(file, {...options, node: true});const runWithNodeOptionSync = (file, commandArguments, options) => Array.isArray(commandArguments) ? execaSync(file, commandArguments, {...options, node: true}) : execaSync(file, {...options, node: true});const runWithIpc = (file, options) => execa('node', [file], {...options, ipc: true});
const testNodeSuccess = async execaMethod => { const {exitCode, stdout} = await execaMethod('noop.js', [foobarString]); assert.equal(exitCode, 0); assert.equal(stdout, foobarString);};
test('execaNode() succeeds', () => testNodeSuccess(execaNode));test('The "node" option succeeds', () => testNodeSuccess(runWithNodeOption));test('The "node" option succeeds - sync', () => testNodeSuccess(runWithNodeOptionSync));
test('execaNode(options) succeeds', async () => { const {stdout} = await execaNode({stripFinalNewline: false})('noop.js', [foobarString]); assert.equal(stdout, `${foobarString}\n`);});
test('execaNode`...` succeeds', async () => { const {stdout} = await execaNode`noop.js ${foobarString}`; assert.equal(stdout, foobarString);});
test('execaNode().pipe(execaNode()) succeeds', async () => { const {stdout} = await execaNode('noop.js').pipe(execaNode('--version')); assert.equal(stdout, version);});
test('execaNode().pipe(execa()) requires using "node"', async () => { await assertRejects(execaNode('noop.js').pipe(execa('--version')));});
test('execaNode().pipe(...) requires using "node"', async () => { await assertRejects(execaNode('noop.js').pipe('--version'));});
test('execaNode().pipe`...` requires using "node"', async () => { await assertRejects(execaNode('noop.js').pipe`--version`);});
test('execaNode() cannot set the "node" option to false', () => { assertThrows(() => { execaNode('empty.js', {node: false}); }, {message: /The "node" option cannot be false/});});
const testDoubleNode = (nodePath, execaMethod) => { assertThrows(() => { execaMethod(nodePath, ['noop.js']); }, {message: /does not need to be "node"/});};
test('Cannot use "node" as binary - execaNode()', () => testDoubleNode('node', execaNode));test('Cannot use "node" as binary - "node" option', () => testDoubleNode('node', runWithNodeOption));test('Cannot use "node" as binary - "node" option sync', () => testDoubleNode('node', runWithNodeOptionSync));test('Cannot use path to "node" as binary - execaNode()', () => testDoubleNode(process.execPath, execaNode));test('Cannot use path to "node" as binary - "node" option', () => testDoubleNode(process.execPath, runWithNodeOption));test('Cannot use path to "node" as binary - "node" option sync', () => testDoubleNode(process.execPath, runWithNodeOptionSync));test('Cannot use deno style nodePath as binary - execaNode()', () => testDoubleNode(getDenoNodePath(), execaNode));test('Cannot use deno style nodePath as binary - "node" option', () => testDoubleNode(getDenoNodePath(), runWithNodeOption));test('Cannot use deno style nodePath as binary - "node" option sync', () => testDoubleNode(getDenoNodePath(), runWithNodeOptionSync));
const getNodePath = async () => { const {path} = await getNode(TEST_NODE_VERSION); return path;};
const TEST_NODE_VERSION = '16.0.0';
// `nodeOptions` defaults to `process.execArgv`, which `node --test` fills with its own flags.// Those flags are unknown to the Node.js version downloaded by `getNode()`, so they are not forwarded.const downloadedNodeOptions = {nodeOptions: []};
const testNodePath = async (execaMethod, mapPath) => { const nodePath = mapPath(await getNodePath()); const {stdout} = await execaMethod('--version', [], {nodePath, ...downloadedNodeOptions}); assert.equal(stdout, `v${TEST_NODE_VERSION}`);};
test('The "nodePath" option can be used - execaNode()', () => testNodePath(execaNode, identity));test('The "nodePath" option can be a file URL - execaNode()', () => testNodePath(execaNode, pathToFileURL));test('The "nodePath" option can be used - "node" option', () => testNodePath(runWithNodeOption, identity));test('The "nodePath" option can be a file URL - "node" option', () => testNodePath(runWithNodeOption, pathToFileURL));test('The "nodePath" option can be used - "node" option sync', () => testNodePath(runWithNodeOptionSync, identity));test('The "nodePath" option can be a file URL - "node" option sync', () => testNodePath(runWithNodeOptionSync, pathToFileURL));
const testNodePathDefault = async execaMethod => { const {stdout} = await execaMethod('--version'); assert.equal(stdout, process.version);};
test('The "nodePath" option defaults to the current Node.js binary - execaNode()', () => testNodePathDefault(execaNode));test('The "nodePath" option defaults to the current Node.js binary - "node" option', () => testNodePathDefault(runWithNodeOption));test('The "nodePath" option defaults to the current Node.js binary - "node" option sync', () => testNodePathDefault(runWithNodeOptionSync));
const testNodePathInvalid = execaMethod => { assertThrows(() => { execaMethod('noop.js', [], {nodePath: true}); }, {message: /The "nodePath" option must be a string or a file URL/});};
test('The "nodePath" option must be a string or URL - execaNode()', () => testNodePathInvalid(execaNode));test('The "nodePath" option must be a string or URL - "node" option', () => testNodePathInvalid(runWithNodeOption));test('The "nodePath" option must be a string or URL - "node" option sync', () => testNodePathInvalid(runWithNodeOptionSync));
const testFormerNodePath = execaMethod => { assertThrows(() => { execaMethod('noop.js', [], {execPath: process.execPath}); }, {message: /The "execPath" option has been removed/});};
test('The "execPath" option cannot be used - execaNode()', () => testFormerNodePath(execaNode));test('The "execPath" option cannot be used - "node" option', () => testFormerNodePath(runWithNodeOption));test('The "execPath" option cannot be used - "node" option sync', () => testFormerNodePath(runWithNodeOptionSync));
const nodePathArguments = ['-p', ['process.env.Path || process.env.PATH']];
const testSubprocessNodePath = async (execaMethod, mapPath) => { const nodePath = mapPath(await getNodePath()); const {stdout} = await execaMethod(...nodePathArguments, {nodePath, ...downloadedNodeOptions}); assert.ok(stdout.includes(TEST_NODE_VERSION));};
test('The "nodePath" option impacts the subprocess - execaNode()', () => testSubprocessNodePath(execaNode, identity));test('The "nodePath" option impacts the subprocess - "node" option', () => testSubprocessNodePath(runWithNodeOption, identity));test('The "nodePath" option impacts the subprocess - "node" option sync', () => testSubprocessNodePath(runWithNodeOptionSync, identity));
const testSubprocessNodePathDefault = async execaMethod => { const {stdout} = await execaMethod(...nodePathArguments); assert.ok(stdout.includes(path.dirname(process.execPath)));};
test('The "nodePath" option defaults to the current Node.js binary in the subprocess - execaNode()', () => testSubprocessNodePathDefault(execaNode));test('The "nodePath" option defaults to the current Node.js binary in the subprocess - "node" option', () => testSubprocessNodePathDefault(runWithNodeOption));test('The "nodePath" option defaults to the current Node.js binary in the subprocess - "node" option sync', () => testSubprocessNodePathDefault(runWithNodeOptionSync));
test('The "nodePath" option requires "node: true" to impact the subprocess', async () => { const nodePath = await getNodePath(); const {stdout} = await execa('node', nodePathArguments.flat(), {nodePath}); assert.ok(!stdout.includes(TEST_NODE_VERSION));});
const testSubprocessNodePathCwd = async execaMethod => { const nodePath = await getNodePath(); const cwd = path.dirname(path.dirname(nodePath)); const relativeExecPath = path.relative(cwd, nodePath); const {stdout} = await execaMethod(...nodePathArguments, {nodePath: relativeExecPath, cwd, ...downloadedNodeOptions}); assert.ok(stdout.includes(TEST_NODE_VERSION));};
test('The "nodePath" option is relative to "cwd" when used in the subprocess - execaNode()', () => testSubprocessNodePathCwd(execaNode));test('The "nodePath" option is relative to "cwd" when used in the subprocess - "node" option', () => testSubprocessNodePathCwd(runWithNodeOption));test('The "nodePath" option is relative to "cwd" when used in the subprocess - "node" option sync', () => testSubprocessNodePathCwd(runWithNodeOptionSync));
const testCwdNodePath = async execaMethod => { const nodePath = await getNodePath(); const cwd = path.dirname(path.dirname(nodePath)); const relativeExecPath = path.relative(cwd, nodePath); const {stdout} = await execaMethod('--version', [], {nodePath: relativeExecPath, cwd, ...downloadedNodeOptions}); assert.equal(stdout, `v${TEST_NODE_VERSION}`);};
test('The "nodePath" option is relative to "cwd" - execaNode()', () => testCwdNodePath(execaNode));test('The "nodePath" option is relative to "cwd" - "node" option', () => testCwdNodePath(runWithNodeOption));test('The "nodePath" option is relative to "cwd" - "node" option sync', () => testCwdNodePath(runWithNodeOptionSync));
const testDenoExecPath = async execaMethod => { const {exitCode, stdout} = await execaMethod('noop.js', [], {nodePath: getDenoNodePath()}); assert.equal(exitCode, 0); assert.equal(stdout, foobarString);};
test('The deno style "nodePath" option can be used - execaNode()', () => testDenoExecPath(execaNode));test('The deno style "nodePath" option can be used - "node" option', () => testDenoExecPath(runWithNodeOption));test('The deno style "nodePath" option can be used - "node" option sync', () => testDenoExecPath(runWithNodeOptionSync));
const testNodeOptions = async execaMethod => { const {stdout} = await execaMethod('empty.js', [], {nodeOptions: ['--version']}); assert.equal(stdout, process.version);};
test('The "nodeOptions" option can be used - execaNode()', () => testNodeOptions(execaNode));test('The "nodeOptions" option can be used - "node" option', () => testNodeOptions(runWithNodeOption));test('The "nodeOptions" option can be used - "node" option sync', () => testNodeOptions(runWithNodeOptionSync));
const spawnNestedExecaNode = (realExecArgv, fakeExecArgv, execaMethod, nodeOptions) => execa( 'node', [...realExecArgv, 'nested-node.js', fakeExecArgv, execaMethod, nodeOptions, 'noop.js', foobarString], {...fullStdio, cwd: FIXTURES_DIRECTORY},);
const testInspectRemoval = async (fakeExecArgv, execaMethod) => { const {stdout, stdio} = await spawnNestedExecaNode([], fakeExecArgv, execaMethod, ''); assert.equal(stdout, foobarString); assert.equal(stdio[3], '');};
test('The "nodeOptions" option removes --inspect without a port when defined by current process - execaNode()', () => testInspectRemoval('--inspect', 'execaNode'));test('The "nodeOptions" option removes --inspect without a port when defined by current process - "node" option', () => testInspectRemoval('--inspect', 'nodeOption'));test('The "nodeOptions" option removes --inspect with a port when defined by current process - execaNode()', () => testInspectRemoval('--inspect=9222', 'execaNode'));test('The "nodeOptions" option removes --inspect with a port when defined by current process - "node" option', () => testInspectRemoval('--inspect=9222', 'nodeOption'));test('The "nodeOptions" option removes --inspect-brk without a port when defined by current process - execaNode()', () => testInspectRemoval('--inspect-brk', 'execaNode'));test('The "nodeOptions" option removes --inspect-brk without a port when defined by current process - "node" option', () => testInspectRemoval('--inspect-brk', 'nodeOption'));test('The "nodeOptions" option removes --inspect-brk with a port when defined by current process - execaNode()', () => testInspectRemoval('--inspect-brk=9223', 'execaNode'));test('The "nodeOptions" option removes --inspect-brk with a port when defined by current process - "node" option', () => testInspectRemoval('--inspect-brk=9223', 'nodeOption'));
const testInspectDifferentPort = async execaMethod => { const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9225'], '', execaMethod, '--inspect=9224'); assert.equal(stdout, foobarString); assert.ok(stdio[3].includes('Debugger listening'));};
test('The "nodeOptions" option allows --inspect with a different port even when defined by current process - execaNode()', () => testInspectDifferentPort('execaNode'));test('The "nodeOptions" option allows --inspect with a different port even when defined by current process - "node" option', () => testInspectDifferentPort('nodeOption'));
const testInspectSamePort = async execaMethod => { const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9226'], '', execaMethod, '--inspect=9226'); assert.equal(stdout, foobarString); assert.ok(stdio[3].includes('address already in use'));};
test('The "nodeOptions" option forbids --inspect with the same port when defined by current process - execaNode()', () => testInspectSamePort('execaNode'));test('The "nodeOptions" option forbids --inspect with the same port when defined by current process - "node" option', () => testInspectSamePort('nodeOption'));
const testIpc = async (execaMethod, options) => { const subprocess = execaMethod('ipc-echo.js', [], options);
await subprocess.sendMessage(foobarString); assert.equal(await subprocess.getOneMessage(), foobarString);
const {stdio} = await subprocess; assert.equal(stdio.length, 4); assert.equal(stdio[3], undefined);};
test('execaNode() adds an ipc channel', () => testIpc(execaNode, {}));test('The "node" option adds an ipc channel', () => testIpc(runWithNodeOption, {}));test('The "ipc" option adds an ipc channel', () => testIpc(runWithIpc, {}));test('The "ipc" option works with "stdio: \'pipe\'"', () => testIpc(runWithIpc, {stdio: 'pipe'}));test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\']"', () => testIpc(runWithIpc, {stdio: ['pipe', 'pipe', 'pipe']}));test('The "ipc" option works with "stdout: \'pipe\'"', () => testIpc(runWithIpc, {stdout: 'pipe'}));
const NO_SEND_MESSAGE = 'sendMessage() can only be used';
test('No ipc channel is added by default', async () => { const {message, stdio} = await assertRejects(execa('node', ['ipc-send.js'])); assert.ok(message.includes(NO_SEND_MESSAGE)); assert.equal(stdio.length, 3);});
const testDisableIpc = async execaMethod => { const {failed, message, stdio} = await execaMethod('ipc-send.js', [], {ipc: false, reject: false}); assert.equal(failed, true); assert.ok(message.includes(NO_SEND_MESSAGE)); assert.equal(stdio.length, 3);};
test('Can disable "ipc" - execaNode()', () => testDisableIpc(execaNode));test('Can disable "ipc" - "node" option', () => testDisableIpc(runWithNodeOption));test('Can disable "ipc" - "node" option sync', () => testDisableIpc(runWithNodeOptionSync));
const NO_IPC_MESSAGE = /The "ipc: true" option cannot be used/;
const testNoIpcSync = node => { assertThrows(() => { execaSync('node', ['ipc-send.js'], {ipc: true, node}); }, {message: NO_IPC_MESSAGE});};
test('Cannot use "ipc: true" with execaSync()', () => testNoIpcSync(undefined));test('Cannot use "ipc: true" with execaSync() - "node: false"', () => testNoIpcSync(false));
test('Cannot use "ipc: true" with execaSync() - "node: true"', () => { assertThrows(() => { execaSync('ipc-send.js', {ipc: true, node: true}); }, {message: NO_IPC_MESSAGE});});
const testNoShell = async execaMethod => { const {failed, message} = await execaMethod('node --version', [], {shell: true, reject: false}); assert.equal(failed, true); assert.ok(message.includes('MODULE_NOT_FOUND'));};
test('Cannot use "shell: true" - execaNode()', () => testNoShell(execaNode));test('Cannot use "shell: true" - "node" option', () => testNoShell(runWithNodeOption));test('Cannot use "shell: true" - "node" option sync', () => testNoShell(runWithNodeOptionSync));