Skip to content
Myr

Fix default value of `stdin` with `$` (#550)

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

authored by ehmicky · committed by GitHub · parent 881fbad · Browse files · 8 files+67 added−9 removed
This commit

State

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

Diff stats

8 files+67 added−9 removed

  • tests:+50 added−2 removed
  • other:+17 added−7 removed

8 files+67 added−9 removed

added:test/fixtures/stdin-script.js+5 added
modified:test/helpers/fixtures-dir.js+1 added-1 removed
modified:test/command.js+15 added
1import {inspect} from 'node:util';2import test from 'ava';3import {isStream} from 'is-stream';4import {execa, execaSync, execaCommand, execaCommandSync, $} from '../index.js';5import {setFixtureDir} from './helpers/fixtures-dir.js';67setFixtureDir();
265 unmodified lines
273test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');274275test('$({stdio: \'inherit\'}).sync`noop.js`', invalidExpression, $({stdio: 'inherit'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');276test('[ $({stdio: \'inherit\'}).sync`noop.js` ]', invalidExpression, [$({stdio: 'inherit'}).sync`noop.js`], 'Unexpected "undefined" stdout in template expression');277278test('$ stdin defaults to "inherit"', async t => {279 const {stdout} = await $({input: 'foo'})`stdin-script.js`;280 t.is(stdout, 'foo');281});282283test('$.sync stdin defaults to "inherit"', t => {284 const {stdout} = $({input: 'foo'}).sync`stdin-script.js`;285 t.is(stdout, 'foo');286});287288test('$ stdin has no default value when stdio is set', t => {289 t.true(isStream($({stdio: 'pipe'})`noop.js`.stdin));290});
modified:test/stream.js+25 added-1 removed
4 unmodified lines
5import test from 'ava';6import getStream from 'get-stream';7import {pEvent} from 'p-event';8import tempfile from 'tempfile';9import {execa, execaSync} from '../index.js';9import {execa, execaSync, $} from '../index.js';10import {setFixtureDir} from './helpers/fixtures-dir.js';1112setFixtureDir();13
56 unmodified lines
70 const {stdout} = await execa('stdin.js', {input: stream});71 t.is(stdout, 'howdy');72});7374test('input option can be used with $', async t => {75 const {stdout} = await $({input: 'foobar'})`stdin.js`;76 t.is(stdout, 'foobar');77});7879test('inputFile can be set', async t => {80 const inputFile = tempfile();81 fs.writeFileSync(inputFile, 'howdy');82 const {stdout} = await execa('stdin.js', {inputFile});83 t.is(stdout, 'howdy');84});8586test('inputFile can be set with $', async t => {87 const inputFile = tempfile();88 fs.writeFileSync(inputFile, 'howdy');89 const {stdout} = await $({inputFile})`stdin.js`;90 t.is(stdout, 'howdy');91});9293test('inputFile and input cannot be both set', t => {94 t.throws(() => execa('stdin.js', {inputFile: '', input: ''}), {95 message: /cannot be both set/,96 });
10 unmodified lines
107 const {stdout} = execaSync('stdin.js', {input: 'foobar'});108 t.is(stdout, 'foobar');109});110111test('input option can be used with $.sync', t => {112 const {stdout} = $({input: 'foobar'}).sync`stdin.js`;113 t.is(stdout, 'foobar');114});115116test('input option can be a Buffer - sync', t => {117 const {stdout} = execaSync('stdin.js', {input: Buffer.from('testing12', 'utf8')});118 t.is(stdout, 'testing12');119});
21 unmodified lines
141 const {stdout} = execaSync('stdin.js', {inputFile});142 t.is(stdout, 'howdy');143});144145test('inputFile option can be used with $.sync', t => {146 const inputFile = tempfile();147 fs.writeFileSync(inputFile, 'howdy');148 const {stdout} = $({inputFile}).sync`stdin.js`;149 t.is(stdout, 'howdy');150});151152test('inputFile and input cannot be both set - sync', t => {153 t.throws(() => execaSync('stdin.js', {inputFile: '', input: ''}), {154 message: /cannot be both set/,155 });
101 unmodified lines
modified:test/test.js+4 added
98 unmodified lines
99test('preferLocal: undefined with $', async t => {100 await t.notThrowsAsync($({env: getPathWithoutLocalDir()})`ava --version`);101});102103test('preferLocal: undefined with $.sync', t => {104 t.notThrows(() => $({env: getPathWithoutLocalDir()}).sync`ava --version`);105});106107test('localDir option', async t => {108 const command = process.platform === 'win32' ? 'echo %PATH%' : 'echo $PATH';109 const {stdout} = await execa(command, {shell: true, preferLocal: true, localDir: '/test'});110 const envPaths = stdout.split(path.delimiter);
153 unmodified lines
modified:index.d.ts+2 added-2 removed
25 unmodified lines
26 Prefer locally installed binaries when looking for a binary to execute.2728 If you `$ npm install foo`, you can then `execa('foo')`.2930 @default `true` with `$`/`$.sync`, `false` otherwise30 @default `true` with `$`, `false` otherwise31 */32 readonly preferLocal?: boolean;3334 /**
27 unmodified lines
6263 /**64 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).6566 @default 'pipe'66 @default `inherit` with `$`, `pipe` otherwise67 */68 readonly stdin?: StdioOption;6970 /**
860 unmodified lines
modified:index.js+13 added-3 removed
230 unmodified lines
231 killed: false,232 };233}234235const normalizeScriptStdin = ({input, inputFile, stdio}) => input === undefined && inputFile === undefined && stdio === undefined236 ? {stdin: 'inherit'}237 : {};238239const normalizeScriptOptions = (options = {}) => ({240 preferLocal: true,241 ...normalizeScriptStdin(options),242 ...options,243});244245function create$(options) {246 function $(templatesOrOptions, ...expressions) {247 if (!Array.isArray(templatesOrOptions)) {248 return create$({...options, ...templatesOrOptions});249 }250251 const [file, ...args] = parseTemplates(templatesOrOptions, expressions);242 return execa(file, args, options);252 return execa(file, args, normalizeScriptOptions(options));253 }254255 $.sync = (templates, ...expressions) => {256 if (!Array.isArray(templates)) {257 throw new TypeError('Please use $(options).sync`command` instead of $.sync(options)`command`.');258 }259260 const [file, ...args] = parseTemplates(templates, expressions);251 return execaSync(file, args, options);261 return execaSync(file, args, normalizeScriptOptions(options));262 };263264 return $;265}266257export const $ = create$({preferLocal: true});267export const $ = create$();268269export function execaCommand(command, options) {270 const [file, ...args] = parseCommand(command);271 return execa(file, args, options);
38 unmodified lines
modified:readme.md+2 added-2 removed
465 unmodified lines
466467#### preferLocal468469Type: `boolean`\470Default: `true` with [`$`](#command)/[`$.sync`](#synccommand), `false` otherwise470Default: `true` with [`$`](#command), `false` otherwise471472Prefer locally installed binaries when looking for a binary to execute.\473If you `$ npm install foo`, you can then `execa('foo')`.474
45 unmodified lines
520521#### stdin522523Type: `string | number | Stream | undefined`\524Default: `pipe`524Default: `inherit` with [`$`](#command), `pipe` otherwise525526Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).527528#### stdout
258 unmodified lines