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
Provenance
- Commit
bd3b478- Tree
611f92b- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
08eb743
19 files changed+292 added−260 removed
modified:lib/arguments/fd-options.js+2 added-2 removed
123345674 unmodified lines1213141516161718192085 unmodified linesimport {parseFd} from './specific.js';
export const getWritable = (destination, to = 'stdin') => {export const getToStream = (destination, to = 'stdin') => { const isWritable = true; const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(destination); const fdNumber = getFdNumber(fileDescriptors, to, isWritable); const destinationStream = destination.stdio[fdNumber];4 unmodified lines
return destinationStream;};
export const getReadable = (source, from = 'stdout') => {export const getFromStream = (source, from = 'stdout') => { const isWritable = false; const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(source); const fdNumber = getFdNumber(fileDescriptors, from, isWritable); const sourceStream = fdNumber === 'all' ? source.all : source.stdio[fdNumber];85 unmodified linesmodified:lib/arguments/options.js+1 added-1 removed
8 unmodified lines910111213131415161765 unmodified lines8 unmodified linesimport {normalizeCwd} from './cwd.js';import {normalizeFileUrl} from './file-url.js';import {normalizeFdSpecificOptions} from './specific.js';
export const handleOptions = (filePath, rawArguments, rawOptions) => {export const normalizeOptions = (filePath, rawArguments, rawOptions) => { rawOptions.cwd = normalizeCwd(rawOptions.cwd); const [processedFile, processedArguments, processedOptions] = handleNodeOption(filePath, rawArguments, rawOptions);
const {command: file, args: commandArguments, options: initialOptions} = crossSpawn._parse(processedFile, processedArguments, processedOptions);65 unmodified linesmodified:lib/convert/iterable.js+2 added-2 removed
1 import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';2-import {getReadable} from '../arguments/fd-options.js';2+import {getFromStream} from '../arguments/fd-options.js';3 import {iterateOnSubprocessStream} from '../io/iterate.js';4 5 export 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 linesmodified:lib/convert/readable.js+2 added-2 removed
1 import {Readable} from 'node:stream';2 import {callbackify} from 'node:util';3 import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';4-import {getReadable} from '../arguments/fd-options.js';4+import {getFromStream} from '../arguments/fd-options.js';5 import {iterateOnSubprocessStream, DEFAULT_OBJECT_HIGH_WATER_MARK} from '../io/iterate.js';6 import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';7 import {8 createDeferred,32 unmodified lines41 };42 43 // Retrieve `stdout` (or other stream depending on `from`)44 export 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 linesmodified:lib/convert/writable.js+2 added-2 removed
1 import {Writable} from 'node:stream';2 import {callbackify} from 'node:util';3-import {getWritable} from '../arguments/fd-options.js';3+import {getToStream} from '../arguments/fd-options.js';4 import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';5 import {6 safeWaitForSubprocessStdout,7 waitForSubprocessStdin,20 unmodified lines28 };29 30 // Retrieve `stdin` (or other stream depending on `to`)31 export 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 linesmodified:lib/io/max-buffer.js+11 added-1 removed
49 unmodified lines50 export const isMaxBufferSync = (resultError, output, maxBuffer) => resultError?.code === 'ENOBUFS'51 && output !== null52 && output.some(result => result !== null && result.length > getMaxBufferSync(maxBuffer));53 54+// When `maxBuffer` is hit, ensure the result is truncated55+export const truncateMaxBufferSync = (result, isMaxBuffer, maxBuffer) => {56+ if (!isMaxBuffer) {57+ return result;58+ }59+60+ const maxBufferValue = getMaxBufferSync(maxBuffer);61+ return result.length > maxBufferValue ? result.slice(0, maxBufferValue) : result;62+};63+64 // `spawnSync()` does not allow differentiating `maxBuffer` per file descriptor, so we always use `stdout`55-export const getMaxBufferSync = maxBuffer => maxBuffer[1];65+export const getMaxBufferSync = ([, stdoutMaxBuffer]) => stdoutMaxBuffer;modified:lib/io/output-sync.js+2 added-6 removed
2 unmodified lines3 import {runGeneratorsSync} from '../transform/generator.js';4 import {splitLinesSync} from '../transform/split.js';5 import {joinToString, joinToUint8Array, bufferToUint8Array} from '../utils/uint-array.js';6 import {FILE_TYPES} from '../stdio/type.js';7-import {getMaxBufferSync} from './max-buffer.js';7+import {truncateMaxBufferSync} from './max-buffer.js';8 9 // Apply `stdout`/`stderr` options, after spawning, in sync mode10 export const transformOutputSync = ({fileDescriptors, syncResult: {output}, options, isMaxBuffer, verboseInfo}) => {11 if (output === null) {17 unmodified lines29 if (result === null) {30 return;31 }32 33- 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 lines66 return returnedResult;67 }68 };69 70-const truncateResult = (result, isMaxBuffer, maxBuffer) => isMaxBuffer && result.length > maxBuffer71- ? result.slice(0, maxBuffer)72- : result;73-70 const runOutputGeneratorsSync = (chunks, stdioItems, encoding, state) => {71 try {72 return runGeneratorsSync(chunks, stdioItems, encoding, false);73 } catch (error) {29 unmodified linesadded:lib/methods/bind.js+23 added
1+import isPlainObject from 'is-plain-obj';2+import {FD_SPECIFIC_OPTIONS} from '../arguments/specific.js';3+4+// Deep merge specific options like `env`. Shallow merge the other ones.5+export 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+};14+15+const mergeOption = (optionName, boundOptionValue, optionValue) => {16+ if (DEEP_OPTIONS.has(optionName) && isPlainObject(boundOptionValue) && isPlainObject(optionValue)) {17+ return {...boundOptionValue, ...optionValue};18+ }19+20+ return optionValue;21+};22+23+const DEEP_OPTIONS = new Set(['env', ...FD_SPECIFIC_OPTIONS]);modified:lib/methods/create.js+1 added-22 removed
1 import isPlainObject from 'is-plain-obj';2-import {FD_SPECIFIC_OPTIONS} from '../arguments/specific.js';2 import {normalizeParameters} from './parameters.js';3 import {isTemplateString, parseTemplates} from './template.js';4 import {execaCoreSync} from './main-sync.js';5 import {execaCoreAsync} from './main-async.js';6+import {mergeOptions} from './bind.js';7 8 export const createExeca = (mapArguments, boundOptions, deepOptions, setBoundExeca) => {9 const createNested = (mapArguments, boundOptions, setBoundExeca) => createExeca(mapArguments, boundOptions, deepOptions, setBoundExeca);10 const boundExeca = (...execaArguments) => callBoundExeca({46 unmodified lines57 options,58 isSync,59 };60 };61-62-// Deep merge specific options like `env`. Shallow merge the other ones.63-const 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-};72-73-const mergeOption = (optionName, boundOptionValue, optionValue) => {74- if (DEEP_OPTIONS.has(optionName) && isPlainObject(boundOptionValue) && isPlainObject(optionValue)) {75- return {...boundOptionValue, ...optionValue};76- }77-78- return optionValue;79-};80-81-const DEEP_OPTIONS = new Set(['env', ...FD_SPECIFIC_OPTIONS]);modified:lib/methods/main-async.js+2 added-2 removed
1 import {setMaxListeners} from 'node:events';2 import {spawn} from 'node:child_process';3 import {MaxBufferError} from 'get-stream';4 import {handleCommand} from '../arguments/command.js';5-import {handleOptions} from '../arguments/options.js';5+import {normalizeOptions} from '../arguments/options.js';6 import {SUBPROCESS_OPTIONS} from '../arguments/fd-options.js';7 import {makeError, makeSuccessResult} from '../return/result.js';8 import {handleResult} from '../return/reject.js';9 import {handleEarlyError} from '../return/early-error.js';35 unmodified lines45 const handleAsyncArguments = (rawFile, rawArguments, rawOptions) => {46 const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArguments, rawOptions);47 48 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 linesmodified:lib/methods/main-sync.js+2 added-2 removed
1 import {spawnSync} from 'node:child_process';2 import {handleCommand} from '../arguments/command.js';3-import {handleOptions} from '../arguments/options.js';3+import {normalizeOptions} from '../arguments/options.js';4 import {makeError, makeEarlyError, makeSuccessResult} from '../return/result.js';5 import {handleResult} from '../return/reject.js';6 import {handleStdioSync} from '../stdio/handle-sync.js';7 import {stripNewline} from '../io/strip-newline.js';23 unmodified lines31 const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArguments, rawOptions);32 33 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 linesmodified:lib/methods/template.js+22 added-17 removed
1 import {ChildProcess} from 'node:child_process';2+import isPlainObject from 'is-plain-obj';3 import {isUint8Array, uint8ArrayToString} from '../utils/uint-array.js';4 5 export const isTemplateString = templates => Array.isArray(templates) && Array.isArray(templates.raw);6 109 unmodified lines116 if (typeOfExpression === 'number') {117 return String(expression);118 }119 119- 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+ }123 127- if (typeOfStdout === 'string') {128- return expression.stdout;129- }130-131- if (isUint8Array(expression.stdout)) {132- return uint8ArrayToString(expression.stdout);133- }134-135- 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 }128 129 throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);130 };131 141-const isSubprocess = value => value instanceof ChildProcess;132+const getSubprocessResult = ({stdout}) => {133+ if (typeof stdout === 'string') {134+ return stdout;135+ }136+137+ if (isUint8Array(stdout)) {138+ return uint8ArrayToString(stdout);139+ }140+141+ if (stdout === undefined) {142+ throw new TypeError('Missing result.stdout in template expression. This is probably due to the previous subprocess\' "stdout" option.');143+ }144+145+ throw new TypeError(`Unexpected "${typeof stdout}" stdout in template expression`);146+};modified:lib/pipe/pipe-arguments.js+3 added-3 removed
1 import {normalizeParameters} from '../methods/parameters.js';2 import {getStartTime} from '../return/duration.js';3-import {SUBPROCESS_OPTIONS, getWritable, getReadable} from '../arguments/fd-options.js';3+import {SUBPROCESS_OPTIONS, getToStream, getFromStream} from '../arguments/fd-options.js';4 5 export const normalizePipeArguments = ({source, sourcePromise, boundOptions, createNested}, ...pipeArguments) => {6 const startTime = getStartTime();7 const {24 unmodified lines32 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 lines75 const mapDestinationArguments = ({options}) => ({options: {...options, stdin: 'pipe', piped: true}});76 77 const 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 linemodified:lib/terminate/kill.js+5 added-1 removed
19 unmodified lines20 21 const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;22 23 // Monkey-patches `subprocess.kill()` to add `forceKillAfterDelay` behavior and `.kill(error)`24-export const subprocessKill = ({kill, subprocess, options: {forceKillAfterDelay, killSignal}, controller}, signalOrError, errorArgument) => {24+export 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 linesadded:test/methods/bind.js+109 added
1+import {join} from 'node:path';2+import test from 'ava';3+import {4+ execa,5+ execaSync,6+ execaNode,7+ $,8+} from '../../index.js';9+import {foobarString, foobarUppercase} from '../helpers/input.js';10+import {uppercaseGenerator} from '../helpers/generator.js';11+import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';12+13+setFixtureDirectory();14+15+const NOOP_PATH = join(FIXTURES_DIRECTORY, 'noop.js');16+const PRINT_ENV_PATH = join(FIXTURES_DIRECTORY, 'environment.js');17+18+const testBindOptions = async (t, execaMethod) => {19+ const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString]);20+ t.is(stdout, `${foobarString}\n`);21+};22+23+test('execa() can bind options', testBindOptions, execa);24+test('execaNode() can bind options', testBindOptions, execaNode);25+test('$ can bind options', testBindOptions, $);26+27+const testBindOptionsSync = (t, execaMethod) => {28+ const {stdout} = execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString]);29+ t.is(stdout, `${foobarString}\n`);30+};31+32+test('execaSync() can bind options', testBindOptionsSync, execaSync);33+test('$.sync can bind options', testBindOptionsSync, $.sync);34+35+const testBindPriority = async (t, execaMethod) => {36+ const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString], {stripFinalNewline: true});37+ t.is(stdout, foobarString);38+};39+40+test('execa() bound options have lower priority', testBindPriority, execa);41+test('execaSync() bound options have lower priority', testBindPriority, execaSync);42+test('execaNode() bound options have lower priority', testBindPriority, execaNode);43+test('$ bound options have lower priority', testBindPriority, $);44+test('$.sync bound options have lower priority', testBindPriority, $.sync);45+46+const testBindUndefined = async (t, execaMethod) => {47+ const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString], {stripFinalNewline: undefined});48+ t.is(stdout, foobarString);49+};50+51+test('execa() undefined options use default value', testBindUndefined, execa);52+test('execaSync() undefined options use default value', testBindUndefined, execaSync);53+test('execaNode() undefined options use default value', testBindUndefined, execaNode);54+test('$ undefined options use default value', testBindUndefined, $);55+test('$.sync undefined options use default value', testBindUndefined, $.sync);56+57+const 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+};61+62+test('execa() bound options are merged', testMergeEnv, execa);63+test('execaSync() bound options are merged', testMergeEnv, execaSync);64+test('execaNode() bound options are merged', testMergeEnv, execaNode);65+test('$ bound options are merged', testMergeEnv, $);66+test('$.sync bound options are merged', testMergeEnv, $.sync);67+68+const 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+};72+73+test('execa() bound options are merged multiple times', testMergeMultiple, execa);74+test('execaSync() bound options are merged multiple times', testMergeMultiple, execaSync);75+test('execaNode() bound options are merged multiple times', testMergeMultiple, execaNode);76+test('$ bound options are merged multiple times', testMergeMultiple, $);77+test('$.sync bound options are merged multiple times', testMergeMultiple, $.sync);78+79+const 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+};84+85+test('execa() bound options merge fd-specific ones', testMergeFdSpecific, execa);86+test('execaNode() bound options merge fd-specific ones', testMergeFdSpecific, execaNode);87+test('$ bound options merge fd-specific ones', testMergeFdSpecific, $);88+89+const 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+};93+94+test('execa() bound options are merged even if undefined', testMergeEnvUndefined, execa);95+test('execaSync() bound options are merged even if undefined', testMergeEnvUndefined, execaSync);96+test('execaNode() bound options are merged even if undefined', testMergeEnvUndefined, execaNode);97+test('$ bound options are merged even if undefined', testMergeEnvUndefined, $);98+test('$.sync bound options are merged even if undefined', testMergeEnvUndefined, $.sync);99+100+const 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+};104+105+test('execa() bound options only merge specific ones', testMergeSpecific, execa);106+test('execaSync() bound options only merge specific ones', testMergeSpecific, execaSync);107+test('execaNode() bound options only merge specific ones', testMergeSpecific, execaNode);108+test('$ bound options only merge specific ones', testMergeSpecific, $);109+test('$.sync bound options only merge specific ones', testMergeSpecific, $.sync);deleted:test/methods/create-bind.js-109 removed
1-import {join} from 'node:path';2-import test from 'ava';3-import {4- execa,5- execaSync,6- execaNode,7- $,8-} from '../../index.js';9-import {foobarString, foobarUppercase} from '../helpers/input.js';10-import {uppercaseGenerator} from '../helpers/generator.js';11-import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';12-13-setFixtureDirectory();14-15-const NOOP_PATH = join(FIXTURES_DIRECTORY, 'noop.js');16-const PRINT_ENV_PATH = join(FIXTURES_DIRECTORY, 'environment.js');17-18-const testBindOptions = async (t, execaMethod) => {19- const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString]);20- t.is(stdout, `${foobarString}\n`);21-};22-23-test('execa() can bind options', testBindOptions, execa);24-test('execaNode() can bind options', testBindOptions, execaNode);25-test('$ can bind options', testBindOptions, $);26-27-const testBindOptionsSync = (t, execaMethod) => {28- const {stdout} = execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString]);29- t.is(stdout, `${foobarString}\n`);30-};31-32-test('execaSync() can bind options', testBindOptionsSync, execaSync);33-test('$.sync can bind options', testBindOptionsSync, $.sync);34-35-const testBindPriority = async (t, execaMethod) => {36- const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString], {stripFinalNewline: true});37- t.is(stdout, foobarString);38-};39-40-test('execa() bound options have lower priority', testBindPriority, execa);41-test('execaSync() bound options have lower priority', testBindPriority, execaSync);42-test('execaNode() bound options have lower priority', testBindPriority, execaNode);43-test('$ bound options have lower priority', testBindPriority, $);44-test('$.sync bound options have lower priority', testBindPriority, $.sync);45-46-const testBindUndefined = async (t, execaMethod) => {47- const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString], {stripFinalNewline: undefined});48- t.is(stdout, foobarString);49-};50-51-test('execa() undefined options use default value', testBindUndefined, execa);52-test('execaSync() undefined options use default value', testBindUndefined, execaSync);53-test('execaNode() undefined options use default value', testBindUndefined, execaNode);54-test('$ undefined options use default value', testBindUndefined, $);55-test('$.sync undefined options use default value', testBindUndefined, $.sync);56-57-const 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-};61-62-test('execa() bound options are merged', testMergeEnv, execa);63-test('execaSync() bound options are merged', testMergeEnv, execaSync);64-test('execaNode() bound options are merged', testMergeEnv, execaNode);65-test('$ bound options are merged', testMergeEnv, $);66-test('$.sync bound options are merged', testMergeEnv, $.sync);67-68-const 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-};72-73-test('execa() bound options are merged multiple times', testMergeMultiple, execa);74-test('execaSync() bound options are merged multiple times', testMergeMultiple, execaSync);75-test('execaNode() bound options are merged multiple times', testMergeMultiple, execaNode);76-test('$ bound options are merged multiple times', testMergeMultiple, $);77-test('$.sync bound options are merged multiple times', testMergeMultiple, $.sync);78-79-const 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-};84-85-test('execa() bound options merge fd-specific ones', testMergeFdSpecific, execa);86-test('execaNode() bound options merge fd-specific ones', testMergeFdSpecific, execaNode);87-test('$ bound options merge fd-specific ones', testMergeFdSpecific, $);88-89-const 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-};93-94-test('execa() bound options are merged even if undefined', testMergeEnvUndefined, execa);95-test('execaSync() bound options are merged even if undefined', testMergeEnvUndefined, execaSync);96-test('execaNode() bound options are merged even if undefined', testMergeEnvUndefined, execaNode);97-test('$ bound options are merged even if undefined', testMergeEnvUndefined, $);98-test('$.sync bound options are merged even if undefined', testMergeEnvUndefined, $.sync);99-100-const 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-};104-105-test('execa() bound options only merge specific ones', testMergeSpecific, execa);106-test('execaSync() bound options only merge specific ones', testMergeSpecific, execaSync);107-test('execaNode() bound options only merge specific ones', testMergeSpecific, execaNode);108-test('$ bound options only merge specific ones', testMergeSpecific, $);109-test('$.sync bound options only merge specific ones', testMergeSpecific, $.sync);deleted:test/methods/create-main.js-63 removed
1-import {join} from 'node:path';2-import test from 'ava';3-import {4- execa,5- execaSync,6- execaNode,7- $,8-} from '../../index.js';9-import {foobarString, foobarArray} from '../helpers/input.js';10-import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';11-12-setFixtureDirectory();13-14-const NOOP_PATH = join(FIXTURES_DIRECTORY, 'noop.js');15-16-const testTemplate = async (t, execaMethod) => {17- const {stdout} = await execaMethod`${NOOP_PATH} ${foobarString}`;18- t.is(stdout, foobarString);19-};20-21-test('execa() can use template strings', testTemplate, execa);22-test('execaNode() can use template strings', testTemplate, execaNode);23-test('$ can use template strings', testTemplate, $);24-25-const testTemplateSync = (t, execaMethod) => {26- const {stdout} = execaMethod`${NOOP_PATH} ${foobarString}`;27- t.is(stdout, foobarString);28-};29-30-test('execaSync() can use template strings', testTemplateSync, execaSync);31-test('$.sync can use template strings', testTemplateSync, $.sync);32-33-const testTemplateOptions = async (t, execaMethod) => {34- const {stdout} = await execaMethod({stripFinalNewline: false})`${NOOP_PATH} ${foobarString}`;35- t.is(stdout, `${foobarString}\n`);36-};37-38-test('execa() can use template strings with options', testTemplateOptions, execa);39-test('execaNode() can use template strings with options', testTemplateOptions, execaNode);40-test('$ can use template strings with options', testTemplateOptions, $);41-42-const testTemplateOptionsSync = (t, execaMethod) => {43- const {stdout} = execaMethod({stripFinalNewline: false})`${NOOP_PATH} ${foobarString}`;44- t.is(stdout, `${foobarString}\n`);45-};46-47-test('execaSync() can use template strings with options', testTemplateOptionsSync, execaSync);48-test('$.sync can use template strings with options', testTemplateOptionsSync, $.sync);49-50-const 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-};55-56-test('allow commands with spaces and no array arguments', testSpacedCommand, undefined, execa);57-test('allow commands with spaces and array arguments', testSpacedCommand, foobarArray, execa);58-test('allow commands with spaces and no array arguments, execaSync', testSpacedCommand, undefined, execaSync);59-test('allow commands with spaces and array arguments, execaSync', testSpacedCommand, foobarArray, execaSync);60-test('allow commands with spaces and no array arguments, $', testSpacedCommand, undefined, $);61-test('allow commands with spaces and array arguments, $', testSpacedCommand, foobarArray, $);62-test('allow commands with spaces and no array arguments, $.sync', testSpacedCommand, undefined, $.sync);63-test('allow commands with spaces and array arguments, $.sync', testSpacedCommand, foobarArray, $.sync);added:test/methods/create.js+63 added
1+import {join} from 'node:path';2+import test from 'ava';3+import {4+ execa,5+ execaSync,6+ execaNode,7+ $,8+} from '../../index.js';9+import {foobarString, foobarArray} from '../helpers/input.js';10+import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';11+12+setFixtureDirectory();13+14+const NOOP_PATH = join(FIXTURES_DIRECTORY, 'noop.js');15+16+const testTemplate = async (t, execaMethod) => {17+ const {stdout} = await execaMethod`${NOOP_PATH} ${foobarString}`;18+ t.is(stdout, foobarString);19+};20+21+test('execa() can use template strings', testTemplate, execa);22+test('execaNode() can use template strings', testTemplate, execaNode);23+test('$ can use template strings', testTemplate, $);24+25+const testTemplateSync = (t, execaMethod) => {26+ const {stdout} = execaMethod`${NOOP_PATH} ${foobarString}`;27+ t.is(stdout, foobarString);28+};29+30+test('execaSync() can use template strings', testTemplateSync, execaSync);31+test('$.sync can use template strings', testTemplateSync, $.sync);32+33+const testTemplateOptions = async (t, execaMethod) => {34+ const {stdout} = await execaMethod({stripFinalNewline: false})`${NOOP_PATH} ${foobarString}`;35+ t.is(stdout, `${foobarString}\n`);36+};37+38+test('execa() can use template strings with options', testTemplateOptions, execa);39+test('execaNode() can use template strings with options', testTemplateOptions, execaNode);40+test('$ can use template strings with options', testTemplateOptions, $);41+42+const testTemplateOptionsSync = (t, execaMethod) => {43+ const {stdout} = execaMethod({stripFinalNewline: false})`${NOOP_PATH} ${foobarString}`;44+ t.is(stdout, `${foobarString}\n`);45+};46+47+test('execaSync() can use template strings with options', testTemplateOptionsSync, execaSync);48+test('$.sync can use template strings with options', testTemplateOptionsSync, $.sync);49+50+const 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+};55+56+test('allow commands with spaces and no array arguments', testSpacedCommand, undefined, execa);57+test('allow commands with spaces and array arguments', testSpacedCommand, foobarArray, execa);58+test('allow commands with spaces and no array arguments, execaSync', testSpacedCommand, undefined, execaSync);59+test('allow commands with spaces and array arguments, execaSync', testSpacedCommand, foobarArray, execaSync);60+test('allow commands with spaces and no array arguments, $', testSpacedCommand, undefined, $);61+test('allow commands with spaces and array arguments, $', testSpacedCommand, foobarArray, $);62+test('allow commands with spaces and no array arguments, $.sync', testSpacedCommand, undefined, $.sync);63+test('allow commands with spaces and array arguments, $.sync', testSpacedCommand, foobarArray, $.sync);modified:test/methods/template.js+40 added-25 removed
295 unmodified lines296 test('$`\\t`', testEmptyScript, () => $` `);297 test('$`\\n`', testEmptyScript, () => $`298 `);299 300-const testInvalidExpression = (t, invalidExpression, execaMethod) => {301- const expression = typeof invalidExpression === 'function' ? invalidExpression() : invalidExpression;300+const testInvalidExpression = (t, invalidExpression) => {301 t.throws(303- () => execaMethod`echo.js ${expression}`,302+ () => $`echo.js ${invalidExpression}`,303 {message: /in template expression/},304 );305 };306 308-test('$ throws on invalid expression - undefined', testInvalidExpression, undefined, $);309-test('$ throws on invalid expression - null', testInvalidExpression, null, $);310-test('$ throws on invalid expression - true', testInvalidExpression, true, $);311-test('$ throws on invalid expression - {}', testInvalidExpression, {}, $);312-test('$ throws on invalid expression - {foo: "bar"}', testInvalidExpression, {foo: 'bar'}, $);313-test('$ throws on invalid expression - {stdout: undefined}', testInvalidExpression, {stdout: undefined}, $);314-test('$ throws on invalid expression - {stdout: 1}', testInvalidExpression, {stdout: 1}, $);315-test('$ throws on invalid expression - Promise.resolve()', testInvalidExpression, Promise.resolve(), $);316-test('$ throws on invalid expression - Promise.resolve({stdout: "foo"})', testInvalidExpression, Promise.resolve({foo: 'bar'}), $);317-test('$ throws on invalid expression - $', testInvalidExpression, () => $`noop.js`, $);318-test('$ throws on invalid expression - $(options).sync', testInvalidExpression, () => $({stdio: 'ignore'}).sync`noop.js`, $);319-test('$ throws on invalid expression - [undefined]', testInvalidExpression, [undefined], $);320-test('$ throws on invalid expression - [null]', testInvalidExpression, [null], $);321-test('$ throws on invalid expression - [true]', testInvalidExpression, [true], $);322-test('$ throws on invalid expression - [{}]', testInvalidExpression, [{}], $);323-test('$ throws on invalid expression - [{foo: "bar"}]', testInvalidExpression, [{foo: 'bar'}], $);324-test('$ throws on invalid expression - [{stdout: undefined}]', testInvalidExpression, [{stdout: undefined}], $);325-test('$ throws on invalid expression - [{stdout: 1}]', testInvalidExpression, [{stdout: 1}], $);326-test('$ throws on invalid expression - [Promise.resolve()]', testInvalidExpression, [Promise.resolve()], $);327-test('$ throws on invalid expression - [Promise.resolve({stdout: "foo"})]', testInvalidExpression, [Promise.resolve({stdout: 'foo'})], $);328-test('$ throws on invalid expression - [$]', testInvalidExpression, () => [$`noop.js`], $);329-test('$ throws on invalid expression - [$(options).sync]', testInvalidExpression, () => [$({stdio: 'ignore'}).sync`noop.js`], $);307+test('$ throws on invalid expression - undefined', testInvalidExpression, undefined);308+test('$ throws on invalid expression - null', testInvalidExpression, null);309+test('$ throws on invalid expression - true', testInvalidExpression, true);310+test('$ throws on invalid expression - {}', testInvalidExpression, {});311+test('$ throws on invalid expression - {foo: "bar"}', testInvalidExpression, {foo: 'bar'});312+test('$ throws on invalid expression - {stdout: 1}', testInvalidExpression, {stdout: 1});313+test('$ throws on invalid expression - [undefined]', testInvalidExpression, [undefined]);314+test('$ throws on invalid expression - [null]', testInvalidExpression, [null]);315+test('$ throws on invalid expression - [true]', testInvalidExpression, [true]);316+test('$ throws on invalid expression - [{}]', testInvalidExpression, [{}]);317+test('$ throws on invalid expression - [{foo: "bar"}]', testInvalidExpression, [{foo: 'bar'}]);318+test('$ throws on invalid expression - [{stdout: 1}]', testInvalidExpression, [{stdout: 1}]);319+320+const testMissingOutput = (t, invalidExpression) => {321+ t.throws(322+ () => $`echo.js ${invalidExpression()}`,323+ {message: /Missing result.stdout/},324+ );325+};326+327+test('$ throws on invalid expression - {stdout: undefined}', testMissingOutput, () => ({stdout: undefined}));328+test('$ throws on invalid expression - [{stdout: undefined}]', testMissingOutput, () => [{stdout: undefined}]);329+test('$ throws on invalid expression - $(options).sync', testMissingOutput, () => $({stdio: 'ignore'}).sync`noop.js`);330+test('$ throws on invalid expression - [$(options).sync]', testMissingOutput, () => [$({stdio: 'ignore'}).sync`noop.js`]);331+332+const testInvalidPromise = (t, invalidExpression) => {333+ t.throws(334+ () => $`echo.js ${invalidExpression()}`,335+ {message: /Please use \${await subprocess}/},336+ );337+};338+339+test('$ throws on invalid expression - Promise.resolve()', testInvalidPromise, async () => ({}));340+test('$ throws on invalid expression - Promise.resolve({stdout: "foo"})', testInvalidPromise, async () => ({foo: 'bar'}));341+test('$ throws on invalid expression - [Promise.resolve()]', testInvalidPromise, () => [Promise.resolve()]);342+test('$ throws on invalid expression - [Promise.resolve({stdout: "foo"})]', testInvalidPromise, () => [Promise.resolve({stdout: 'foo'})]);343+test('$ throws on invalid expression - $', testInvalidPromise, () => $`noop.js`);344+test('$ throws on invalid expression - [$]', testInvalidPromise, () => [$`noop.js`]);