Skip to content
Myr

Refactor logic and rename some variables (#983)

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

authored by ehmicky · committed by GitHub · parent 08eb743 · Browse files · 19 files+292 added−260 removed
This commit

State

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

Diff stats

19 files+292 added−260 removed

  • tests:+212 added−197 removed
  • other:+80 added−63 removed

19 files+292 added−260 removed

modified:lib/arguments/fd-options.js+2 added-2 removed
modified:lib/arguments/options.js+1 added-1 removed
modified:lib/convert/iterable.js+2 added-2 removed
1import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';2import {getReadable} from '../arguments/fd-options.js';2import {getFromStream} from '../arguments/fd-options.js';3import {iterateOnSubprocessStream} from '../io/iterate.js';45export const createIterable = (subprocess, encoding, {6 from,7 binary: binaryOption = false,8 preserveNewlines = false,9} = {}) => {10 const binary = binaryOption || BINARY_ENCODINGS.has(encoding);11 const subprocessStdout = getReadable(subprocess, from);11 const subprocessStdout = getFromStream(subprocess, from);12 const onStdoutData = iterateOnSubprocessStream({13 subprocessStdout,14 subprocess,15 binary,
18 unmodified lines
modified:lib/convert/readable.js+2 added-2 removed
1import {Readable} from 'node:stream';2import {callbackify} from 'node:util';3import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';4import {getReadable} from '../arguments/fd-options.js';4import {getFromStream} from '../arguments/fd-options.js';5import {iterateOnSubprocessStream, DEFAULT_OBJECT_HIGH_WATER_MARK} from '../io/iterate.js';6import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';7import {8 createDeferred,
32 unmodified lines
41};4243// Retrieve `stdout` (or other stream depending on `from`)44export const getSubprocessStdout = (subprocess, from, concurrentStreams) => {45 const subprocessStdout = getReadable(subprocess, from);45 const subprocessStdout = getFromStream(subprocess, from);46 const waitReadableDestroy = addConcurrentStream(concurrentStreams, subprocessStdout, 'readableDestroy');47 return {subprocessStdout, waitReadableDestroy};48};49
64 unmodified lines
modified:lib/convert/writable.js+2 added-2 removed
1import {Writable} from 'node:stream';2import {callbackify} from 'node:util';3import {getWritable} from '../arguments/fd-options.js';3import {getToStream} from '../arguments/fd-options.js';4import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';5import {6 safeWaitForSubprocessStdout,7 waitForSubprocessStdin,
20 unmodified lines
28};2930// Retrieve `stdin` (or other stream depending on `to`)31export const getSubprocessStdin = (subprocess, to, concurrentStreams) => {32 const subprocessStdin = getWritable(subprocess, to);32 const subprocessStdin = getToStream(subprocess, to);33 const waitWritableFinal = addConcurrentStream(concurrentStreams, subprocessStdin, 'writableFinal');34 const waitWritableDestroy = addConcurrentStream(concurrentStreams, subprocessStdin, 'writableDestroy');35 return {subprocessStdin, waitWritableFinal, waitWritableDestroy};36};
54 unmodified lines
modified:lib/io/max-buffer.js+11 added-1 removed
49 unmodified lines
50export const isMaxBufferSync = (resultError, output, maxBuffer) => resultError?.code === 'ENOBUFS'51 && output !== null52 && output.some(result => result !== null && result.length > getMaxBufferSync(maxBuffer));5354// When `maxBuffer` is hit, ensure the result is truncated55export const truncateMaxBufferSync = (result, isMaxBuffer, maxBuffer) => {56 if (!isMaxBuffer) {57 return result;58 }5960 const maxBufferValue = getMaxBufferSync(maxBuffer);61 return result.length > maxBufferValue ? result.slice(0, maxBufferValue) : result;62};6364// `spawnSync()` does not allow differentiating `maxBuffer` per file descriptor, so we always use `stdout`55export const getMaxBufferSync = maxBuffer => maxBuffer[1];65export const getMaxBufferSync = ([, stdoutMaxBuffer]) => stdoutMaxBuffer;
modified:lib/io/output-sync.js+2 added-6 removed
2 unmodified lines
3import {runGeneratorsSync} from '../transform/generator.js';4import {splitLinesSync} from '../transform/split.js';5import {joinToString, joinToUint8Array, bufferToUint8Array} from '../utils/uint-array.js';6import {FILE_TYPES} from '../stdio/type.js';7import {getMaxBufferSync} from './max-buffer.js';7import {truncateMaxBufferSync} from './max-buffer.js';89// Apply `stdout`/`stderr` options, after spawning, in sync mode10export const transformOutputSync = ({fileDescriptors, syncResult: {output}, options, isMaxBuffer, verboseInfo}) => {11 if (output === null) {
17 unmodified lines
29 if (result === null) {30 return;31 }3233 const truncatedResult = truncateResult(result, isMaxBuffer, getMaxBufferSync(maxBuffer));33 const truncatedResult = truncateMaxBufferSync(result, isMaxBuffer, maxBuffer);34 const uint8ArrayResult = bufferToUint8Array(truncatedResult);35 const {stdioItems, objectMode} = fileDescriptors[fdNumber];36 const chunks = runOutputGeneratorsSync([uint8ArrayResult], stdioItems, encoding, state);37 const {serializedResult, finalResult = serializedResult} = serializeChunks({
28 unmodified lines
66 return returnedResult;67 }68};6970const truncateResult = (result, isMaxBuffer, maxBuffer) => isMaxBuffer && result.length > maxBuffer71 ? result.slice(0, maxBuffer)72 : result;7370const runOutputGeneratorsSync = (chunks, stdioItems, encoding, state) => {71 try {72 return runGeneratorsSync(chunks, stdioItems, encoding, false);73 } catch (error) {
29 unmodified lines
added:lib/methods/bind.js+23 added
1import isPlainObject from 'is-plain-obj';2import {FD_SPECIFIC_OPTIONS} from '../arguments/specific.js';34// Deep merge specific options like `env`. Shallow merge the other ones.5export const mergeOptions = (boundOptions, options) => {6 const newOptions = Object.fromEntries(7 Object.entries(options).map(([optionName, optionValue]) => [8 optionName,9 mergeOption(optionName, boundOptions[optionName], optionValue),10 ]),11 );12 return {...boundOptions, ...newOptions};13};1415const mergeOption = (optionName, boundOptionValue, optionValue) => {16 if (DEEP_OPTIONS.has(optionName) && isPlainObject(boundOptionValue) && isPlainObject(optionValue)) {17 return {...boundOptionValue, ...optionValue};18 }1920 return optionValue;21};2223const DEEP_OPTIONS = new Set(['env', ...FD_SPECIFIC_OPTIONS]);
modified:lib/methods/create.js+1 added-22 removed
1import isPlainObject from 'is-plain-obj';2import {FD_SPECIFIC_OPTIONS} from '../arguments/specific.js';2import {normalizeParameters} from './parameters.js';3import {isTemplateString, parseTemplates} from './template.js';4import {execaCoreSync} from './main-sync.js';5import {execaCoreAsync} from './main-async.js';6import {mergeOptions} from './bind.js';78export const createExeca = (mapArguments, boundOptions, deepOptions, setBoundExeca) => {9 const createNested = (mapArguments, boundOptions, setBoundExeca) => createExeca(mapArguments, boundOptions, deepOptions, setBoundExeca);10 const boundExeca = (...execaArguments) => callBoundExeca({
46 unmodified lines
57 options,58 isSync,59 };60};6162// Deep merge specific options like `env`. Shallow merge the other ones.63const mergeOptions = (boundOptions, options) => {64 const newOptions = Object.fromEntries(65 Object.entries(options).map(([optionName, optionValue]) => [66 optionName,67 mergeOption(optionName, boundOptions[optionName], optionValue),68 ]),69 );70 return {...boundOptions, ...newOptions};71};7273const mergeOption = (optionName, boundOptionValue, optionValue) => {74 if (DEEP_OPTIONS.has(optionName) && isPlainObject(boundOptionValue) && isPlainObject(optionValue)) {75 return {...boundOptionValue, ...optionValue};76 }7778 return optionValue;79};8081const DEEP_OPTIONS = new Set(['env', ...FD_SPECIFIC_OPTIONS]);
modified:lib/methods/main-async.js+2 added-2 removed
1import {setMaxListeners} from 'node:events';2import {spawn} from 'node:child_process';3import {MaxBufferError} from 'get-stream';4import {handleCommand} from '../arguments/command.js';5import {handleOptions} from '../arguments/options.js';5import {normalizeOptions} from '../arguments/options.js';6import {SUBPROCESS_OPTIONS} from '../arguments/fd-options.js';7import {makeError, makeSuccessResult} from '../return/result.js';8import {handleResult} from '../return/reject.js';9import {handleEarlyError} from '../return/early-error.js';
35 unmodified lines
45const handleAsyncArguments = (rawFile, rawArguments, rawOptions) => {46 const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArguments, rawOptions);4748 try {49 const {file, commandArguments, options: normalizedOptions} = handleOptions(rawFile, rawArguments, rawOptions);49 const {file, commandArguments, options: normalizedOptions} = normalizeOptions(rawFile, rawArguments, rawOptions);50 const options = handleAsyncOptions(normalizedOptions);51 const fileDescriptors = handleStdioAsync(options, verboseInfo);52 return {53 file,
124 unmodified lines
modified:lib/methods/main-sync.js+2 added-2 removed
1import {spawnSync} from 'node:child_process';2import {handleCommand} from '../arguments/command.js';3import {handleOptions} from '../arguments/options.js';3import {normalizeOptions} from '../arguments/options.js';4import {makeError, makeEarlyError, makeSuccessResult} from '../return/result.js';5import {handleResult} from '../return/reject.js';6import {handleStdioSync} from '../stdio/handle-sync.js';7import {stripNewline} from '../io/strip-newline.js';
23 unmodified lines
31 const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArguments, rawOptions);3233 try {34 const syncOptions = normalizeSyncOptions(rawOptions);35 const {file, commandArguments, options} = handleOptions(rawFile, rawArguments, syncOptions);35 const {file, commandArguments, options} = normalizeOptions(rawFile, rawArguments, syncOptions);36 validateSyncOptions(options);37 const fileDescriptors = handleStdioSync(options, verboseInfo);38 return {39 file,
117 unmodified lines
modified:lib/methods/template.js+22 added-17 removed
1import {ChildProcess} from 'node:child_process';2import isPlainObject from 'is-plain-obj';3import {isUint8Array, uint8ArrayToString} from '../utils/uint-array.js';45export const isTemplateString = templates => Array.isArray(templates) && Array.isArray(templates.raw);6
109 unmodified lines
116 if (typeOfExpression === 'number') {117 return String(expression);118 }119119 if (120 typeOfExpression === 'object'121 && expression !== null122 && !isSubprocess(expression)123 && 'stdout' in expression124 ) {125 const typeOfStdout = typeof expression.stdout;120 if (isPlainObject(expression) && 'stdout' in expression) {121 return getSubprocessResult(expression);122 }123127 if (typeOfStdout === 'string') {128 return expression.stdout;129 }130131 if (isUint8Array(expression.stdout)) {132 return uint8ArrayToString(expression.stdout);133 }134135 throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);124 if (expression instanceof ChildProcess || Object.prototype.toString.call(expression) === '[object Promise]') {125 // eslint-disable-next-line no-template-curly-in-string126 throw new TypeError('Unexpected subprocess in template expression. Please use ${await subprocess} instead of ${subprocess}.');127 }128129 throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);130};131141const isSubprocess = value => value instanceof ChildProcess;132const getSubprocessResult = ({stdout}) => {133 if (typeof stdout === 'string') {134 return stdout;135 }136137 if (isUint8Array(stdout)) {138 return uint8ArrayToString(stdout);139 }140141 if (stdout === undefined) {142 throw new TypeError('Missing result.stdout in template expression. This is probably due to the previous subprocess\' "stdout" option.');143 }144145 throw new TypeError(`Unexpected "${typeof stdout}" stdout in template expression`);146};
modified:lib/pipe/pipe-arguments.js+3 added-3 removed
1import {normalizeParameters} from '../methods/parameters.js';2import {getStartTime} from '../return/duration.js';3import {SUBPROCESS_OPTIONS, getWritable, getReadable} from '../arguments/fd-options.js';3import {SUBPROCESS_OPTIONS, getToStream, getFromStream} from '../arguments/fd-options.js';45export const normalizePipeArguments = ({source, sourcePromise, boundOptions, createNested}, ...pipeArguments) => {6 const startTime = getStartTime();7 const {
24 unmodified lines
32 const {33 destination,34 pipeOptions: {from, to, unpipeSignal} = {},35 } = getDestination(boundOptions, createNested, ...pipeArguments);36 const destinationStream = getWritable(destination, to);36 const destinationStream = getToStream(destination, to);37 return {38 destination,39 destinationStream,40 from,
34 unmodified lines
75const mapDestinationArguments = ({options}) => ({options: {...options, stdin: 'pipe', piped: true}});7677const getSourceStream = (source, from) => {78 try {79 const sourceStream = getReadable(source, from);79 const sourceStream = getFromStream(source, from);80 return {sourceStream};81 } catch (error) {82 return {sourceError: error};83 }
1 unmodified line
modified:lib/terminate/kill.js+5 added-1 removed
19 unmodified lines
2021const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;2223// Monkey-patches `subprocess.kill()` to add `forceKillAfterDelay` behavior and `.kill(error)`24export const subprocessKill = ({kill, subprocess, options: {forceKillAfterDelay, killSignal}, controller}, signalOrError, errorArgument) => {24export const subprocessKill = (25 {kill, subprocess, options: {forceKillAfterDelay, killSignal}, controller},26 signalOrError,27 errorArgument,28) => {29 const {signal, error} = parseKillArguments(signalOrError, errorArgument, killSignal);30 emitKillError(subprocess, error);31 const killResult = kill(signal);32 setKillTimeout({
55 unmodified lines
added:test/methods/bind.js+109 added
1import {join} from 'node:path';2import test from 'ava';3import {4 execa,5 execaSync,6 execaNode,7 $,8} from '../../index.js';9import {foobarString, foobarUppercase} from '../helpers/input.js';10import {uppercaseGenerator} from '../helpers/generator.js';11import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';1213setFixtureDirectory();1415const NOOP_PATH = join(FIXTURES_DIRECTORY, 'noop.js');16const PRINT_ENV_PATH = join(FIXTURES_DIRECTORY, 'environment.js');1718const testBindOptions = async (t, execaMethod) => {19 const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString]);20 t.is(stdout, `${foobarString}\n`);21};2223test('execa() can bind options', testBindOptions, execa);24test('execaNode() can bind options', testBindOptions, execaNode);25test('$ can bind options', testBindOptions, $);2627const testBindOptionsSync = (t, execaMethod) => {28 const {stdout} = execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString]);29 t.is(stdout, `${foobarString}\n`);30};3132test('execaSync() can bind options', testBindOptionsSync, execaSync);33test('$.sync can bind options', testBindOptionsSync, $.sync);3435const testBindPriority = async (t, execaMethod) => {36 const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString], {stripFinalNewline: true});37 t.is(stdout, foobarString);38};3940test('execa() bound options have lower priority', testBindPriority, execa);41test('execaSync() bound options have lower priority', testBindPriority, execaSync);42test('execaNode() bound options have lower priority', testBindPriority, execaNode);43test('$ bound options have lower priority', testBindPriority, $);44test('$.sync bound options have lower priority', testBindPriority, $.sync);4546const testBindUndefined = async (t, execaMethod) => {47 const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString], {stripFinalNewline: undefined});48 t.is(stdout, foobarString);49};5051test('execa() undefined options use default value', testBindUndefined, execa);52test('execaSync() undefined options use default value', testBindUndefined, execaSync);53test('execaNode() undefined options use default value', testBindUndefined, execaNode);54test('$ undefined options use default value', testBindUndefined, $);55test('$.sync undefined options use default value', testBindUndefined, $.sync);5657const testMergeEnv = async (t, execaMethod) => {58 const {stdout} = await execaMethod({env: {FOO: 'foo'}})(PRINT_ENV_PATH, {env: {BAR: 'bar'}});59 t.is(stdout, 'foo\nbar');60};6162test('execa() bound options are merged', testMergeEnv, execa);63test('execaSync() bound options are merged', testMergeEnv, execaSync);64test('execaNode() bound options are merged', testMergeEnv, execaNode);65test('$ bound options are merged', testMergeEnv, $);66test('$.sync bound options are merged', testMergeEnv, $.sync);6768const testMergeMultiple = async (t, execaMethod) => {69 const {stdout} = await execaMethod({env: {FOO: 'baz'}})({env: {BAR: 'bar'}})(PRINT_ENV_PATH, {env: {FOO: 'foo'}});70 t.is(stdout, 'foo\nbar');71};7273test('execa() bound options are merged multiple times', testMergeMultiple, execa);74test('execaSync() bound options are merged multiple times', testMergeMultiple, execaSync);75test('execaNode() bound options are merged multiple times', testMergeMultiple, execaNode);76test('$ bound options are merged multiple times', testMergeMultiple, $);77test('$.sync bound options are merged multiple times', testMergeMultiple, $.sync);7879const testMergeFdSpecific = async (t, execaMethod) => {80 const {isMaxBuffer, shortMessage} = await t.throwsAsync(execaMethod({maxBuffer: {stdout: 1}})(NOOP_PATH, [foobarString], {maxBuffer: {stderr: 100}}));81 t.true(isMaxBuffer);82 t.true(shortMessage.includes('Command\'s stdout was larger than 1'));83};8485test('execa() bound options merge fd-specific ones', testMergeFdSpecific, execa);86test('execaNode() bound options merge fd-specific ones', testMergeFdSpecific, execaNode);87test('$ bound options merge fd-specific ones', testMergeFdSpecific, $);8889const testMergeEnvUndefined = async (t, execaMethod) => {90 const {stdout} = await execaMethod({env: {FOO: 'foo'}})(PRINT_ENV_PATH, {env: {BAR: undefined}});91 t.is(stdout, 'foo\nundefined');92};9394test('execa() bound options are merged even if undefined', testMergeEnvUndefined, execa);95test('execaSync() bound options are merged even if undefined', testMergeEnvUndefined, execaSync);96test('execaNode() bound options are merged even if undefined', testMergeEnvUndefined, execaNode);97test('$ bound options are merged even if undefined', testMergeEnvUndefined, $);98test('$.sync bound options are merged even if undefined', testMergeEnvUndefined, $.sync);99100const testMergeSpecific = async (t, execaMethod) => {101 const {stdout} = await execaMethod({stdout: {transform: uppercaseGenerator().transform, objectMode: true}})(NOOP_PATH, {stdout: {transform: uppercaseGenerator().transform}});102 t.is(stdout, foobarUppercase);103};104105test('execa() bound options only merge specific ones', testMergeSpecific, execa);106test('execaSync() bound options only merge specific ones', testMergeSpecific, execaSync);107test('execaNode() bound options only merge specific ones', testMergeSpecific, execaNode);108test('$ bound options only merge specific ones', testMergeSpecific, $);109test('$.sync bound options only merge specific ones', testMergeSpecific, $.sync);
deleted:test/methods/create-bind.js-109 removed
1import {join} from 'node:path';2import test from 'ava';3import {4 execa,5 execaSync,6 execaNode,7 $,8} from '../../index.js';9import {foobarString, foobarUppercase} from '../helpers/input.js';10import {uppercaseGenerator} from '../helpers/generator.js';11import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';1213setFixtureDirectory();1415const NOOP_PATH = join(FIXTURES_DIRECTORY, 'noop.js');16const PRINT_ENV_PATH = join(FIXTURES_DIRECTORY, 'environment.js');1718const testBindOptions = async (t, execaMethod) => {19 const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString]);20 t.is(stdout, `${foobarString}\n`);21};2223test('execa() can bind options', testBindOptions, execa);24test('execaNode() can bind options', testBindOptions, execaNode);25test('$ can bind options', testBindOptions, $);2627const testBindOptionsSync = (t, execaMethod) => {28 const {stdout} = execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString]);29 t.is(stdout, `${foobarString}\n`);30};3132test('execaSync() can bind options', testBindOptionsSync, execaSync);33test('$.sync can bind options', testBindOptionsSync, $.sync);3435const testBindPriority = async (t, execaMethod) => {36 const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString], {stripFinalNewline: true});37 t.is(stdout, foobarString);38};3940test('execa() bound options have lower priority', testBindPriority, execa);41test('execaSync() bound options have lower priority', testBindPriority, execaSync);42test('execaNode() bound options have lower priority', testBindPriority, execaNode);43test('$ bound options have lower priority', testBindPriority, $);44test('$.sync bound options have lower priority', testBindPriority, $.sync);4546const testBindUndefined = async (t, execaMethod) => {47 const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString], {stripFinalNewline: undefined});48 t.is(stdout, foobarString);49};5051test('execa() undefined options use default value', testBindUndefined, execa);52test('execaSync() undefined options use default value', testBindUndefined, execaSync);53test('execaNode() undefined options use default value', testBindUndefined, execaNode);54test('$ undefined options use default value', testBindUndefined, $);55test('$.sync undefined options use default value', testBindUndefined, $.sync);5657const testMergeEnv = async (t, execaMethod) => {58 const {stdout} = await execaMethod({env: {FOO: 'foo'}})(PRINT_ENV_PATH, {env: {BAR: 'bar'}});59 t.is(stdout, 'foo\nbar');60};6162test('execa() bound options are merged', testMergeEnv, execa);63test('execaSync() bound options are merged', testMergeEnv, execaSync);64test('execaNode() bound options are merged', testMergeEnv, execaNode);65test('$ bound options are merged', testMergeEnv, $);66test('$.sync bound options are merged', testMergeEnv, $.sync);6768const testMergeMultiple = async (t, execaMethod) => {69 const {stdout} = await execaMethod({env: {FOO: 'baz'}})({env: {BAR: 'bar'}})(PRINT_ENV_PATH, {env: {FOO: 'foo'}});70 t.is(stdout, 'foo\nbar');71};7273test('execa() bound options are merged multiple times', testMergeMultiple, execa);74test('execaSync() bound options are merged multiple times', testMergeMultiple, execaSync);75test('execaNode() bound options are merged multiple times', testMergeMultiple, execaNode);76test('$ bound options are merged multiple times', testMergeMultiple, $);77test('$.sync bound options are merged multiple times', testMergeMultiple, $.sync);7879const testMergeFdSpecific = async (t, execaMethod) => {80 const {isMaxBuffer, shortMessage} = await t.throwsAsync(execaMethod({maxBuffer: {stdout: 1}})(NOOP_PATH, [foobarString], {maxBuffer: {stderr: 100}}));81 t.true(isMaxBuffer);82 t.true(shortMessage.includes('Command\'s stdout was larger than 1'));83};8485test('execa() bound options merge fd-specific ones', testMergeFdSpecific, execa);86test('execaNode() bound options merge fd-specific ones', testMergeFdSpecific, execaNode);87test('$ bound options merge fd-specific ones', testMergeFdSpecific, $);8889const testMergeEnvUndefined = async (t, execaMethod) => {90 const {stdout} = await execaMethod({env: {FOO: 'foo'}})(PRINT_ENV_PATH, {env: {BAR: undefined}});91 t.is(stdout, 'foo\nundefined');92};9394test('execa() bound options are merged even if undefined', testMergeEnvUndefined, execa);95test('execaSync() bound options are merged even if undefined', testMergeEnvUndefined, execaSync);96test('execaNode() bound options are merged even if undefined', testMergeEnvUndefined, execaNode);97test('$ bound options are merged even if undefined', testMergeEnvUndefined, $);98test('$.sync bound options are merged even if undefined', testMergeEnvUndefined, $.sync);99100const testMergeSpecific = async (t, execaMethod) => {101 const {stdout} = await execaMethod({stdout: {transform: uppercaseGenerator().transform, objectMode: true}})(NOOP_PATH, {stdout: {transform: uppercaseGenerator().transform}});102 t.is(stdout, foobarUppercase);103};104105test('execa() bound options only merge specific ones', testMergeSpecific, execa);106test('execaSync() bound options only merge specific ones', testMergeSpecific, execaSync);107test('execaNode() bound options only merge specific ones', testMergeSpecific, execaNode);108test('$ bound options only merge specific ones', testMergeSpecific, $);109test('$.sync bound options only merge specific ones', testMergeSpecific, $.sync);
deleted:test/methods/create-main.js-63 removed
1import {join} from 'node:path';2import test from 'ava';3import {4 execa,5 execaSync,6 execaNode,7 $,8} from '../../index.js';9import {foobarString, foobarArray} from '../helpers/input.js';10import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';1112setFixtureDirectory();1314const NOOP_PATH = join(FIXTURES_DIRECTORY, 'noop.js');1516const testTemplate = async (t, execaMethod) => {17 const {stdout} = await execaMethod`${NOOP_PATH} ${foobarString}`;18 t.is(stdout, foobarString);19};2021test('execa() can use template strings', testTemplate, execa);22test('execaNode() can use template strings', testTemplate, execaNode);23test('$ can use template strings', testTemplate, $);2425const testTemplateSync = (t, execaMethod) => {26 const {stdout} = execaMethod`${NOOP_PATH} ${foobarString}`;27 t.is(stdout, foobarString);28};2930test('execaSync() can use template strings', testTemplateSync, execaSync);31test('$.sync can use template strings', testTemplateSync, $.sync);3233const testTemplateOptions = async (t, execaMethod) => {34 const {stdout} = await execaMethod({stripFinalNewline: false})`${NOOP_PATH} ${foobarString}`;35 t.is(stdout, `${foobarString}\n`);36};3738test('execa() can use template strings with options', testTemplateOptions, execa);39test('execaNode() can use template strings with options', testTemplateOptions, execaNode);40test('$ can use template strings with options', testTemplateOptions, $);4142const testTemplateOptionsSync = (t, execaMethod) => {43 const {stdout} = execaMethod({stripFinalNewline: false})`${NOOP_PATH} ${foobarString}`;44 t.is(stdout, `${foobarString}\n`);45};4647test('execaSync() can use template strings with options', testTemplateOptionsSync, execaSync);48test('$.sync can use template strings with options', testTemplateOptionsSync, $.sync);4950const testSpacedCommand = async (t, commandArguments, execaMethod) => {51 const {stdout} = await execaMethod('command with space.js', commandArguments);52 const expectedStdout = commandArguments === undefined ? '' : commandArguments.join('\n');53 t.is(stdout, expectedStdout);54};5556test('allow commands with spaces and no array arguments', testSpacedCommand, undefined, execa);57test('allow commands with spaces and array arguments', testSpacedCommand, foobarArray, execa);58test('allow commands with spaces and no array arguments, execaSync', testSpacedCommand, undefined, execaSync);59test('allow commands with spaces and array arguments, execaSync', testSpacedCommand, foobarArray, execaSync);60test('allow commands with spaces and no array arguments, $', testSpacedCommand, undefined, $);61test('allow commands with spaces and array arguments, $', testSpacedCommand, foobarArray, $);62test('allow commands with spaces and no array arguments, $.sync', testSpacedCommand, undefined, $.sync);63test('allow commands with spaces and array arguments, $.sync', testSpacedCommand, foobarArray, $.sync);
added:test/methods/create.js+63 added
1import {join} from 'node:path';2import test from 'ava';3import {4 execa,5 execaSync,6 execaNode,7 $,8} from '../../index.js';9import {foobarString, foobarArray} from '../helpers/input.js';10import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';1112setFixtureDirectory();1314const NOOP_PATH = join(FIXTURES_DIRECTORY, 'noop.js');1516const testTemplate = async (t, execaMethod) => {17 const {stdout} = await execaMethod`${NOOP_PATH} ${foobarString}`;18 t.is(stdout, foobarString);19};2021test('execa() can use template strings', testTemplate, execa);22test('execaNode() can use template strings', testTemplate, execaNode);23test('$ can use template strings', testTemplate, $);2425const testTemplateSync = (t, execaMethod) => {26 const {stdout} = execaMethod`${NOOP_PATH} ${foobarString}`;27 t.is(stdout, foobarString);28};2930test('execaSync() can use template strings', testTemplateSync, execaSync);31test('$.sync can use template strings', testTemplateSync, $.sync);3233const testTemplateOptions = async (t, execaMethod) => {34 const {stdout} = await execaMethod({stripFinalNewline: false})`${NOOP_PATH} ${foobarString}`;35 t.is(stdout, `${foobarString}\n`);36};3738test('execa() can use template strings with options', testTemplateOptions, execa);39test('execaNode() can use template strings with options', testTemplateOptions, execaNode);40test('$ can use template strings with options', testTemplateOptions, $);4142const testTemplateOptionsSync = (t, execaMethod) => {43 const {stdout} = execaMethod({stripFinalNewline: false})`${NOOP_PATH} ${foobarString}`;44 t.is(stdout, `${foobarString}\n`);45};4647test('execaSync() can use template strings with options', testTemplateOptionsSync, execaSync);48test('$.sync can use template strings with options', testTemplateOptionsSync, $.sync);4950const testSpacedCommand = async (t, commandArguments, execaMethod) => {51 const {stdout} = await execaMethod('command with space.js', commandArguments);52 const expectedStdout = commandArguments === undefined ? '' : commandArguments.join('\n');53 t.is(stdout, expectedStdout);54};5556test('allow commands with spaces and no array arguments', testSpacedCommand, undefined, execa);57test('allow commands with spaces and array arguments', testSpacedCommand, foobarArray, execa);58test('allow commands with spaces and no array arguments, execaSync', testSpacedCommand, undefined, execaSync);59test('allow commands with spaces and array arguments, execaSync', testSpacedCommand, foobarArray, execaSync);60test('allow commands with spaces and no array arguments, $', testSpacedCommand, undefined, $);61test('allow commands with spaces and array arguments, $', testSpacedCommand, foobarArray, $);62test('allow commands with spaces and no array arguments, $.sync', testSpacedCommand, undefined, $.sync);63test('allow commands with spaces and array arguments, $.sync', testSpacedCommand, foobarArray, $.sync);
modified:test/methods/template.js+40 added-25 removed
295 unmodified lines
296test('$`\\t`', testEmptyScript, () => $` `);297test('$`\\n`', testEmptyScript, () => $`298`);299300const testInvalidExpression = (t, invalidExpression, execaMethod) => {301 const expression = typeof invalidExpression === 'function' ? invalidExpression() : invalidExpression;300const testInvalidExpression = (t, invalidExpression) => {301 t.throws(303 () => execaMethod`echo.js ${expression}`,302 () => $`echo.js ${invalidExpression}`,303 {message: /in template expression/},304 );305};306308test('$ throws on invalid expression - undefined', testInvalidExpression, undefined, $);309test('$ throws on invalid expression - null', testInvalidExpression, null, $);310test('$ throws on invalid expression - true', testInvalidExpression, true, $);311test('$ throws on invalid expression - {}', testInvalidExpression, {}, $);312test('$ throws on invalid expression - {foo: "bar"}', testInvalidExpression, {foo: 'bar'}, $);313test('$ throws on invalid expression - {stdout: undefined}', testInvalidExpression, {stdout: undefined}, $);314test('$ throws on invalid expression - {stdout: 1}', testInvalidExpression, {stdout: 1}, $);315test('$ throws on invalid expression - Promise.resolve()', testInvalidExpression, Promise.resolve(), $);316test('$ throws on invalid expression - Promise.resolve({stdout: "foo"})', testInvalidExpression, Promise.resolve({foo: 'bar'}), $);317test('$ throws on invalid expression - $', testInvalidExpression, () => $`noop.js`, $);318test('$ throws on invalid expression - $(options).sync', testInvalidExpression, () => $({stdio: 'ignore'}).sync`noop.js`, $);319test('$ throws on invalid expression - [undefined]', testInvalidExpression, [undefined], $);320test('$ throws on invalid expression - [null]', testInvalidExpression, [null], $);321test('$ throws on invalid expression - [true]', testInvalidExpression, [true], $);322test('$ throws on invalid expression - [{}]', testInvalidExpression, [{}], $);323test('$ throws on invalid expression - [{foo: "bar"}]', testInvalidExpression, [{foo: 'bar'}], $);324test('$ throws on invalid expression - [{stdout: undefined}]', testInvalidExpression, [{stdout: undefined}], $);325test('$ throws on invalid expression - [{stdout: 1}]', testInvalidExpression, [{stdout: 1}], $);326test('$ throws on invalid expression - [Promise.resolve()]', testInvalidExpression, [Promise.resolve()], $);327test('$ throws on invalid expression - [Promise.resolve({stdout: "foo"})]', testInvalidExpression, [Promise.resolve({stdout: 'foo'})], $);328test('$ throws on invalid expression - [$]', testInvalidExpression, () => [$`noop.js`], $);329test('$ throws on invalid expression - [$(options).sync]', testInvalidExpression, () => [$({stdio: 'ignore'}).sync`noop.js`], $);307test('$ throws on invalid expression - undefined', testInvalidExpression, undefined);308test('$ throws on invalid expression - null', testInvalidExpression, null);309test('$ throws on invalid expression - true', testInvalidExpression, true);310test('$ throws on invalid expression - {}', testInvalidExpression, {});311test('$ throws on invalid expression - {foo: "bar"}', testInvalidExpression, {foo: 'bar'});312test('$ throws on invalid expression - {stdout: 1}', testInvalidExpression, {stdout: 1});313test('$ throws on invalid expression - [undefined]', testInvalidExpression, [undefined]);314test('$ throws on invalid expression - [null]', testInvalidExpression, [null]);315test('$ throws on invalid expression - [true]', testInvalidExpression, [true]);316test('$ throws on invalid expression - [{}]', testInvalidExpression, [{}]);317test('$ throws on invalid expression - [{foo: "bar"}]', testInvalidExpression, [{foo: 'bar'}]);318test('$ throws on invalid expression - [{stdout: 1}]', testInvalidExpression, [{stdout: 1}]);319320const testMissingOutput = (t, invalidExpression) => {321 t.throws(322 () => $`echo.js ${invalidExpression()}`,323 {message: /Missing result.stdout/},324 );325};326327test('$ throws on invalid expression - {stdout: undefined}', testMissingOutput, () => ({stdout: undefined}));328test('$ throws on invalid expression - [{stdout: undefined}]', testMissingOutput, () => [{stdout: undefined}]);329test('$ throws on invalid expression - $(options).sync', testMissingOutput, () => $({stdio: 'ignore'}).sync`noop.js`);330test('$ throws on invalid expression - [$(options).sync]', testMissingOutput, () => [$({stdio: 'ignore'}).sync`noop.js`]);331332const testInvalidPromise = (t, invalidExpression) => {333 t.throws(334 () => $`echo.js ${invalidExpression()}`,335 {message: /Please use \${await subprocess}/},336 );337};338339test('$ throws on invalid expression - Promise.resolve()', testInvalidPromise, async () => ({}));340test('$ throws on invalid expression - Promise.resolve({stdout: "foo"})', testInvalidPromise, async () => ({foo: 'bar'}));341test('$ throws on invalid expression - [Promise.resolve()]', testInvalidPromise, () => [Promise.resolve()]);342test('$ throws on invalid expression - [Promise.resolve({stdout: "foo"})]', testInvalidPromise, () => [Promise.resolve({stdout: 'foo'})]);343test('$ throws on invalid expression - $', testInvalidPromise, () => $`noop.js`);344test('$ throws on invalid expression - [$]', testInvalidPromise, () => [$`noop.js`]);