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
Provenance
- Commit
6fe7e51- Tree
8233902- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
881fbad
8 files changed+67 added−9 removed
added:test/fixtures/stdin-script.js+5 added
12345#!/usr/bin/env nodeimport {$} from '../../index.js';import {FIXTURES_DIR} from '../helpers/fixtures-dir.js';
await $({stdout: 'inherit'})`node ${`${FIXTURES_DIR}/stdin.js`}`;modified:test/helpers/fixtures-dir.js+1 added-1 removed
2 unmodified lines3456778910113 unmodified lines2 unmodified linesimport {fileURLToPath} from 'node:url';import pathKey from 'path-key';
export const PATH_KEY = pathKey();const FIXTURES_DIR = fileURLToPath(new URL('../fixtures', import.meta.url));export const FIXTURES_DIR = fileURLToPath(new URL('../fixtures', import.meta.url));
// Add the fixtures directory to PATH so fixtures can be executed without adding// `node`. This is only meant to make writing tests simpler.export const setFixtureDir = () => {3 unmodified linesmodified:test/command.js+15 added
1 import {inspect} from 'node:util';2 import test from 'ava';3+import {isStream} from 'is-stream';4 import {execa, execaSync, execaCommand, execaCommandSync, $} from '../index.js';5 import {setFixtureDir} from './helpers/fixtures-dir.js';6 7 setFixtureDir();265 unmodified lines273 test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');274 275 test('$({stdio: \'inherit\'}).sync`noop.js`', invalidExpression, $({stdio: 'inherit'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');276 test('[ $({stdio: \'inherit\'}).sync`noop.js` ]', invalidExpression, [$({stdio: 'inherit'}).sync`noop.js`], 'Unexpected "undefined" stdout in template expression');277+278+test('$ stdin defaults to "inherit"', async t => {279+ const {stdout} = await $({input: 'foo'})`stdin-script.js`;280+ t.is(stdout, 'foo');281+});282+283+test('$.sync stdin defaults to "inherit"', t => {284+ const {stdout} = $({input: 'foo'}).sync`stdin-script.js`;285+ t.is(stdout, 'foo');286+});287+288+test('$ 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 lines5 import test from 'ava';6 import getStream from 'get-stream';7 import {pEvent} from 'p-event';8 import tempfile from 'tempfile';9-import {execa, execaSync} from '../index.js';9+import {execa, execaSync, $} from '../index.js';10 import {setFixtureDir} from './helpers/fixtures-dir.js';11 12 setFixtureDir();13 56 unmodified lines70 const {stdout} = await execa('stdin.js', {input: stream});71 t.is(stdout, 'howdy');72 });73 74+test('input option can be used with $', async t => {75+ const {stdout} = await $({input: 'foobar'})`stdin.js`;76+ t.is(stdout, 'foobar');77+});78+79 test('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 });85 86+test('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+});92+93 test('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 lines107 const {stdout} = execaSync('stdin.js', {input: 'foobar'});108 t.is(stdout, 'foobar');109 });110 111+test('input option can be used with $.sync', t => {112+ const {stdout} = $({input: 'foobar'}).sync`stdin.js`;113+ t.is(stdout, 'foobar');114+});115+116 test('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 lines141 const {stdout} = execaSync('stdin.js', {inputFile});142 t.is(stdout, 'howdy');143 });144 145+test('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+});151+152 test('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 linesmodified:test/test.js+4 added
98 unmodified lines99 test('preferLocal: undefined with $', async t => {100 await t.notThrowsAsync($({env: getPathWithoutLocalDir()})`ava --version`);101 });102 103+test('preferLocal: undefined with $.sync', t => {104+ t.notThrows(() => $({env: getPathWithoutLocalDir()}).sync`ava --version`);105+});106+107 test('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 linesmodified:index.d.ts+2 added-2 removed
25 unmodified lines26 Prefer locally installed binaries when looking for a binary to execute.27 28 If you `$ npm install foo`, you can then `execa('foo')`.29 30- @default `true` with `$`/`$.sync`, `false` otherwise30+ @default `true` with `$`, `false` otherwise31 */32 readonly preferLocal?: boolean;33 34 /**27 unmodified lines62 63 /**64 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).65 66- @default 'pipe'66+ @default `inherit` with `$`, `pipe` otherwise67 */68 readonly stdin?: StdioOption;69 70 /**860 unmodified linesmodified:index.js+13 added-3 removed
230 unmodified lines231 killed: false,232 };233 }234 235+const normalizeScriptStdin = ({input, inputFile, stdio}) => input === undefined && inputFile === undefined && stdio === undefined236+ ? {stdin: 'inherit'}237+ : {};238+239+const normalizeScriptOptions = (options = {}) => ({240+ preferLocal: true,241+ ...normalizeScriptStdin(options),242+ ...options,243+});244+245 function create$(options) {246 function $(templatesOrOptions, ...expressions) {247 if (!Array.isArray(templatesOrOptions)) {248 return create$({...options, ...templatesOrOptions});249 }250 251 const [file, ...args] = parseTemplates(templatesOrOptions, expressions);242- return execa(file, args, options);252+ return execa(file, args, normalizeScriptOptions(options));253 }254 255 $.sync = (templates, ...expressions) => {256 if (!Array.isArray(templates)) {257 throw new TypeError('Please use $(options).sync`command` instead of $.sync(options)`command`.');258 }259 260 const [file, ...args] = parseTemplates(templates, expressions);251- return execaSync(file, args, options);261+ return execaSync(file, args, normalizeScriptOptions(options));262 };263 264 return $;265 }266 257-export const $ = create$({preferLocal: true});267+export const $ = create$();268 269 export function execaCommand(command, options) {270 const [file, ...args] = parseCommand(command);271 return execa(file, args, options);38 unmodified linesmodified:readme.md+2 added-2 removed
465 unmodified lines466 467 #### preferLocal468 469 Type: `boolean`\470-Default: `true` with [`$`](#command)/[`$.sync`](#synccommand), `false` otherwise470+Default: `true` with [`$`](#command), `false` otherwise471 472 Prefer locally installed binaries when looking for a binary to execute.\473 If you `$ npm install foo`, you can then `execa('foo')`.474 45 unmodified lines520 521 #### stdin522 523 Type: `string | number | Stream | undefined`\524-Default: `pipe`524+Default: `inherit` with [`$`](#command), `pipe` otherwise525 526 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).527 528 #### stdout258 unmodified lines