Skip to content
Myr

node.js

This file

Path

test/node.js

249 lines · 12.6 KiB · JavaScript

Last changed by

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

History of this file

Take it

The walk stopped after 200 commits: lines older than 7707880 point at it.

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

line 1why, one step back
1import {dirname, relative} from 'node:path';

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 2why, one step back
2import process from 'node:process';

Allow `nodePath` option to be a file URL (#632)ehmickybefore adoption77e1477

line 3why, one step back
3import {pathToFileURL} from 'node:url';

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 4why, one step back
4import test from 'ava';

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 5why, one step back
5import getNode from 'get-node';

Minor tweaksSindre Sorhusbefore adoption244e87a

line 6why, one step back
6import {pEvent} from 'p-event';

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 7–9why, one step back
7import {execa, execaSync, execaNode} from '../index.js';
8import {FIXTURES_DIR} from './helpers/fixtures-dir.js';9import {identity, fullStdio} from './helpers/stdio.js';

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

line 10why, one step back
10import {foobarString} from './helpers/input.js';

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 11why, one step back
11

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 12why, one step back
12process.chdir(FIXTURES_DIR);

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 13why, one step back
13

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

lines 14–19why, one step back
14const 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});

Add `node` option (#804)ehmickybefore adoption663136b

lines 20–23why, one step back
20const runWithIpc = (file, options) => execa('node', [file], {...options, ipc: true});
2122const testNodeSuccess = async (t, execaMethod) => {23 const {exitCode, stdout} = await execaMethod('noop.js', [foobarString]);

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 24why, one step back
24 t.is(exitCode, 0);

Add `node` option (#804)ehmickybefore adoption663136b

lines 25–26why, one step back
25 t.is(stdout, foobarString);
26};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 27why, one step back
27

Add `node` option (#804)ehmickybefore adoption663136b

lines 28–35why, one step back
28test('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/});

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 36–37why, one step back
36});
37

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

line 38why, one step back
38const testDoubleNode = (t, nodePath, execaMethod) => {

Add `node` option (#804)ehmickybefore adoption663136b

line 39why, one step back
39 t.throws(() => {

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

line 40why, one step back
40 execaMethod(nodePath, ['noop.js']);

Add `node` option (#804)ehmickybefore adoption663136b

lines 41–50why, one step back
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);50

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 51–57why, one step back
51const getNodePath = async () => {
52 const {path} = await getNode(TEST_NODE_VERSION);53 return path;54};5556const TEST_NODE_VERSION = '16.0.0';57

Add `node` option (#804)ehmickybefore adoption663136b

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

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

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

Add `node` option (#804)ehmickybefore adoption663136b

line 60why, one step back
60 const {stdout} = await execaMethod('--version', [], {nodePath});

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 61–63why, one step back
61 t.is(stdout, `v${TEST_NODE_VERSION}`);
62};63

Add `node` option (#804)ehmickybefore adoption663136b

lines 64–67why, one step back
64test.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);

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

lines 68–69why, one step back
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);

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 70why, one step back
70

Add `node` option (#804)ehmickybefore adoption663136b

lines 71–72why, one step back
71const testNodePathDefault = async (t, execaMethod) => {
72 const {stdout} = await execaMethod('--version');

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 73why, one step back
73 t.is(stdout, process.version);

Add `node` option (#804)ehmickybefore adoption663136b

line 74why, one step back
74};

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 75why, one step back
75

Add `node` option (#804)ehmickybefore adoption663136b

lines 76–77why, one step back
76test('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);

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

line 78why, one step back
78test('The "nodePath" option defaults to the current Node.js binary - "node" option sync', testNodePathDefault, runWithNodeOptionSync);

Add `node` option (#804)ehmickybefore adoption663136b

lines 79–89why, one step back
79
80const 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);89

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

lines 90–99why, one step back
90const 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

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

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

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 101why, one step back
101

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

lines 102–104why, one step back
102const testChildNodePath = async (t, mapPath) => {
103 const nodePath = mapPath(await getNodePath());104 const {stdout} = await execa(...nodePathArguments, {preferLocal: true, nodePath});

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

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

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

lines 108–109why, one step back
108test.serial('The "nodePath" option impacts the child process', testChildNodePath, identity);
109test.serial('The "nodePath" option can be a file URL', testChildNodePath, pathToFileURL);

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 110why, one step back
110

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

line 111why, one step back
111test('The "nodePath" option defaults to the current Node.js binary in the child process', async t => {

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 112–113why, one step back
112 const {stdout} = await execa(...nodePathArguments, {preferLocal: true});
113 t.true(stdout.includes(dirname(process.execPath)));

Allow `nodePath` option to be a file URL (#632)ehmickybefore adoption77e1477

lines 114–115why, one step back
114});
115

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

lines 116–118why, one step back
116test.serial('The "nodePath" option requires "preferLocal: true" to impact the child process', async t => {
117 const nodePath = await getNodePath();118 const {stdout} = await execa(...nodePathArguments, {nodePath});

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 119why, one step back
119 t.false(stdout.includes(TEST_NODE_VERSION));

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 120–121why, one step back
120});
121

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

lines 122–141why, one step back
122test.serial('The "nodePath" option is relative to "cwd" when used in the child process', async t => {
123 const nodePath = await getNodePath();124 const cwd = dirname(dirname(nodePath));125 const relativeExecPath = relative(cwd, nodePath);126 const {stdout} = await execa(...nodePathArguments, {preferLocal: true, nodePath: relativeExecPath, cwd});127 t.true(stdout.includes(TEST_NODE_VERSION));128});129130const testCwdNodePath = async (t, execaMethod) => {131 const nodePath = await getNodePath();132 const cwd = dirname(dirname(nodePath));133 const relativeExecPath = relative(cwd, nodePath);134 const {stdout} = await execaMethod('--version', [], {nodePath: relativeExecPath, cwd});135 t.is(stdout, `v${TEST_NODE_VERSION}`);136};137138test.serial('The "nodePath" option is relative to "cwd" - execaNode()', testCwdNodePath, execaNode);139test.serial('The "nodePath" option is relative to "cwd" - "node" option', testCwdNodePath, runWithNodeOption);140test.serial('The "nodePath" option is relative to "cwd" - "node" option sync', testCwdNodePath, runWithNodeOptionSync);141

Add `node` option (#804)ehmickybefore adoption663136b

lines 142–143why, one step back
142const testNodeOptions = async (t, execaMethod) => {
143 const {stdout} = await execaMethod('empty.js', [], {nodeOptions: ['--version']});

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 144why, one step back
144 t.is(stdout, process.version);

Add `node` option (#804)ehmickybefore adoption663136b

line 145why, one step back
145};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 146why, one step back
146

Add `node` option (#804)ehmickybefore adoption663136b

lines 147–148why, one step back
147test('The "nodeOptions" option can be used - execaNode()', testNodeOptions, execaNode);
148test('The "nodeOptions" option can be used - "node" option', testNodeOptions, runWithNodeOption);

Merge `execPath` and `nodePath` options (#812)ehmickybefore adoptionfa04e3c

line 149why, one step back
149test('The "nodeOptions" option can be used - "node" option sync', testNodeOptions, runWithNodeOptionSync);

Add `node` option (#804)ehmickybefore adoption663136b

lines 150–151why, one step back
150
151const spawnNestedExecaNode = (realExecArgv, fakeExecArgv, execaMethod, nodeOptions) => execa(

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

line 152why, one step back
152 'node',

Add `node` option (#804)ehmickybefore adoption663136b

line 153why, one step back
153 [...realExecArgv, 'nested-node.js', fakeExecArgv, execaMethod, nodeOptions, 'noop.js', foobarString],

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

line 154why, one step back
154 {...fullStdio, cwd: FIXTURES_DIR},

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 155–156why, one step back
155);
156

Add `node` option (#804)ehmickybefore adoption663136b

lines 157–158why, one step back
157const testInspectRemoval = async (t, fakeExecArgv, execaMethod) => {
158 const {stdout, stdio} = await spawnNestedExecaNode([], fakeExecArgv, execaMethod, '');

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

lines 159–161why, one step back
159 t.is(stdout, foobarString);
160 t.is(stdio[3], '');161};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 162why, one step back
162

Add `node` option (#804)ehmickybefore adoption663136b

lines 163–170why, one step back
163test('The "nodeOptions" option removes --inspect without a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect', 'execaNode');
164test('The "nodeOptions" option removes --inspect without a port when defined by parent process - "node" option', testInspectRemoval, '--inspect', 'nodeOption');165test('The "nodeOptions" option removes --inspect with a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect=9222', 'execaNode');166test('The "nodeOptions" option removes --inspect with a port when defined by parent process - "node" option', testInspectRemoval, '--inspect=9222', 'nodeOption');167test('The "nodeOptions" option removes --inspect-brk without a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect-brk', 'execaNode');168test('The "nodeOptions" option removes --inspect-brk without a port when defined by parent process - "node" option', testInspectRemoval, '--inspect-brk', 'nodeOption');169test('The "nodeOptions" option removes --inspect-brk with a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect-brk=9223', 'execaNode');170test('The "nodeOptions" option removes --inspect-brk with a port when defined by parent process - "node" option', testInspectRemoval, '--inspect-brk=9223', 'nodeOption');

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 171why, one step back
171

Add `node` option (#804)ehmickybefore adoption663136b

lines 172–173why, one step back
172const testInspectDifferentPort = async (t, execaMethod) => {
173 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9225'], '', execaMethod, '--inspect=9224');

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

lines 174–175why, one step back
174 t.is(stdout, foobarString);
175 t.true(stdio[3].includes('Debugger listening'));

Add `node` option (#804)ehmickybefore adoption663136b

line 176why, one step back
176};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 177why, one step back
177

Add `node` option (#804)ehmickybefore adoption663136b

lines 178–182why, one step back
178test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by parent process - execaNode()', testInspectDifferentPort, 'execaNode');
179test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by parent process - "node" option', testInspectDifferentPort, 'nodeOption');180181const testInspectSamePort = async (t, execaMethod) => {182 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9226'], '', execaMethod, '--inspect=9226');

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

lines 183–184why, one step back
183 t.is(stdout, foobarString);
184 t.true(stdio[3].includes('address already in use'));

Add `node` option (#804)ehmickybefore adoption663136b

line 185why, one step back
185};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 186why, one step back
186

Add `node` option (#804)ehmickybefore adoption663136b

lines 187–188why, one step back
187test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by parent process - execaNode()', testInspectSamePort, 'execaNode');
188test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by parent process - "node" option', testInspectSamePort, 'nodeOption');

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 189–190why, one step back
189
190const testIpc = async (t, execaMethod, options) => {

Add `node` option (#804)ehmickybefore adoption663136b

line 191why, one step back
191 const subprocess = execaMethod('send.js', [], options);

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 192why, one step back
192 await pEvent(subprocess, 'message');

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 193–198why, one step back
193 subprocess.send(foobarString);
194 const {stdout, stdio} = await subprocess;195 t.is(stdout, foobarString);196 t.is(stdio.length, 4);197 t.is(stdio[3], undefined);198};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 199why, one step back
199

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 200why, one step back
200test('execaNode() adds an ipc channel', testIpc, execaNode, {});

Add `node` option (#804)ehmickybefore adoption663136b

line 201why, one step back
201test('The "node" option adds an ipc channel', testIpc, runWithNodeOption, {});

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 202–206why, one step back
202test('The "ipc" option adds an ipc channel', testIpc, runWithIpc, {});
203test('The "ipc" option works with "stdio: \'pipe\'"', testIpc, runWithIpc, {stdio: 'pipe'});204test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe']});205test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\', \'ipc\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe', 'ipc']});206test('The "ipc" option works with "stdout: \'pipe\'"', testIpc, runWithIpc, {stdout: 'pipe'});

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 207why, one step back
207

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 208–211why, one step back
208test('No ipc channel is added by default', async t => {
209 const {stdio} = await t.throwsAsync(execa('node', ['send.js']), {message: /process.send is not a function/});210 t.is(stdio.length, 3);211});

Fix processes not exiting on `inputFile` validation (#588)ehmickybefore adoption5b1a1c7

line 212why, one step back
212

Add `node` option (#804)ehmickybefore adoption663136b

lines 213–216why, one step back
213const testDisableIpc = async (t, execaMethod) => {
214 const {failed, message, stdio} = await execaMethod('send.js', [], {ipc: false, reject: false});215 t.true(failed);216 t.true(message.includes('process.send is not a function'));

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 217why, one step back
217 t.is(stdio.length, 3);

Add `node` option (#804)ehmickybefore adoption663136b

line 218why, one step back
218};

Improve some tests (#655)ehmickybefore adoption6cf1c5e

line 219why, one step back
219

Add `node` option (#804)ehmickybefore adoption663136b

lines 220–226why, one step back
220test('Can disable "ipc" - execaNode()', testDisableIpc, execaNode);
221test('Can disable "ipc" - "node" option', testDisableIpc, runWithNodeOption);222test('Can disable "ipc" - "node" option sync', testDisableIpc, runWithNodeOptionSync);223224const NO_IPC_MESSAGE = /The "ipc: true" option cannot be used/;225226const testNoIpcSync = (t, node) => {

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 227why, one step back
227 t.throws(() => {

Add `node` option (#804)ehmickybefore adoption663136b

lines 228–238why, one step back
228 execaSync('node', ['send.js'], {ipc: true, node});
229 }, {message: NO_IPC_MESSAGE});230};231232test('Cannot use "ipc: true" with execaSync()', testNoIpcSync, undefined);233test('Cannot use "ipc: true" with execaSync() - "node: false"', testNoIpcSync, false);234235test('Cannot use "ipc: true" with execaSync() - "node: true"', t => {236 t.throws(() => {237 execaSync('send.js', {ipc: true, node: true});238 }, {message: NO_IPC_MESSAGE});

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 239why, one step back
239});

Add `node` option (#804)ehmickybefore adoption663136b

lines 240–249why, one step back
240
241const testNoShell = async (t, execaMethod) => {242 const {failed, message} = await execaMethod('node --version', [], {shell: true, reject: false});243 t.true(failed);244 t.true(message.includes('MODULE_NOT_FOUND'));245};246247test('Cannot use "shell: true" - execaNode()', testNoShell, execaNode);248test('Cannot use "shell: true" - "node" option', testNoShell, runWithNodeOption);249test('Cannot use "shell: true" - "node" option sync', testNoShell, runWithNodeOptionSync);