Skip to content
Myr

duplicate.js

This file

Path

test/stdio/duplicate.js

199 lines · 11.1 KiB · JavaScript

Last changed by

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

History of this file

Take it

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

lines 1–199why, one step back
1import {once} from 'node:events';
2import {createReadStream, createWriteStream} from 'node:fs';3import {readFile, writeFile, rm} from 'node:fs/promises';4import {Readable, Writable} from 'node:stream';5import {pathToFileURL} from 'node:url';6import test from 'ava';7import tempfile from 'tempfile';8import {execa, execaSync} from '../../index.js';9import {setFixtureDirectory} from '../helpers/fixtures-directory.js';10import {11 uppercaseGenerator,12 appendGenerator,13 appendAsyncGenerator,14 casedSuffix,15} from '../helpers/generator.js';16import {appendDuplex} from '../helpers/duplex.js';17import {appendWebTransform} from '../helpers/web-transform.js';18import {foobarString, foobarUint8Array, foobarUppercase} from '../helpers/input.js';19import {fullStdio} from '../helpers/stdio.js';20import {nestedExecaAsync, nestedExecaSync} from '../helpers/nested.js';21import {getAbsolutePath} from '../helpers/file-path.js';22import {noopDuplex} from '../helpers/stream.js';2324setFixtureDirectory();2526const getNativeStream = stream => stream;27const getNonNativeStream = stream => ['pipe', stream];28const getWebWritableStream = stream => Writable.toWeb(stream);2930const getDummyDuplex = () => ({transform: noopDuplex()});31const getDummyWebTransformStream = () => new TransformStream();3233const getDummyPath = async () => {34 const filePath = tempfile();35 await writeFile(filePath, '');36 return filePath;37};3839const getDummyFilePath = async () => ({file: await getDummyPath()});40const getDummyFileURL = async () => pathToFileURL((await getDummyPath()));41const duplexName = 'a Duplex stream';42const webTransformName = 'a web TransformStream';43const filePathName = 'a file path string';44const fileURLName = 'a file URL';4546const getDifferentInputs = stdioOption => ({stdio: [stdioOption, 'pipe', 'pipe', stdioOption]});47const getDifferentOutputs = stdioOption => ({stdout: stdioOption, stderr: stdioOption});48const getDifferentInputsOutputs = stdioOption => ({stdin: stdioOption, stdout: stdioOption});49const differentInputsName = '`stdin` and `stdio[3]`';50const differentOutputsName = '`stdout` and `stderr`';51const differentInputsOutputsName = '`stdin` and `stdout`';5253test('Can use multiple "pipe" on same input file descriptor', async t => {54 const subprocess = execa('stdin.js', {stdin: ['pipe', 'pipe']});55 subprocess.stdin.end(foobarString);56 const {stdout} = await subprocess;57 t.is(stdout, foobarString);58});5960const testTwoPipeOutput = async (t, execaMethod) => {61 const {stdout} = await execaMethod('noop.js', [foobarString], {stdout: ['pipe', 'pipe']});62 t.is(stdout, foobarString);63};6465test('Can use multiple "pipe" on same output file descriptor', testTwoPipeOutput, execa);66test('Can use multiple "pipe" on same output file descriptor, sync', testTwoPipeOutput, execaSync);6768test('Can repeat same stream on same input file descriptor', async t => {69 const stream = Readable.from([foobarString]);70 const {stdout} = await execa('stdin.js', {stdin: ['pipe', stream, stream]});71 t.is(stdout, foobarString);72});7374test('Can repeat same stream on same output file descriptor', async t => {75 let stdout = '';76 const stream = new Writable({77 write(chunk, encoding, done) {78 stdout += chunk.toString();79 done();80 },81 });82 await execa('noop-fd.js', ['1', foobarString], {stdout: ['pipe', stream, stream]});83 t.is(stdout, foobarString);84});8586// eslint-disable-next-line max-params87const testTwoGenerators = async (t, producesTwo, execaMethod, firstGenerator, secondGenerator = firstGenerator) => {88 const {stdout} = await execaMethod('noop-fd.js', ['1', foobarString], {stdout: [firstGenerator, secondGenerator]});89 const expectedSuffix = producesTwo ? `${casedSuffix}${casedSuffix}` : casedSuffix;90 t.is(stdout, `${foobarString}${expectedSuffix}`);91};9293test('Can use multiple identical generators', testTwoGenerators, true, execa, appendGenerator().transform);94test('Can use multiple identical generators, options object', testTwoGenerators, true, execa, appendGenerator());95test('Can use multiple identical generators, async', testTwoGenerators, true, execa, appendAsyncGenerator().transform);96test('Can use multiple identical generators, options object, async', testTwoGenerators, true, execa, appendAsyncGenerator());97test('Can use multiple identical generators, sync', testTwoGenerators, true, execaSync, appendGenerator().transform);98test('Can use multiple identical generators, options object, sync', testTwoGenerators, true, execaSync, appendGenerator());99test('Ignore duplicate identical duplexes', testTwoGenerators, false, execa, appendDuplex());100test('Ignore duplicate identical webTransforms', testTwoGenerators, false, execa, appendWebTransform());101test('Can use multiple generators with duplexes', testTwoGenerators, true, execa, appendGenerator(false, false, true), appendDuplex());102test('Can use multiple generators with webTransforms', testTwoGenerators, true, execa, appendGenerator(false, false, true), appendWebTransform());103test('Can use multiple duplexes with webTransforms', testTwoGenerators, true, execa, appendDuplex(), appendWebTransform());104105const testMultiplePipeOutput = async (t, execaMethod) => {106 const {stdout, stderr} = await execaMethod('noop-both.js', [foobarString], fullStdio);107 t.is(stdout, foobarString);108 t.is(stderr, foobarString);109};110111test('Can use multiple "pipe" on different output file descriptors', testMultiplePipeOutput, execa);112test('Can use multiple "pipe" on different output file descriptors, sync', testMultiplePipeOutput, execaSync);113114test('Can re-use same generator on different input file descriptors', async t => {115 const {stdout} = await execa('stdin-fd-both.js', ['3'], getDifferentInputs([foobarUint8Array, uppercaseGenerator(false, false, true)]));116 t.is(stdout, `${foobarUppercase}${foobarUppercase}`);117});118119test('Can re-use same generator on different output file descriptors', async t => {120 const {stdout, stderr} = await execa('noop-both.js', [foobarString], getDifferentOutputs(uppercaseGenerator(false, false, true)));121 t.is(stdout, foobarUppercase);122 t.is(stderr, foobarUppercase);123});124125test('Can re-use same non-native Readable stream on different input file descriptors', async t => {126 const filePath = tempfile();127 await writeFile(filePath, foobarString);128 const stream = createReadStream(filePath);129 await once(stream, 'open');130 const {stdout} = await execa('stdin-fd-both.js', ['3'], getDifferentInputs([new Uint8Array(0), stream]));131 t.is(stdout, `${foobarString}${foobarString}`);132 await rm(filePath);133});134135const testMultipleStreamOutput = async (t, getStreamOption) => {136 const filePath = tempfile();137 const stream = createWriteStream(filePath);138 await once(stream, 'open');139 await execa('noop-both.js', [foobarString], getDifferentOutputs(getStreamOption(stream)));140 t.is(await readFile(filePath, 'utf8'), `${foobarString}\n${foobarString}\n`);141 await rm(filePath);142};143144test('Can re-use same native Writable stream on different output file descriptors', testMultipleStreamOutput, getNativeStream);145test('Can re-use same non-native Writable stream on different output file descriptors', testMultipleStreamOutput, getNonNativeStream);146test('Can re-use same web Writable stream on different output file descriptors', testMultipleStreamOutput, getWebWritableStream);147148const testMultipleInheritOutput = async (t, execaMethod) => {149 const {stdout} = await execaMethod('noop-both.js', [foobarString], getDifferentOutputs(1)).parent;150 t.is(stdout, `${foobarString}\n${foobarString}`);151};152153test('Can re-use same parent file descriptor on different output file descriptors', testMultipleInheritOutput, nestedExecaAsync);154test('Can re-use same parent file descriptor on different output file descriptors, sync', testMultipleInheritOutput, nestedExecaSync);155156const testMultipleFileInput = async (t, mapFile) => {157 const filePath = tempfile();158 await writeFile(filePath, foobarString);159 const {stdout} = await execa('stdin-fd-both.js', ['3'], getDifferentInputs([new Uint8Array(0), mapFile(filePath)]));160 t.is(stdout, `${foobarString}${foobarString}`);161 await rm(filePath);162};163164test('Can re-use same file path on different input file descriptors', testMultipleFileInput, getAbsolutePath);165test('Can re-use same file URL on different input file descriptors', testMultipleFileInput, pathToFileURL);166167const testMultipleFileOutput = async (t, mapFile, execaMethod) => {168 const filePath = tempfile();169 await execaMethod('noop-both.js', [foobarString], getDifferentOutputs(mapFile(filePath)));170 t.is(await readFile(filePath, 'utf8'), `${foobarString}\n${foobarString}\n`);171 await rm(filePath);172};173174test('Can re-use same file path on different output file descriptors', testMultipleFileOutput, getAbsolutePath, execa);175test('Can re-use same file path on different output file descriptors, sync', testMultipleFileOutput, getAbsolutePath, execaSync);176test('Can re-use same file URL on different output file descriptors', testMultipleFileOutput, pathToFileURL, execa);177test('Can re-use same file URL on different output file descriptors, sync', testMultipleFileOutput, pathToFileURL, execaSync);178179// eslint-disable-next-line max-params180const testMultipleInvalid = async (t, getDummyStream, typeName, getStdio, fdName, execaMethod) => {181 const stdioOption = await getDummyStream();182 t.throws(() => {183 execaMethod('empty.js', getStdio(stdioOption));184 }, {message: `The ${fdName} options must not target ${typeName} that is the same.`});185 if (stdioOption.transform !== undefined) {186 t.true(stdioOption.transform.destroyed);187 }188};189190test('Cannot use same Duplex on different input file descriptors', testMultipleInvalid, getDummyDuplex, duplexName, getDifferentInputs, differentInputsName, execa);191test('Cannot use same Duplex on different output file descriptors', testMultipleInvalid, getDummyDuplex, duplexName, getDifferentOutputs, differentOutputsName, execa);192test('Cannot use same Duplex on both input and output file descriptors', testMultipleInvalid, getDummyDuplex, duplexName, getDifferentInputsOutputs, differentInputsOutputsName, execa);193test('Cannot use same TransformStream on different input file descriptors', testMultipleInvalid, getDummyWebTransformStream, webTransformName, getDifferentInputs, differentInputsName, execa);194test('Cannot use same TransformStream on different output file descriptors', testMultipleInvalid, getDummyWebTransformStream, webTransformName, getDifferentOutputs, differentOutputsName, execa);195test('Cannot use same TransformStream on both input and output file descriptors', testMultipleInvalid, getDummyWebTransformStream, webTransformName, getDifferentInputsOutputs, differentInputsOutputsName, execa);196test('Cannot use same file path on both input and output file descriptors', testMultipleInvalid, getDummyFilePath, filePathName, getDifferentInputsOutputs, differentInputsOutputsName, execa);197test('Cannot use same file URL on both input and output file descriptors', testMultipleInvalid, getDummyFileURL, fileURLName, getDifferentInputsOutputs, differentInputsOutputsName, execa);198test('Cannot use same file path on both input and output file descriptors, sync', testMultipleInvalid, getDummyFilePath, filePathName, getDifferentInputsOutputs, differentInputsOutputsName, execaSync);199test('Cannot use same file URL on both input and output file descriptors, sync', testMultipleInvalid, getDummyFileURL, fileURLName, getDifferentInputsOutputs, differentInputsOutputsName, execaSync);