Fix deno node:process execPath compatibility (#1160)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by CJ · committed by GitHub · parent d3a146e · Browse files · 5 files·+49 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: ehmicky <ehmicky@users.noreply.github.com>
Diff stats
5 files+49 added−2 removed
- tests:+36 added−0 removed
- other:+13 added−2 removed
Provenance
- Commit
f3a1bf3- Tree
50b7cb7- Author
CJ <cj@null.computer>- Committer
GitHub <noreply@github.com>- Parent
d3a146e
5 files changed+49 added−2 removed
modified:lib/arguments/file-url.js+11 added-1 removed
123455678910111213141516171819202122232425import {fileURLToPath} from 'node:url';
// Allow some arguments/options to be either a file path string or a file URLexport const safeNormalizeFileUrl = (file, name) => { const fileString = normalizeFileUrl(file); const fileString = normalizeFileUrl(normalizeDenoExecPath(file));
if (typeof fileString !== 'string') { throw new TypeError(`${name} must be a string or a file URL: ${fileString}.`); }
return fileString;};
// In Deno node:process execPath is a special object, not just a string:// https://github.com/denoland/deno/blob/f460188e583f00144000aa0d8ade08218d47c3c1/ext/node/polyfills/process.ts#L344const normalizeDenoExecPath = file => isDenoExecPath(file) ? file.toString() : file;
export const isDenoExecPath = file => typeof file !== 'string' && file && Object.getPrototypeOf(file) === String.prototype;
// Same but also allows other values, e.g. `boolean` for the `shell` optionexport const normalizeFileUrl = file => file instanceof URL ? fileURLToPath(file) : file;modified:lib/pipe/pipe-arguments.js+2 added-1 removed
1234567847 unmodified lines5657585959606162636427 unmodified linesimport {normalizeParameters} from '../methods/parameters.js';import {getStartTime} from '../return/duration.js';import {SUBPROCESS_OPTIONS, getToStream, getFromStream} from '../arguments/fd-options.js';import {isDenoExecPath} from '../arguments/file-url.js';
// Normalize and validate arguments passed to `source.pipe(destination)`export const normalizePipeArguments = ({source, sourcePromise, boundOptions, createNested}, ...pipeArguments) => { const startTime = getStartTime();47 unmodified lines const destination = createNested(mapDestinationArguments, boundOptions)(firstArgument, ...pipeArguments); return {destination, pipeOptions: boundOptions}; }
if (typeof firstArgument === 'string' || firstArgument instanceof URL) { if (typeof firstArgument === 'string' || firstArgument instanceof URL || isDenoExecPath(firstArgument)) { if (Object.keys(boundOptions).length > 0) { throw new TypeError('Please use .pipe("file", ..., options) or .pipe(execa("file", ..., options)) instead of .pipe(options)("file", ...).'); }
27 unmodified linesmodified:test/helpers/file-path.js+11 added
123456789101112131415import path from 'node:path';import process from 'node:process';
export const getAbsolutePath = file => ({file});export const getRelativePath = filePath => ({file: path.relative('.', filePath)});// Defined as getter so call to toString is not cachedexport const getDenoNodePath = () => Object.freeze({ __proto__: String.prototype, toString() { return process.execPath; }, get length() { return this.toString().length; },});modified:test/methods/node.js+14 added
5 unmodified lines6 import {execa, execaSync, execaNode} from '../../index.js';7 import {FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';8 import {identity, fullStdio} from '../helpers/stdio.js';9 import {foobarString} from '../helpers/input.js';10+import {getDenoNodePath} from '../helpers/file-path.js';11 12 process.chdir(FIXTURES_DIRECTORY);13 14 const runWithNodeOption = (file, commandArguments, options) => Array.isArray(commandArguments)58 unmodified lines73 test('Cannot use "node" as binary - "node" option sync', testDoubleNode, 'node', runWithNodeOptionSync);74 test('Cannot use path to "node" as binary - execaNode()', testDoubleNode, process.execPath, execaNode);75 test('Cannot use path to "node" as binary - "node" option', testDoubleNode, process.execPath, runWithNodeOption);76 test('Cannot use path to "node" as binary - "node" option sync', testDoubleNode, process.execPath, runWithNodeOptionSync);77+test('Cannot use deno style nodePath as binary - execaNode()', testDoubleNode, getDenoNodePath(), execaNode);78+test('Cannot use deno style nodePath as binary - "node" option', testDoubleNode, getDenoNodePath(), runWithNodeOption);79+test('Cannot use deno style nodePath as binary - "node" option sync', testDoubleNode, getDenoNodePath(), runWithNodeOptionSync);80 81 const getNodePath = async () => {82 const {path} = await getNode(TEST_NODE_VERSION);83 return path;93 unmodified lines177 test.serial('The "nodePath" option is relative to "cwd" - execaNode()', testCwdNodePath, execaNode);178 test.serial('The "nodePath" option is relative to "cwd" - "node" option', testCwdNodePath, runWithNodeOption);179 test.serial('The "nodePath" option is relative to "cwd" - "node" option sync', testCwdNodePath, runWithNodeOptionSync);180 181+const 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+};186+187+test('The deno style "nodePath" option can be used - execaNode()', testDenoExecPath, execaNode);188+test('The deno style "nodePath" option can be used - "node" option', testDenoExecPath, runWithNodeOption);189+test('The deno style "nodePath" option can be used - "node" option sync', testDenoExecPath, runWithNodeOptionSync);190+191 const testNodeOptions = async (t, execaMethod) => {192 const {stdout} = await execaMethod('empty.js', [], {nodeOptions: ['--version']});193 t.is(stdout, process.version);194 };108 unmodified linesmodified:test/pipe/pipe-arguments.js+11 added
2 unmodified lines3 import test from 'ava';4 import {$, execa} from '../../index.js';5 import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';6 import {foobarString} from '../helpers/input.js';7+import {getDenoNodePath} from '../helpers/file-path.js';8 9 setFixtureDirectory();10 11 test('$.pipe(subprocess)', async t => {61 unmodified lines73 const {stdout} = await execa('noop.js', [foobarString]).pipe('node', ['stdin.js'], {cwd: FIXTURES_DIRECTORY});74 t.is(stdout, foobarString);75 });76 77+test('execa.$.pipe("file", commandArguments, options with denoNodePath)`', async t => {78+ const {stdout} = await execa('noop.js', [foobarString]).pipe('node', ['stdin.js'], {cwd: FIXTURES_DIRECTORY, nodePath: getDenoNodePath()});79+ t.is(stdout, foobarString);80+});81+82+test('execa.$.pipe("file", commandArguments, denoNodePath)`', async t => {83+ const {stdout} = await execa('noop.js', [foobarString]).pipe(getDenoNodePath(), ['stdin.js'], {cwd: FIXTURES_DIRECTORY});84+ t.is(stdout, foobarString);85+});86+87 test('$.pipe.pipe("file", commandArguments, options)', async t => {88 const {stdout} = await $`noop.js ${foobarString}`89 .pipe`stdin.js`90 .pipe('node', ['stdin.js'], {cwd: FIXTURES_DIRECTORY});193 unmodified lines