Skip to content
Myr

Allow `stdin` to be a file URL (#610)

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

authored by ehmicky · committed by GitHub · parent 85a9821 · Browse files · 5 files+81 added−6 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+81 added−6 removed

  • tests:+45 added−0 removed
  • other:+36 added−6 removed

5 files+81 added−6 removed

modified:lib/stdio.js+31 added-2 removed
modified:test/stream.js+45 added
2 unmodified lines
3import process from 'node:process';4import fs from 'node:fs';5import Stream from 'node:stream';6import {promisify} from 'node:util';7import {pathToFileURL} from 'node:url';8import test from 'ava';9import getStream from 'get-stream';10import {pEvent} from 'p-event';11import tempfile from 'tempfile';
146 unmodified lines
158 execa('stdin.js', {stdin: ['foo', 'bar'], inputFile: 'dummy.txt'});159 }, {message: /`inputFile` and `stdin` options/});160});161162test('stdin option cannot be a file URL when "input" is used', t => {163 t.throws(() => {164 execa('stdin.js', {stdin: pathToFileURL('unknown'), input: 'foobar'});165 }, {message: /`input` and `stdin` options/});166});167168test('stdin option cannot be a file URL when "inputFile" is used', t => {169 t.throws(() => {170 execa('stdin.js', {stdin: pathToFileURL('unknown'), inputFile: 'dummy.txt'});171 }, {message: /`inputFile` and `stdin` options/});172});173174test('stdin option cannot be a generic iterable string', async t => {175 await t.throwsAsync(() => execa('stdin.js', {stdin: 'foobar'}), {code: 'ERR_INVALID_SYNC_FORK_INPUT'});176});177
42 unmodified lines
220 const {stdout} = await $({input: 'foobar'})`stdin.js`;221 t.is(stdout, 'foobar');222});223224test('stdin can be a file URL', async t => {225 const inputFile = tempfile();226 fs.writeFileSync(inputFile, 'howdy');227 const stdin = pathToFileURL(inputFile);228 const {stdout} = await execa('stdin.js', {stdin});229 t.is(stdout, 'howdy');230});231232test('stdin cannot be a non-file URL', async t => {233 await t.throws(() => {234 execa('stdin.js', {stdin: new URL('https://example.com')});235 }, {message: /pathToFileURL/});236});237238test('inputFile can be set', async t => {239 const inputFile = tempfile();240 fs.writeFileSync(inputFile, 'howdy');241 const {stdout} = await execa('stdin.js', {inputFile});
18 unmodified lines
260 execa('stdin.js', {inputFile: '', stdin: 'ignore'});261 }, {message: /`inputFile` and `stdin` options/});262});263264test('stdin file URL errors should be handled', async t => {265 await t.throwsAsync(execa('stdin.js', {stdin: pathToFileURL('unknown')}), {code: 'ENOENT'});266});267268test('inputFile errors should be handled', async t => {269 await t.throwsAsync(execa('stdin.js', {inputFile: 'unknown'}), {code: 'ENOENT'});270});271
35 unmodified lines
307 {message: /The `input` option cannot be a stream in sync mode/},308 );309});310311test('stdin can be a file URL - sync', t => {312 const inputFile = tempfile();313 fs.writeFileSync(inputFile, 'howdy');314 const stdin = pathToFileURL(inputFile);315 const {stdout} = execaSync('stdin.js', {stdin});316 t.is(stdout, 'howdy');317});318319test('stdin cannot be a non-file URL - sync', async t => {320 await t.throws(() => {321 execaSync('stdin.js', {stdin: new URL('https://example.com')});322 }, {message: /pathToFileURL/});323});324325test('inputFile can be set - sync', t => {326 const inputFile = tempfile();327 fs.writeFileSync(inputFile, 'howdy');328 const {stdout} = execaSync('stdin.js', {inputFile});
121 unmodified lines
modified:index.d.ts+2 added-2 removed
10 unmodified lines
11 | Stream12 | number13 | undefined;1415export type StdinOption = StdioOption | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>;15export type StdinOption = StdioOption | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array> | URL;1617type EncodingOption =18 | 'utf8'19 // eslint-disable-next-line unicorn/text-encoding-identifier-case
67 unmodified lines
8788 /**89 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).9091 It can also be an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or an [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols), providing neither [`execaSync()`](#execasyncfile-arguments-options), the [`input` option](#input) nor the [`inputFile` option](#inputfile) is used.91 It can also be a file URL, an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or an [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols), providing neither [`execaSync()`](#execasyncfile-arguments-options), the [`input` option](#input) nor the [`inputFile` option](#inputfile) is used.9293 @default `inherit` with `$`, `pipe` otherwise94 */95 readonly stdin?: StdinOption;
904 unmodified lines
modified:index.test-d.ts+1 added
162 unmodified lines
163execa('unicorns', {stdin: stringGenerator()});164execa('unicorns', {stdin: binaryGenerator()});165expectError(execa('unicorns', {stdin: [0]}));166expectError(execa('unicorns', {stdin: numberGenerator()}));167execa('unicorns', {stdin: new URL('file:///test')});168execa('unicorns', {stdin: 1});169execa('unicorns', {stdin: undefined});170execa('unicorns', {stdout: 'pipe'});171execa('unicorns', {stdout: 'overlapped'});
195 unmodified lines
modified:readme.md+2 added-2 removed
557 unmodified lines
558If the input is not a file, use the [`input` option](#input) instead.559560#### stdin561562Type: `string | number | Stream | undefined | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>`\562Type: `string | number | Stream | undefined | URL | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>`\563Default: `inherit` with [`$`](#command), `pipe` otherwise564565Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).566567It can also be an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or an [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols), providing neither [`execaSync()`](#execasyncfile-arguments-options), the [`input` option](#input) nor the [`inputFile` option](#inputfile) is used.567It can also be a file URL, an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or an [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols), providing neither [`execaSync()`](#execasyncfile-arguments-options), the [`input` option](#input) nor the [`inputFile` option](#inputfile) is used.568569#### stdout570571Type: `string | number | Stream | undefined`\
242 unmodified lines