Allow `shell` option to be a file URL (#635)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 640ae39 · Browse files · 6 files·+18 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+18 added−10 removed
- tests:+8 added−3 removed
- other:+10 added−7 removed
Provenance
- Commit
189d486- Tree
108d5ce- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
640ae39
6 files changed+18 added−10 removed
modified:test/test.js+8 added-3 removed
2 unmodified lines3 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 which from 'which';8 import {execa, execaSync, execaNode, $} from '../index.js';9 import {setFixtureDir, PATH_KEY, FIXTURES_DIR_URL} from './helpers/fixtures-dir.js';10 11 setFixtureDir();233 unmodified lines245 const {stdout} = await execa('node test/fixtures/noop.js foo', {shell: true});246 t.is(stdout, 'foo');247 });248 248-test('can use `options.shell: string`', async t => {249- const shell = process.platform === 'win32' ? 'cmd.exe' : '/bin/bash';249+const testShellPath = async (t, mapPath) => {250+ const shellPath = process.platform === 'win32' ? 'cmd.exe' : 'bash';251+ const shell = mapPath(await which(shellPath));252 const {stdout} = await execa('node test/fixtures/noop.js foo', {shell});253 t.is(stdout, 'foo');252-});254+};255 256+test('can use `options.shell: string`', testShellPath, identity);257+test('can use `options.shell: file URL`', testShellPath, pathToFileURL);258+259 test('use extend environment with `extendEnv: true` and `shell: true`', async t => {260 process.env.TEST = 'test';261 const command = process.platform === 'win32' ? 'echo %TEST%' : 'echo $TEST';262 const {stdout} = await execa(command, {shell: true, env: {}, extendEnv: true});43 unmodified linesmodified:index.d.ts+1 added-1 removed
254 unmodified lines255 - unsafe, potentially allowing command injection.256 257 @default false258 */259- readonly shell?: boolean | string;259+ readonly shell?: boolean | string | URL;260 261 /**262 Specify the character encoding used to decode the `stdout` and `stderr` output. If set to `'buffer'`, then `stdout` and `stderr` will be a `Uint8Array` instead of a string.263 806 unmodified linesmodified:index.js+6 added-5 removed
28 unmodified lines29 30 return env;31 };32 33+const normalizeFileUrl = file => file instanceof URL ? fileURLToPath(file) : file;34+35 const getFilePath = file => {34- if (file instanceof URL) {35- return fileURLToPath(file);36- }36+ const fileString = normalizeFileUrl(file);37 38- if (typeof file !== 'string') {38+ if (typeof fileString !== 'string') {39 throw new TypeError('First argument must be a string or a file URL.');40 }41 42- return file;42+ return fileString;43 };44 45 const handleArguments = (file, args, options = {}) => {46 const parsed = crossSpawn._parse(file, args, options);15 unmodified lines62 all: false,63 windowsHide: true,64 verbose: verboseDefault,65 ...options,66+ shell: normalizeFileUrl(options.shell),67 };68 69 options.env = getEnv(options);70 263 unmodified linesmodified:index.test-d.ts+1 added
262 unmodified lines263 execa('unicorns', {uid: 0});264 execa('unicorns', {gid: 0});265 execa('unicorns', {shell: true});266 execa('unicorns', {shell: '/bin/sh'});267+execa('unicorns', {shell: fileUrl});268 execa('unicorns', {timeout: 1000});269 execa('unicorns', {maxBuffer: 1000});270 execa('unicorns', {killSignal: 'SIGTERM'});271 execa('unicorns', {killSignal: 9});139 unmodified linesmodified:package.json+1 added
63 unmodified lines64656667686970717219 unmodified lines63 unmodified lines "p-event": "^6.0.0", "path-key": "^4.0.0", "tempfile": "^5.0.0", "tsd": "^0.29.0", "which": "^4.0.0", "xo": "^0.56.0" }, "c8": { "reporter": [19 unmodified linesmodified:readme.md+1 added-1 removed
716 unmodified lines717 Sets the group identity of the process.718 719 #### shell720 721-Type: `boolean | string`\721+Type: `boolean | string | URL`\722 Default: `false`723 724 If `true`, runs `file` inside of a shell. Uses `/bin/sh` on UNIX and `cmd.exe` on Windows. A different shell can be specified as a string. The shell should understand the `-c` switch on UNIX or `/d /s /c` on Windows.725 136 unmodified lines