node.js
This file
Path
test/methods/node.js
305 lines · 15.2 KiB · JavaScript
Last changed by
Upgrade xo (#980)ehmickybefore adoptionccfa412
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–8why, one step back1import {once} from 'node:events';2import {dirname, relative} from 'node:path';3import process, {version} from 'node:process';4import {pathToFileURL} from 'node:url';5import test from 'ava';6import getNode from 'get-node';7import {pEvent} from 'p-event';8import {execa, execaSync, execaNode} from '../../index.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
line 9why, one step back9import {FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';Rename files (#977)ehmickybefore adoption4b2e316
lines 10–12why, one step back10import {identity, fullStdio} from '../helpers/stdio.js';11import {foobarString} from '../helpers/input.js';12Upgrade xo (#980)ehmickybefore adoptionccfa412
line 13why, one step back13process.chdir(FIXTURES_DIRECTORY);Rename files (#977)ehmickybefore adoption4b2e316
line 14why, one step back14Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 15–16why, one step back15const runWithNodeOption = (file, commandArguments, options) => Array.isArray(commandArguments)16 ? execa(file, commandArguments, {...options, node: true})Rename files (#977)ehmickybefore adoption4b2e316
line 17why, one step back17 : execa(file, {...options, node: true});Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 18–19why, one step back18const runWithNodeOptionSync = (file, commandArguments, options) => Array.isArray(commandArguments)19 ? execaSync(file, commandArguments, {...options, node: true})Rename files (#977)ehmickybefore adoption4b2e316
lines 20–190why, one step back20 : execaSync(file, {...options, node: true});21const runWithIpc = (file, options) => execa('node', [file], {...options, ipc: true});2223const testNodeSuccess = async (t, execaMethod) => {24 const {exitCode, stdout} = await execaMethod('noop.js', [foobarString]);25 t.is(exitCode, 0);26 t.is(stdout, foobarString);27};2829test('execaNode() succeeds', testNodeSuccess, execaNode);30test('The "node" option succeeds', testNodeSuccess, runWithNodeOption);31test('The "node" option succeeds - sync', testNodeSuccess, runWithNodeOptionSync);3233test('execaNode(options) succeeds', async t => {34 const {stdout} = await execaNode({stripFinalNewline: false})('noop.js', [foobarString]);35 t.is(stdout, `${foobarString}\n`);36});3738test('execaNode`...` succeeds', async t => {39 const {stdout} = await execaNode`noop.js ${foobarString}`;40 t.is(stdout, foobarString);41});4243test('execaNode().pipe(execaNode()) succeeds', async t => {44 const {stdout} = await execaNode('noop.js').pipe(execaNode('--version'));45 t.is(stdout, version);46});4748test('execaNode().pipe(execa()) requires using "node"', async t => {49 await t.throwsAsync(execaNode('noop.js').pipe(execa('--version')));50});5152test('execaNode().pipe(...) requires using "node"', async t => {53 await t.throwsAsync(execaNode('noop.js').pipe('--version'));54});5556test('execaNode().pipe`...` requires using "node"', async t => {57 await t.throwsAsync(execaNode('noop.js').pipe`--version`);58});5960test('execaNode() cannot set the "node" option to false', t => {61 t.throws(() => {62 execaNode('empty.js', {node: false});63 }, {message: /The "node" option cannot be false/});64});6566const testDoubleNode = (t, nodePath, execaMethod) => {67 t.throws(() => {68 execaMethod(nodePath, ['noop.js']);69 }, {message: /does not need to be "node"/});70};7172test('Cannot use "node" as binary - execaNode()', testDoubleNode, 'node', execaNode);73test('Cannot use "node" as binary - "node" option', testDoubleNode, 'node', runWithNodeOption);74test('Cannot use "node" as binary - "node" option sync', testDoubleNode, 'node', runWithNodeOptionSync);75test('Cannot use path to "node" as binary - execaNode()', testDoubleNode, process.execPath, execaNode);76test('Cannot use path to "node" as binary - "node" option', testDoubleNode, process.execPath, runWithNodeOption);77test('Cannot use path to "node" as binary - "node" option sync', testDoubleNode, process.execPath, runWithNodeOptionSync);7879const getNodePath = async () => {80 const {path} = await getNode(TEST_NODE_VERSION);81 return path;82};8384const TEST_NODE_VERSION = '16.0.0';8586const testNodePath = async (t, execaMethod, mapPath) => {87 const nodePath = mapPath(await getNodePath());88 const {stdout} = await execaMethod('--version', [], {nodePath});89 t.is(stdout, `v${TEST_NODE_VERSION}`);90};9192test.serial('The "nodePath" option can be used - execaNode()', testNodePath, execaNode, identity);93test.serial('The "nodePath" option can be a file URL - execaNode()', testNodePath, execaNode, pathToFileURL);94test.serial('The "nodePath" option can be used - "node" option', testNodePath, runWithNodeOption, identity);95test.serial('The "nodePath" option can be a file URL - "node" option', testNodePath, runWithNodeOption, pathToFileURL);96test.serial('The "nodePath" option can be used - "node" option sync', testNodePath, runWithNodeOptionSync, identity);97test.serial('The "nodePath" option can be a file URL - "node" option sync', testNodePath, runWithNodeOptionSync, pathToFileURL);9899const testNodePathDefault = async (t, execaMethod) => {100 const {stdout} = await execaMethod('--version');101 t.is(stdout, process.version);102};103104test('The "nodePath" option defaults to the current Node.js binary - execaNode()', testNodePathDefault, execaNode);105test('The "nodePath" option defaults to the current Node.js binary - "node" option', testNodePathDefault, runWithNodeOption);106test('The "nodePath" option defaults to the current Node.js binary - "node" option sync', testNodePathDefault, runWithNodeOptionSync);107108const testNodePathInvalid = (t, execaMethod) => {109 t.throws(() => {110 execaMethod('noop.js', [], {nodePath: true});111 }, {message: /The "nodePath" option must be a string or a file URL/});112};113114test('The "nodePath" option must be a string or URL - execaNode()', testNodePathInvalid, execaNode);115test('The "nodePath" option must be a string or URL - "node" option', testNodePathInvalid, runWithNodeOption);116test('The "nodePath" option must be a string or URL - "node" option sync', testNodePathInvalid, runWithNodeOptionSync);117118const testFormerNodePath = (t, execaMethod) => {119 t.throws(() => {120 execaMethod('noop.js', [], {execPath: process.execPath});121 }, {message: /The "execPath" option has been removed/});122};123124test('The "execPath" option cannot be used - execaNode()', testFormerNodePath, execaNode);125test('The "execPath" option cannot be used - "node" option', testFormerNodePath, runWithNodeOption);126test('The "execPath" option cannot be used - "node" option sync', testFormerNodePath, runWithNodeOptionSync);127128const nodePathArguments = ['-p', ['process.env.Path || process.env.PATH']];129130const testSubprocessNodePath = async (t, execaMethod, mapPath) => {131 const nodePath = mapPath(await getNodePath());132 const {stdout} = await execaMethod(...nodePathArguments, {nodePath});133 t.true(stdout.includes(TEST_NODE_VERSION));134};135136test.serial('The "nodePath" option impacts the subprocess - execaNode()', testSubprocessNodePath, execaNode, identity);137test.serial('The "nodePath" option impacts the subprocess - "node" option', testSubprocessNodePath, runWithNodeOption, identity);138test.serial('The "nodePath" option impacts the subprocess - "node" option sync', testSubprocessNodePath, runWithNodeOptionSync, identity);139140const testSubprocessNodePathDefault = async (t, execaMethod) => {141 const {stdout} = await execaMethod(...nodePathArguments);142 t.true(stdout.includes(dirname(process.execPath)));143};144145test('The "nodePath" option defaults to the current Node.js binary in the subprocess - execaNode()', testSubprocessNodePathDefault, execaNode);146test('The "nodePath" option defaults to the current Node.js binary in the subprocess - "node" option', testSubprocessNodePathDefault, runWithNodeOption);147test('The "nodePath" option defaults to the current Node.js binary in the subprocess - "node" option sync', testSubprocessNodePathDefault, runWithNodeOptionSync);148149test.serial('The "nodePath" option requires "node: true" to impact the subprocess', async t => {150 const nodePath = await getNodePath();151 const {stdout} = await execa('node', nodePathArguments.flat(), {nodePath});152 t.false(stdout.includes(TEST_NODE_VERSION));153});154155const testSubprocessNodePathCwd = async (t, execaMethod) => {156 const nodePath = await getNodePath();157 const cwd = dirname(dirname(nodePath));158 const relativeExecPath = relative(cwd, nodePath);159 const {stdout} = await execaMethod(...nodePathArguments, {nodePath: relativeExecPath, cwd});160 t.true(stdout.includes(TEST_NODE_VERSION));161};162163test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - execaNode()', testSubprocessNodePathCwd, execaNode);164test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - "node" option', testSubprocessNodePathCwd, runWithNodeOption);165test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - "node" option sync', testSubprocessNodePathCwd, runWithNodeOptionSync);166167const testCwdNodePath = async (t, execaMethod) => {168 const nodePath = await getNodePath();169 const cwd = dirname(dirname(nodePath));170 const relativeExecPath = relative(cwd, nodePath);171 const {stdout} = await execaMethod('--version', [], {nodePath: relativeExecPath, cwd});172 t.is(stdout, `v${TEST_NODE_VERSION}`);173};174175test.serial('The "nodePath" option is relative to "cwd" - execaNode()', testCwdNodePath, execaNode);176test.serial('The "nodePath" option is relative to "cwd" - "node" option', testCwdNodePath, runWithNodeOption);177test.serial('The "nodePath" option is relative to "cwd" - "node" option sync', testCwdNodePath, runWithNodeOptionSync);178179const testNodeOptions = async (t, execaMethod) => {180 const {stdout} = await execaMethod('empty.js', [], {nodeOptions: ['--version']});181 t.is(stdout, process.version);182};183184test('The "nodeOptions" option can be used - execaNode()', testNodeOptions, execaNode);185test('The "nodeOptions" option can be used - "node" option', testNodeOptions, runWithNodeOption);186test('The "nodeOptions" option can be used - "node" option sync', testNodeOptions, runWithNodeOptionSync);187188const spawnNestedExecaNode = (realExecArgv, fakeExecArgv, execaMethod, nodeOptions) => execa(189 'node',190 [...realExecArgv, 'nested-node.js', fakeExecArgv, execaMethod, nodeOptions, 'noop.js', foobarString],Upgrade xo (#980)ehmickybefore adoptionccfa412
line 191why, one step back191 {...fullStdio, cwd: FIXTURES_DIRECTORY},Rename files (#977)ehmickybefore adoption4b2e316
lines 192–305why, one step back192);193194const testInspectRemoval = async (t, fakeExecArgv, execaMethod) => {195 const {stdout, stdio} = await spawnNestedExecaNode([], fakeExecArgv, execaMethod, '');196 t.is(stdout, foobarString);197 t.is(stdio[3], '');198};199200test('The "nodeOptions" option removes --inspect without a port when defined by current process - execaNode()', testInspectRemoval, '--inspect', 'execaNode');201test('The "nodeOptions" option removes --inspect without a port when defined by current process - "node" option', testInspectRemoval, '--inspect', 'nodeOption');202test('The "nodeOptions" option removes --inspect with a port when defined by current process - execaNode()', testInspectRemoval, '--inspect=9222', 'execaNode');203test('The "nodeOptions" option removes --inspect with a port when defined by current process - "node" option', testInspectRemoval, '--inspect=9222', 'nodeOption');204test('The "nodeOptions" option removes --inspect-brk without a port when defined by current process - execaNode()', testInspectRemoval, '--inspect-brk', 'execaNode');205test('The "nodeOptions" option removes --inspect-brk without a port when defined by current process - "node" option', testInspectRemoval, '--inspect-brk', 'nodeOption');206test('The "nodeOptions" option removes --inspect-brk with a port when defined by current process - execaNode()', testInspectRemoval, '--inspect-brk=9223', 'execaNode');207test('The "nodeOptions" option removes --inspect-brk with a port when defined by current process - "node" option', testInspectRemoval, '--inspect-brk=9223', 'nodeOption');208209const testInspectDifferentPort = async (t, execaMethod) => {210 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9225'], '', execaMethod, '--inspect=9224');211 t.is(stdout, foobarString);212 t.true(stdio[3].includes('Debugger listening'));213};214215test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by current process - execaNode()', testInspectDifferentPort, 'execaNode');216test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by current process - "node" option', testInspectDifferentPort, 'nodeOption');217218const testInspectSamePort = async (t, execaMethod) => {219 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9226'], '', execaMethod, '--inspect=9226');220 t.is(stdout, foobarString);221 t.true(stdio[3].includes('address already in use'));222};223224test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by current process - execaNode()', testInspectSamePort, 'execaNode');225test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by current process - "node" option', testInspectSamePort, 'nodeOption');226227const testIpc = async (t, execaMethod, options) => {228 const subprocess = execaMethod('send.js', [], options);229 await pEvent(subprocess, 'message');230 subprocess.send(foobarString);231 const {stdout, stdio} = await subprocess;232 t.is(stdout, foobarString);233 t.is(stdio.length, 4);234 t.is(stdio[3], undefined);235};236237test('execaNode() adds an ipc channel', testIpc, execaNode, {});238test('The "node" option adds an ipc channel', testIpc, runWithNodeOption, {});239test('The "ipc" option adds an ipc channel', testIpc, runWithIpc, {});240test('The "ipc" option works with "stdio: \'pipe\'"', testIpc, runWithIpc, {stdio: 'pipe'});241test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe']});242test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\', \'ipc\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe', 'ipc']});243test('The "ipc" option works with "stdout: \'pipe\'"', testIpc, runWithIpc, {stdout: 'pipe'});244245test('No ipc channel is added by default', async t => {246 const {stdio} = await t.throwsAsync(execa('node', ['send.js']), {message: /process.send is not a function/});247 t.is(stdio.length, 3);248});249250const testDisableIpc = async (t, execaMethod) => {251 const {failed, message, stdio} = await execaMethod('send.js', [], {ipc: false, reject: false});252 t.true(failed);253 t.true(message.includes('process.send is not a function'));254 t.is(stdio.length, 3);255};256257test('Can disable "ipc" - execaNode()', testDisableIpc, execaNode);258test('Can disable "ipc" - "node" option', testDisableIpc, runWithNodeOption);259test('Can disable "ipc" - "node" option sync', testDisableIpc, runWithNodeOptionSync);260261const NO_IPC_MESSAGE = /The "ipc: true" option cannot be used/;262263const testNoIpcSync = (t, node) => {264 t.throws(() => {265 execaSync('node', ['send.js'], {ipc: true, node});266 }, {message: NO_IPC_MESSAGE});267};268269test('Cannot use "ipc: true" with execaSync()', testNoIpcSync, undefined);270test('Cannot use "ipc: true" with execaSync() - "node: false"', testNoIpcSync, false);271272test('Cannot use "ipc: true" with execaSync() - "node: true"', t => {273 t.throws(() => {274 execaSync('send.js', {ipc: true, node: true});275 }, {message: NO_IPC_MESSAGE});276});277278const testNoShell = async (t, execaMethod) => {279 const {failed, message} = await execaMethod('node --version', [], {shell: true, reject: false});280 t.true(failed);281 t.true(message.includes('MODULE_NOT_FOUND'));282};283284test('Cannot use "shell: true" - execaNode()', testNoShell, execaNode);285test('Cannot use "shell: true" - "node" option', testNoShell, runWithNodeOption);286test('Cannot use "shell: true" - "node" option sync', testNoShell, runWithNodeOptionSync);287288test('The "serialization" option defaults to "advanced"', async t => {289 const subprocess = execa('node', ['ipc-echo.js'], {ipc: true});290 subprocess.send([0n]);291 const [message] = await once(subprocess, 'message');292 t.is(message[0], 0n);293 await subprocess;294});295296test('The "serialization" option can be set to "json"', async t => {297 const subprocess = execa('node', ['ipc-echo.js'], {ipc: true, serialization: 'json'});298 t.throws(() => {299 subprocess.send([0n]);300 }, {message: /serialize a BigInt/});301 subprocess.send(0);302 const [message] = await once(subprocess, 'message');303 t.is(message, 0);304 await subprocess;305});