node.js
This file
Path
test/methods/node.js
304 lines · 15.2 KiB · JavaScript
Last changed by
Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–6why, one step back1import {dirname, relative} from 'node:path';2import process, {version} from 'node:process';3import {pathToFileURL} from 'node:url';4import test from 'ava';5import getNode from 'get-node';6import {execa, execaSync, execaNode} from '../../index.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
line 7why, one step back7import {FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';Rename files (#977)ehmickybefore adoption4b2e316
lines 8–10why, one step back8import {identity, fullStdio} from '../helpers/stdio.js';9import {foobarString} from '../helpers/input.js';10Upgrade xo (#980)ehmickybefore adoptionccfa412
line 11why, one step back11process.chdir(FIXTURES_DIRECTORY);Rename files (#977)ehmickybefore adoption4b2e316
line 12why, one step back12Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 13–14why, one step back13const runWithNodeOption = (file, commandArguments, options) => Array.isArray(commandArguments)14 ? execa(file, commandArguments, {...options, node: true})Rename files (#977)ehmickybefore adoption4b2e316
line 15why, one step back15 : execa(file, {...options, node: true});Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 16–17why, one step back16const runWithNodeOptionSync = (file, commandArguments, options) => Array.isArray(commandArguments)17 ? execaSync(file, commandArguments, {...options, node: true})Rename files (#977)ehmickybefore adoption4b2e316
lines 18–188why, one step back18 : execaSync(file, {...options, node: true});19const runWithIpc = (file, options) => execa('node', [file], {...options, ipc: true});2021const testNodeSuccess = async (t, execaMethod) => {22 const {exitCode, stdout} = await execaMethod('noop.js', [foobarString]);23 t.is(exitCode, 0);24 t.is(stdout, foobarString);25};2627test('execaNode() succeeds', testNodeSuccess, execaNode);28test('The "node" option succeeds', testNodeSuccess, runWithNodeOption);29test('The "node" option succeeds - sync', testNodeSuccess, runWithNodeOptionSync);3031test('execaNode(options) succeeds', async t => {32 const {stdout} = await execaNode({stripFinalNewline: false})('noop.js', [foobarString]);33 t.is(stdout, `${foobarString}\n`);34});3536test('execaNode`...` succeeds', async t => {37 const {stdout} = await execaNode`noop.js ${foobarString}`;38 t.is(stdout, foobarString);39});4041test('execaNode().pipe(execaNode()) succeeds', async t => {42 const {stdout} = await execaNode('noop.js').pipe(execaNode('--version'));43 t.is(stdout, version);44});4546test('execaNode().pipe(execa()) requires using "node"', async t => {47 await t.throwsAsync(execaNode('noop.js').pipe(execa('--version')));48});4950test('execaNode().pipe(...) requires using "node"', async t => {51 await t.throwsAsync(execaNode('noop.js').pipe('--version'));52});5354test('execaNode().pipe`...` requires using "node"', async t => {55 await t.throwsAsync(execaNode('noop.js').pipe`--version`);56});5758test('execaNode() cannot set the "node" option to false', t => {59 t.throws(() => {60 execaNode('empty.js', {node: false});61 }, {message: /The "node" option cannot be false/});62});6364const testDoubleNode = (t, nodePath, execaMethod) => {65 t.throws(() => {66 execaMethod(nodePath, ['noop.js']);67 }, {message: /does not need to be "node"/});68};6970test('Cannot use "node" as binary - execaNode()', testDoubleNode, 'node', execaNode);71test('Cannot use "node" as binary - "node" option', testDoubleNode, 'node', runWithNodeOption);72test('Cannot use "node" as binary - "node" option sync', testDoubleNode, 'node', runWithNodeOptionSync);73test('Cannot use path to "node" as binary - execaNode()', testDoubleNode, process.execPath, execaNode);74test('Cannot use path to "node" as binary - "node" option', testDoubleNode, process.execPath, runWithNodeOption);75test('Cannot use path to "node" as binary - "node" option sync', testDoubleNode, process.execPath, runWithNodeOptionSync);7677const getNodePath = async () => {78 const {path} = await getNode(TEST_NODE_VERSION);79 return path;80};8182const TEST_NODE_VERSION = '16.0.0';8384const testNodePath = async (t, execaMethod, mapPath) => {85 const nodePath = mapPath(await getNodePath());86 const {stdout} = await execaMethod('--version', [], {nodePath});87 t.is(stdout, `v${TEST_NODE_VERSION}`);88};8990test.serial('The "nodePath" option can be used - execaNode()', testNodePath, execaNode, identity);91test.serial('The "nodePath" option can be a file URL - execaNode()', testNodePath, execaNode, pathToFileURL);92test.serial('The "nodePath" option can be used - "node" option', testNodePath, runWithNodeOption, identity);93test.serial('The "nodePath" option can be a file URL - "node" option', testNodePath, runWithNodeOption, pathToFileURL);94test.serial('The "nodePath" option can be used - "node" option sync', testNodePath, runWithNodeOptionSync, identity);95test.serial('The "nodePath" option can be a file URL - "node" option sync', testNodePath, runWithNodeOptionSync, pathToFileURL);9697const testNodePathDefault = async (t, execaMethod) => {98 const {stdout} = await execaMethod('--version');99 t.is(stdout, process.version);100};101102test('The "nodePath" option defaults to the current Node.js binary - execaNode()', testNodePathDefault, execaNode);103test('The "nodePath" option defaults to the current Node.js binary - "node" option', testNodePathDefault, runWithNodeOption);104test('The "nodePath" option defaults to the current Node.js binary - "node" option sync', testNodePathDefault, runWithNodeOptionSync);105106const testNodePathInvalid = (t, execaMethod) => {107 t.throws(() => {108 execaMethod('noop.js', [], {nodePath: true});109 }, {message: /The "nodePath" option must be a string or a file URL/});110};111112test('The "nodePath" option must be a string or URL - execaNode()', testNodePathInvalid, execaNode);113test('The "nodePath" option must be a string or URL - "node" option', testNodePathInvalid, runWithNodeOption);114test('The "nodePath" option must be a string or URL - "node" option sync', testNodePathInvalid, runWithNodeOptionSync);115116const testFormerNodePath = (t, execaMethod) => {117 t.throws(() => {118 execaMethod('noop.js', [], {execPath: process.execPath});119 }, {message: /The "execPath" option has been removed/});120};121122test('The "execPath" option cannot be used - execaNode()', testFormerNodePath, execaNode);123test('The "execPath" option cannot be used - "node" option', testFormerNodePath, runWithNodeOption);124test('The "execPath" option cannot be used - "node" option sync', testFormerNodePath, runWithNodeOptionSync);125126const nodePathArguments = ['-p', ['process.env.Path || process.env.PATH']];127128const testSubprocessNodePath = async (t, execaMethod, mapPath) => {129 const nodePath = mapPath(await getNodePath());130 const {stdout} = await execaMethod(...nodePathArguments, {nodePath});131 t.true(stdout.includes(TEST_NODE_VERSION));132};133134test.serial('The "nodePath" option impacts the subprocess - execaNode()', testSubprocessNodePath, execaNode, identity);135test.serial('The "nodePath" option impacts the subprocess - "node" option', testSubprocessNodePath, runWithNodeOption, identity);136test.serial('The "nodePath" option impacts the subprocess - "node" option sync', testSubprocessNodePath, runWithNodeOptionSync, identity);137138const testSubprocessNodePathDefault = async (t, execaMethod) => {139 const {stdout} = await execaMethod(...nodePathArguments);140 t.true(stdout.includes(dirname(process.execPath)));141};142143test('The "nodePath" option defaults to the current Node.js binary in the subprocess - execaNode()', testSubprocessNodePathDefault, execaNode);144test('The "nodePath" option defaults to the current Node.js binary in the subprocess - "node" option', testSubprocessNodePathDefault, runWithNodeOption);145test('The "nodePath" option defaults to the current Node.js binary in the subprocess - "node" option sync', testSubprocessNodePathDefault, runWithNodeOptionSync);146147test.serial('The "nodePath" option requires "node: true" to impact the subprocess', async t => {148 const nodePath = await getNodePath();149 const {stdout} = await execa('node', nodePathArguments.flat(), {nodePath});150 t.false(stdout.includes(TEST_NODE_VERSION));151});152153const testSubprocessNodePathCwd = async (t, execaMethod) => {154 const nodePath = await getNodePath();155 const cwd = dirname(dirname(nodePath));156 const relativeExecPath = relative(cwd, nodePath);157 const {stdout} = await execaMethod(...nodePathArguments, {nodePath: relativeExecPath, cwd});158 t.true(stdout.includes(TEST_NODE_VERSION));159};160161test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - execaNode()', testSubprocessNodePathCwd, execaNode);162test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - "node" option', testSubprocessNodePathCwd, runWithNodeOption);163test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - "node" option sync', testSubprocessNodePathCwd, runWithNodeOptionSync);164165const testCwdNodePath = async (t, execaMethod) => {166 const nodePath = await getNodePath();167 const cwd = dirname(dirname(nodePath));168 const relativeExecPath = relative(cwd, nodePath);169 const {stdout} = await execaMethod('--version', [], {nodePath: relativeExecPath, cwd});170 t.is(stdout, `v${TEST_NODE_VERSION}`);171};172173test.serial('The "nodePath" option is relative to "cwd" - execaNode()', testCwdNodePath, execaNode);174test.serial('The "nodePath" option is relative to "cwd" - "node" option', testCwdNodePath, runWithNodeOption);175test.serial('The "nodePath" option is relative to "cwd" - "node" option sync', testCwdNodePath, runWithNodeOptionSync);176177const testNodeOptions = async (t, execaMethod) => {178 const {stdout} = await execaMethod('empty.js', [], {nodeOptions: ['--version']});179 t.is(stdout, process.version);180};181182test('The "nodeOptions" option can be used - execaNode()', testNodeOptions, execaNode);183test('The "nodeOptions" option can be used - "node" option', testNodeOptions, runWithNodeOption);184test('The "nodeOptions" option can be used - "node" option sync', testNodeOptions, runWithNodeOptionSync);185186const spawnNestedExecaNode = (realExecArgv, fakeExecArgv, execaMethod, nodeOptions) => execa(187 'node',188 [...realExecArgv, 'nested-node.js', fakeExecArgv, execaMethod, nodeOptions, 'noop.js', foobarString],Upgrade xo (#980)ehmickybefore adoptionccfa412
line 189why, one step back189 {...fullStdio, cwd: FIXTURES_DIRECTORY},Rename files (#977)ehmickybefore adoption4b2e316
lines 190–225why, one step back190);191192const testInspectRemoval = async (t, fakeExecArgv, execaMethod) => {193 const {stdout, stdio} = await spawnNestedExecaNode([], fakeExecArgv, execaMethod, '');194 t.is(stdout, foobarString);195 t.is(stdio[3], '');196};197198test('The "nodeOptions" option removes --inspect without a port when defined by current process - execaNode()', testInspectRemoval, '--inspect', 'execaNode');199test('The "nodeOptions" option removes --inspect without a port when defined by current process - "node" option', testInspectRemoval, '--inspect', 'nodeOption');200test('The "nodeOptions" option removes --inspect with a port when defined by current process - execaNode()', testInspectRemoval, '--inspect=9222', 'execaNode');201test('The "nodeOptions" option removes --inspect with a port when defined by current process - "node" option', testInspectRemoval, '--inspect=9222', 'nodeOption');202test('The "nodeOptions" option removes --inspect-brk without a port when defined by current process - execaNode()', testInspectRemoval, '--inspect-brk', 'execaNode');203test('The "nodeOptions" option removes --inspect-brk without a port when defined by current process - "node" option', testInspectRemoval, '--inspect-brk', 'nodeOption');204test('The "nodeOptions" option removes --inspect-brk with a port when defined by current process - execaNode()', testInspectRemoval, '--inspect-brk=9223', 'execaNode');205test('The "nodeOptions" option removes --inspect-brk with a port when defined by current process - "node" option', testInspectRemoval, '--inspect-brk=9223', 'nodeOption');206207const testInspectDifferentPort = async (t, execaMethod) => {208 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9225'], '', execaMethod, '--inspect=9224');209 t.is(stdout, foobarString);210 t.true(stdio[3].includes('Debugger listening'));211};212213test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by current process - execaNode()', testInspectDifferentPort, 'execaNode');214test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by current process - "node" option', testInspectDifferentPort, 'nodeOption');215216const testInspectSamePort = async (t, execaMethod) => {217 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9226'], '', execaMethod, '--inspect=9226');218 t.is(stdout, foobarString);219 t.true(stdio[3].includes('address already in use'));220};221222test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by current process - execaNode()', testInspectSamePort, 'execaNode');223test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by current process - "node" option', testInspectSamePort, 'nodeOption');224225const testIpc = async (t, execaMethod, options) => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 226–231why, one step back226 const subprocess = execaMethod('ipc-echo.js', [], options);227228 await subprocess.sendMessage(foobarString);229 t.is(await subprocess.getOneMessage(), foobarString);230231 const {stdio} = await subprocess;Rename files (#977)ehmickybefore adoption4b2e316
lines 232–243why, one step back232 t.is(stdio.length, 4);233 t.is(stdio[3], undefined);234};235236test('execaNode() adds an ipc channel', testIpc, execaNode, {});237test('The "node" option adds an ipc channel', testIpc, runWithNodeOption, {});238test('The "ipc" option adds an ipc channel', testIpc, runWithIpc, {});239test('The "ipc" option works with "stdio: \'pipe\'"', testIpc, runWithIpc, {stdio: 'pipe'});240test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe']});241test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\', \'ipc\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe', 'ipc']});242test('The "ipc" option works with "stdout: \'pipe\'"', testIpc, runWithIpc, {stdout: 'pipe'});243Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 244–245why, one step back244const NO_SEND_MESSAGE = 'sendMessage() can only be used';245Rename files (#977)ehmickybefore adoption4b2e316
line 246why, one step back246test('No ipc channel is added by default', async t => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 247–248why, one step back247 const {message, stdio} = await t.throwsAsync(execa('node', ['ipc-send.js']));248 t.true(message.includes(NO_SEND_MESSAGE));Rename files (#977)ehmickybefore adoption4b2e316
lines 249–252why, one step back249 t.is(stdio.length, 3);250});251252const testDisableIpc = async (t, execaMethod) => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 253why, one step back253 const {failed, message, stdio} = await execaMethod('ipc-send.js', [], {ipc: false, reject: false});Rename files (#977)ehmickybefore adoption4b2e316
line 254why, one step back254 t.true(failed);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 255why, one step back255 t.true(message.includes(NO_SEND_MESSAGE));Rename files (#977)ehmickybefore adoption4b2e316
lines 256–266why, one step back256 t.is(stdio.length, 3);257};258259test('Can disable "ipc" - execaNode()', testDisableIpc, execaNode);260test('Can disable "ipc" - "node" option', testDisableIpc, runWithNodeOption);261test('Can disable "ipc" - "node" option sync', testDisableIpc, runWithNodeOptionSync);262263const NO_IPC_MESSAGE = /The "ipc: true" option cannot be used/;264265const testNoIpcSync = (t, node) => {266 t.throws(() => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 267why, one step back267 execaSync('node', ['ipc-send.js'], {ipc: true, node});Rename files (#977)ehmickybefore adoption4b2e316
lines 268–275why, one step back268 }, {message: NO_IPC_MESSAGE});269};270271test('Cannot use "ipc: true" with execaSync()', testNoIpcSync, undefined);272test('Cannot use "ipc: true" with execaSync() - "node: false"', testNoIpcSync, false);273274test('Cannot use "ipc: true" with execaSync() - "node: true"', t => {275 t.throws(() => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 276why, one step back276 execaSync('ipc-send.js', {ipc: true, node: true});Rename files (#977)ehmickybefore adoption4b2e316
lines 277–291why, one step back277 }, {message: NO_IPC_MESSAGE});278});279280const testNoShell = async (t, execaMethod) => {281 const {failed, message} = await execaMethod('node --version', [], {shell: true, reject: false});282 t.true(failed);283 t.true(message.includes('MODULE_NOT_FOUND'));284};285286test('Cannot use "shell: true" - execaNode()', testNoShell, execaNode);287test('Cannot use "shell: true" - "node" option', testNoShell, runWithNodeOption);288test('Cannot use "shell: true" - "node" option sync', testNoShell, runWithNodeOptionSync);289290test('The "serialization" option defaults to "advanced"', async t => {291 const subprocess = execa('node', ['ipc-echo.js'], {ipc: true});Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 292–293why, one step back292 await subprocess.sendMessage([0n]);293 const message = await subprocess.getOneMessage();Rename files (#977)ehmickybefore adoption4b2e316
lines 294–299why, one step back294 t.is(message[0], 0n);295 await subprocess;296});297298test('The "serialization" option can be set to "json"', async t => {299 const subprocess = execa('node', ['ipc-echo.js'], {ipc: true, serialization: 'json'});Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 300–302why, one step back300 await t.throwsAsync(() => subprocess.sendMessage([0n]), {message: /serialize a BigInt/});301 await subprocess.sendMessage(0);302 t.is(await subprocess.getOneMessage(), 0);Rename files (#977)ehmickybefore adoption4b2e316
lines 303–304why, one step back303 await subprocess;304});