Refactor tests (#628)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent c405105 · Browse files · 11 files·+554 added−654 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+554 added−654 removed
- tests:+554 added−654 removed
Provenance
- Commit
30ecc81- Tree
10448f1- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
c405105
11 files changed+554 added−654 removed
added:test/helpers/stdio.js+10 added
12345678910export const getStdinOption = stdioOption => ({stdin: stdioOption});export const getStdoutOption = stdioOption => ({stdout: stdioOption});export const getStderrOption = stdioOption => ({stderr: stdioOption});export const getPlainStdioOption = stdioOption => ({stdio: stdioOption});export const getInputOption = input => ({input});export const getInputFileOption = inputFile => ({inputFile});
export const getScriptSync = $ => $.sync;
export const identity = value => value;added:test/stdio/file-descriptor.js+32 added
1234567891011121314151617181920212223242526272829303132import {readFile, open, rm} from 'node:fs/promises';import test from 'ava';import tempfile from 'tempfile';import {execa, execaSync} from '../../index.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {getStdinOption, getStdoutOption, getStderrOption} from '../helpers/stdio.js';
setFixtureDir();
const getStdinProp = ({stdin}) => stdin;
const testFileDescriptorOption = async (t, fixtureName, getOptions, execaMethod) => { const filePath = tempfile(); const fileDescriptor = await open(filePath, 'w'); await execaMethod(fixtureName, ['foobar'], getOptions(fileDescriptor)); t.is(await readFile(filePath, 'utf8'), 'foobar\n'); await rm(filePath);};
test('pass `stdout` to a file descriptor', testFileDescriptorOption, 'noop.js', getStdoutOption, execa);test('pass `stderr` to a file descriptor', testFileDescriptorOption, 'noop-err.js', getStderrOption, execa);test('pass `stdout` to a file descriptor - sync', testFileDescriptorOption, 'noop.js', getStdoutOption, execaSync);test('pass `stderr` to a file descriptor - sync', testFileDescriptorOption, 'noop-err.js', getStderrOption, execaSync);
const testStdinWrite = async (t, getStreamProp, fixtureName, getOptions) => { const subprocess = execa(fixtureName, getOptions('pipe')); getStreamProp(subprocess).end('unicorns'); const {stdout} = await subprocess; t.is(stdout, 'unicorns');};
test('you can write to child.stdin', testStdinWrite, getStdinProp, 'stdin.js', getStdinOption);added:test/stdio/file-path.js+162 added
1+import {readFile, writeFile, rm} from 'node:fs/promises';2+import {relative, dirname, basename} from 'node:path';3+import process from 'node:process';4+import {pathToFileURL} from 'node:url';5+import test from 'ava';6+import tempfile from 'tempfile';7+import {execa, execaSync, $} from '../../index.js';8+import {setFixtureDir} from '../helpers/fixtures-dir.js';9+import {getStdinOption, getStdoutOption, getStderrOption, getInputFileOption, getScriptSync, identity} from '../helpers/stdio.js';10+11+setFixtureDir();12+13+const nonFileUrl = new URL('https://example.com');14+15+const getRelativePath = filePath => relative('.', filePath);16+17+const testStdinFile = async (t, mapFilePath, getOptions, execaMethod) => {18+ const filePath = tempfile();19+ await writeFile(filePath, 'foobar');20+ const {stdout} = await execaMethod('stdin.js', getOptions(mapFilePath(filePath)));21+ t.is(stdout, 'foobar');22+ await rm(filePath);23+};24+25+test('inputFile can be a file URL', testStdinFile, pathToFileURL, getInputFileOption, execa);26+test('stdin can be a file URL', testStdinFile, pathToFileURL, getStdinOption, execa);27+test('inputFile can be an absolute file path', testStdinFile, identity, getInputFileOption, execa);28+test('stdin can be an absolute file path', testStdinFile, identity, getStdinOption, execa);29+test('inputFile can be a relative file path', testStdinFile, getRelativePath, getInputFileOption, execa);30+test('stdin can be a relative file path', testStdinFile, getRelativePath, getStdinOption, execa);31+test('inputFile can be a file URL - sync', testStdinFile, pathToFileURL, getInputFileOption, execaSync);32+test('stdin can be a file URL - sync', testStdinFile, pathToFileURL, getStdinOption, execaSync);33+test('inputFile can be an absolute file path - sync', testStdinFile, identity, getInputFileOption, execaSync);34+test('stdin can be an absolute file path - sync', testStdinFile, identity, getStdinOption, execaSync);35+test('inputFile can be a relative file path - sync', testStdinFile, getRelativePath, getInputFileOption, execaSync);36+test('stdin can be a relative file path - sync', testStdinFile, getRelativePath, getStdinOption, execaSync);37+38+// eslint-disable-next-line max-params39+const testOutputFile = async (t, mapFile, fixtureName, getOptions, execaMethod) => {40+ const filePath = tempfile();41+ await execaMethod(fixtureName, ['foobar'], getOptions(mapFile(filePath)));42+ t.is(await readFile(filePath, 'utf8'), 'foobar\n');43+ await rm(filePath);44+};45+46+test('stdout can be a file URL', testOutputFile, pathToFileURL, 'noop.js', getStdoutOption, execa);47+test('stderr can be a file URL', testOutputFile, pathToFileURL, 'noop-err.js', getStderrOption, execa);48+test('stdout can be an absolute file path', testOutputFile, identity, 'noop.js', getStdoutOption, execa);49+test('stderr can be an absolute file path', testOutputFile, identity, 'noop-err.js', getStderrOption, execa);50+test('stdout can be a relative file path', testOutputFile, getRelativePath, 'noop.js', getStdoutOption, execa);51+test('stderr can be a relative file path', testOutputFile, getRelativePath, 'noop-err.js', getStderrOption, execa);52+test('stdout can be a file URL - sync', testOutputFile, pathToFileURL, 'noop.js', getStdoutOption, execaSync);53+test('stderr can be a file URL - sync', testOutputFile, pathToFileURL, 'noop-err.js', getStderrOption, execaSync);54+test('stdout can be an absolute file path - sync', testOutputFile, identity, 'noop.js', getStdoutOption, execaSync);55+test('stderr can be an absolute file path - sync', testOutputFile, identity, 'noop-err.js', getStderrOption, execaSync);56+test('stdout can be a relative file path - sync', testOutputFile, getRelativePath, 'noop.js', getStdoutOption, execaSync);57+test('stderr can be a relative file path - sync', testOutputFile, getRelativePath, 'noop-err.js', getStderrOption, execaSync);58+59+const testStdioNonFileUrl = (t, getOptions, execaMethod) => {60+ t.throws(() => {61+ execaMethod('noop.js', getOptions(nonFileUrl));62+ }, {message: /pathToFileURL/});63+};64+65+test('inputFile cannot be a non-file URL', testStdioNonFileUrl, getInputFileOption, execa);66+test('stdin cannot be a non-file URL', testStdioNonFileUrl, getStdinOption, execa);67+test('stdout cannot be a non-file URL', testStdioNonFileUrl, getStdoutOption, execa);68+test('stderr cannot be a non-file URL', testStdioNonFileUrl, getStderrOption, execa);69+test('inputFile cannot be a non-file URL - sync', testStdioNonFileUrl, getInputFileOption, execaSync);70+test('stdin cannot be a non-file URL - sync', testStdioNonFileUrl, getStdinOption, execaSync);71+test('stdout cannot be a non-file URL - sync', testStdioNonFileUrl, getStdoutOption, execaSync);72+test('stderr cannot be a non-file URL - sync', testStdioNonFileUrl, getStderrOption, execaSync);73+74+const testInputFileValidUrl = async (t, execaMethod) => {75+ const filePath = tempfile();76+ await writeFile(filePath, 'foobar');77+ const currentCwd = process.cwd();78+ process.chdir(dirname(filePath));79+80+ try {81+ const {stdout} = await execaMethod('stdin.js', {inputFile: basename(filePath)});82+ t.is(stdout, 'foobar');83+ } finally {84+ process.chdir(currentCwd);85+ await rm(filePath);86+ }87+};88+89+test.serial('inputFile does not need to start with . when being a relative file path', testInputFileValidUrl, execa);90+test.serial('inputFile does not need to start with . when being a relative file path - sync', testInputFileValidUrl, execaSync);91+92+const testStdioValidUrl = (t, getOptions, execaMethod) => {93+ t.throws(() => {94+ execaMethod('noop.js', getOptions('foobar'));95+ }, {message: /absolute file path/});96+};97+98+test('stdin must start with . when being a relative file path', testStdioValidUrl, getStdinOption, execa);99+test('stdout must start with . when being a relative file path', testStdioValidUrl, getStdoutOption, execa);100+test('stderr must start with . when being a relative file path', testStdioValidUrl, getStderrOption, execa);101+test('stdin must start with . when being a relative file path - sync', testStdioValidUrl, getStdinOption, execaSync);102+test('stdout must start with . when being a relative file path - sync', testStdioValidUrl, getStdoutOption, execaSync);103+test('stderr must start with . when being a relative file path - sync', testStdioValidUrl, getStderrOption, execaSync);104+105+const testFileError = async (t, mapFile, getOptions) => {106+ await t.throwsAsync(107+ execa('noop.js', getOptions(mapFile('./unknown/file'))),108+ {code: 'ENOENT'},109+ );110+};111+112+test('inputFile file URL errors should be handled', testFileError, pathToFileURL, getInputFileOption);113+test('stdin file URL errors should be handled', testFileError, pathToFileURL, getStdinOption);114+test('stdout file URL errors should be handled', testFileError, pathToFileURL, getStdoutOption);115+test('stderr file URL errors should be handled', testFileError, pathToFileURL, getStderrOption);116+test('inputFile file path errors should be handled', testFileError, identity, getInputFileOption);117+test('stdin file path errors should be handled', testFileError, identity, getStdinOption);118+test('stdout file path errors should be handled', testFileError, identity, getStdoutOption);119+test('stderr file path errors should be handled', testFileError, identity, getStderrOption);120+121+const testFileErrorSync = (t, mapFile, getOptions) => {122+ t.throws(() => {123+ execaSync('noop.js', getOptions(mapFile('./unknown/file')));124+ }, {code: 'ENOENT'});125+};126+127+test('inputFile file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getInputFileOption);128+test('stdin file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getStdinOption);129+test('stdout file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getStdoutOption);130+test('stderr file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getStderrOption);131+test('inputFile file path errors should be handled - sync', testFileErrorSync, identity, getInputFileOption);132+test('stdin file path errors should be handled - sync', testFileErrorSync, identity, getStdinOption);133+test('stdout file path errors should be handled - sync', testFileErrorSync, identity, getStdoutOption);134+test('stderr file path errors should be handled - sync', testFileErrorSync, identity, getStderrOption);135+136+const testInputFile = async (t, execaMethod) => {137+ const inputFile = tempfile();138+ await writeFile(inputFile, 'foobar');139+ const {stdout} = await execaMethod('stdin.js', {inputFile});140+ t.is(stdout, 'foobar');141+ await rm(inputFile);142+};143+144+test('inputFile can be set', testInputFile, execa);145+test('inputFile can be set - sync', testInputFile, execa);146+147+const testInputFileScript = async (t, getExecaMethod) => {148+ const inputFile = tempfile();149+ await writeFile(inputFile, 'foobar');150+ const {stdout} = await getExecaMethod($({inputFile}))`stdin.js`;151+ t.is(stdout, 'foobar');152+ await rm(inputFile);153+};154+155+test('inputFile can be set with $', testInputFileScript, identity);156+test('inputFile can be set with $.sync', testInputFileScript, getScriptSync);157+158+test('inputFile option cannot be set when stdin is set', t => {159+ t.throws(() => {160+ execa('stdin.js', {inputFile: '', stdin: 'ignore'});161+ }, {message: /`inputFile` and `stdin` options/});162+});added:test/stdio/input.js+66 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import {Readable} from 'node:stream';import {pathToFileURL} from 'node:url';import test from 'ava';import {execa, execaSync, $} from '../../index.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {getStdinOption, getPlainStdioOption, getScriptSync, identity} from '../helpers/stdio.js';
setFixtureDir();
const textEncoder = new TextEncoder();const binaryFoobar = textEncoder.encode('foobar');
const testInputOptionError = (t, stdin, inputName) => { t.throws(() => { execa('stdin.js', {stdin, [inputName]: 'foobar'}); }, {message: new RegExp(`\`${inputName}\` and \`stdin\` options`)});};
test('stdin option cannot be an iterable when "input" is used', testInputOptionError, ['foo', 'bar'], 'input');test('stdin option cannot be an iterable when "inputFile" is used', testInputOptionError, ['foo', 'bar'], 'inputFile');test('stdin option cannot be a file URL when "input" is used', testInputOptionError, pathToFileURL('unknown'), 'input');test('stdin option cannot be a file URL when "inputFile" is used', testInputOptionError, pathToFileURL('unknown'), 'inputFile');test('stdin option cannot be a file path when "input" is used', testInputOptionError, './unknown', 'input');test('stdin option cannot be a file path when "inputFile" is used', testInputOptionError, './unknown', 'inputFile');test('stdin option cannot be a ReadableStream when "input" is used', testInputOptionError, new ReadableStream(), 'input');test('stdin option cannot be a ReadableStream when "inputFile" is used', testInputOptionError, new ReadableStream(), 'inputFile');
const testInput = async (t, input, execaMethod) => { const {stdout} = await execaMethod('stdin.js', {input}); t.is(stdout, 'foobar');};
test('input option can be a String', testInput, 'foobar', execa);test('input option can be a String - sync', testInput, 'foobar', execaSync);test('input option can be a Uint8Array', testInput, binaryFoobar, execa);test('input option can be a Uint8Array - sync', testInput, binaryFoobar, execaSync);
const testInputScript = async (t, getExecaMethod) => { const {stdout} = await getExecaMethod($({input: 'foobar'}))`stdin.js`; t.is(stdout, 'foobar');};
test('input option can be used with $', testInputScript, identity);test('input option can be used with $.sync', testInputScript, getScriptSync);
const testInputWithStdinError = (t, input, getOptions, execaMethod) => { t.throws(() => { execaMethod('stdin.js', {input, ...getOptions('ignore')}); }, {message: /`input` and `stdin` options/});};
test('input option cannot be a String when stdin is set', testInputWithStdinError, 'foobar', getStdinOption, execa);test('input option cannot be a String when stdio is set', testInputWithStdinError, 'foobar', getPlainStdioOption, execa);test('input option cannot be a String when stdin is set - sync', testInputWithStdinError, 'foobar', getStdinOption, execaSync);test('input option cannot be a String when stdio is set - sync', testInputWithStdinError, 'foobar', getPlainStdioOption, execaSync);test('input option cannot be a Node.js Readable when stdin is set', testInputWithStdinError, new Readable(), getStdinOption, execa);test('input option cannot be a Node.js Readable when stdio is set', testInputWithStdinError, new Readable(), getPlainStdioOption, execa);
const testInputAndInputFile = async (t, execaMethod) => { t.throws(() => execaMethod('stdin.js', {inputFile: '', input: ''}), { message: /cannot be both set/, });};
test('inputFile and input cannot be both set', testInputAndInputFile, execa);test('inputFile and input cannot be both set - sync', testInputAndInputFile, execaSync);added:test/stdio/iterable.js+60 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import test from 'ava';import {execa, execaSync} from '../../index.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {getStdinOption, getStdoutOption, getStderrOption} from '../helpers/stdio.js';
setFixtureDir();
const textEncoder = new TextEncoder();const binaryFoo = textEncoder.encode('foo');const binaryBar = textEncoder.encode('bar');
const stringGenerator = function * () { yield * ['foo', 'bar'];};
const binaryGenerator = function * () { yield * [binaryFoo, binaryBar];};
// eslint-disable-next-line require-yieldconst throwingGenerator = function * () { throw new Error('generator error');};
const testIterable = async (t, stdioOption, fixtureName, getOptions) => { const {stdout} = await execa(fixtureName, getOptions(stdioOption)); t.is(stdout, 'foobar');};
test('stdin option can be a sync iterable of strings', testIterable, ['foo', 'bar'], 'stdin.js', getStdinOption);test('stdin option can be a sync iterable of Uint8Arrays', testIterable, [binaryFoo, binaryBar], 'stdin.js', getStdinOption);test('stdin option can be an sync iterable of strings', testIterable, stringGenerator(), 'stdin.js', getStdinOption);test('stdin option can be an sync iterable of Uint8Arrays', testIterable, binaryGenerator(), 'stdin.js', getStdinOption);
const testIterableSync = (t, stdioOption, fixtureName, getOptions) => { t.throws(() => { execaSync(fixtureName, getOptions(stdioOption)); }, {message: /an iterable in sync mode/});};
test('stdin option cannot be a sync iterable - sync', testIterableSync, ['foo', 'bar'], 'stdin.js', getStdinOption);test('stdin option cannot be an async iterable - sync', testIterableSync, stringGenerator(), 'stdin.js', getStdinOption);
const testIterableError = async (t, fixtureName, getOptions) => { const {originalMessage} = await t.throwsAsync(execa(fixtureName, getOptions(throwingGenerator()))); t.is(originalMessage, 'generator error');};
test('stdin option handles errors in iterables', testIterableError, 'stdin.js', getStdinOption);
const testNoIterableOutput = (t, getOptions, execaMethod) => { t.throws(() => { execaMethod('noop.js', getOptions(['foo', 'bar'])); }, {message: /cannot be an iterable/});};
test('stdout option cannot be an iterable', testNoIterableOutput, getStdoutOption, execa);test('stderr option cannot be an iterable', testNoIterableOutput, getStderrOption, execa);test('stdout option cannot be an iterable - sync', testNoIterableOutput, getStdoutOption, execaSync);test('stderr option cannot be an iterable - sync', testNoIterableOutput, getStderrOption, execaSync);added:test/stdio/node-stream.js+68 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import {once} from 'node:events';import {createReadStream, createWriteStream} from 'node:fs';import {readFile, writeFile, rm} from 'node:fs/promises';import {Readable, Writable, PassThrough} from 'node:stream';import test from 'ava';import tempfile from 'tempfile';import {execa, execaSync} from '../../index.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {getStdinOption, getStdoutOption, getStderrOption, getInputOption} from '../helpers/stdio.js';
setFixtureDir();
const createNoFileReadable = value => { const stream = new PassThrough(); stream.write(value); stream.end(); return stream;};
const testNodeStreamSync = (t, StreamClass, getOptions, optionName) => { t.throws(() => { execaSync('noop.js', getOptions(new StreamClass())); }, {message: `The \`${optionName}\` option cannot be a Node.js stream in sync mode.`});};
test('input cannot be a Node.js Readable - sync', testNodeStreamSync, Readable, getInputOption, 'input');
test('input can be a Node.js Readable without a file descriptor', async t => { const {stdout} = await execa('stdin.js', {input: createNoFileReadable('foobar')}); t.is(stdout, 'foobar');});
const testNoFileStream = async (t, getOptions, StreamClass) => { await t.throwsAsync(execa('noop.js', getOptions(new StreamClass())), {code: 'ERR_INVALID_ARG_VALUE'});};
test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, getStdinOption, Readable);test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, getStdoutOption, Writable);test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, getStderrOption, Writable);
const testFileReadable = async (t, fixtureName, getOptions) => { const filePath = tempfile(); await writeFile(filePath, 'foobar'); const stream = createReadStream(filePath); await once(stream, 'open');
const {stdout} = await execa(fixtureName, getOptions(stream)); t.is(stdout, 'foobar');
await rm(filePath);};
test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'stdin.js', getInputOption);test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 'stdin.js', getStdinOption);
const testFileWritable = async (t, getOptions, fixtureName) => { const filePath = tempfile(); const stream = createWriteStream(filePath); await once(stream, 'open');
await execa(fixtureName, ['foobar'], getOptions(stream)); t.is(await readFile(filePath, 'utf8'), 'foobar\n');
await rm(filePath);};
test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, getStdoutOption, 'noop.js');test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, getStderrOption, 'noop-err.js');added:test/stdio/normalize.js+66 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import {inspect} from 'node:util';import test from 'ava';import {normalizeStdio, normalizeStdioNode} from '../../lib/stdio/normalize.js';
const macro = (t, input, expected, func) => { if (expected instanceof Error) { t.throws(() => { normalizeStdio(input); }, {message: expected.message}); return; }
t.deepEqual(func(input), expected);};
const macroTitle = name => (title, input) => `${name} ${(inspect(input))}`;
const stdioMacro = (...args) => macro(...args, normalizeStdio);stdioMacro.title = macroTitle('execa()');
test(stdioMacro, undefined, undefined);test(stdioMacro, null, undefined);
test(stdioMacro, {stdio: 'inherit'}, 'inherit');test(stdioMacro, {stdio: 'pipe'}, 'pipe');test(stdioMacro, {stdio: 'ignore'}, 'ignore');test(stdioMacro, {stdio: [0, 1, 2]}, [0, 1, 2]);
test(stdioMacro, {}, [undefined, undefined, undefined]);test(stdioMacro, {stdio: []}, [undefined, undefined, undefined]);test(stdioMacro, {stdin: 'pipe'}, ['pipe', undefined, undefined]);test(stdioMacro, {stdout: 'ignore'}, [undefined, 'ignore', undefined]);test(stdioMacro, {stderr: 'inherit'}, [undefined, undefined, 'inherit']);test(stdioMacro, {stdin: 'pipe', stdout: 'ignore', stderr: 'inherit'}, ['pipe', 'ignore', 'inherit']);test(stdioMacro, {stdin: 'pipe', stdout: 'ignore'}, ['pipe', 'ignore', undefined]);test(stdioMacro, {stdin: 'pipe', stderr: 'inherit'}, ['pipe', undefined, 'inherit']);test(stdioMacro, {stdout: 'ignore', stderr: 'inherit'}, [undefined, 'ignore', 'inherit']);test(stdioMacro, {stdin: 0, stdout: 1, stderr: 2}, [0, 1, 2]);test(stdioMacro, {stdin: 0, stdout: 1}, [0, 1, undefined]);test(stdioMacro, {stdin: 0, stderr: 2}, [0, undefined, 2]);test(stdioMacro, {stdout: 1, stderr: 2}, [undefined, 1, 2]);
test(stdioMacro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of type `string` or `Array`, got `object`'));
test(stdioMacro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));test(stdioMacro, {stdin: 'inherit', stdio: ['pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));test(stdioMacro, {stdin: 'inherit', stdio: [undefined, 'pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));test(stdioMacro, {stdin: 0, stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));
const forkMacro = (...args) => macro(...args, normalizeStdioNode);forkMacro.title = macroTitle('execaNode()');
test(forkMacro, undefined, [undefined, undefined, undefined, 'ipc']);test(forkMacro, {stdio: 'ignore'}, ['ignore', 'ignore', 'ignore', 'ipc']);test(forkMacro, {stdio: 'ipc'}, 'ipc');test(forkMacro, {stdio: [0, 1, 2]}, [0, 1, 2, 'ipc']);test(forkMacro, {stdio: [0, 1, 2, 3]}, [0, 1, 2, 3, 'ipc']);test(forkMacro, {stdio: [0, 1, 2, 'ipc']}, [0, 1, 2, 'ipc']);
test(forkMacro, {stdio: [0, 1, undefined]}, [0, 1, undefined, 'ipc']);test(forkMacro, {stdio: [0, 1, 2, undefined]}, [0, 1, 2, undefined, 'ipc']);test(forkMacro, {stdout: 'ignore'}, [undefined, 'ignore', undefined, 'ipc']);test(forkMacro, {stdout: 'ignore', stderr: 'ignore'}, [undefined, 'ignore', 'ignore', 'ipc']);
test(forkMacro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of type `string` or `Array`, got `object`'));test(forkMacro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));added:test/stdio/web-stream.js+52 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import {Readable} from 'node:stream';import test from 'ava';import {execa, execaSync} from '../../index.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {getStdinOption, getStdoutOption, getStderrOption} from '../helpers/stdio.js';
setFixtureDir();
const testReadableStream = async (t, fixtureName, getOptions) => { const readableStream = Readable.toWeb(Readable.from('foobar')); const {stdout} = await execa(fixtureName, getOptions(readableStream)); t.is(stdout, 'foobar');};
test('stdin can be a ReadableStream', testReadableStream, 'stdin.js', getStdinOption);
const testWritableStream = async (t, fixtureName, getOptions) => { const result = []; const writableStream = new WritableStream({ write(chunk) { result.push(chunk); }, }); await execa(fixtureName, ['foobar'], getOptions(writableStream)); t.is(result.join(''), 'foobar\n');};
test('stdout can be a WritableStream', testWritableStream, 'noop.js', getStdoutOption);test('stderr can be a WritableStream', testWritableStream, 'noop-err.js', getStderrOption);
const testWebStreamSync = (t, StreamClass, getOptions, optionName) => { t.throws(() => { execaSync('noop.js', getOptions(new StreamClass())); }, {message: `The \`${optionName}\` option cannot be a web stream in sync mode.`});};
test('stdin cannot be a ReadableStream - sync', testWebStreamSync, ReadableStream, getStdinOption, 'stdin');test('stdout cannot be a WritableStream - sync', testWebStreamSync, WritableStream, getStdoutOption, 'stdout');test('stderr cannot be a WritableStream - sync', testWebStreamSync, WritableStream, getStderrOption, 'stderr');
const testWritableStreamError = async (t, getOptions) => { const writableStream = new WritableStream({ start(controller) { controller.error(new Error('foobar')); }, }); const {originalMessage} = await t.throwsAsync(execa('noop.js', getOptions(writableStream))); t.is(originalMessage, 'foobar');};
test('stdout option handles errors in WritableStream', testWritableStreamError, getStdoutOption);test('stderr option handles errors in WritableStream', testWritableStreamError, getStderrOption);modified:test/pipe.js+3 added-2 removed
1 import {PassThrough, Readable} from 'node:stream';2 import {spawn} from 'node:child_process';3-import {readFile} from 'node:fs/promises';3+import {readFile, rm} from 'node:fs/promises';4 import tempfile from 'tempfile';5 import test from 'ava';6 import getStream from 'get-stream';7 import {execa} from '../index.js';23 unmodified lines31 test('pipeAll() can pipe stdout to streams', pipeToStream, 'noop.js', 'pipeAll', 'stdout');32 test('pipeAll() can pipe stderr to streams', pipeToStream, 'noop-err.js', 'pipeAll', 'stderr');33 34 const pipeToFile = async (t, fixtureName, funcName, streamName) => {35- const file = tempfile({extension: '.txt'});35+ const file = tempfile();36 const result = await execa(fixtureName, ['test'], {all: true})[funcName](file);37 t.is(result[streamName], 'test');38 t.is(await readFile(file, 'utf8'), 'test\n');39+ await rm(file);40 };41 42 // `test.serial()` is due to a race condition: `execa(...).pipe*(file)` might resolve before the file stream has resolved43 test.serial('pipeStdout() can pipe to files', pipeToFile, 'noop.js', 'pipeStdout', 'stdout');37 unmodified linesdeleted:test/stdio.js-66 removed
1-import {inspect} from 'node:util';2-import test from 'ava';3-import {normalizeStdio, normalizeStdioNode} from '../lib/stdio/normalize.js';4-5-const macro = (t, input, expected, func) => {6- if (expected instanceof Error) {7- t.throws(() => {8- normalizeStdio(input);9- }, {message: expected.message});10- return;11- }12-13- t.deepEqual(func(input), expected);14-};15-16-const macroTitle = name => (title, input) => `${name} ${(inspect(input))}`;17-18-const stdioMacro = (...args) => macro(...args, normalizeStdio);19-stdioMacro.title = macroTitle('execa()');20-21-test(stdioMacro, undefined, undefined);22-test(stdioMacro, null, undefined);23-24-test(stdioMacro, {stdio: 'inherit'}, 'inherit');25-test(stdioMacro, {stdio: 'pipe'}, 'pipe');26-test(stdioMacro, {stdio: 'ignore'}, 'ignore');27-test(stdioMacro, {stdio: [0, 1, 2]}, [0, 1, 2]);28-29-test(stdioMacro, {}, [undefined, undefined, undefined]);30-test(stdioMacro, {stdio: []}, [undefined, undefined, undefined]);31-test(stdioMacro, {stdin: 'pipe'}, ['pipe', undefined, undefined]);32-test(stdioMacro, {stdout: 'ignore'}, [undefined, 'ignore', undefined]);33-test(stdioMacro, {stderr: 'inherit'}, [undefined, undefined, 'inherit']);34-test(stdioMacro, {stdin: 'pipe', stdout: 'ignore', stderr: 'inherit'}, ['pipe', 'ignore', 'inherit']);35-test(stdioMacro, {stdin: 'pipe', stdout: 'ignore'}, ['pipe', 'ignore', undefined]);36-test(stdioMacro, {stdin: 'pipe', stderr: 'inherit'}, ['pipe', undefined, 'inherit']);37-test(stdioMacro, {stdout: 'ignore', stderr: 'inherit'}, [undefined, 'ignore', 'inherit']);38-test(stdioMacro, {stdin: 0, stdout: 1, stderr: 2}, [0, 1, 2]);39-test(stdioMacro, {stdin: 0, stdout: 1}, [0, 1, undefined]);40-test(stdioMacro, {stdin: 0, stderr: 2}, [0, undefined, 2]);41-test(stdioMacro, {stdout: 1, stderr: 2}, [undefined, 1, 2]);42-43-test(stdioMacro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of type `string` or `Array`, got `object`'));44-45-test(stdioMacro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));46-test(stdioMacro, {stdin: 'inherit', stdio: ['pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));47-test(stdioMacro, {stdin: 'inherit', stdio: [undefined, 'pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));48-test(stdioMacro, {stdin: 0, stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));49-50-const forkMacro = (...args) => macro(...args, normalizeStdioNode);51-forkMacro.title = macroTitle('execaNode()');52-53-test(forkMacro, undefined, [undefined, undefined, undefined, 'ipc']);54-test(forkMacro, {stdio: 'ignore'}, ['ignore', 'ignore', 'ignore', 'ipc']);55-test(forkMacro, {stdio: 'ipc'}, 'ipc');56-test(forkMacro, {stdio: [0, 1, 2]}, [0, 1, 2, 'ipc']);57-test(forkMacro, {stdio: [0, 1, 2, 3]}, [0, 1, 2, 3, 'ipc']);58-test(forkMacro, {stdio: [0, 1, 2, 'ipc']}, [0, 1, 2, 'ipc']);59-60-test(forkMacro, {stdio: [0, 1, undefined]}, [0, 1, undefined, 'ipc']);61-test(forkMacro, {stdio: [0, 1, 2, undefined]}, [0, 1, 2, undefined, 'ipc']);62-test(forkMacro, {stdout: 'ignore'}, [undefined, 'ignore', undefined, 'ipc']);63-test(forkMacro, {stdout: 'ignore', stderr: 'ignore'}, [undefined, 'ignore', 'ignore', 'ipc']);64-65-test(forkMacro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of type `string` or `Array`, got `object`'));66-test(forkMacro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));modified:test/stream.js+35 added-586 removed
1 import {Buffer} from 'node:buffer';2 import {exec} from 'node:child_process';3 import process from 'node:process';4-import {once} from 'node:events';5-import fs from 'node:fs';6-import {readFile, writeFile, rm} from 'node:fs/promises';7-import {relative} from 'node:path';8-import Stream from 'node:stream';4 import {setTimeout} from 'node:timers/promises';5 import {promisify} from 'node:util';11-import {pathToFileURL} from 'node:url';6 import test from 'ava';7 import getStream from 'get-stream';8 import {pEvent} from 'p-event';15-import tempfile from 'tempfile';16-import {execa, execaSync, $} from '../index.js';9+import {execa, execaSync} from '../index.js';10 import {setFixtureDir, FIXTURES_DIR} from './helpers/fixtures-dir.js';11 12 const pExec = promisify(exec);13 14 setFixtureDir();15 23-const nonFileUrl = new URL('https://example.com');24-25-test('encoding option can be buffer', async t => {26- const {stdout} = await execa('noop.js', ['foo'], {encoding: 'buffer'});27- t.true(ArrayBuffer.isView(stdout));28- t.is(textDecoder.decode(stdout), 'foo');29-});30-31-test('encoding option can be buffer - Sync', t => {32- const {stdout} = execaSync('noop.js', ['foo'], {encoding: 'buffer'});33- t.true(ArrayBuffer.isView(stdout));34- t.is(textDecoder.decode(stdout), 'foo');35-});36-16 const checkEncoding = async (t, encoding) => {17 const {stdout} = await execa('noop-no-newline.js', [STRING_TO_ENCODE], {encoding});18 t.is(stdout, BUFFER_TO_ENCODE.toString(encoding));19 17 unmodified lines37 test('can pass encoding "hex"', checkEncoding, 'hex');38 test('can pass encoding "base64"', checkEncoding, 'base64');39 test('can pass encoding "base64url"', checkEncoding, 'base64url');40 62-const checkBufferEncoding = async (t, encoding) => {63- const {stdout} = await execa('noop-no-newline.js', [STRING_TO_ENCODE], {encoding});41+const checkBufferEncoding = async (t, execaMethod) => {42+ const {stdout} = await execaMethod('noop-no-newline.js', [STRING_TO_ENCODE], {encoding: 'buffer'});43+ t.true(ArrayBuffer.isView(stdout));44 t.true(BUFFER_TO_ENCODE.equals(stdout));45 66- const {stdout: nativeStdout} = await pExec(`node noop-no-newline.js ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});46+ const {stdout: nativeStdout} = await pExec(`node noop-no-newline.js ${STRING_TO_ENCODE}`, {encoding: 'buffer', cwd: FIXTURES_DIR});47+ t.true(Buffer.isBuffer(nativeStdout));48 t.true(BUFFER_TO_ENCODE.equals(nativeStdout));49 };50 70-test('can pass encoding "buffer"', checkBufferEncoding, 'buffer');51+test('can pass encoding "buffer"', checkBufferEncoding, execa);52+test('can pass encoding "buffer" - sync', checkBufferEncoding, execaSync);53 54 test('validate unknown encodings', async t => {55 await t.throwsAsync(execa('noop.js', {encoding: 'unknownEncoding'}), {code: 'ERR_UNKNOWN_ENCODING'});56 });57 76-test('pass `stdout` to a file descriptor', async t => {77- const file = tempfile({extension: '.txt'});78- await execa('noop.js', ['foo bar'], {stdout: fs.openSync(file, 'w')});79- t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');80-});81-82-test('pass `stderr` to a file descriptor', async t => {83- const file = tempfile({extension: '.txt'});84- await execa('noop-err.js', ['foo bar'], {stderr: fs.openSync(file, 'w')});85- t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');86-});87-58 test.serial('result.all shows both `stdout` and `stderr` intermixed', async t => {59 const {all} = await execa('noop-132.js', {all: true});60 t.is(all, '132');61 });2 unmodified lines64 const {all} = await execa('noop.js');65 t.is(all, undefined);66 });67 98-test('stdout/stderr/all are undefined if ignored', async t => {99- const {stdout, stderr, all} = await execa('noop.js', {stdio: 'ignore', all: true});100- t.is(stdout, undefined);101- t.is(stderr, undefined);68+test('result.all is undefined if ignored', async t => {69+ const {all} = await execa('noop.js', {stdio: 'ignore', all: true});70 t.is(all, undefined);71 });72 105-test('stdout/stderr/all are undefined if ignored in sync mode', t => {106- const {stdout, stderr, all} = execaSync('noop.js', {stdio: 'ignore', all: true});107- t.is(stdout, undefined);108- t.is(stderr, undefined);109- t.is(all, undefined);110-});111-112-test('stdin option can be a sync iterable of strings', async t => {113- const {stdout} = await execa('stdin.js', {stdin: ['foo', 'bar']});114- t.is(stdout, 'foobar');115-});116-117-const textEncoder = new TextEncoder();118-const textDecoder = new TextDecoder();119-const binaryFoo = textEncoder.encode('foo');120-const binaryBar = textEncoder.encode('bar');121-122-test('stdin option can be a sync iterable of Uint8Arrays', async t => {123- const {stdout} = await execa('stdin.js', {stdin: [binaryFoo, binaryBar]});124- t.is(stdout, 'foobar');125-});126-127-const stringGenerator = function * () {128- yield * ['foo', 'bar'];73+const testIgnore = async (t, streamName, execaMethod) => {74+ const result = await execaMethod('noop.js', {[streamName]: 'ignore'});75+ t.is(result[streamName], undefined);76 };77 131-const binaryGenerator = function * () {132- yield * [binaryFoo, binaryBar];133-};78+test('stdout is undefined if ignored', testIgnore, 'stdout', execa);79+test('stderr is undefined if ignored', testIgnore, 'stderr', execa);80+test('stdout is undefined if ignored - sync', testIgnore, 'stdout', execaSync);81+test('stderr is undefined if ignored - sync', testIgnore, 'stderr', execaSync);82 135-const throwingGenerator = function * () {136- yield 'foo';137- throw new Error('generator error');138-};139-140-test('stdin option can be an async iterable of strings', async t => {141- const {stdout} = await execa('stdin.js', {stdin: stringGenerator()});142- t.is(stdout, 'foobar');143-});144-145-test('stdin option can be an async iterable of Uint8Arrays', async t => {146- const {stdout} = await execa('stdin.js', {stdin: binaryGenerator()});147- t.is(stdout, 'foobar');148-});149-150-test('stdin option cannot be a sync iterable with execa.sync()', t => {151- t.throws(() => {152- execaSync('stdin.js', {stdin: ['foo', 'bar']});153- }, {message: /an iterable in sync mode/});154-});155-156-test('stdin option cannot be an async iterable with execa.sync()', t => {157- t.throws(() => {158- execaSync('stdin.js', {stdin: stringGenerator()});159- }, {message: /an iterable in sync mode/});160-});161-162-test('stdin option cannot be an iterable when "input" is used', t => {163- t.throws(() => {164- execa('stdin.js', {stdin: ['foo', 'bar'], input: 'foobar'});165- }, {message: /`input` and `stdin` options/});166-});167-168-test('stdin option cannot be an iterable when "inputFile" is used', t => {169- t.throws(() => {170- execa('stdin.js', {stdin: ['foo', 'bar'], inputFile: 'dummy.txt'});171- }, {message: /`inputFile` and `stdin` options/});172-});173-174-test('stdin option cannot be a file URL when "input" is used', t => {175- t.throws(() => {176- execa('stdin.js', {stdin: pathToFileURL('unknown'), input: 'foobar'});177- }, {message: /`input` and `stdin` options/});178-});179-180-test('stdin option cannot be a file URL when "inputFile" is used', t => {181- t.throws(() => {182- execa('stdin.js', {stdin: pathToFileURL('unknown'), inputFile: 'dummy.txt'});183- }, {message: /`inputFile` and `stdin` options/});184-});185-186-test('stdin option cannot be a file path when "input" is used', t => {187- t.throws(() => {188- execa('stdin.js', {stdin: './unknown', input: 'foobar'});189- }, {message: /`input` and `stdin` options/});190-});191-192-test('stdin option cannot be a file path when "inputFile" is used', t => {193- t.throws(() => {194- execa('stdin.js', {stdin: './unknown', inputFile: 'dummy.txt'});195- }, {message: /`inputFile` and `stdin` options/});196-});197-198-test('stdin option handles errors in iterables', async t => {199- const {originalMessage} = await t.throwsAsync(execa('stdin.js', {stdin: throwingGenerator()}));200- t.is(originalMessage, 'generator error');201-});202-203-const testNoIterableOutput = (t, optionName, execaMethod) => {204- t.throws(() => {205- execaMethod('noop.js', {[optionName]: ['foo', 'bar']});206- }, {message: /cannot be an iterable/});207-};208-209-test('stdout option cannot be an iterable', testNoIterableOutput, 'stdout', execa);210-test('stderr option cannot be an iterable', testNoIterableOutput, 'stderr', execa);211-test('stdout option cannot be an iterable - sync', testNoIterableOutput, 'stdout', execaSync);212-test('stderr option cannot be an iterable - sync', testNoIterableOutput, 'stderr', execaSync);213-214-const testWritableStreamError = async (t, streamName) => {215- const writableStream = new WritableStream({216- start(controller) {217- controller.error(new Error('foobar'));218- },219- });220- const {originalMessage} = await t.throwsAsync(execa('noop.js', {[streamName]: writableStream}));221- t.is(originalMessage, 'foobar');222-};223-224-test('stdout option handles errors in WritableStream', testWritableStreamError, 'stdout');225-test('stderr option handles errors in WritableStream', testWritableStreamError, 'stderr');226-227-test('input option can be a String', async t => {228- const {stdout} = await execa('stdin.js', {input: 'foobar'});229- t.is(stdout, 'foobar');230-});231-232-test('input option can be a Uint8Array', async t => {233- const {stdout} = await execa('stdin.js', {input: binaryFoo});234- t.is(stdout, 'foo');235-});236-237-test('input option cannot be a String when stdin is set', t => {238- t.throws(() => {239- execa('stdin.js', {input: 'foobar', stdin: 'ignore'});240- }, {message: /`input` and `stdin` options/});241-});242-243-test('input option cannot be a String when stdio is set', t => {244- t.throws(() => {245- execa('stdin.js', {input: 'foobar', stdio: 'ignore'});246- }, {message: /`input` and `stdin` options/});247-});248-249-const createNoFileReadable = value => {250- const stream = new Stream.PassThrough();251- stream.write(value);252- stream.end();253- return stream;254-};255-256-test('input can be a Node.js Readable without a file descriptor', async t => {257- const {stdout} = await execa('stdin.js', {input: createNoFileReadable('foobar')});258- t.is(stdout, 'foobar');259-});260-261-const testNoFileStream = async (t, optionName, StreamClass) => {262- await t.throwsAsync(execa('noop.js', {[optionName]: new StreamClass()}), {code: 'ERR_INVALID_ARG_VALUE'});263-};264-265-test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 'stdin', Stream.Readable);266-test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 'stdout', Stream.Writable);267-test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 'stderr', Stream.Writable);268-269-const createFileReadable = async value => {270- const filePath = tempfile();271- await writeFile(filePath, value);272- const stream = fs.createReadStream(filePath);273- await once(stream, 'open');274- const cleanup = () => rm(filePath);275- return {stream, cleanup};276-};277-278-const testFileReadable = async (t, optionName) => {279- const {stream, cleanup} = await createFileReadable('foobar');280- try {281- const {stdout} = await execa('stdin.js', {[optionName]: stream});282- t.is(stdout, 'foobar');283- } finally {284- await cleanup();285- }286-};287-288-test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'input');289-test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 'stdin');290-291-const createFileWritable = async () => {292- const filePath = tempfile();293- const stream = fs.createWriteStream(filePath);294- await once(stream, 'open');295- const cleanup = () => rm(filePath);296- return {stream, filePath, cleanup};297-};298-299-const testFileWritable = async (t, optionName, fixtureName) => {300- const {stream, filePath, cleanup} = await createFileWritable();301- try {302- await execa(fixtureName, ['foobar'], {[optionName]: stream});303- t.is(await readFile(filePath, 'utf8'), 'foobar\n');304- } finally {305- await cleanup();306- }307-};308-309-test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, 'stdout', 'noop.js');310-test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, 'stderr', 'noop-err.js');311-312-test('input option cannot be a Node.js Readable when stdin is set', t => {313- t.throws(() => {314- execa('stdin.js', {input: new Stream.PassThrough(), stdin: 'ignore'});315- }, {message: /`input` and `stdin` options/});316-});317-318-test('input option can be used with $', async t => {319- const {stdout} = await $({input: 'foobar'})`stdin.js`;320- t.is(stdout, 'foobar');321-});322-323-test('stdin can be a ReadableStream', async t => {324- const stdin = Stream.Readable.toWeb(Stream.Readable.from('howdy'));325- const {stdout} = await execa('stdin.js', {stdin});326- t.is(stdout, 'howdy');327-});328-329-const testWritableStream = async (t, streamName, fixtureName) => {330- const result = [];331- const writableStream = new WritableStream({332- write(chunk) {333- result.push(chunk);334- },335- });336- await execa(fixtureName, ['foobar'], {[streamName]: writableStream});337- t.is(result.join(''), 'foobar\n');338-};339-340-test('stdout can be a WritableStream', testWritableStream, 'stdout', 'noop.js');341-test('stderr can be a WritableStream', testWritableStream, 'stderr', 'noop-err.js');342-343-test('stdin cannot be a ReadableStream when input is used', t => {344- const stdin = Stream.Readable.toWeb(Stream.Readable.from('howdy'));345- t.throws(() => {346- execa('stdin.js', {stdin, input: 'foobar'});347- }, {message: /`input` and `stdin` options/});348-});349-350-test('stdin cannot be a ReadableStream when inputFile is used', t => {351- const stdin = Stream.Readable.toWeb(Stream.Readable.from('howdy'));352- t.throws(() => {353- execa('stdin.js', {stdin, inputFile: 'dummy.txt'});354- }, {message: /`inputFile` and `stdin` options/});355-});356-357-test('stdin can be a file URL', async t => {358- const inputFile = tempfile();359- fs.writeFileSync(inputFile, 'howdy');360- const {stdout} = await execa('stdin.js', {stdin: pathToFileURL(inputFile)});361- t.is(stdout, 'howdy');362-});363-364-const testOutputFileUrl = async (t, streamName, fixtureName) => {365- const outputFile = tempfile();366- await execa(fixtureName, ['foobar'], {[streamName]: pathToFileURL(outputFile)});367- t.is(await readFile(outputFile, 'utf8'), 'foobar\n');368-};369-370-test('stdout can be a file URL', testOutputFileUrl, 'stdout', 'noop.js');371-test('stderr can be a file URL', testOutputFileUrl, 'stderr', 'noop-err.js');372-373-const testStdioNonFileUrl = (t, streamName, method) => {374- t.throws(() => {375- method('noop.js', {[streamName]: nonFileUrl});376- }, {message: /pathToFileURL/});377-};378-379-test('stdin cannot be a non-file URL', testStdioNonFileUrl, 'stdin', execa);380-test('stdout cannot be a non-file URL', testStdioNonFileUrl, 'stdout', execa);381-test('stderr cannot be a non-file URL', testStdioNonFileUrl, 'stderr', execa);382-test('stdin cannot be a non-file URL - sync', testStdioNonFileUrl, 'stdin', execaSync);383-test('stdout cannot be a non-file URL - sync', testStdioNonFileUrl, 'stdout', execaSync);384-test('stderr cannot be a non-file URL - sync', testStdioNonFileUrl, 'stderr', execaSync);385-386-test('stdin can be an absolute file path', async t => {387- const inputFile = tempfile();388- fs.writeFileSync(inputFile, 'howdy');389- const {stdout} = await execa('stdin.js', {stdin: inputFile});390- t.is(stdout, 'howdy');391-});392-393-const testOutputAbsoluteFile = async (t, streamName, fixtureName) => {394- const outputFile = tempfile();395- await execa(fixtureName, ['foobar'], {[streamName]: outputFile});396- t.is(await readFile(outputFile, 'utf8'), 'foobar\n');397-};398-399-test('stdout can be an absolute file path', testOutputAbsoluteFile, 'stdout', 'noop.js');400-test('stderr can be an absolute file path', testOutputAbsoluteFile, 'stderr', 'noop-err.js');401-402-test('stdin can be a relative file path', async t => {403- const inputFile = tempfile();404- fs.writeFileSync(inputFile, 'howdy');405- const {stdout} = await execa('stdin.js', {stdin: relative('.', inputFile)});406- t.is(stdout, 'howdy');407-});408-409-const testOutputRelativeFile = async (t, streamName, fixtureName) => {410- const outputFile = tempfile();411- await execa(fixtureName, ['foobar'], {[streamName]: relative('.', outputFile)});412- t.is(await readFile(outputFile, 'utf8'), 'foobar\n');413-};414-415-test('stdout can be a relative file path', testOutputRelativeFile, 'stdout', 'noop.js');416-test('stderr can be a relative file path', testOutputRelativeFile, 'stderr', 'noop-err.js');417-418-const testStdioValidUrl = (t, streamName, method) => {419- t.throws(() => {420- method('noop.js', {[streamName]: 'foobar'});421- }, {message: /absolute file path/});422-};423-424-test('stdin must start with . when being a relative file path', testStdioValidUrl, 'stdin', execa);425-test('stdout must start with . when being a relative file path', testStdioValidUrl, 'stdout', execa);426-test('stderr must start with . when being a relative file path', testStdioValidUrl, 'stderr', execa);427-test('stdin must start with . when being a relative file path - sync', testStdioValidUrl, 'stdin', execaSync);428-test('stdout must start with . when being a relative file path - sync', testStdioValidUrl, 'stdout', execaSync);429-test('stderr must start with . when being a relative file path - sync', testStdioValidUrl, 'stderr', execaSync);430-431-test('inputFile can be set', async t => {432- const inputFile = tempfile();433- fs.writeFileSync(inputFile, 'howdy');434- const {stdout} = await execa('stdin.js', {inputFile});435- t.is(stdout, 'howdy');436-});437-438-test('inputFile can be set with $', async t => {439- const inputFile = tempfile();440- fs.writeFileSync(inputFile, 'howdy');441- const {stdout} = await $({inputFile})`stdin.js`;442- t.is(stdout, 'howdy');443-});444-445-test('inputFile and input cannot be both set', t => {446- t.throws(() => execa('stdin.js', {inputFile: '', input: ''}), {447- message: /cannot be both set/,448- });449-});450-451-test('inputFile option cannot be set when stdin is set', t => {452- t.throws(() => {453- execa('stdin.js', {inputFile: '', stdin: 'ignore'});454- }, {message: /`inputFile` and `stdin` options/});455-});456-457-const testFileUrlError = async (t, streamName) => {458- await t.throwsAsync(459- execa('noop.js', {[streamName]: pathToFileURL('./unknown/file')}),460- {code: 'ENOENT'},83+const testMaxBuffer = async (t, streamName) => {84+ await t.notThrowsAsync(execa('max-buffer.js', [streamName, '10'], {maxBuffer: 10}));85+ const {[streamName]: stream, all} = await t.throwsAsync(86+ execa('max-buffer.js', [streamName, '11'], {maxBuffer: 10, all: true}),87+ {message: new RegExp(`max-buffer.js ${streamName}`)},88 );89+ t.is(stream, '.'.repeat(10));90+ t.is(all, '.'.repeat(10));91 };92 464-test('stdin file URL errors should be handled', testFileUrlError, 'stdin');465-test('stdout file URL errors should be handled', testFileUrlError, 'stdout');466-test('stderr file URL errors should be handled', testFileUrlError, 'stderr');93+test('maxBuffer affects stdout', testMaxBuffer, 'stdout');94+test('maxBuffer affects stderr', testMaxBuffer, 'stderr');95 468-const testFileUrlErrorSync = (t, streamName) => {469- t.throws(() => {470- execaSync('noop.js', {[streamName]: pathToFileURL('./unknown/file')});471- }, {code: 'ENOENT'});472-};473-474-test('stdin file URL errors should be handled - sync', testFileUrlErrorSync, 'stdin');475-test('stdout file URL errors should be handled - sync', testFileUrlErrorSync, 'stdout');476-test('stderr file URL errors should be handled - sync', testFileUrlErrorSync, 'stderr');477-478-const testFilePathError = async (t, streamName) => {479- await t.throwsAsync(480- execa('noop.js', {[streamName]: './unknown/file'}),481- {code: 'ENOENT'},482- );483-};484-485-test('stdin file path errors should be handled', testFilePathError, 'stdin');486-test('stdout file path errors should be handled', testFilePathError, 'stdout');487-test('stderr file path errors should be handled', testFilePathError, 'stderr');488-489-const testFilePathErrorSync = (t, streamName) => {490- t.throws(() => {491- execaSync('noop.js', {[streamName]: './unknown/file'});492- }, {code: 'ENOENT'});493-};494-495-test('stdin file path errors should be handled - sync', testFilePathErrorSync, 'stdin');496-test('stdout file path errors should be handled - sync', testFilePathErrorSync, 'stdout');497-test('stderr file path errors should be handled - sync', testFilePathErrorSync, 'stderr');498-499-test('inputFile errors should be handled', async t => {500- await t.throwsAsync(execa('stdin.js', {inputFile: 'unknown'}), {code: 'ENOENT'});501-});502-503-test('you can write to child.stdin', async t => {504- const subprocess = execa('stdin.js');505- subprocess.stdin.end('unicorns');506- const {stdout} = await subprocess;507- t.is(stdout, 'unicorns');508-});509-510-test('input option can be a String - sync', t => {511- const {stdout} = execaSync('stdin.js', {input: 'foobar'});512- t.is(stdout, 'foobar');513-});514-515-test('input option can be used with $.sync', t => {516- const {stdout} = $({input: 'foobar'}).sync`stdin.js`;517- t.is(stdout, 'foobar');518-});519-520-test('input option can be a Uint8Array - sync', t => {521- const {stdout} = execaSync('stdin.js', {input: binaryFoo});522- t.is(stdout, 'foo');523-});524-525-test('opts.stdout:ignore - stdout will not collect data', async t => {526- const {stdout} = await execa('stdin.js', {527- input: 'hello',528- stdio: [undefined, 'ignore', undefined],529- });530- t.is(stdout, undefined);531-});532-533-test('input cannot be a Node.js Readable in sync mode', t => {534- t.throws(() => {535- execaSync('stdin.js', {input: new Stream.PassThrough()});536- }, {message: /The `input` option cannot be a Node\.js stream in sync mode/});537-});538-539-test('stdin cannot be a ReadableStream in sync mode', t => {540- const stdin = Stream.Readable.toWeb(Stream.Readable.from('howdy'));541- t.throws(() => {542- execaSync('stdin.js', {stdin});543- }, {message: /The `stdin` option cannot be a web stream in sync mode/});544-});545-546-const testWritableStreamSync = (t, streamName) => {547- t.throws(() => {548- execaSync('noop.js', {[streamName]: new WritableStream()});549- }, {message: new RegExp(`The \`${streamName}\` option cannot be a web stream in sync mode`)});550-};551-552-test('stdout cannot be a WritableStream in sync mode', testWritableStreamSync, 'stdout');553-test('stderr cannot be a WritableStream in sync mode', testWritableStreamSync, 'stderr');554-555-test('stdin can be a file URL - sync', t => {556- const inputFile = tempfile();557- fs.writeFileSync(inputFile, 'howdy');558- const stdin = pathToFileURL(inputFile);559- const {stdout} = execaSync('stdin.js', {stdin});560- t.is(stdout, 'howdy');561-});562-563-const testOutputFileUrlSync = (t, streamName, fixtureName) => {564- const outputFile = tempfile();565- execaSync(fixtureName, ['foobar'], {[streamName]: pathToFileURL(outputFile)});566- t.is(fs.readFileSync(outputFile, 'utf8'), 'foobar\n');567-};568-569-test('stdout can be a file URL - sync', testOutputFileUrlSync, 'stdout', 'noop.js');570-test('stderr can be a file URL - sync', testOutputFileUrlSync, 'stderr', 'noop-err.js');571-572-test('stdin can be an absolute file path - sync', t => {573- const inputFile = tempfile();574- fs.writeFileSync(inputFile, 'howdy');575- const {stdout} = execaSync('stdin.js', {stdin: inputFile});576- t.is(stdout, 'howdy');577-});578-579-const testOutputAbsoluteFileSync = (t, streamName, fixtureName) => {580- const outputFile = tempfile();581- execaSync(fixtureName, ['foobar'], {[streamName]: outputFile});582- t.is(fs.readFileSync(outputFile, 'utf8'), 'foobar\n');583-};584-585-test('stdout can be an absolute file path - sync', testOutputAbsoluteFileSync, 'stdout', 'noop.js');586-test('stderr can be an absolute file path - sync', testOutputAbsoluteFileSync, 'stderr', 'noop-err.js');587-588-test('stdin can be a relative file path - sync', t => {589- const inputFile = tempfile();590- fs.writeFileSync(inputFile, 'howdy');591- const stdin = relative('.', inputFile);592- const {stdout} = execaSync('stdin.js', {stdin});593- t.is(stdout, 'howdy');594-});595-596-const testOutputRelativeFileSync = (t, streamName, fixtureName) => {597- const outputFile = tempfile();598- execaSync(fixtureName, ['foobar'], {[streamName]: relative('.', outputFile)});599- t.is(fs.readFileSync(outputFile, 'utf8'), 'foobar\n');600-};601-602-test('stdout can be a relative file path - sync', testOutputRelativeFileSync, 'stdout', 'noop.js');603-test('stderr can be a relative file path - sync', testOutputRelativeFileSync, 'stderr', 'noop-err.js');604-605-test('inputFile can be set - sync', t => {606- const inputFile = tempfile();607- fs.writeFileSync(inputFile, 'howdy');608- const {stdout} = execaSync('stdin.js', {inputFile});609- t.is(stdout, 'howdy');610-});611-612-test('inputFile option can be used with $.sync', t => {613- const inputFile = tempfile();614- fs.writeFileSync(inputFile, 'howdy');615- const {stdout} = $({inputFile}).sync`stdin.js`;616- t.is(stdout, 'howdy');617-});618-619-test('inputFile and input cannot be both set - sync', t => {620- t.throws(() => execaSync('stdin.js', {inputFile: '', input: ''}), {621- message: /cannot be both set/,622- });623-});624-625-test('maxBuffer affects stdout', async t => {626- await t.notThrowsAsync(execa('max-buffer.js', ['stdout', '10'], {maxBuffer: 10}));627- const {stdout, all} = await t.throwsAsync(execa('max-buffer.js', ['stdout', '11'], {maxBuffer: 10, all: true}), {message: /max-buffer.js stdout/});628- t.is(stdout, '.'.repeat(10));629- t.is(all, '.'.repeat(10));630-});631-632-test('maxBuffer affects stderr', async t => {633- await t.notThrowsAsync(execa('max-buffer.js', ['stderr', '10'], {maxBuffer: 10}));634- const {stderr, all} = await t.throwsAsync(execa('max-buffer.js', ['stderr', '11'], {maxBuffer: 10, all: true}), {message: /max-buffer.js stderr/});635- t.is(stderr, '.'.repeat(10));636- t.is(all, '.'.repeat(10));637-});638-639-test('do not buffer stdout when `buffer` set to `false`', async t => {640- const promise = execa('max-buffer.js', ['stdout', '10'], {buffer: false});641- const [result, stdout] = await Promise.all([96+const testNoMaxBuffer = async (t, streamName) => {97+ const promise = execa('max-buffer.js', [streamName, '10'], {buffer: false});98+ const [result, output] = await Promise.all([99 promise,643- getStream(promise.stdout),100+ getStream(promise[streamName]),101 ]);102 646- t.is(result.stdout, undefined);647- t.is(stdout, '.........\n');648-});103+ t.is(result[streamName], undefined);104+ t.is(output, '.........\n');105+};106 650-test('do not buffer stderr when `buffer` set to `false`', async t => {651- const promise = execa('max-buffer.js', ['stderr', '10'], {buffer: false});652- const [result, stderr] = await Promise.all([653- promise,654- getStream(promise.stderr),655- ]);107+test('do not buffer stdout when `buffer` set to `false`', testNoMaxBuffer, 'stdout');108+test('do not buffer stderr when `buffer` set to `false`', testNoMaxBuffer, 'stderr');109 657- t.is(result.stderr, undefined);658- t.is(stderr, '.........\n');659-});660-110 test('do not buffer when streaming', async t => {111 const {stdout} = execa('max-buffer.js', ['stdout', '21'], {maxBuffer: 10});112 const result = await getStream(stdout);113 t.is(result, '....................\n');96 unmodified lines