Skip to content
Myr

node.js

This file

Path

test/arguments/node.js

258 lines · 13.6 KiB · JavaScript

Last changed by

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

History of this file

Take it

Split files (#837)ehmickybefore adoption672c2d7

lines 1–99why, one step back
1import {dirname, relative} from 'node:path';
2import process from 'node:process';3import {pathToFileURL} from 'node:url';4import test from 'ava';5import getNode from 'get-node';6import {pEvent} from 'p-event';7import {execa, execaSync, execaNode} from '../../index.js';8import {FIXTURES_DIR} from '../helpers/fixtures-dir.js';9import {identity, fullStdio} from '../helpers/stdio.js';10import {foobarString} from '../helpers/input.js';1112process.chdir(FIXTURES_DIR);1314const runWithNodeOption = (file, args, options) => Array.isArray(args)15 ? execa(file, args, {...options, node: true})16 : execa(file, {...options, node: true});17const runWithNodeOptionSync = (file, args, options) => Array.isArray(args)18 ? execaSync(file, args, {...options, node: true})19 : execaSync(file, {...options, node: true});20const runWithIpc = (file, options) => execa('node', [file], {...options, ipc: true});2122const testNodeSuccess = async (t, execaMethod) => {23 const {exitCode, stdout} = await execaMethod('noop.js', [foobarString]);24 t.is(exitCode, 0);25 t.is(stdout, foobarString);26};2728test('execaNode() succeeds', testNodeSuccess, execaNode);29test('The "node" option succeeds', testNodeSuccess, runWithNodeOption);30test('The "node" option succeeds - sync', testNodeSuccess, runWithNodeOptionSync);3132test('execaNode() cannot set the "node" option to false', t => {33 t.throws(() => {34 execaNode('empty.js', {node: false});35 }, {message: /The "node" option cannot be false/});36});3738const testDoubleNode = (t, nodePath, execaMethod) => {39 t.throws(() => {40 execaMethod(nodePath, ['noop.js']);41 }, {message: /does not need to be "node"/});42};4344test('Cannot use "node" as binary - execaNode()', testDoubleNode, 'node', execaNode);45test('Cannot use "node" as binary - "node" option', testDoubleNode, 'node', runWithNodeOption);46test('Cannot use "node" as binary - "node" option sync', testDoubleNode, 'node', runWithNodeOptionSync);47test('Cannot use path to "node" as binary - execaNode()', testDoubleNode, process.execPath, execaNode);48test('Cannot use path to "node" as binary - "node" option', testDoubleNode, process.execPath, runWithNodeOption);49test('Cannot use path to "node" as binary - "node" option sync', testDoubleNode, process.execPath, runWithNodeOptionSync);5051const getNodePath = async () => {52 const {path} = await getNode(TEST_NODE_VERSION);53 return path;54};5556const TEST_NODE_VERSION = '16.0.0';5758const testNodePath = async (t, execaMethod, mapPath) => {59 const nodePath = mapPath(await getNodePath());60 const {stdout} = await execaMethod('--version', [], {nodePath});61 t.is(stdout, `v${TEST_NODE_VERSION}`);62};6364test.serial('The "nodePath" option can be used - execaNode()', testNodePath, execaNode, identity);65test.serial('The "nodePath" option can be a file URL - execaNode()', testNodePath, execaNode, pathToFileURL);66test.serial('The "nodePath" option can be used - "node" option', testNodePath, runWithNodeOption, identity);67test.serial('The "nodePath" option can be a file URL - "node" option', testNodePath, runWithNodeOption, pathToFileURL);68test.serial('The "nodePath" option can be used - "node" option sync', testNodePath, runWithNodeOptionSync, identity);69test.serial('The "nodePath" option can be a file URL - "node" option sync', testNodePath, runWithNodeOptionSync, pathToFileURL);7071const testNodePathDefault = async (t, execaMethod) => {72 const {stdout} = await execaMethod('--version');73 t.is(stdout, process.version);74};7576test('The "nodePath" option defaults to the current Node.js binary - execaNode()', testNodePathDefault, execaNode);77test('The "nodePath" option defaults to the current Node.js binary - "node" option', testNodePathDefault, runWithNodeOption);78test('The "nodePath" option defaults to the current Node.js binary - "node" option sync', testNodePathDefault, runWithNodeOptionSync);7980const testNodePathInvalid = (t, execaMethod) => {81 t.throws(() => {82 execaMethod('noop.js', [], {nodePath: true});83 }, {message: /The "nodePath" option must be a string or a file URL/});84};8586test('The "nodePath" option must be a string or URL - execaNode()', testNodePathInvalid, execaNode);87test('The "nodePath" option must be a string or URL - "node" option', testNodePathInvalid, runWithNodeOption);88test('The "nodePath" option must be a string or URL - "node" option sync', testNodePathInvalid, runWithNodeOptionSync);8990const testFormerNodePath = (t, execaMethod) => {91 t.throws(() => {92 execaMethod('noop.js', [], {execPath: process.execPath});93 }, {message: /The "execPath" option has been removed/});94};9596test('The "execPath" option cannot be used - execaNode()', testFormerNodePath, execaNode);97test('The "execPath" option cannot be used - "node" option', testFormerNodePath, runWithNodeOption);98test('The "execPath" option cannot be used - "node" option sync', testFormerNodePath, runWithNodeOptionSync);99

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 100why, one step back
100const nodePathArguments = ['-p', ['process.env.Path || process.env.PATH']];

Split files (#837)ehmickybefore adoption672c2d7

line 101why, one step back
101

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 102why, one step back
102const testChildNodePath = async (t, execaMethod, mapPath) => {

Split files (#837)ehmickybefore adoption672c2d7

line 103why, one step back
103 const nodePath = mapPath(await getNodePath());

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 104why, one step back
104 const {stdout} = await execaMethod(...nodePathArguments, {nodePath});

Split files (#837)ehmickybefore adoption672c2d7

lines 105–107why, one step back
105 t.true(stdout.includes(TEST_NODE_VERSION));
106};107

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

lines 108–110why, one step back
108test.serial('The "nodePath" option impacts the child process - execaNode()', testChildNodePath, execaNode, identity);
109test.serial('The "nodePath" option impacts the child process - "node" option', testChildNodePath, runWithNodeOption, identity);110test.serial('The "nodePath" option impacts the child process - "node" option sync', testChildNodePath, runWithNodeOptionSync, identity);

Split files (#837)ehmickybefore adoption672c2d7

line 111why, one step back
111

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

lines 112–113why, one step back
112const testChildNodePathDefault = async (t, execaMethod) => {
113 const {stdout} = await execaMethod(...nodePathArguments);

Split files (#837)ehmickybefore adoption672c2d7

line 114why, one step back
114 t.true(stdout.includes(dirname(process.execPath)));

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 115why, one step back
115};

Split files (#837)ehmickybefore adoption672c2d7

line 116why, one step back
116

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

lines 117–121why, one step back
117test('The "nodePath" option defaults to the current Node.js binary in the child process - execaNode()', testChildNodePathDefault, execaNode);
118test('The "nodePath" option defaults to the current Node.js binary in the child process - "node" option', testChildNodePathDefault, runWithNodeOption);119test('The "nodePath" option defaults to the current Node.js binary in the child process - "node" option sync', testChildNodePathDefault, runWithNodeOptionSync);120121test.serial('The "nodePath" option requires "node: true" to impact the child process', async t => {

Split files (#837)ehmickybefore adoption672c2d7

line 122why, one step back
122 const nodePath = await getNodePath();

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 123why, one step back
123 const {stdout} = await execa('node', nodePathArguments.flat(), {nodePath});

Split files (#837)ehmickybefore adoption672c2d7

lines 124–126why, one step back
124 t.false(stdout.includes(TEST_NODE_VERSION));
125});126

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 127why, one step back
127const testChildNodePathCwd = async (t, execaMethod) => {

Split files (#837)ehmickybefore adoption672c2d7

lines 128–130why, one step back
128 const nodePath = await getNodePath();
129 const cwd = dirname(dirname(nodePath));130 const relativeExecPath = relative(cwd, nodePath);

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 131why, one step back
131 const {stdout} = await execaMethod(...nodePathArguments, {nodePath: relativeExecPath, cwd});

Split files (#837)ehmickybefore adoption672c2d7

line 132why, one step back
132 t.true(stdout.includes(TEST_NODE_VERSION));

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

lines 133–137why, one step back
133};
134135test.serial('The "nodePath" option is relative to "cwd" when used in the child process - execaNode()', testChildNodePathCwd, execaNode);136test.serial('The "nodePath" option is relative to "cwd" when used in the child process - "node" option', testChildNodePathCwd, runWithNodeOption);137test.serial('The "nodePath" option is relative to "cwd" when used in the child process - "node" option sync', testChildNodePathCwd, runWithNodeOptionSync);

Split files (#837)ehmickybefore adoption672c2d7

lines 138–258why, one step back
138
139const testCwdNodePath = async (t, execaMethod) => {140 const nodePath = await getNodePath();141 const cwd = dirname(dirname(nodePath));142 const relativeExecPath = relative(cwd, nodePath);143 const {stdout} = await execaMethod('--version', [], {nodePath: relativeExecPath, cwd});144 t.is(stdout, `v${TEST_NODE_VERSION}`);145};146147test.serial('The "nodePath" option is relative to "cwd" - execaNode()', testCwdNodePath, execaNode);148test.serial('The "nodePath" option is relative to "cwd" - "node" option', testCwdNodePath, runWithNodeOption);149test.serial('The "nodePath" option is relative to "cwd" - "node" option sync', testCwdNodePath, runWithNodeOptionSync);150151const testNodeOptions = async (t, execaMethod) => {152 const {stdout} = await execaMethod('empty.js', [], {nodeOptions: ['--version']});153 t.is(stdout, process.version);154};155156test('The "nodeOptions" option can be used - execaNode()', testNodeOptions, execaNode);157test('The "nodeOptions" option can be used - "node" option', testNodeOptions, runWithNodeOption);158test('The "nodeOptions" option can be used - "node" option sync', testNodeOptions, runWithNodeOptionSync);159160const spawnNestedExecaNode = (realExecArgv, fakeExecArgv, execaMethod, nodeOptions) => execa(161 'node',162 [...realExecArgv, 'nested-node.js', fakeExecArgv, execaMethod, nodeOptions, 'noop.js', foobarString],163 {...fullStdio, cwd: FIXTURES_DIR},164);165166const testInspectRemoval = async (t, fakeExecArgv, execaMethod) => {167 const {stdout, stdio} = await spawnNestedExecaNode([], fakeExecArgv, execaMethod, '');168 t.is(stdout, foobarString);169 t.is(stdio[3], '');170};171172test('The "nodeOptions" option removes --inspect without a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect', 'execaNode');173test('The "nodeOptions" option removes --inspect without a port when defined by parent process - "node" option', testInspectRemoval, '--inspect', 'nodeOption');174test('The "nodeOptions" option removes --inspect with a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect=9222', 'execaNode');175test('The "nodeOptions" option removes --inspect with a port when defined by parent process - "node" option', testInspectRemoval, '--inspect=9222', 'nodeOption');176test('The "nodeOptions" option removes --inspect-brk without a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect-brk', 'execaNode');177test('The "nodeOptions" option removes --inspect-brk without a port when defined by parent process - "node" option', testInspectRemoval, '--inspect-brk', 'nodeOption');178test('The "nodeOptions" option removes --inspect-brk with a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect-brk=9223', 'execaNode');179test('The "nodeOptions" option removes --inspect-brk with a port when defined by parent process - "node" option', testInspectRemoval, '--inspect-brk=9223', 'nodeOption');180181const testInspectDifferentPort = async (t, execaMethod) => {182 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9225'], '', execaMethod, '--inspect=9224');183 t.is(stdout, foobarString);184 t.true(stdio[3].includes('Debugger listening'));185};186187test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by parent process - execaNode()', testInspectDifferentPort, 'execaNode');188test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by parent process - "node" option', testInspectDifferentPort, 'nodeOption');189190const testInspectSamePort = async (t, execaMethod) => {191 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9226'], '', execaMethod, '--inspect=9226');192 t.is(stdout, foobarString);193 t.true(stdio[3].includes('address already in use'));194};195196test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by parent process - execaNode()', testInspectSamePort, 'execaNode');197test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by parent process - "node" option', testInspectSamePort, 'nodeOption');198199const testIpc = async (t, execaMethod, options) => {200 const subprocess = execaMethod('send.js', [], options);201 await pEvent(subprocess, 'message');202 subprocess.send(foobarString);203 const {stdout, stdio} = await subprocess;204 t.is(stdout, foobarString);205 t.is(stdio.length, 4);206 t.is(stdio[3], undefined);207};208209test('execaNode() adds an ipc channel', testIpc, execaNode, {});210test('The "node" option adds an ipc channel', testIpc, runWithNodeOption, {});211test('The "ipc" option adds an ipc channel', testIpc, runWithIpc, {});212test('The "ipc" option works with "stdio: \'pipe\'"', testIpc, runWithIpc, {stdio: 'pipe'});213test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe']});214test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\', \'ipc\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe', 'ipc']});215test('The "ipc" option works with "stdout: \'pipe\'"', testIpc, runWithIpc, {stdout: 'pipe'});216217test('No ipc channel is added by default', async t => {218 const {stdio} = await t.throwsAsync(execa('node', ['send.js']), {message: /process.send is not a function/});219 t.is(stdio.length, 3);220});221222const testDisableIpc = async (t, execaMethod) => {223 const {failed, message, stdio} = await execaMethod('send.js', [], {ipc: false, reject: false});224 t.true(failed);225 t.true(message.includes('process.send is not a function'));226 t.is(stdio.length, 3);227};228229test('Can disable "ipc" - execaNode()', testDisableIpc, execaNode);230test('Can disable "ipc" - "node" option', testDisableIpc, runWithNodeOption);231test('Can disable "ipc" - "node" option sync', testDisableIpc, runWithNodeOptionSync);232233const NO_IPC_MESSAGE = /The "ipc: true" option cannot be used/;234235const testNoIpcSync = (t, node) => {236 t.throws(() => {237 execaSync('node', ['send.js'], {ipc: true, node});238 }, {message: NO_IPC_MESSAGE});239};240241test('Cannot use "ipc: true" with execaSync()', testNoIpcSync, undefined);242test('Cannot use "ipc: true" with execaSync() - "node: false"', testNoIpcSync, false);243244test('Cannot use "ipc: true" with execaSync() - "node: true"', t => {245 t.throws(() => {246 execaSync('send.js', {ipc: true, node: true});247 }, {message: NO_IPC_MESSAGE});248});249250const testNoShell = async (t, execaMethod) => {251 const {failed, message} = await execaMethod('node --version', [], {shell: true, reject: false});252 t.true(failed);253 t.true(message.includes('MODULE_NOT_FOUND'));254};255256test('Cannot use "shell: true" - execaNode()', testNoShell, execaNode);257test('Cannot use "shell: true" - "node" option', testNoShell, runWithNodeOption);258test('Cannot use "shell: true" - "node" option sync', testNoShell, runWithNodeOptionSync);