Allow `cwd` and `localDir` options to be URLs (#492)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent e4b9295 · Browse files · 5 files·+39 added−5 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+39 added−5 removed
- tests:+24 added−1 removed
- other:+15 added−4 removed
Provenance
- Commit
93ab929- Tree
87583d8- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
e4b9295
5 files changed+39 added−5 removed
modified:test/test.js+24 added-1 removed
1 import path from 'node:path';2 import process from 'node:process';3-import {fileURLToPath} from 'node:url';3+import {fileURLToPath, pathToFileURL} from 'node:url';4 import test from 'ava';5 import isRunning from 'is-running';6 import getNode from 'get-node';7+import semver from 'semver';8 import {execa, execaSync} from '../index.js';9 10 process.env.PATH = fileURLToPath(new URL('./fixtures', import.meta.url)) + path.delimiter + process.env.PATH;11 process.env.FOO = 'foo';186 unmodified lines198 const {stdout} = await execa('environment.js', [], {env: {BAR: 'bar', PATH: process.env.PATH}, extendEnv: false});199 t.deepEqual(stdout.split('\n'), ['undefined', 'bar']);200 });201 202+test('can use `options.cwd` as a string', async t => {203+ const cwd = '/';204+ const {stdout} = await execa('node', ['-p', 'process.cwd()'], {cwd});205+ t.is(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));206+});207+208+if (semver.satisfies(process.version, '^14.18.0 || >=16.4.0')) {209+ test('localDir option can be a URL', async t => {210+ const command = process.platform === 'win32' ? 'echo %PATH%' : 'echo $PATH';211+ const {stdout} = await execa(command, {shell: true, preferLocal: true, localDir: pathToFileURL('/test')});212+ const envPaths = stdout.split(path.delimiter);213+ t.true(envPaths.some(envPath => envPath.endsWith('.bin')));214+ });215+216+ test('can use `options.cwd` as a URL', async t => {217+ const cwd = '/';218+ const cwdUrl = pathToFileURL(cwd);219+ const {stdout} = await execa('node', ['-p', 'process.cwd()'], {cwd: cwdUrl});220+ t.is(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));221+ });222+}223+224 test('can use `options.shell: true`', async t => {225 const {stdout} = await execa('node test/fixtures/noop.js foo', {shell: true});226 t.is(stdout, 'foo');227 });23 unmodified linesmodified:index.d.ts+6 added-2 removed
31 unmodified lines32 33 /**34 Preferred path to find locally installed binaries in (use with `preferLocal`).35 36+ Using a `URL` is only supported in Node.js `14.18.0`, `16.14.0` or above.37+38 @default process.cwd()39 */38- readonly localDir?: string;40+ readonly localDir?: string | URL;41 42 /**43 Path to the Node.js executable to use in child processes.44 67 unmodified lines112 113 /**114 Current working directory of the child process.115 116+ Using a `URL` is only supported in Node.js `14.18.0`, `16.14.0` or above.117+118 @default process.cwd()119 */116- readonly cwd?: string;120+ readonly cwd?: string | URL;121 122 /**123 Environment key-value pairs. Extends automatically from `process.env`. Set `extendEnv` to `false` if you don't want this.124 432 unmodified linesmodified:index.test-d.ts+2 added
88 unmodified lines89 /* eslint-disable @typescript-eslint/no-floating-promises */90 execa('unicorns', {cleanup: false});91 execa('unicorns', {preferLocal: false});92 execa('unicorns', {localDir: '.'});93+execa('unicorns', {localDir: new URL('file:///test')});94 execa('unicorns', {execPath: '/path'});95 execa('unicorns', {buffer: false});96 execa('unicorns', {input: ''});97 execa('unicorns', {input: Buffer.from('')});23 unmodified lines121 execa('unicorns', {reject: false});122 execa('unicorns', {stripFinalNewline: false});123 execa('unicorns', {extendEnv: false});124 execa('unicorns', {cwd: '.'});125+execa('unicorns', {cwd: new URL('file:///test')});126 // eslint-disable-next-line @typescript-eslint/naming-convention127 execa('unicorns', {env: {PATH: ''}});128 execa('unicorns', {argv0: ''});129 execa('unicorns', {stdio: 'pipe'});78 unmodified linesmodified:package.json+1 added
56 unmodified lines57585960616263646512 unmodified lines56 unmodified lines "c8": "^7.10.0", "get-node": "^12.0.0", "is-running": "^2.1.0", "p-event": "^5.0.1", "semver": "^7.3.5", "tempfile": "^4.0.0", "tsd": "^0.18.0", "xo": "^0.46.4" },12 unmodified linesmodified:readme.md+6 added-2 removed
355 unmodified lines356 If you `$ npm install foo`, you can then `execa('foo')`.357 358 #### localDir359 360-Type: `string`\360+Type: `string | URL`\361 Default: `process.cwd()`362 363 Preferred path to find locally installed binaries in (use with `preferLocal`).364 365+Using a `URL` is only supported in Node.js `14.18.0`, `16.14.0` or above.366+367 #### execPath368 369 Type: `string`\370 Default: `process.execPath` (Current Node.js executable)76 unmodified lines447 Execa also accepts the below options which are the same as the options for [`child_process#spawn()`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)/[`child_process#exec()`](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback)448 449 #### cwd450 449-Type: `string`\451+Type: `string | URL`\452 Default: `process.cwd()`453 454 Current working directory of the child process.455 456+Using a `URL` is only supported in Node.js `14.18.0`, `16.14.0` or above.457+458 #### env459 460 Type: `object`\461 Default: `process.env`197 unmodified lines