Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 316f562 · Browse files · 5 files+30 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+30 added−21 removed

  • tests:+22 added−13 removed
  • other:+8 added−8 removed

5 files+30 added−21 removed

modified:lib/arguments/node.js+1 added-1 removed
modified:lib/arguments/options.js+3 added-3 removed
modified:test/arguments/node.js+22 added-13 removed
95 unmodified lines
96test('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);99100const nodePathArguments = ['node', ['-p', 'process.env.Path || process.env.PATH']];100const nodePathArguments = ['-p', ['process.env.Path || process.env.PATH']];101102const testChildNodePath = async (t, mapPath) => {102const testChildNodePath = async (t, execaMethod, mapPath) => {103 const nodePath = mapPath(await getNodePath());104 const {stdout} = await execa(...nodePathArguments, {preferLocal: true, nodePath});104 const {stdout} = await execaMethod(...nodePathArguments, {nodePath});105 t.true(stdout.includes(TEST_NODE_VERSION));106};107108test.serial('The "nodePath" option impacts the child process', testChildNodePath, identity);109test.serial('The "nodePath" option can be a file URL', testChildNodePath, pathToFileURL);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);111111test('The "nodePath" option defaults to the current Node.js binary in the child process', async t => {112 const {stdout} = await execa(...nodePathArguments, {preferLocal: true});112const testChildNodePathDefault = async (t, execaMethod) => {113 const {stdout} = await execaMethod(...nodePathArguments);114 t.true(stdout.includes(dirname(process.execPath)));114});115};116117test('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);120116test.serial('The "nodePath" option requires "preferLocal: true" to impact the child process', async t => {121test.serial('The "nodePath" option requires "node: true" to impact the child process', async t => {122 const nodePath = await getNodePath();118 const {stdout} = await execa(...nodePathArguments, {nodePath});123 const {stdout} = await execa('node', nodePathArguments.flat(), {nodePath});124 t.false(stdout.includes(TEST_NODE_VERSION));125});126122test.serial('The "nodePath" option is relative to "cwd" when used in the child process', async t => {127const testChildNodePathCwd = async (t, execaMethod) => {128 const nodePath = await getNodePath();129 const cwd = dirname(dirname(nodePath));130 const relativeExecPath = relative(cwd, nodePath);126 const {stdout} = await execa(...nodePathArguments, {preferLocal: true, nodePath: relativeExecPath, cwd});131 const {stdout} = await execaMethod(...nodePathArguments, {nodePath: relativeExecPath, cwd});132 t.true(stdout.includes(TEST_NODE_VERSION));128});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);138139const testCwdNodePath = async (t, execaMethod) => {140 const nodePath = await getNodePath();141 const cwd = dirname(dirname(nodePath));142 const relativeExecPath = relative(cwd, nodePath);
116 unmodified lines
modified:index.d.ts+2 added-2 removed
293 unmodified lines
294295 /**296 Path to the Node.js executable.297298 When the `node` option is `true`, this is used to to create the child process. When the `preferLocal` option is `true`, this is used in the child process itself.299298 For example, this can be used together with [`get-node`](https://github.com/ehmicky/get-node) to run a specific Node.js version.299300 Requires the `node` option to be `true`.301302 @default [`process.execPath`](https://nodejs.org/api/process.html#process_process_execpath) (current Node.js executable)303 */304 readonly nodePath?: string | URL;305
1073 unmodified lines
modified:readme.md+2 added-2 removed
625 unmodified lines
626Default: [`process.execPath`](https://nodejs.org/api/process.html#process_process_execpath) (current Node.js executable)627628Path to the Node.js executable.629630When the [`node`](#node) option is `true`, this is used to to create the child process. When the [`preferLocal`](#preferlocal) option is `true`, this is used in the child process itself.631630For example, this can be used together with [`get-node`](https://github.com/ehmicky/get-node) to run a specific Node.js version.631632Requires the [`node`](#node) option to be `true`.633634#### verbose635636Type: `boolean`\637Default: `false`
374 unmodified lines