node.js
This file
Path
test/methods/node.js
302 lines · 15.6 KiB · JavaScript
Last changed by
Fix deno node:process execPath compatibility (#1160)CJbefore adoptionf3a1bf3
History of this fileTake it
Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0
line 1why, one step back1import path from 'node:path';Rename files (#977)ehmickybefore adoption4b2e316
lines 2–6why, one step back2import 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–9why, one step back8import {identity, fullStdio} from '../helpers/stdio.js';9import {foobarString} from '../helpers/input.js';Fix deno node:process execPath compatibility (#1160)CJbefore adoptionf3a1bf3
line 10why, one step back10import {getDenoNodePath} from '../helpers/file-path.js';Rename files (#977)ehmickybefore adoption4b2e316
line 11why, one step back11Upgrade xo (#980)ehmickybefore adoptionccfa412
line 12why, one step back12process.chdir(FIXTURES_DIRECTORY);Rename files (#977)ehmickybefore adoption4b2e316
line 13why, one step back13Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 14–15why, one step back14const runWithNodeOption = (file, commandArguments, options) => Array.isArray(commandArguments)15 ? execa(file, commandArguments, {...options, node: true})Rename files (#977)ehmickybefore adoption4b2e316
line 16why, one step back16 : execa(file, {...options, node: true});Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 17–18why, one step back17const runWithNodeOptionSync = (file, commandArguments, options) => Array.isArray(commandArguments)18 ? execaSync(file, commandArguments, {...options, node: true})Rename files (#977)ehmickybefore adoption4b2e316
lines 19–76why, one step back19 : 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(options) succeeds', async t => {33 const {stdout} = await execaNode({stripFinalNewline: false})('noop.js', [foobarString]);34 t.is(stdout, `${foobarString}\n`);35});3637test('execaNode`...` succeeds', async t => {38 const {stdout} = await execaNode`noop.js ${foobarString}`;39 t.is(stdout, foobarString);40});4142test('execaNode().pipe(execaNode()) succeeds', async t => {43 const {stdout} = await execaNode('noop.js').pipe(execaNode('--version'));44 t.is(stdout, version);45});4647test('execaNode().pipe(execa()) requires using "node"', async t => {48 await t.throwsAsync(execaNode('noop.js').pipe(execa('--version')));49});5051test('execaNode().pipe(...) requires using "node"', async t => {52 await t.throwsAsync(execaNode('noop.js').pipe('--version'));53});5455test('execaNode().pipe`...` requires using "node"', async t => {56 await t.throwsAsync(execaNode('noop.js').pipe`--version`);57});5859test('execaNode() cannot set the "node" option to false', t => {60 t.throws(() => {61 execaNode('empty.js', {node: false});62 }, {message: /The "node" option cannot be false/});63});6465const testDoubleNode = (t, nodePath, execaMethod) => {66 t.throws(() => {67 execaMethod(nodePath, ['noop.js']);68 }, {message: /does not need to be "node"/});69};7071test('Cannot use "node" as binary - execaNode()', testDoubleNode, 'node', execaNode);72test('Cannot use "node" as binary - "node" option', testDoubleNode, 'node', runWithNodeOption);73test('Cannot use "node" as binary - "node" option sync', testDoubleNode, 'node', runWithNodeOptionSync);74test('Cannot use path to "node" as binary - execaNode()', testDoubleNode, process.execPath, execaNode);75test('Cannot use path to "node" as binary - "node" option', testDoubleNode, process.execPath, runWithNodeOption);76test('Cannot use path to "node" as binary - "node" option sync', testDoubleNode, process.execPath, runWithNodeOptionSync);Fix deno node:process execPath compatibility (#1160)CJbefore adoptionf3a1bf3
lines 77–79why, one step back77test('Cannot use deno style nodePath as binary - execaNode()', testDoubleNode, getDenoNodePath(), execaNode);78test('Cannot use deno style nodePath as binary - "node" option', testDoubleNode, getDenoNodePath(), runWithNodeOption);79test('Cannot use deno style nodePath as binary - "node" option sync', testDoubleNode, getDenoNodePath(), runWithNodeOptionSync);Rename files (#977)ehmickybefore adoption4b2e316
lines 80–143why, one step back8081const getNodePath = async () => {82 const {path} = await getNode(TEST_NODE_VERSION);83 return path;84};8586const TEST_NODE_VERSION = '16.0.0';8788const testNodePath = async (t, execaMethod, mapPath) => {89 const nodePath = mapPath(await getNodePath());90 const {stdout} = await execaMethod('--version', [], {nodePath});91 t.is(stdout, `v${TEST_NODE_VERSION}`);92};9394test.serial('The "nodePath" option can be used - execaNode()', testNodePath, execaNode, identity);95test.serial('The "nodePath" option can be a file URL - execaNode()', testNodePath, execaNode, pathToFileURL);96test.serial('The "nodePath" option can be used - "node" option', testNodePath, runWithNodeOption, identity);97test.serial('The "nodePath" option can be a file URL - "node" option', testNodePath, runWithNodeOption, pathToFileURL);98test.serial('The "nodePath" option can be used - "node" option sync', testNodePath, runWithNodeOptionSync, identity);99test.serial('The "nodePath" option can be a file URL - "node" option sync', testNodePath, runWithNodeOptionSync, pathToFileURL);100101const testNodePathDefault = async (t, execaMethod) => {102 const {stdout} = await execaMethod('--version');103 t.is(stdout, process.version);104};105106test('The "nodePath" option defaults to the current Node.js binary - execaNode()', testNodePathDefault, execaNode);107test('The "nodePath" option defaults to the current Node.js binary - "node" option', testNodePathDefault, runWithNodeOption);108test('The "nodePath" option defaults to the current Node.js binary - "node" option sync', testNodePathDefault, runWithNodeOptionSync);109110const testNodePathInvalid = (t, execaMethod) => {111 t.throws(() => {112 execaMethod('noop.js', [], {nodePath: true});113 }, {message: /The "nodePath" option must be a string or a file URL/});114};115116test('The "nodePath" option must be a string or URL - execaNode()', testNodePathInvalid, execaNode);117test('The "nodePath" option must be a string or URL - "node" option', testNodePathInvalid, runWithNodeOption);118test('The "nodePath" option must be a string or URL - "node" option sync', testNodePathInvalid, runWithNodeOptionSync);119120const testFormerNodePath = (t, execaMethod) => {121 t.throws(() => {122 execaMethod('noop.js', [], {execPath: process.execPath});123 }, {message: /The "execPath" option has been removed/});124};125126test('The "execPath" option cannot be used - execaNode()', testFormerNodePath, execaNode);127test('The "execPath" option cannot be used - "node" option', testFormerNodePath, runWithNodeOption);128test('The "execPath" option cannot be used - "node" option sync', testFormerNodePath, runWithNodeOptionSync);129130const nodePathArguments = ['-p', ['process.env.Path || process.env.PATH']];131132const testSubprocessNodePath = async (t, execaMethod, mapPath) => {133 const nodePath = mapPath(await getNodePath());134 const {stdout} = await execaMethod(...nodePathArguments, {nodePath});135 t.true(stdout.includes(TEST_NODE_VERSION));136};137138test.serial('The "nodePath" option impacts the subprocess - execaNode()', testSubprocessNodePath, execaNode, identity);139test.serial('The "nodePath" option impacts the subprocess - "node" option', testSubprocessNodePath, runWithNodeOption, identity);140test.serial('The "nodePath" option impacts the subprocess - "node" option sync', testSubprocessNodePath, runWithNodeOptionSync, identity);141142const testSubprocessNodePathDefault = async (t, execaMethod) => {143 const {stdout} = await execaMethod(...nodePathArguments);Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0
line 144why, one step back144 t.true(stdout.includes(path.dirname(process.execPath)));Rename files (#977)ehmickybefore adoption4b2e316
lines 145–158why, one step back145};146147test('The "nodePath" option defaults to the current Node.js binary in the subprocess - execaNode()', testSubprocessNodePathDefault, execaNode);148test('The "nodePath" option defaults to the current Node.js binary in the subprocess - "node" option', testSubprocessNodePathDefault, runWithNodeOption);149test('The "nodePath" option defaults to the current Node.js binary in the subprocess - "node" option sync', testSubprocessNodePathDefault, runWithNodeOptionSync);150151test.serial('The "nodePath" option requires "node: true" to impact the subprocess', async t => {152 const nodePath = await getNodePath();153 const {stdout} = await execa('node', nodePathArguments.flat(), {nodePath});154 t.false(stdout.includes(TEST_NODE_VERSION));155});156157const testSubprocessNodePathCwd = async (t, execaMethod) => {158 const nodePath = await getNodePath();Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0
lines 159–160why, one step back159 const cwd = path.dirname(path.dirname(nodePath));160 const relativeExecPath = path.relative(cwd, nodePath);Rename files (#977)ehmickybefore adoption4b2e316
lines 161–170why, one step back161 const {stdout} = await execaMethod(...nodePathArguments, {nodePath: relativeExecPath, cwd});162 t.true(stdout.includes(TEST_NODE_VERSION));163};164165test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - execaNode()', testSubprocessNodePathCwd, execaNode);166test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - "node" option', testSubprocessNodePathCwd, runWithNodeOption);167test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - "node" option sync', testSubprocessNodePathCwd, runWithNodeOptionSync);168169const testCwdNodePath = async (t, execaMethod) => {170 const nodePath = await getNodePath();Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0
lines 171–172why, one step back171 const cwd = path.dirname(path.dirname(nodePath));172 const relativeExecPath = path.relative(cwd, nodePath);Rename files (#977)ehmickybefore adoption4b2e316
lines 173–179why, one step back173 const {stdout} = await execaMethod('--version', [], {nodePath: relativeExecPath, cwd});174 t.is(stdout, `v${TEST_NODE_VERSION}`);175};176177test.serial('The "nodePath" option is relative to "cwd" - execaNode()', testCwdNodePath, execaNode);178test.serial('The "nodePath" option is relative to "cwd" - "node" option', testCwdNodePath, runWithNodeOption);179test.serial('The "nodePath" option is relative to "cwd" - "node" option sync', testCwdNodePath, runWithNodeOptionSync);Fix deno node:process execPath compatibility (#1160)CJbefore adoptionf3a1bf3
lines 180–189why, one step back180181const testDenoExecPath = async (t, execaMethod) => {182 const {exitCode, stdout} = await execaMethod('noop.js', [], {nodePath: getDenoNodePath()});183 t.is(exitCode, 0);184 t.is(stdout, foobarString);185};186187test('The deno style "nodePath" option can be used - execaNode()', testDenoExecPath, execaNode);188test('The deno style "nodePath" option can be used - "node" option', testDenoExecPath, runWithNodeOption);189test('The deno style "nodePath" option can be used - "node" option sync', testDenoExecPath, runWithNodeOptionSync);Rename files (#977)ehmickybefore adoption4b2e316
lines 190–202why, one step back190191const testNodeOptions = async (t, execaMethod) => {192 const {stdout} = await execaMethod('empty.js', [], {nodeOptions: ['--version']});193 t.is(stdout, process.version);194};195196test('The "nodeOptions" option can be used - execaNode()', testNodeOptions, execaNode);197test('The "nodeOptions" option can be used - "node" option', testNodeOptions, runWithNodeOption);198test('The "nodeOptions" option can be used - "node" option sync', testNodeOptions, runWithNodeOptionSync);199200const spawnNestedExecaNode = (realExecArgv, fakeExecArgv, execaMethod, nodeOptions) => execa(201 'node',202 [...realExecArgv, 'nested-node.js', fakeExecArgv, execaMethod, nodeOptions, 'noop.js', foobarString],Upgrade xo (#980)ehmickybefore adoptionccfa412
line 203why, one step back203 {...fullStdio, cwd: FIXTURES_DIRECTORY},Rename files (#977)ehmickybefore adoption4b2e316
lines 204–239why, one step back204);205206const testInspectRemoval = async (t, fakeExecArgv, execaMethod) => {207 const {stdout, stdio} = await spawnNestedExecaNode([], fakeExecArgv, execaMethod, '');208 t.is(stdout, foobarString);209 t.is(stdio[3], '');210};211212test('The "nodeOptions" option removes --inspect without a port when defined by current process - execaNode()', testInspectRemoval, '--inspect', 'execaNode');213test('The "nodeOptions" option removes --inspect without a port when defined by current process - "node" option', testInspectRemoval, '--inspect', 'nodeOption');214test('The "nodeOptions" option removes --inspect with a port when defined by current process - execaNode()', testInspectRemoval, '--inspect=9222', 'execaNode');215test('The "nodeOptions" option removes --inspect with a port when defined by current process - "node" option', testInspectRemoval, '--inspect=9222', 'nodeOption');216test('The "nodeOptions" option removes --inspect-brk without a port when defined by current process - execaNode()', testInspectRemoval, '--inspect-brk', 'execaNode');217test('The "nodeOptions" option removes --inspect-brk without a port when defined by current process - "node" option', testInspectRemoval, '--inspect-brk', 'nodeOption');218test('The "nodeOptions" option removes --inspect-brk with a port when defined by current process - execaNode()', testInspectRemoval, '--inspect-brk=9223', 'execaNode');219test('The "nodeOptions" option removes --inspect-brk with a port when defined by current process - "node" option', testInspectRemoval, '--inspect-brk=9223', 'nodeOption');220221const testInspectDifferentPort = async (t, execaMethod) => {222 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9225'], '', execaMethod, '--inspect=9224');223 t.is(stdout, foobarString);224 t.true(stdio[3].includes('Debugger listening'));225};226227test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by current process - execaNode()', testInspectDifferentPort, 'execaNode');228test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by current process - "node" option', testInspectDifferentPort, 'nodeOption');229230const testInspectSamePort = async (t, execaMethod) => {231 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9226'], '', execaMethod, '--inspect=9226');232 t.is(stdout, foobarString);233 t.true(stdio[3].includes('address already in use'));234};235236test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by current process - execaNode()', testInspectSamePort, 'execaNode');237test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by current process - "node" option', testInspectSamePort, 'nodeOption');238239const testIpc = async (t, execaMethod, options) => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 240–241why, one step back240 const subprocess = execaMethod('ipc-echo.js', [], options);241Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 242–243why, one step back242 await subprocess.sendMessage(foobarString);243 t.is(await subprocess.getOneMessage(), foobarString);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 244–245why, one step back244245 const {stdio} = await subprocess;Rename files (#977)ehmickybefore adoption4b2e316
lines 246–257why, one step back246 t.is(stdio.length, 4);247 t.is(stdio[3], undefined);248};249250test('execaNode() adds an ipc channel', testIpc, execaNode, {});251test('The "node" option adds an ipc channel', testIpc, runWithNodeOption, {});252test('The "ipc" option adds an ipc channel', testIpc, runWithIpc, {});253test('The "ipc" option works with "stdio: \'pipe\'"', testIpc, runWithIpc, {stdio: 'pipe'});254test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe']});255test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\', \'ipc\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe', 'ipc']});256test('The "ipc" option works with "stdout: \'pipe\'"', testIpc, runWithIpc, {stdout: 'pipe'});257Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 258–259why, one step back258const NO_SEND_MESSAGE = 'sendMessage() can only be used';259Rename files (#977)ehmickybefore adoption4b2e316
line 260why, one step back260test('No ipc channel is added by default', async t => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 261–262why, one step back261 const {message, stdio} = await t.throwsAsync(execa('node', ['ipc-send.js']));262 t.true(message.includes(NO_SEND_MESSAGE));Rename files (#977)ehmickybefore adoption4b2e316
lines 263–266why, one step back263 t.is(stdio.length, 3);264});265266const testDisableIpc = async (t, execaMethod) => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 267why, one step back267 const {failed, message, stdio} = await execaMethod('ipc-send.js', [], {ipc: false, reject: false});Rename files (#977)ehmickybefore adoption4b2e316
line 268why, one step back268 t.true(failed);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 269why, one step back269 t.true(message.includes(NO_SEND_MESSAGE));Rename files (#977)ehmickybefore adoption4b2e316
lines 270–280why, one step back270 t.is(stdio.length, 3);271};272273test('Can disable "ipc" - execaNode()', testDisableIpc, execaNode);274test('Can disable "ipc" - "node" option', testDisableIpc, runWithNodeOption);275test('Can disable "ipc" - "node" option sync', testDisableIpc, runWithNodeOptionSync);276277const NO_IPC_MESSAGE = /The "ipc: true" option cannot be used/;278279const testNoIpcSync = (t, node) => {280 t.throws(() => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 281why, one step back281 execaSync('node', ['ipc-send.js'], {ipc: true, node});Rename files (#977)ehmickybefore adoption4b2e316
lines 282–289why, one step back282 }, {message: NO_IPC_MESSAGE});283};284285test('Cannot use "ipc: true" with execaSync()', testNoIpcSync, undefined);286test('Cannot use "ipc: true" with execaSync() - "node: false"', testNoIpcSync, false);287288test('Cannot use "ipc: true" with execaSync() - "node: true"', t => {289 t.throws(() => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 290why, one step back290 execaSync('ipc-send.js', {ipc: true, node: true});Rename files (#977)ehmickybefore adoption4b2e316
lines 291–302why, one step back291 }, {message: NO_IPC_MESSAGE});292});293294const testNoShell = async (t, execaMethod) => {295 const {failed, message} = await execaMethod('node --version', [], {shell: true, reject: false});296 t.true(failed);297 t.true(message.includes('MODULE_NOT_FOUND'));298};299300test('Cannot use "shell: true" - execaNode()', testNoShell, execaNode);301test('Cannot use "shell: true" - "node" option', testNoShell, runWithNodeOption);302test('Cannot use "shell: true" - "node" option sync', testNoShell, runWithNodeOptionSync);