Skip to content
Myr

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

11 files+554 added−654 removed

added:test/helpers/stdio.js+10 added
added:test/stdio/file-descriptor.js+32 added
added:test/stdio/file-path.js+162 added
1import {readFile, writeFile, rm} from 'node:fs/promises';2import {relative, dirname, basename} from 'node:path';3import process from 'node:process';4import {pathToFileURL} from 'node:url';5import test from 'ava';6import tempfile from 'tempfile';7import {execa, execaSync, $} from '../../index.js';8import {setFixtureDir} from '../helpers/fixtures-dir.js';9import {getStdinOption, getStdoutOption, getStderrOption, getInputFileOption, getScriptSync, identity} from '../helpers/stdio.js';1011setFixtureDir();1213const nonFileUrl = new URL('https://example.com');1415const getRelativePath = filePath => relative('.', filePath);1617const 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};2425test('inputFile can be a file URL', testStdinFile, pathToFileURL, getInputFileOption, execa);26test('stdin can be a file URL', testStdinFile, pathToFileURL, getStdinOption, execa);27test('inputFile can be an absolute file path', testStdinFile, identity, getInputFileOption, execa);28test('stdin can be an absolute file path', testStdinFile, identity, getStdinOption, execa);29test('inputFile can be a relative file path', testStdinFile, getRelativePath, getInputFileOption, execa);30test('stdin can be a relative file path', testStdinFile, getRelativePath, getStdinOption, execa);31test('inputFile can be a file URL - sync', testStdinFile, pathToFileURL, getInputFileOption, execaSync);32test('stdin can be a file URL - sync', testStdinFile, pathToFileURL, getStdinOption, execaSync);33test('inputFile can be an absolute file path - sync', testStdinFile, identity, getInputFileOption, execaSync);34test('stdin can be an absolute file path - sync', testStdinFile, identity, getStdinOption, execaSync);35test('inputFile can be a relative file path - sync', testStdinFile, getRelativePath, getInputFileOption, execaSync);36test('stdin can be a relative file path - sync', testStdinFile, getRelativePath, getStdinOption, execaSync);3738// eslint-disable-next-line max-params39const 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};4546test('stdout can be a file URL', testOutputFile, pathToFileURL, 'noop.js', getStdoutOption, execa);47test('stderr can be a file URL', testOutputFile, pathToFileURL, 'noop-err.js', getStderrOption, execa);48test('stdout can be an absolute file path', testOutputFile, identity, 'noop.js', getStdoutOption, execa);49test('stderr can be an absolute file path', testOutputFile, identity, 'noop-err.js', getStderrOption, execa);50test('stdout can be a relative file path', testOutputFile, getRelativePath, 'noop.js', getStdoutOption, execa);51test('stderr can be a relative file path', testOutputFile, getRelativePath, 'noop-err.js', getStderrOption, execa);52test('stdout can be a file URL - sync', testOutputFile, pathToFileURL, 'noop.js', getStdoutOption, execaSync);53test('stderr can be a file URL - sync', testOutputFile, pathToFileURL, 'noop-err.js', getStderrOption, execaSync);54test('stdout can be an absolute file path - sync', testOutputFile, identity, 'noop.js', getStdoutOption, execaSync);55test('stderr can be an absolute file path - sync', testOutputFile, identity, 'noop-err.js', getStderrOption, execaSync);56test('stdout can be a relative file path - sync', testOutputFile, getRelativePath, 'noop.js', getStdoutOption, execaSync);57test('stderr can be a relative file path - sync', testOutputFile, getRelativePath, 'noop-err.js', getStderrOption, execaSync);5859const testStdioNonFileUrl = (t, getOptions, execaMethod) => {60 t.throws(() => {61 execaMethod('noop.js', getOptions(nonFileUrl));62 }, {message: /pathToFileURL/});63};6465test('inputFile cannot be a non-file URL', testStdioNonFileUrl, getInputFileOption, execa);66test('stdin cannot be a non-file URL', testStdioNonFileUrl, getStdinOption, execa);67test('stdout cannot be a non-file URL', testStdioNonFileUrl, getStdoutOption, execa);68test('stderr cannot be a non-file URL', testStdioNonFileUrl, getStderrOption, execa);69test('inputFile cannot be a non-file URL - sync', testStdioNonFileUrl, getInputFileOption, execaSync);70test('stdin cannot be a non-file URL - sync', testStdioNonFileUrl, getStdinOption, execaSync);71test('stdout cannot be a non-file URL - sync', testStdioNonFileUrl, getStdoutOption, execaSync);72test('stderr cannot be a non-file URL - sync', testStdioNonFileUrl, getStderrOption, execaSync);7374const testInputFileValidUrl = async (t, execaMethod) => {75 const filePath = tempfile();76 await writeFile(filePath, 'foobar');77 const currentCwd = process.cwd();78 process.chdir(dirname(filePath));7980 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};8889test.serial('inputFile does not need to start with . when being a relative file path', testInputFileValidUrl, execa);90test.serial('inputFile does not need to start with . when being a relative file path - sync', testInputFileValidUrl, execaSync);9192const testStdioValidUrl = (t, getOptions, execaMethod) => {93 t.throws(() => {94 execaMethod('noop.js', getOptions('foobar'));95 }, {message: /absolute file path/});96};9798test('stdin must start with . when being a relative file path', testStdioValidUrl, getStdinOption, execa);99test('stdout must start with . when being a relative file path', testStdioValidUrl, getStdoutOption, execa);100test('stderr must start with . when being a relative file path', testStdioValidUrl, getStderrOption, execa);101test('stdin must start with . when being a relative file path - sync', testStdioValidUrl, getStdinOption, execaSync);102test('stdout must start with . when being a relative file path - sync', testStdioValidUrl, getStdoutOption, execaSync);103test('stderr must start with . when being a relative file path - sync', testStdioValidUrl, getStderrOption, execaSync);104105const testFileError = async (t, mapFile, getOptions) => {106 await t.throwsAsync(107 execa('noop.js', getOptions(mapFile('./unknown/file'))),108 {code: 'ENOENT'},109 );110};111112test('inputFile file URL errors should be handled', testFileError, pathToFileURL, getInputFileOption);113test('stdin file URL errors should be handled', testFileError, pathToFileURL, getStdinOption);114test('stdout file URL errors should be handled', testFileError, pathToFileURL, getStdoutOption);115test('stderr file URL errors should be handled', testFileError, pathToFileURL, getStderrOption);116test('inputFile file path errors should be handled', testFileError, identity, getInputFileOption);117test('stdin file path errors should be handled', testFileError, identity, getStdinOption);118test('stdout file path errors should be handled', testFileError, identity, getStdoutOption);119test('stderr file path errors should be handled', testFileError, identity, getStderrOption);120121const testFileErrorSync = (t, mapFile, getOptions) => {122 t.throws(() => {123 execaSync('noop.js', getOptions(mapFile('./unknown/file')));124 }, {code: 'ENOENT'});125};126127test('inputFile file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getInputFileOption);128test('stdin file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getStdinOption);129test('stdout file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getStdoutOption);130test('stderr file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getStderrOption);131test('inputFile file path errors should be handled - sync', testFileErrorSync, identity, getInputFileOption);132test('stdin file path errors should be handled - sync', testFileErrorSync, identity, getStdinOption);133test('stdout file path errors should be handled - sync', testFileErrorSync, identity, getStdoutOption);134test('stderr file path errors should be handled - sync', testFileErrorSync, identity, getStderrOption);135136const 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};143144test('inputFile can be set', testInputFile, execa);145test('inputFile can be set - sync', testInputFile, execa);146147const 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};154155test('inputFile can be set with $', testInputFileScript, identity);156test('inputFile can be set with $.sync', testInputFileScript, getScriptSync);157158test('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
added:test/stdio/iterable.js+60 added
added:test/stdio/node-stream.js+68 added
added:test/stdio/normalize.js+66 added
added:test/stdio/web-stream.js+52 added
modified:test/pipe.js+3 added-2 removed
1import {PassThrough, Readable} from 'node:stream';2import {spawn} from 'node:child_process';3import {readFile} from 'node:fs/promises';3import {readFile, rm} from 'node:fs/promises';4import tempfile from 'tempfile';5import test from 'ava';6import getStream from 'get-stream';7import {execa} from '../index.js';
23 unmodified lines
31test('pipeAll() can pipe stdout to streams', pipeToStream, 'noop.js', 'pipeAll', 'stdout');32test('pipeAll() can pipe stderr to streams', pipeToStream, 'noop-err.js', 'pipeAll', 'stderr');3334const 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};4142// `test.serial()` is due to a race condition: `execa(...).pipe*(file)` might resolve before the file stream has resolved43test.serial('pipeStdout() can pipe to files', pipeToFile, 'noop.js', 'pipeStdout', 'stdout');
37 unmodified lines
deleted:test/stdio.js-66 removed
1import {inspect} from 'node:util';2import test from 'ava';3import {normalizeStdio, normalizeStdioNode} from '../lib/stdio/normalize.js';45const macro = (t, input, expected, func) => {6 if (expected instanceof Error) {7 t.throws(() => {8 normalizeStdio(input);9 }, {message: expected.message});10 return;11 }1213 t.deepEqual(func(input), expected);14};1516const macroTitle = name => (title, input) => `${name} ${(inspect(input))}`;1718const stdioMacro = (...args) => macro(...args, normalizeStdio);19stdioMacro.title = macroTitle('execa()');2021test(stdioMacro, undefined, undefined);22test(stdioMacro, null, undefined);2324test(stdioMacro, {stdio: 'inherit'}, 'inherit');25test(stdioMacro, {stdio: 'pipe'}, 'pipe');26test(stdioMacro, {stdio: 'ignore'}, 'ignore');27test(stdioMacro, {stdio: [0, 1, 2]}, [0, 1, 2]);2829test(stdioMacro, {}, [undefined, undefined, undefined]);30test(stdioMacro, {stdio: []}, [undefined, undefined, undefined]);31test(stdioMacro, {stdin: 'pipe'}, ['pipe', undefined, undefined]);32test(stdioMacro, {stdout: 'ignore'}, [undefined, 'ignore', undefined]);33test(stdioMacro, {stderr: 'inherit'}, [undefined, undefined, 'inherit']);34test(stdioMacro, {stdin: 'pipe', stdout: 'ignore', stderr: 'inherit'}, ['pipe', 'ignore', 'inherit']);35test(stdioMacro, {stdin: 'pipe', stdout: 'ignore'}, ['pipe', 'ignore', undefined]);36test(stdioMacro, {stdin: 'pipe', stderr: 'inherit'}, ['pipe', undefined, 'inherit']);37test(stdioMacro, {stdout: 'ignore', stderr: 'inherit'}, [undefined, 'ignore', 'inherit']);38test(stdioMacro, {stdin: 0, stdout: 1, stderr: 2}, [0, 1, 2]);39test(stdioMacro, {stdin: 0, stdout: 1}, [0, 1, undefined]);40test(stdioMacro, {stdin: 0, stderr: 2}, [0, undefined, 2]);41test(stdioMacro, {stdout: 1, stderr: 2}, [undefined, 1, 2]);4243test(stdioMacro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of type `string` or `Array`, got `object`'));4445test(stdioMacro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));46test(stdioMacro, {stdin: 'inherit', stdio: ['pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));47test(stdioMacro, {stdin: 'inherit', stdio: [undefined, 'pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));48test(stdioMacro, {stdin: 0, stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));4950const forkMacro = (...args) => macro(...args, normalizeStdioNode);51forkMacro.title = macroTitle('execaNode()');5253test(forkMacro, undefined, [undefined, undefined, undefined, 'ipc']);54test(forkMacro, {stdio: 'ignore'}, ['ignore', 'ignore', 'ignore', 'ipc']);55test(forkMacro, {stdio: 'ipc'}, 'ipc');56test(forkMacro, {stdio: [0, 1, 2]}, [0, 1, 2, 'ipc']);57test(forkMacro, {stdio: [0, 1, 2, 3]}, [0, 1, 2, 3, 'ipc']);58test(forkMacro, {stdio: [0, 1, 2, 'ipc']}, [0, 1, 2, 'ipc']);5960test(forkMacro, {stdio: [0, 1, undefined]}, [0, 1, undefined, 'ipc']);61test(forkMacro, {stdio: [0, 1, 2, undefined]}, [0, 1, 2, undefined, 'ipc']);62test(forkMacro, {stdout: 'ignore'}, [undefined, 'ignore', undefined, 'ipc']);63test(forkMacro, {stdout: 'ignore', stderr: 'ignore'}, [undefined, 'ignore', 'ignore', 'ipc']);6465test(forkMacro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of type `string` or `Array`, got `object`'));66test(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
1import {Buffer} from 'node:buffer';2import {exec} from 'node:child_process';3import process from 'node:process';4import {once} from 'node:events';5import fs from 'node:fs';6import {readFile, writeFile, rm} from 'node:fs/promises';7import {relative} from 'node:path';8import Stream from 'node:stream';4import {setTimeout} from 'node:timers/promises';5import {promisify} from 'node:util';11import {pathToFileURL} from 'node:url';6import test from 'ava';7import getStream from 'get-stream';8import {pEvent} from 'p-event';15import tempfile from 'tempfile';16import {execa, execaSync, $} from '../index.js';9import {execa, execaSync} from '../index.js';10import {setFixtureDir, FIXTURES_DIR} from './helpers/fixtures-dir.js';1112const pExec = promisify(exec);1314setFixtureDir();1523const nonFileUrl = new URL('https://example.com');2425test('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});3031test('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});3616const 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 lines
37test('can pass encoding "hex"', checkEncoding, 'hex');38test('can pass encoding "base64"', checkEncoding, 'base64');39test('can pass encoding "base64url"', checkEncoding, 'base64url');4062const checkBufferEncoding = async (t, encoding) => {63 const {stdout} = await execa('noop-no-newline.js', [STRING_TO_ENCODE], {encoding});41const 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));4566 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};5070test('can pass encoding "buffer"', checkBufferEncoding, 'buffer');51test('can pass encoding "buffer"', checkBufferEncoding, execa);52test('can pass encoding "buffer" - sync', checkBufferEncoding, execaSync);5354test('validate unknown encodings', async t => {55 await t.throwsAsync(execa('noop.js', {encoding: 'unknownEncoding'}), {code: 'ERR_UNKNOWN_ENCODING'});56});5776test('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});8182test('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});8758test.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 lines
64 const {all} = await execa('noop.js');65 t.is(all, undefined);66});6798test('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);68test('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});72105test('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});111112test('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});116117const textEncoder = new TextEncoder();118const textDecoder = new TextDecoder();119const binaryFoo = textEncoder.encode('foo');120const binaryBar = textEncoder.encode('bar');121122test('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});126127const stringGenerator = function * () {128 yield * ['foo', 'bar'];73const testIgnore = async (t, streamName, execaMethod) => {74 const result = await execaMethod('noop.js', {[streamName]: 'ignore'});75 t.is(result[streamName], undefined);76};77131const binaryGenerator = function * () {132 yield * [binaryFoo, binaryBar];133};78test('stdout is undefined if ignored', testIgnore, 'stdout', execa);79test('stderr is undefined if ignored', testIgnore, 'stderr', execa);80test('stdout is undefined if ignored - sync', testIgnore, 'stdout', execaSync);81test('stderr is undefined if ignored - sync', testIgnore, 'stderr', execaSync);82135const throwingGenerator = function * () {136 yield 'foo';137 throw new Error('generator error');138};139140test('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});144145test('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});149150test('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});155156test('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});161162test('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});167168test('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});173174test('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});179180test('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});185186test('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});191192test('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});197198test('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});202203const testNoIterableOutput = (t, optionName, execaMethod) => {204 t.throws(() => {205 execaMethod('noop.js', {[optionName]: ['foo', 'bar']});206 }, {message: /cannot be an iterable/});207};208209test('stdout option cannot be an iterable', testNoIterableOutput, 'stdout', execa);210test('stderr option cannot be an iterable', testNoIterableOutput, 'stderr', execa);211test('stdout option cannot be an iterable - sync', testNoIterableOutput, 'stdout', execaSync);212test('stderr option cannot be an iterable - sync', testNoIterableOutput, 'stderr', execaSync);213214const 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};223224test('stdout option handles errors in WritableStream', testWritableStreamError, 'stdout');225test('stderr option handles errors in WritableStream', testWritableStreamError, 'stderr');226227test('input option can be a String', async t => {228 const {stdout} = await execa('stdin.js', {input: 'foobar'});229 t.is(stdout, 'foobar');230});231232test('input option can be a Uint8Array', async t => {233 const {stdout} = await execa('stdin.js', {input: binaryFoo});234 t.is(stdout, 'foo');235});236237test('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});242243test('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});248249const createNoFileReadable = value => {250 const stream = new Stream.PassThrough();251 stream.write(value);252 stream.end();253 return stream;254};255256test('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});260261const testNoFileStream = async (t, optionName, StreamClass) => {262 await t.throwsAsync(execa('noop.js', {[optionName]: new StreamClass()}), {code: 'ERR_INVALID_ARG_VALUE'});263};264265test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 'stdin', Stream.Readable);266test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 'stdout', Stream.Writable);267test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 'stderr', Stream.Writable);268269const 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};277278const 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};287288test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'input');289test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 'stdin');290291const 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};298299const 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};308309test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, 'stdout', 'noop.js');310test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, 'stderr', 'noop-err.js');311312test('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});317318test('input option can be used with $', async t => {319 const {stdout} = await $({input: 'foobar'})`stdin.js`;320 t.is(stdout, 'foobar');321});322323test('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});328329const 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};339340test('stdout can be a WritableStream', testWritableStream, 'stdout', 'noop.js');341test('stderr can be a WritableStream', testWritableStream, 'stderr', 'noop-err.js');342343test('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});349350test('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});356357test('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});363364const 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};369370test('stdout can be a file URL', testOutputFileUrl, 'stdout', 'noop.js');371test('stderr can be a file URL', testOutputFileUrl, 'stderr', 'noop-err.js');372373const testStdioNonFileUrl = (t, streamName, method) => {374 t.throws(() => {375 method('noop.js', {[streamName]: nonFileUrl});376 }, {message: /pathToFileURL/});377};378379test('stdin cannot be a non-file URL', testStdioNonFileUrl, 'stdin', execa);380test('stdout cannot be a non-file URL', testStdioNonFileUrl, 'stdout', execa);381test('stderr cannot be a non-file URL', testStdioNonFileUrl, 'stderr', execa);382test('stdin cannot be a non-file URL - sync', testStdioNonFileUrl, 'stdin', execaSync);383test('stdout cannot be a non-file URL - sync', testStdioNonFileUrl, 'stdout', execaSync);384test('stderr cannot be a non-file URL - sync', testStdioNonFileUrl, 'stderr', execaSync);385386test('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});392393const 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};398399test('stdout can be an absolute file path', testOutputAbsoluteFile, 'stdout', 'noop.js');400test('stderr can be an absolute file path', testOutputAbsoluteFile, 'stderr', 'noop-err.js');401402test('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});408409const 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};414415test('stdout can be a relative file path', testOutputRelativeFile, 'stdout', 'noop.js');416test('stderr can be a relative file path', testOutputRelativeFile, 'stderr', 'noop-err.js');417418const testStdioValidUrl = (t, streamName, method) => {419 t.throws(() => {420 method('noop.js', {[streamName]: 'foobar'});421 }, {message: /absolute file path/});422};423424test('stdin must start with . when being a relative file path', testStdioValidUrl, 'stdin', execa);425test('stdout must start with . when being a relative file path', testStdioValidUrl, 'stdout', execa);426test('stderr must start with . when being a relative file path', testStdioValidUrl, 'stderr', execa);427test('stdin must start with . when being a relative file path - sync', testStdioValidUrl, 'stdin', execaSync);428test('stdout must start with . when being a relative file path - sync', testStdioValidUrl, 'stdout', execaSync);429test('stderr must start with . when being a relative file path - sync', testStdioValidUrl, 'stderr', execaSync);430431test('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});437438test('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});444445test('inputFile and input cannot be both set', t => {446 t.throws(() => execa('stdin.js', {inputFile: '', input: ''}), {447 message: /cannot be both set/,448 });449});450451test('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});456457const testFileUrlError = async (t, streamName) => {458 await t.throwsAsync(459 execa('noop.js', {[streamName]: pathToFileURL('./unknown/file')}),460 {code: 'ENOENT'},83const 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};92464test('stdin file URL errors should be handled', testFileUrlError, 'stdin');465test('stdout file URL errors should be handled', testFileUrlError, 'stdout');466test('stderr file URL errors should be handled', testFileUrlError, 'stderr');93test('maxBuffer affects stdout', testMaxBuffer, 'stdout');94test('maxBuffer affects stderr', testMaxBuffer, 'stderr');95468const testFileUrlErrorSync = (t, streamName) => {469 t.throws(() => {470 execaSync('noop.js', {[streamName]: pathToFileURL('./unknown/file')});471 }, {code: 'ENOENT'});472};473474test('stdin file URL errors should be handled - sync', testFileUrlErrorSync, 'stdin');475test('stdout file URL errors should be handled - sync', testFileUrlErrorSync, 'stdout');476test('stderr file URL errors should be handled - sync', testFileUrlErrorSync, 'stderr');477478const testFilePathError = async (t, streamName) => {479 await t.throwsAsync(480 execa('noop.js', {[streamName]: './unknown/file'}),481 {code: 'ENOENT'},482 );483};484485test('stdin file path errors should be handled', testFilePathError, 'stdin');486test('stdout file path errors should be handled', testFilePathError, 'stdout');487test('stderr file path errors should be handled', testFilePathError, 'stderr');488489const testFilePathErrorSync = (t, streamName) => {490 t.throws(() => {491 execaSync('noop.js', {[streamName]: './unknown/file'});492 }, {code: 'ENOENT'});493};494495test('stdin file path errors should be handled - sync', testFilePathErrorSync, 'stdin');496test('stdout file path errors should be handled - sync', testFilePathErrorSync, 'stdout');497test('stderr file path errors should be handled - sync', testFilePathErrorSync, 'stderr');498499test('inputFile errors should be handled', async t => {500 await t.throwsAsync(execa('stdin.js', {inputFile: 'unknown'}), {code: 'ENOENT'});501});502503test('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});509510test('input option can be a String - sync', t => {511 const {stdout} = execaSync('stdin.js', {input: 'foobar'});512 t.is(stdout, 'foobar');513});514515test('input option can be used with $.sync', t => {516 const {stdout} = $({input: 'foobar'}).sync`stdin.js`;517 t.is(stdout, 'foobar');518});519520test('input option can be a Uint8Array - sync', t => {521 const {stdout} = execaSync('stdin.js', {input: binaryFoo});522 t.is(stdout, 'foo');523});524525test('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});532533test('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});538539test('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});545546const 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};551552test('stdout cannot be a WritableStream in sync mode', testWritableStreamSync, 'stdout');553test('stderr cannot be a WritableStream in sync mode', testWritableStreamSync, 'stderr');554555test('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});562563const 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};568569test('stdout can be a file URL - sync', testOutputFileUrlSync, 'stdout', 'noop.js');570test('stderr can be a file URL - sync', testOutputFileUrlSync, 'stderr', 'noop-err.js');571572test('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});578579const testOutputAbsoluteFileSync = (t, streamName, fixtureName) => {580 const outputFile = tempfile();581 execaSync(fixtureName, ['foobar'], {[streamName]: outputFile});582 t.is(fs.readFileSync(outputFile, 'utf8'), 'foobar\n');583};584585test('stdout can be an absolute file path - sync', testOutputAbsoluteFileSync, 'stdout', 'noop.js');586test('stderr can be an absolute file path - sync', testOutputAbsoluteFileSync, 'stderr', 'noop-err.js');587588test('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});595596const 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};601602test('stdout can be a relative file path - sync', testOutputRelativeFileSync, 'stdout', 'noop.js');603test('stderr can be a relative file path - sync', testOutputRelativeFileSync, 'stderr', 'noop-err.js');604605test('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});611612test('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});618619test('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});624625test('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});631632test('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});638639test('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([96const 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 ]);102646 t.is(result.stdout, undefined);647 t.is(stdout, '.........\n');648});103 t.is(result[streamName], undefined);104 t.is(output, '.........\n');105};106650test('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 ]);107test('do not buffer stdout when `buffer` set to `false`', testNoMaxBuffer, 'stdout');108test('do not buffer stderr when `buffer` set to `false`', testNoMaxBuffer, 'stderr');109657 t.is(result.stderr, undefined);658 t.is(stderr, '.........\n');659});660110test('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