Skip to content
Myr

Strip newlines in transforms (#919)

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

authored by ehmicky · committed by GitHub · parent 98f403c · Browse files · 27 files+680 added−288 removed
This commit

State

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

Diff stats

27 files+680 added−288 removed

  • tests:+523 added−244 removed
  • other:+157 added−44 removed

27 files+680 added−288 removed

modified:docs/transform.md+60 added
modified:lib/return/error.js+1 added-1 removed
178 unmodified lines
179 return escapeLines(fixCwdError(originalMessage, cwd));180};181182const serializeMessagePart = messagePart => Array.isArray(messagePart)183 ? messagePart.map(messageItem => serializeMessageItem(messageItem)).join('')183 ? messagePart.map(messageItem => stripFinalNewline(serializeMessageItem(messageItem))).filter(Boolean).join('\n')184 : serializeMessageItem(messagePart);185186const serializeMessageItem = messageItem => {187 if (typeof messageItem === 'string') {
9 unmodified lines
modified:lib/stdio/generator.js+7 added-5 removed
1import {generatorsToTransform} from './transform.js';2import {getEncodingTransformGenerator} from './encoding-transform.js';3import {getLinesGenerator} from './split.js';3import {getSplitLinesGenerator, getAppendNewlineGenerator} from './split.js';4import {pipeStreams} from './pipeline.js';5import {isGeneratorOptions, isAsyncGenerator} from './type.js';6import {getValidateTransformReturn} from './validate.js';7
10 unmodified lines
18 return [...nonGenerators, ...sortGenerators(newGenerators)];19};2021const normalizeGenerator = ({value, ...stdioStream}, index, newGenerators) => {22 const {transform, final, binary = false, objectMode} = isGeneratorOptions(value) ? value : {transform: value};22 const {transform, final, binary = false, preserveNewlines = false, objectMode} = isGeneratorOptions(value) ? value : {transform: value};23 const objectModes = stdioStream.direction === 'output'24 ? getOutputObjectModes(objectMode, index, newGenerators)25 : getInputObjectModes(objectMode, index, newGenerators);26 return {...stdioStream, value: {transform, final, binary, ...objectModes}};26 return {...stdioStream, value: {transform, final, binary, preserveNewlines, ...objectModes}};27};2829/*30`objectMode` determines the return value's type, i.e. the `readableObjectMode`.
36 unmodified lines
6768Chunks are currently processed serially. We could add a `concurrency` option to parallelize in the future.69*/70export const generatorToDuplexStream = ({71 value: {transform, final, binary, writableObjectMode, readableObjectMode},71 value: {transform, final, binary, writableObjectMode, readableObjectMode, preserveNewlines},72 encoding,73 forceEncoding,74 optionName,75}) => {76 const state = {};77 const generators = [78 getEncodingTransformGenerator(encoding, writableObjectMode, forceEncoding),78 getLinesGenerator(encoding, writableObjectMode, binary),79 getSplitLinesGenerator({encoding, binary, preserveNewlines, writableObjectMode, state}),80 {transform, final},81 {transform: getValidateTransformReturn(readableObjectMode, optionName)},82 getAppendNewlineGenerator({binary, preserveNewlines, readableObjectMode, state}),83 ].filter(Boolean);84 const transformAsync = isAsyncGenerator(transform);85 const finalAsync = isAsyncGenerator(final);86 const duplexStream = generatorsToTransform(generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode});
20 unmodified lines
modified:lib/stdio/handle.js+1 added-1 removed
2 unmodified lines
3import {addStreamDirection} from './direction.js';4import {normalizeStdio} from './option.js';5import {handleNativeStream} from './native.js';6import {handleInputOptions} from './input.js';7import {handleStreamsLines} from './split.js';7import {handleStreamsLines} from './lines.js';8import {handleStreamsEncoding} from './encoding-final.js';9import {normalizeGenerators} from './generator.js';10import {forwardStdio} from './forward.js';11
88 unmodified lines
added:lib/stdio/lines.js+23 added
modified:lib/stdio/split.js+48 added-35 removed
1import {isUint8Array} from '../utils.js';2import {willPipeStreams} from './forward.js';24// Split chunks line-wise for streams exposed to users like `subprocess.stdout`.5// Appending a noop transform in object mode is enough to do this, since every non-binary transform iterates line-wise.6export const handleStreamsLines = (stdioStreams, {lines}, isSync) => shouldSplitLines(stdioStreams, lines, isSync)7 ? [8 ...stdioStreams,9 {10 ...stdioStreams[0],11 type: 'generator',12 value: {transform: linesEndGenerator, objectMode: true},13 },14 ]15 : stdioStreams;1617const shouldSplitLines = (stdioStreams, lines, isSync) => stdioStreams[0].direction === 'output'18 && lines19 && !isSync20 && willPipeStreams(stdioStreams);2122const linesEndGenerator = function * (chunk) {23 yield chunk;24};253// Split chunks line-wise for generators passed to the `std*` options27export const getLinesGenerator = (encoding, writableObjectMode, binary) => {4export const getSplitLinesGenerator = ({encoding, binary, preserveNewlines, writableObjectMode, state}) => {5 if (binary || writableObjectMode) {6 return;7 }89 const info = encoding === 'buffer' ? linesUint8ArrayInfo : linesStringInfo;33 const state = {previousChunks: info.emptyValue};10 state.previousChunks = info.emptyValue;11 return {35 transform: linesGenerator.bind(undefined, state, info),12 transform: splitGenerator.bind(undefined, state, preserveNewlines, info),13 final: linesFinal.bind(undefined, state),14 };15};16
5 unmodified lines
22};2324const linesUint8ArrayInfo = {25 emptyValue: new Uint8Array(0),26 windowsNewline: new Uint8Array([0x0D, 0x0A]),27 unixNewline: new Uint8Array([0x0A]),28 CR: 0x0D,49 newline: 0x0A,50 concat: concatUint8Array,29 LF: 0x0A,30 concatBytes: concatUint8Array,31 isValidType: isUint8Array,32};3334const concatString = (firstChunk, secondChunk) => `${firstChunk}${secondChunk}`;35const isString = chunk => typeof chunk === 'string';3637const linesStringInfo = {38 emptyValue: '',39 windowsNewline: '\r\n',40 unixNewline: '\n',41 CR: '\r',59 newline: '\n',60 concat: concatString,42 LF: '\n',43 concatBytes: concatString,44 isValidType: isString,45};4647const linesInfo = [linesStringInfo, linesUint8ArrayInfo];4849// This imperative logic is much faster than using `String.split()` and uses very low memory.50// Also, it allows sharing it with `Uint8Array`.66const linesGenerator = function * (state, {emptyValue, newline, concat}, chunk) {51const splitGenerator = function * (state, preserveNewlines, {emptyValue, CR, LF, concatBytes}, chunk) {52 let {previousChunks} = state;53 let start = -1;5455 for (let end = 0; end < chunk.length; end += 1) {71 if (chunk[end] === newline) {72 let line = chunk.slice(start + 1, end + 1);56 if (chunk[end] === LF) {57 const newlineLength = getNewlineLength({chunk, end, CR, preserveNewlines, state});58 let line = chunk.slice(start + 1, end + 1 - newlineLength);5960 if (previousChunks.length > 0) {75 line = concat(previousChunks, line);61 line = concatBytes(previousChunks, line);62 previousChunks = emptyValue;63 }6465 yield line;
1 unmodified line
67 }68 }6970 if (start !== chunk.length - 1) {85 previousChunks = concat(previousChunks, chunk.slice(start + 1));71 previousChunks = concatBytes(previousChunks, chunk.slice(start + 1));72 }7374 state.previousChunks = previousChunks;75};7677const getNewlineLength = ({chunk, end, CR, preserveNewlines, state}) => {78 if (preserveNewlines) {79 return 0;80 }8182 state.isWindowsNewline = end !== 0 && chunk[end - 1] === CR;83 return state.isWindowsNewline ? 2 : 1;84};8586const linesFinal = function * ({previousChunks}) {87 if (previousChunks.length > 0) {88 yield previousChunks;89 }90};9192// Unless `preserveNewlines: true` is used, we strip the newline of each line.93// This re-adds them after the user `transform` code has run.94export const getAppendNewlineGenerator = ({binary, preserveNewlines, readableObjectMode, state}) => binary || preserveNewlines || readableObjectMode95 ? undefined96 : {transform: appendNewlineGenerator.bind(undefined, state)};9798const appendNewlineGenerator = function * ({isWindowsNewline = false}, chunk) {99 const {unixNewline, windowsNewline, LF, concatBytes} = linesInfo.find(({isValidType}) => isValidType(chunk));100101 if (chunk.at(-1) === LF) {102 yield chunk;103 return;104 }105106 const newline = isWindowsNewline ? windowsNewline : unixNewline;107 yield concatBytes(chunk, newline);108};
modified:lib/verbose/output.js+1 added-2 removed
1import {inspect} from 'node:util';2import stripFinalNewline from 'strip-final-newline';2import {escapeLines} from '../arguments/escape.js';3import {PIPED_STDIO_VALUES} from '../stdio/forward.js';4import {verboseLog} from './log.js';5
50 unmodified lines
56const isPiping = stream => stream._readableState.pipes.length > 0;5758// When `verbose` is `full`, print stdout|stderr59const logOutput = (line, {verboseId}) => {61 const lines = typeof line === 'string' ? stripFinalNewline(line) : inspect(line);60 const lines = typeof line === 'string' ? line : inspect(line);61 const escapedLines = escapeLines(lines);62 const spacedLines = escapedLines.replaceAll('\t', ' '.repeat(TAB_SIZE));63 verboseLog(spacedLines, verboseId, 'output');64};
3 unmodified lines
modified:test/convert/readable.js+2 added-2 removed
316 unmodified lines
317 for await (const line of subprocess.readable()) {318 lines.push(line);319 }320321 const expectedLines = ['aaa\n', 'bbb\n', 'ccc'];321 const expectedLines = ['aaa', 'bbb', 'ccc'];322 t.deepEqual(lines, expectedLines);323 await assertSubprocessOutput(t, subprocess, expectedLines);324});325326test('.readable() can wait for data', async t => {327 const subprocess = execa('noop.js', {stdout: getChunksGenerator([foobarString, foobarString])});327 const subprocess = execa('noop.js', {stdout: getChunksGenerator([foobarString, foobarString], false, true)});328 const stream = subprocess.readable();329330 t.is(stream.read(), null);331 await once(stream, 'readable');
123 unmodified lines
modified:test/convert/writable.js+4 added-4 removed
252 unmodified lines
253 await assertSubprocessOutput(t, subprocess, foobarString, 2);254});255256test('.writable() works with objectMode', async t => {257 const subprocess = getReadWriteSubprocess({stdin: serializeGenerator});257 const subprocess = getReadWriteSubprocess({stdin: serializeGenerator(true, true)});258 const stream = subprocess.writable();259 t.true(stream.writableObjectMode);260 t.is(stream.writableHighWaterMark, defaultObjectHighWaterMark);261 stream.end(foobarObject);
2 unmodified lines
264 await assertSubprocessOutput(t, subprocess, foobarObjectString);265});266267test('.duplex() works with objectMode and writes', async t => {268 const subprocess = getReadWriteSubprocess({stdin: serializeGenerator});268 const subprocess = getReadWriteSubprocess({stdin: serializeGenerator(true, true)});269 const stream = subprocess.duplex();270 t.false(stream.readableObjectMode);271 t.is(stream.readableHighWaterMark, defaultHighWaterMark);272 t.true(stream.writableObjectMode);
45 unmodified lines
318 return '.'.repeat(size * 3);319};320321test('.writable() waits when its buffer is full', async t => {322 const subprocess = getReadWriteSubprocess({stdin: noopAsyncGenerator()});322 const subprocess = getReadWriteSubprocess({stdin: noopAsyncGenerator(false, true)});323 const stream = subprocess.writable();324325 const expectedOutput = await writeUntilFull(t, stream, subprocess);326327 await assertSubprocessOutput(t, subprocess, expectedOutput);328});329330test('.duplex() waits when its buffer is full', async t => {331 const subprocess = getReadWriteSubprocess({stdin: noopAsyncGenerator()});331 const subprocess = getReadWriteSubprocess({stdin: noopAsyncGenerator(false, true)});332 const stream = subprocess.duplex();333334 const expectedOutput = await writeUntilFull(t, stream, subprocess);335
54 unmodified lines
modified:test/fixtures/nested-inherit.js+2 added-5 removed
1#!/usr/bin/env node2import {execa} from '../../index.js';3import {uppercaseGenerator} from '../helpers/generator.js';44const uppercaseGenerator = function * (line) {5 yield line.toUpperCase();6};78await execa('noop-fd.js', ['1'], {stdout: ['inherit', uppercaseGenerator]});5await execa('noop-fd.js', ['1'], {stdout: ['inherit', uppercaseGenerator()]});
modified:test/fixtures/nested-transform.js+1 added-1 removed
2 unmodified lines
3import {execa} from '../../index.js';4import {uppercaseGenerator} from '../helpers/generator.js';56const [options, file, ...args] = process.argv.slice(2);7await execa(file, args, {stdout: uppercaseGenerator, ...JSON.parse(options)});7await execa(file, args, {stdout: uppercaseGenerator(), ...JSON.parse(options)});
modified:test/helpers/generator.js+16 added-8 removed
1import {setImmediate, setInterval} from 'node:timers/promises';2import {foobarObject} from './input.js';34export const noopAsyncGenerator = () => ({4export const noopAsyncGenerator = (objectMode, binary) => ({5 async * transform(line) {6 yield line;7 },8 objectMode,9 binary,10});1112export const addNoopGenerator = (transform, addNoopTransform) => addNoopTransform13 ? [transform, noopGenerator(undefined, true)]
6 unmodified lines
20 objectMode,21 binary,22});2322export const serializeGenerator = {24export const serializeGenerator = (objectMode, binary) => ({25 * transform(object) {26 yield JSON.stringify(object);27 },26 objectMode: true,27};28 objectMode,29 binary,30});3132export const getOutputsGenerator = (inputs, objectMode) => ({33 * transform() {34 yield * inputs;
8 unmodified lines
43export const identityAsyncGenerator = input => async function * () {44 yield input;45};4644export const getOutputGenerator = (input, objectMode) => ({47export const getOutputGenerator = (input, objectMode, binary) => ({48 transform: identityGenerator(input),49 objectMode,50 binary,51});5253export const outputObjectGenerator = getOutputGenerator(foobarObject, true);54
30 unmodified lines
85 yield value;86 }87};8885export const uppercaseGenerator = function * (line) {86 yield line.toUpperCase();87};89export const uppercaseGenerator = (objectMode, binary) => ({90 * transform(line) {91 yield line.toUpperCase();92 },93 objectMode,94 binary,95});9697// eslint-disable-next-line require-yield98export const throwingGenerator = function * () {99 throw new Error('Generator error');
3 unmodified lines
added:test/helpers/lines.js+28 added
1import {Buffer} from 'node:buffer';23export const simpleFull = 'aaa\nbbb\nccc';4export const simpleChunks = [simpleFull];5export const simpleLines = ['aaa\n', 'bbb\n', 'ccc'];6export const simpleFullEndLines = ['aaa\n', 'bbb\n', 'ccc\n'];7export const noNewlinesChunks = ['aaa', 'bbb', 'ccc'];89const textEncoder = new TextEncoder();10const textDecoder = new TextDecoder();1112export const getEncoding = isUint8Array => isUint8Array ? 'buffer' : 'utf8';1314export const stringsToUint8Arrays = (strings, isUint8Array) => isUint8Array15 ? strings.map(string => textEncoder.encode(string))16 : strings;1718export const stringsToBuffers = (strings, isUint8Array) => isUint8Array19 ? strings.map(string => Buffer.from(string))20 : strings;2122export const serializeResult = (result, isUint8Array) => Array.isArray(result)23 ? result.map(resultItem => serializeResultItem(resultItem, isUint8Array))24 : serializeResultItem(result, isUint8Array);2526const serializeResultItem = (resultItem, isUint8Array) => isUint8Array27 ? textDecoder.decode(resultItem)28 : resultItem;
modified:test/pipe/streaming.js+1 added-1 removed
225 unmodified lines
226 const secondPipePromise = secondSource.pipe(destination);227228 t.like(await source, {stdout: [[foobarString]]});229 t.like(await secondSource, {stdout: [[foobarString]]});230 t.like(await destination, {stdout: `${foobarString}${foobarString}`});230 t.like(await destination, {stdout: `${foobarString}\n${foobarString}`});231 t.is(await pipePromise, await destination);232 t.is(await secondPipePromise, await destination);233});234
207 unmodified lines
modified:test/return/error.js+16 added
1 unmodified line
2import test from 'ava';3import {execa, execaSync} from '../../index.js';4import {setFixtureDir} from '../helpers/fixtures-dir.js';5import {fullStdio, getStdio} from '../helpers/stdio.js';6import {foobarString} from '../helpers/input.js';7import {QUOTE} from '../helpers/verbose.js';8import {noopGenerator, outputObjectGenerator} from '../helpers/generator.js';910const isWindows = process.platform === 'win32';11
64 unmodified lines
76test('error.message contains stdout/stderr/stdio even with encoding "buffer", objectMode', testStdioMessage, 'buffer', false, true);77test('error.message contains all if available, objectMode', testStdioMessage, 'utf8', true, true);78test('error.message contains all even with encoding "buffer", objectMode', testStdioMessage, 'buffer', true, true);7980const testLinesMessage = async (t, encoding, stripFinalNewline) => {81 const {message} = await t.throwsAsync(execa('noop-fail.js', ['1', `${foobarString}\n${foobarString}\n`], {82 lines: true,83 encoding,84 stripFinalNewline,85 }));86 t.true(message.endsWith(`noop-fail.js 1 ${QUOTE}${foobarString}\\n${foobarString}\\n${QUOTE}\n\n${foobarString}\n${foobarString}`));87};8889test('error.message handles "lines: true"', testLinesMessage, 'utf8', false);90test('error.message handles "lines: true", stripFinalNewline', testLinesMessage, 'utf8', true);91test('error.message handles "lines: true", buffer', testLinesMessage, 'buffer', false);92test('error.message handles "lines: true", buffer, stripFinalNewline', testLinesMessage, 'buffer', true);9394const testPartialIgnoreMessage = async (t, fdNumber, stdioOption, output) => {95 const {message} = await t.throwsAsync(execa('echo-fail.js', getStdio(fdNumber, stdioOption, 4)));96 t.true(message.endsWith(`echo-fail.js\n\n${output}\n\nfd3`));97};
108 unmodified lines
modified:test/return/output.js+1 added-1 removed
135 unmodified lines
136test('stripFinalNewline: true with stdio[*] - sync', testStripFinalNewline, 3, true, execaSync);137test('stripFinalNewline: false with stdio[*] - sync', testStripFinalNewline, 3, false, execaSync);138139test('stripFinalNewline is not used in objectMode', async t => {140 const {stdout} = await execa('noop-fd.js', ['1', 'foobar\n'], {stripFinalNewline: true, stdout: noopGenerator(true)});140 const {stdout} = await execa('noop-fd.js', ['1', 'foobar\n'], {stripFinalNewline: true, stdout: noopGenerator(true, true)});141 t.deepEqual(stdout, ['foobar\n']);142});
modified:test/stdio/encoding-transform.js+8 added-7 removed
9 unmodified lines
10setFixtureDir();1112const textEncoder = new TextEncoder();1314const getTypeofGenerator = objectMode => ({14const getTypeofGenerator = (objectMode, binary) => ({15 * transform(line) {16 yield Object.prototype.toString.call(line);17 },18 objectMode,19 binary,20});2122// eslint-disable-next-line max-params23const testGeneratorFirstEncoding = async (t, input, encoding, output, objectMode) => {23 const subprocess = execa('stdin.js', {stdin: getTypeofGenerator(objectMode), encoding});24 const subprocess = execa('stdin.js', {stdin: getTypeofGenerator(objectMode, true), encoding});25 subprocess.stdin.end(input);26 const {stdout} = await subprocess;27 const result = Buffer.from(stdout, encoding).toString();28 t.is(result, output);
28 unmodified lines
57const testGeneratorNextEncoding = async (t, input, encoding, firstObjectMode, secondObjectMode, expectedType) => {58 const {stdout} = await execa('noop.js', ['other'], {59 stdout: [60 getOutputGenerator(input, firstObjectMode),60 getTypeofGenerator(secondObjectMode),61 getTypeofGenerator(secondObjectMode, true),62 ],63 encoding,64 });65 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;
29 unmodified lines
9596// eslint-disable-next-line max-params97const testGeneratorReturnType = async (t, input, encoding, reject, objectMode, final) => {98 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';98 const {stdout} = await execa(fixtureName, ['1', 'other'], {99 stdout: convertTransformToFinal(getOutputGenerator(input, objectMode), final),99 const {stdout} = await execa(fixtureName, ['1', foobarString], {100 stdout: convertTransformToFinal(getOutputGenerator(input, objectMode, true), final),101 encoding,102 reject,103 });104 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;
56 unmodified lines
161const breakingLength = multibyteUint8Array.length * 0.75;162const brokenSymbol = '\uFFFD';163164const testMultibyte = async (t, objectMode) => {164 const subprocess = execa('stdin.js', {stdin: noopGenerator(objectMode)});165 const subprocess = execa('stdin.js', {stdin: noopGenerator(objectMode, true)});166 subprocess.stdin.write(multibyteUint8Array.slice(0, breakingLength));167 await scheduler.yield();168 subprocess.stdin.end(multibyteUint8Array.slice(breakingLength));169 const {stdout} = await subprocess;
6 unmodified lines
176const testMultibytePartial = async (t, objectMode) => {177 const {stdout} = await execa('stdin.js', {178 stdin: [179 [multibyteUint8Array.slice(0, breakingLength)],179 noopGenerator(objectMode),180 noopGenerator(objectMode, true),181 ],182 });183 t.is(stdout, `${multibyteChar}${brokenSymbol}`);184};
3 unmodified lines
modified:test/stdio/generator.js+13 added-13 removed
22 unmodified lines
2324const getInputObjectMode = (objectMode, addNoopTransform) => objectMode25 ? {26 input: [foobarObject],27 generators: addNoopGenerator(serializeGenerator, addNoopTransform),27 generators: addNoopGenerator(serializeGenerator(true), addNoopTransform),28 output: foobarObjectString,29 }30 : {31 input: foobarUint8Array,32 generators: addNoopGenerator(uppercaseGenerator, addNoopTransform),32 generators: addNoopGenerator(uppercaseGenerator(), addNoopTransform),33 output: foobarUppercase,34 };3536const getOutputObjectMode = (objectMode, addNoopTransform) => objectMode
2 unmodified lines
39 output: [foobarObject],40 getStreamMethod: getStreamAsArray,41 }42 : {43 generators: addNoopGenerator(uppercaseGenerator, addNoopGenerator),43 generators: addNoopGenerator(uppercaseGenerator(false, true), addNoopTransform),44 output: foobarUppercase,45 getStreamMethod: getStream,46 };47
140 unmodified lines
188 if (objectMode) {189 return outputObjectGenerator;190 }191192 return encoding === 'buffer' ? uppercaseBufferGenerator : uppercaseGenerator;192 return encoding === 'buffer' ? uppercaseBufferGenerator : uppercaseGenerator();193};194195const getStdoutStderrOutput = (output, stdioOption, encoding, objectMode) => {196 if (objectMode && !stdioOption) {
59 unmodified lines
256test('Can use generators with result.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, true, false);257test('Can use generators with error.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, true, false);258259test('Can use generators with input option', async t => {260 const {stdout} = await execa('stdin-fd.js', ['0'], {stdin: uppercaseGenerator, input: foobarUint8Array});260 const {stdout} = await execa('stdin-fd.js', ['0'], {stdin: uppercaseGenerator(), input: foobarUint8Array});261 t.is(stdout, foobarUppercase);262});263264const testInputFile = async (t, getOptions, reversed) => {
5 unmodified lines
270 t.is(stdout, foobarUppercase);271 await rm(filePath);272};273274test('Can use generators with a file as input', testInputFile, filePath => ({stdin: [{file: filePath}, uppercaseGenerator]}), false);275test('Can use generators with a file as input, reversed', testInputFile, filePath => ({stdin: [{file: filePath}, uppercaseGenerator]}), true);276test('Can use generators with inputFile option', testInputFile, filePath => ({inputFile: filePath, stdin: uppercaseGenerator}), false);274test('Can use generators with a file as input', testInputFile, filePath => ({stdin: [{file: filePath}, uppercaseGenerator()]}), false);275test('Can use generators with a file as input, reversed', testInputFile, filePath => ({stdin: [{file: filePath}, uppercaseGenerator()]}), true);276test('Can use generators with inputFile option', testInputFile, filePath => ({inputFile: filePath, stdin: uppercaseGenerator()}), false);277278const testOutputFile = async (t, reversed) => {279 const filePath = tempfile();280 const stdoutOption = [uppercaseGenerator, {file: filePath}];280 const stdoutOption = [uppercaseGenerator(false, true), {file: filePath}];281 const reversedStdoutOption = reversed ? stdoutOption.reverse() : stdoutOption;282 const {stdout} = await execa('noop-fd.js', ['1'], {stdout: reversedStdoutOption});283 t.is(stdout, foobarUppercase);284 t.is(await readFile(filePath, 'utf8'), foobarUppercase);
5 unmodified lines
290291test('Can use generators to a Writable stream', async t => {292 const passThrough = new PassThrough();293 const [{stdout}, streamOutput] = await Promise.all([294 execa('noop-fd.js', ['1', foobarString], {stdout: [uppercaseGenerator, passThrough]}),294 execa('noop-fd.js', ['1', foobarString], {stdout: [uppercaseGenerator(false, true), passThrough]}),295 getStream(passThrough),296 ]);297 t.is(stdout, foobarUppercase);298 t.is(streamOutput, foobarUppercase);299});300301test('Can use generators from a Readable stream', async t => {302 const passThrough = new PassThrough();303 const subprocess = execa('stdin-fd.js', ['0'], {stdin: [passThrough, uppercaseGenerator]});303 const subprocess = execa('stdin-fd.js', ['0'], {stdin: [passThrough, uppercaseGenerator()]});304 passThrough.end(foobarString);305 const {stdout} = await subprocess;306 t.is(stdout, foobarUppercase);307});
9 unmodified lines
317 yield `${line}${casedSuffix}`;318};319320const testAppendInput = async (t, reversed) => {321 const stdin = [foobarUint8Array, uppercaseGenerator, appendGenerator];321 const stdin = [foobarUint8Array, uppercaseGenerator(), appendGenerator];322 const reversedStdin = reversed ? stdin.reverse() : stdin;323 const {stdout} = await execa('stdin-fd.js', ['0'], {stdin: reversedStdin});324 const reversedSuffix = reversed ? casedSuffix.toUpperCase() : casedSuffix;325 t.is(stdout, `${foobarUppercase}${reversedSuffix}`);
2 unmodified lines
328test('Can use multiple generators as input', testAppendInput, false);329test('Can use multiple generators as input, reversed', testAppendInput, true);330331const testAppendOutput = async (t, reversed) => {332 const stdoutOption = [uppercaseGenerator, appendGenerator];332 const stdoutOption = [uppercaseGenerator(), appendGenerator];333 const reversedStdoutOption = reversed ? stdoutOption.reverse() : stdoutOption;334 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: reversedStdoutOption});335 const reversedSuffix = reversed ? casedSuffix.toUpperCase() : casedSuffix;336 t.is(stdout, `${foobarUppercase}${reversedSuffix}`);
9 unmodified lines
modified:test/stdio/iterable.js+1 added-1 removed
52 unmodified lines
53 yield foobarObject;54};5556const testObjectIterable = async (t, stdioOption, fdNumber) => {57 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stdioOption, serializeGenerator]));57 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stdioOption, serializeGenerator(true)]));58 t.is(stdout, foobarObjectString);59};6061test('stdin option can be an array of objects', testObjectIterable, [foobarObject], 0);
71 unmodified lines
added:test/stdio/lines.js+149 added
1import {once} from 'node:events';2import {Writable} from 'node:stream';3import test from 'ava';4import {execa, execaSync} from '../../index.js';5import {setFixtureDir} from '../helpers/fixtures-dir.js';6import {fullStdio} from '../helpers/stdio.js';7import {getChunksGenerator} from '../helpers/generator.js';8import {foobarObject} from '../helpers/input.js';9import {10 simpleFull,11 simpleChunks,12 simpleLines,13 simpleFullEndLines,14 noNewlinesChunks,15 getEncoding,16 stringsToUint8Arrays,17 stringsToBuffers,18 serializeResult,19} from '../helpers/lines.js';2021setFixtureDir();2223// eslint-disable-next-line max-params24const testStreamLines = async (t, fdNumber, input, expectedOutput, isUint8Array, stripFinalNewline) => {25 const {stdio} = await execa('noop-fd.js', [`${fdNumber}`, input], {26 ...fullStdio,27 lines: true,28 encoding: getEncoding(isUint8Array),29 stripFinalNewline,30 });31 const output = serializeResult(stdio[fdNumber], isUint8Array);32 t.deepEqual(output, expectedOutput);33};3435test('"lines: true" splits lines, stdout, string', testStreamLines, 1, simpleFull, simpleLines, false, false);36test('"lines: true" splits lines, stdout, Uint8Array', testStreamLines, 1, simpleFull, simpleLines, true, false);37test('"lines: true" splits lines, stderr, string', testStreamLines, 2, simpleFull, simpleLines, false, false);38test('"lines: true" splits lines, stderr, Uint8Array', testStreamLines, 2, simpleFull, simpleLines, true, false);39test('"lines: true" splits lines, stdio[*], string', testStreamLines, 3, simpleFull, simpleLines, false, false);40test('"lines: true" splits lines, stdio[*], Uint8Array', testStreamLines, 3, simpleFull, simpleLines, true, false);41test('"lines: true" splits lines, stdout, string, stripFinalNewline', testStreamLines, 1, simpleFull, noNewlinesChunks, false, true);42test('"lines: true" splits lines, stdout, Uint8Array, stripFinalNewline', testStreamLines, 1, simpleFull, noNewlinesChunks, true, true);43test('"lines: true" splits lines, stderr, string, stripFinalNewline', testStreamLines, 2, simpleFull, noNewlinesChunks, false, true);44test('"lines: true" splits lines, stderr, Uint8Array, stripFinalNewline', testStreamLines, 2, simpleFull, noNewlinesChunks, true, true);45test('"lines: true" splits lines, stdio[*], string, stripFinalNewline', testStreamLines, 3, simpleFull, noNewlinesChunks, false, true);46test('"lines: true" splits lines, stdio[*], Uint8Array, stripFinalNewline', testStreamLines, 3, simpleFull, noNewlinesChunks, true, true);4748const testStreamLinesNoop = async (t, lines, execaMethod) => {49 const {stdout} = await execaMethod('noop-fd.js', ['1', simpleFull], {lines});50 t.is(stdout, simpleFull);51};5253test('"lines: false" is a noop with execa()', testStreamLinesNoop, false, execa);54test('"lines: false" is a noop with execaSync()', testStreamLinesNoop, false, execaSync);55test('"lines: true" is a noop with execaSync()', testStreamLinesNoop, true, execaSync);5657const bigArray = Array.from({length: 1e5}).fill('.\n');58const bigString = bigArray.join('');59const bigStringNoNewlines = '.'.repeat(1e6);60const bigStringNoNewlinesEnd = `${bigStringNoNewlines}\n`;6162// eslint-disable-next-line max-params63const testStreamLinesGenerator = async (t, input, expectedLines, objectMode, binary) => {64 const {stdout} = await execa('noop.js', {65 stdout: getChunksGenerator(input, objectMode, binary),66 lines: true,67 stripFinalNewline: false,68 });69 t.deepEqual(stdout, expectedLines);70};7172test('"lines: true" works with strings generators', testStreamLinesGenerator, simpleChunks, simpleFullEndLines, false, false);73test('"lines: true" works with strings generators, objectMode', testStreamLinesGenerator, simpleChunks, simpleChunks, true, false);74test('"lines: true" works with strings generators, binary', testStreamLinesGenerator, simpleChunks, simpleLines, false, true);75test('"lines: true" works with strings generators, binary, objectMode', testStreamLinesGenerator, simpleChunks, simpleChunks, true, true);76test('"lines: true" works with big strings generators', testStreamLinesGenerator, [bigString], bigArray, false, false);77test('"lines: true" works with big strings generators, objectMode', testStreamLinesGenerator, [bigString], [bigString], true, false);78test('"lines: true" works with big strings generators without newlines', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlinesEnd], false, false);79test('"lines: true" works with big strings generators without newlines, objectMode', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlines], true, false);8081test('"lines: true" is a noop with objects generators, objectMode', async t => {82 const {stdout} = await execa('noop.js', {83 stdout: getChunksGenerator([foobarObject], true),84 lines: true,85 });86 t.deepEqual(stdout, [foobarObject]);87});8889const singleLine = 'a\n';90const singleLineStrip = 'a';9192const testOtherEncoding = async (t, stripFinalNewline, strippedLine) => {93 const {stdout} = await execa('noop-fd.js', ['1', `${singleLine}${singleLine}`], {94 lines: true,95 encoding: 'base64',96 stripFinalNewline,97 });98 t.deepEqual(stdout, [strippedLine, strippedLine]);99};100101test('"lines: true" does not work with other encodings', testOtherEncoding, false, singleLine);102test('"lines: true" does not work with other encodings, stripFinalNewline', testOtherEncoding, true, singleLineStrip);103104const getSimpleChunkSubprocess = (isUint8Array, stripFinalNewline, options) => execa('noop-fd.js', ['1', ...simpleChunks], {105 lines: true,106 encoding: getEncoding(isUint8Array),107 stripFinalNewline,108 ...options,109});110111const testAsyncIteration = async (t, expectedOutput, isUint8Array, stripFinalNewline) => {112 const subprocess = getSimpleChunkSubprocess(isUint8Array, stripFinalNewline);113 const [stdout] = await Promise.all([subprocess.stdout.toArray(), subprocess]);114 t.deepEqual(stdout, stringsToUint8Arrays(expectedOutput, isUint8Array));115};116117test('"lines: true" works with stream async iteration, string', testAsyncIteration, simpleLines, false, false);118test('"lines: true" works with stream async iteration, Uint8Array', testAsyncIteration, simpleLines, true, false);119test('"lines: true" works with stream async iteration, string, stripFinalNewline', testAsyncIteration, noNewlinesChunks, false, true);120test('"lines: true" works with stream async iteration, Uint8Array, stripFinalNewline', testAsyncIteration, noNewlinesChunks, true, true);121122const testDataEvents = async (t, expectedOutput, isUint8Array, stripFinalNewline) => {123 const subprocess = getSimpleChunkSubprocess(isUint8Array, stripFinalNewline);124 const [[firstLine]] = await Promise.all([once(subprocess.stdout, 'data'), subprocess]);125 t.deepEqual(firstLine, stringsToUint8Arrays(expectedOutput, isUint8Array)[0]);126};127128test('"lines: true" works with stream "data" events, string', testDataEvents, simpleLines, false, false);129test('"lines: true" works with stream "data" events, Uint8Array', testDataEvents, simpleLines, true, false);130test('"lines: true" works with stream "data" events, string, stripFinalNewline', testDataEvents, noNewlinesChunks, false, true);131test('"lines: true" works with stream "data" events, Uint8Array, stripFinalNewline', testDataEvents, noNewlinesChunks, true, true);132133const testWritableStream = async (t, expectedOutput, isUint8Array, stripFinalNewline) => {134 const lines = [];135 const writable = new Writable({136 write(line, encoding, done) {137 lines.push(line);138 done();139 },140 decodeStrings: false,141 });142 await getSimpleChunkSubprocess(isUint8Array, stripFinalNewline, {stdout: ['pipe', writable]});143 t.deepEqual(lines, stringsToBuffers(expectedOutput, isUint8Array));144};145146test('"lines: true" works with writable streams targets, string', testWritableStream, simpleLines, false, false);147test('"lines: true" works with writable streams targets, Uint8Array', testWritableStream, simpleLines, true, false);148test('"lines: true" works with writable streams targets, string, stripFinalNewline', testWritableStream, noNewlinesChunks, false, true);149test('"lines: true" works with writable streams targets, Uint8Array, stripFinalNewline', testWritableStream, noNewlinesChunks, true, true);
modified:test/stdio/split.js+267 added-189 removed
1import {Buffer} from 'node:buffer';2import {once} from 'node:events';3import {Writable} from 'node:stream';4import {scheduler} from 'node:timers/promises';1import test from 'ava';6import {execa, execaSync} from '../../index.js';2import {execa} from '../../index.js';3import {setFixtureDir} from '../helpers/fixtures-dir.js';8import {fullStdio, getStdio} from '../helpers/stdio.js';9import {getChunksGenerator} from '../helpers/generator.js';10import {foobarObject} from '../helpers/input.js';4import {getStdio} from '../helpers/stdio.js';5import {6 getChunksGenerator,7 getOutputsGenerator,8 noopGenerator,9 noopAsyncGenerator,10} from '../helpers/generator.js';11import {foobarString, foobarUint8Array, foobarObject, foobarObjectString} from '../helpers/input.js';12import {13 simpleFull,14 simpleChunks,15 simpleLines,16 simpleFullEndLines,17 noNewlinesChunks,18 getEncoding,19 stringsToUint8Arrays,20 serializeResult,21} from '../helpers/lines.js';2223setFixtureDir();2414const simpleChunk = ['aaa\nbbb\nccc'];15const simpleLines = ['aaa\n', 'bbb\n', 'ccc'];16const windowsChunk = ['aaa\r\nbbb\r\nccc'];25const simpleFullEnd = `${simpleFull}\n`;26const simpleFullEndChunks = [simpleFullEnd];27const windowsFull = 'aaa\r\nbbb\r\nccc';28const windowsFullEnd = `${windowsFull}\r\n`;29const windowsChunks = [windowsFull];30const windowsLines = ['aaa\r\n', 'bbb\r\n', 'ccc'];18const newlineEndChunk = ['aaa\nbbb\nccc\n'];19const newlineEndLines = ['aaa\n', 'bbb\n', 'ccc\n'];20const noNewlinesChunk = ['aaa'];21const noNewlinesChunks = ['aaa', 'bbb', 'ccc'];31const noNewlinesFull = 'aaabbbccc';32const noNewlinesFullEnd = `${noNewlinesFull}\n`;33const noNewlinesLines = ['aaabbbccc'];23const newlineChunk = ['\n'];24const newlinesChunk = ['\n\n\n'];34const singleFull = 'aaa';35const singleFullEnd = `${singleFull}\n`;36const singleFullEndWindows = `${singleFull}\r\n`;37const singleChunks = [singleFull];38const noLines = [];39const emptyFull = '';40const emptyChunks = [emptyFull];41const manyEmptyChunks = [emptyFull, emptyFull, emptyFull];42const newlineFull = '\n';43const newlineChunks = [newlineFull];44const newlinesFull = '\n\n\n';45const newlinesChunks = [newlinesFull];46const newlinesLines = ['\n', '\n', '\n'];26const windowsNewlinesChunk = ['\r\n\r\n\r\n'];47const windowsNewlinesFull = '\r\n\r\n\r\n';48const windowsNewlinesChunks = [windowsNewlinesFull];49const windowsNewlinesLines = ['\r\n', '\r\n', '\r\n'];50const runOverChunks = ['aaa\nb', 'b', 'b\nccc'];2930const bigLine = '.'.repeat(1e5);51const bigFull = '.'.repeat(1e5);52const bigFullEnd = `${bigFull}\n`;53const bigChunks = [bigFull];54const manyChunks = Array.from({length: 1e3}).fill('.');55const manyFull = manyChunks.join('');56const manyFullEnd = `${manyFull}\n`;57const manyLines = [manyFull];58const mixedNewlines = '.\n.\r\n.\n.\r\n.\n';5933const inputGenerator = async function * (input) {34 for (const inputItem of input) {35 yield inputItem;36 // eslint-disable-next-line no-await-in-loop37 await scheduler.yield();38 }39};4060const resultGenerator = function * (lines, chunk) {61 lines.push(chunk);62 yield chunk;63};6446const textEncoder = new TextEncoder();47const textDecoder = new TextDecoder();4849const getEncoding = isUint8Array => isUint8Array ? 'buffer' : 'utf8';5051const stringsToUint8Arrays = (strings, isUint8Array) => isUint8Array52 ? strings.map(string => textEncoder.encode(string))53 : strings;5455const stringsToBuffers = (strings, isUint8Array) => isUint8Array56 ? strings.map(string => Buffer.from(string))57 : strings;5859const getSimpleChunkSubprocess = (isUint8Array, options) => execa('noop-fd.js', ['1', ...simpleChunk], {60 lines: true,61 encoding: getEncoding(isUint8Array),62 ...options,63});6465const serializeResult = (result, isUint8Array, objectMode) => objectMode66 ? result.map(resultItem => serializeResultItem(resultItem, isUint8Array)).join('')67 : serializeResultItem(result, isUint8Array);6869const serializeResultItem = (resultItem, isUint8Array) => isUint8Array70 ? textDecoder.decode(resultItem)71 : resultItem;7265// eslint-disable-next-line max-params74const testLines = async (t, fdNumber, input, expectedLines, isUint8Array, objectMode) => {66const testLines = async (t, fdNumber, input, expectedLines, expectedOutput, isUint8Array, objectMode, preserveNewlines) => {67 const lines = [];68 const {stdio} = await execa('noop-fd.js', [`${fdNumber}`], {69 ...getStdio(fdNumber, [78 {transform: inputGenerator.bind(undefined, stringsToUint8Arrays(input, isUint8Array)), objectMode},79 {transform: resultGenerator.bind(undefined, lines), objectMode},70 getChunksGenerator(input, false, true),71 {transform: resultGenerator.bind(undefined, lines), preserveNewlines, objectMode},72 ]),73 encoding: getEncoding(isUint8Array),74 stripFinalNewline: false,75 });84 t.is(input.join(''), serializeResult(stdio[fdNumber], isUint8Array, objectMode));76 const output = serializeResult(stdio[fdNumber], isUint8Array);77 t.deepEqual(lines, stringsToUint8Arrays(expectedLines, isUint8Array));78 t.deepEqual(output, expectedOutput);79};8088test('Split string stdout - n newlines, 1 chunk', testLines, 1, simpleChunk, simpleLines, false, false);89test('Split string stderr - n newlines, 1 chunk', testLines, 2, simpleChunk, simpleLines, false, false);90test('Split string stdio[*] - n newlines, 1 chunk', testLines, 3, simpleChunk, simpleLines, false, false);91test('Split string stdout - no newline, n chunks', testLines, 1, noNewlinesChunks, noNewlinesLines, false, false);92test('Split string stdout - 0 newlines, 1 chunk', testLines, 1, noNewlinesChunk, noNewlinesChunk, false, false);93test('Split string stdout - Windows newlines', testLines, 1, windowsChunk, windowsLines, false, false);94test('Split string stdout - chunk ends with newline', testLines, 1, newlineEndChunk, newlineEndLines, false, false);95test('Split string stdout - single newline', testLines, 1, newlineChunk, newlineChunk, false, false);96test('Split string stdout - only newlines', testLines, 1, newlinesChunk, newlinesLines, false, false);97test('Split string stdout - only Windows newlines', testLines, 1, windowsNewlinesChunk, windowsNewlinesLines, false, false);98test('Split string stdout - line split over multiple chunks', testLines, 1, runOverChunks, simpleLines, false, false);99test('Split string stdout - 0 newlines, big line', testLines, 1, [bigLine], [bigLine], false, false);100test('Split string stdout - 0 newlines, many chunks', testLines, 1, manyChunks, [manyChunks.join('')], false, false);101test('Split Uint8Array stdout - n newlines, 1 chunk', testLines, 1, simpleChunk, simpleLines, true, false);102test('Split Uint8Array stderr - n newlines, 1 chunk', testLines, 2, simpleChunk, simpleLines, true, false);103test('Split Uint8Array stdio[*] - n newlines, 1 chunk', testLines, 3, simpleChunk, simpleLines, true, false);104test('Split Uint8Array stdout - no newline, n chunks', testLines, 1, noNewlinesChunks, noNewlinesLines, true, false);105test('Split Uint8Array stdout - 0 newlines, 1 chunk', testLines, 1, noNewlinesChunk, noNewlinesChunk, true, false);106test('Split Uint8Array stdout - Windows newlines', testLines, 1, windowsChunk, windowsLines, true, false);107test('Split Uint8Array stdout - chunk ends with newline', testLines, 1, newlineEndChunk, newlineEndLines, true, false);108test('Split Uint8Array stdout - single newline', testLines, 1, newlineChunk, newlineChunk, true, false);109test('Split Uint8Array stdout - only newlines', testLines, 1, newlinesChunk, newlinesLines, true, false);110test('Split Uint8Array stdout - only Windows newlines', testLines, 1, windowsNewlinesChunk, windowsNewlinesLines, true, false);111test('Split Uint8Array stdout - line split over multiple chunks', testLines, 1, runOverChunks, simpleLines, true, false);112test('Split Uint8Array stdout - 0 newlines, big line', testLines, 1, [bigLine], [bigLine], true, false);113test('Split Uint8Array stdout - 0 newlines, many chunks', testLines, 1, manyChunks, [manyChunks.join('')], true, false);114test('Split string stdout - n newlines, 1 chunk, objectMode', testLines, 1, simpleChunk, simpleChunk, false, true);115test('Split string stderr - n newlines, 1 chunk, objectMode', testLines, 2, simpleChunk, simpleChunk, false, true);116test('Split string stdio[*] - n newlines, 1 chunk, objectMode', testLines, 3, simpleChunk, simpleChunk, false, true);117test('Split string stdout - no newline, n chunks, objectMode', testLines, 1, noNewlinesChunks, noNewlinesChunks, false, true);118test('Split string stdout - 0 newlines, 1 chunk, objectMode', testLines, 1, noNewlinesChunk, noNewlinesChunk, false, true);119test('Split string stdout - Windows newlines, objectMode', testLines, 1, windowsChunk, windowsChunk, false, true);120test('Split string stdout - chunk ends with newline, objectMode', testLines, 1, newlineEndChunk, newlineEndChunk, false, true);121test('Split string stdout - single newline, objectMode', testLines, 1, newlineChunk, newlineChunk, false, true);122test('Split string stdout - only newlines, objectMode', testLines, 1, newlinesChunk, newlinesChunk, false, true);123test('Split string stdout - only Windows newlines, objectMode', testLines, 1, windowsNewlinesChunk, windowsNewlinesChunk, false, true);124test('Split string stdout - line split over multiple chunks, objectMode', testLines, 1, runOverChunks, runOverChunks, false, true);125test('Split string stdout - 0 newlines, big line, objectMode', testLines, 1, [bigLine], [bigLine], false, true);126test('Split string stdout - 0 newlines, many chunks, objectMode', testLines, 1, manyChunks, manyChunks, false, true);127test('Split Uint8Array stdout - n newlines, 1 chunk, objectMode', testLines, 1, simpleChunk, simpleChunk, true, true);128test('Split Uint8Array stderr - n newlines, 1 chunk, objectMode', testLines, 2, simpleChunk, simpleChunk, true, true);129test('Split Uint8Array stdio[*] - n newlines, 1 chunk, objectMode', testLines, 3, simpleChunk, simpleChunk, true, true);130test('Split Uint8Array stdout - no newline, n chunks, objectMode', testLines, 1, noNewlinesChunks, noNewlinesChunks, true, true);131test('Split Uint8Array stdout - 0 newlines, 1 chunk, objectMode', testLines, 1, noNewlinesChunk, noNewlinesChunk, true, true);132test('Split Uint8Array stdout - Windows newlines, objectMode', testLines, 1, windowsChunk, windowsChunk, true, true);133test('Split Uint8Array stdout - chunk ends with newline, objectMode', testLines, 1, newlineEndChunk, newlineEndChunk, true, true);134test('Split Uint8Array stdout - single newline, objectMode', testLines, 1, newlineChunk, newlineChunk, true, true);135test('Split Uint8Array stdout - only newlines, objectMode', testLines, 1, newlinesChunk, newlinesChunk, true, true);136test('Split Uint8Array stdout - only Windows newlines, objectMode', testLines, 1, windowsNewlinesChunk, windowsNewlinesChunk, true, true);137test('Split Uint8Array stdout - line split over multiple chunks, objectMode', testLines, 1, runOverChunks, runOverChunks, true, true);138test('Split Uint8Array stdout - 0 newlines, big line, objectMode', testLines, 1, [bigLine], [bigLine], true, true);139test('Split Uint8Array stdout - 0 newlines, many chunks, objectMode', testLines, 1, manyChunks, manyChunks, true, true);81test('Split string stdout - n newlines, 1 chunk', testLines, 1, simpleChunks, simpleLines, simpleFull, false, false, true);82test('Split string stderr - n newlines, 1 chunk', testLines, 2, simpleChunks, simpleLines, simpleFull, false, false, true);83test('Split string stdio[*] - n newlines, 1 chunk', testLines, 3, simpleChunks, simpleLines, simpleFull, false, false, true);84test('Split string stdout - preserveNewlines, n chunks', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesFull, false, false, true);85test('Split string stdout - 0 newlines, 1 chunk', testLines, 1, singleChunks, singleChunks, singleFull, false, false, true);86test('Split string stdout - empty, 1 chunk', testLines, 1, emptyChunks, noLines, emptyFull, false, false, true);87test('Split string stdout - Windows newlines', testLines, 1, windowsChunks, windowsLines, windowsFull, false, false, true);88test('Split string stdout - chunk ends with newline', testLines, 1, simpleFullEndChunks, simpleFullEndLines, simpleFullEnd, false, false, true);89test('Split string stdout - single newline', testLines, 1, newlineChunks, newlineChunks, newlineFull, false, false, true);90test('Split string stdout - only newlines', testLines, 1, newlinesChunks, newlinesLines, newlinesFull, false, false, true);91test('Split string stdout - only Windows newlines', testLines, 1, windowsNewlinesChunks, windowsNewlinesLines, windowsNewlinesFull, false, false, true);92test('Split string stdout - line split over multiple chunks', testLines, 1, runOverChunks, simpleLines, simpleFull, false, false, true);93test('Split string stdout - 0 newlines, big line', testLines, 1, bigChunks, bigChunks, bigFull, false, false, true);94test('Split string stdout - 0 newlines, many chunks', testLines, 1, manyChunks, manyLines, manyFull, false, false, true);95test('Split Uint8Array stdout - n newlines, 1 chunk', testLines, 1, simpleChunks, simpleLines, simpleFull, true, false, true);96test('Split Uint8Array stderr - n newlines, 1 chunk', testLines, 2, simpleChunks, simpleLines, simpleFull, true, false, true);97test('Split Uint8Array stdio[*] - n newlines, 1 chunk', testLines, 3, simpleChunks, simpleLines, simpleFull, true, false, true);98test('Split Uint8Array stdout - preserveNewlines, n chunks', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesFull, true, false, true);99test('Split Uint8Array stdout - empty, 1 chunk', testLines, 1, emptyChunks, noLines, emptyFull, true, false, true);100test('Split Uint8Array stdout - 0 newlines, 1 chunk', testLines, 1, singleChunks, singleChunks, singleFull, true, false, true);101test('Split Uint8Array stdout - Windows newlines', testLines, 1, windowsChunks, windowsLines, windowsFull, true, false, true);102test('Split Uint8Array stdout - chunk ends with newline', testLines, 1, simpleFullEndChunks, simpleFullEndLines, simpleFullEnd, true, false, true);103test('Split Uint8Array stdout - single newline', testLines, 1, newlineChunks, newlineChunks, newlineFull, true, false, true);104test('Split Uint8Array stdout - only newlines', testLines, 1, newlinesChunks, newlinesLines, newlinesFull, true, false, true);105test('Split Uint8Array stdout - only Windows newlines', testLines, 1, windowsNewlinesChunks, windowsNewlinesLines, windowsNewlinesFull, true, false, true);106test('Split Uint8Array stdout - line split over multiple chunks', testLines, 1, runOverChunks, simpleLines, simpleFull, true, false, true);107test('Split Uint8Array stdout - 0 newlines, big line', testLines, 1, bigChunks, bigChunks, bigFull, true, false, true);108test('Split Uint8Array stdout - 0 newlines, many chunks', testLines, 1, manyChunks, manyLines, manyFull, true, false, true);109test('Split string stdout - n newlines, 1 chunk, objectMode', testLines, 1, simpleChunks, simpleLines, simpleLines, false, true, true);110test('Split string stderr - n newlines, 1 chunk, objectMode', testLines, 2, simpleChunks, simpleLines, simpleLines, false, true, true);111test('Split string stdio[*] - n newlines, 1 chunk, objectMode', testLines, 3, simpleChunks, simpleLines, simpleLines, false, true, true);112test('Split string stdout - preserveNewlines, n chunks, objectMode', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesLines, false, true, true);113test('Split string stdout - empty, 1 chunk, objectMode', testLines, 1, emptyChunks, noLines, noLines, false, true, true);114test('Split string stdout - 0 newlines, 1 chunk, objectMode', testLines, 1, singleChunks, singleChunks, singleChunks, false, true, true);115test('Split string stdout - Windows newlines, objectMode', testLines, 1, windowsChunks, windowsLines, windowsLines, false, true, true);116test('Split string stdout - chunk ends with newline, objectMode', testLines, 1, simpleFullEndChunks, simpleFullEndLines, simpleFullEndLines, false, true, true);117test('Split string stdout - single newline, objectMode', testLines, 1, newlineChunks, newlineChunks, newlineChunks, false, true, true);118test('Split string stdout - only newlines, objectMode', testLines, 1, newlinesChunks, newlinesLines, newlinesLines, false, true, true);119test('Split string stdout - only Windows newlines, objectMode', testLines, 1, windowsNewlinesChunks, windowsNewlinesLines, windowsNewlinesLines, false, true, true);120test('Split string stdout - line split over multiple chunks, objectMode', testLines, 1, runOverChunks, simpleLines, simpleLines, false, true, true);121test('Split string stdout - 0 newlines, big line, objectMode', testLines, 1, bigChunks, bigChunks, bigChunks, false, true, true);122test('Split string stdout - 0 newlines, many chunks, objectMode', testLines, 1, manyChunks, manyLines, manyLines, false, true, true);123test('Split Uint8Array stdout - n newlines, 1 chunk, objectMode', testLines, 1, simpleChunks, simpleLines, simpleLines, true, true, true);124test('Split Uint8Array stderr - n newlines, 1 chunk, objectMode', testLines, 2, simpleChunks, simpleLines, simpleLines, true, true, true);125test('Split Uint8Array stdio[*] - n newlines, 1 chunk, objectMode', testLines, 3, simpleChunks, simpleLines, simpleLines, true, true, true);126test('Split Uint8Array stdout - preserveNewlines, n chunks, objectMode', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesLines, true, true, true);127test('Split Uint8Array stdout - empty, 1 chunk, objectMode', testLines, 1, emptyChunks, noLines, noLines, true, true, true);128test('Split Uint8Array stdout - 0 newlines, 1 chunk, objectMode', testLines, 1, singleChunks, singleChunks, singleChunks, true, true, true);129test('Split Uint8Array stdout - Windows newlines, objectMode', testLines, 1, windowsChunks, windowsLines, windowsLines, true, true, true);130test('Split Uint8Array stdout - chunk ends with newline, objectMode', testLines, 1, simpleFullEndChunks, simpleFullEndLines, simpleFullEndLines, true, true, true);131test('Split Uint8Array stdout - single newline, objectMode', testLines, 1, newlineChunks, newlineChunks, newlineChunks, true, true, true);132test('Split Uint8Array stdout - only newlines, objectMode', testLines, 1, newlinesChunks, newlinesLines, newlinesLines, true, true, true);133test('Split Uint8Array stdout - only Windows newlines, objectMode', testLines, 1, windowsNewlinesChunks, windowsNewlinesLines, windowsNewlinesLines, true, true, true);134test('Split Uint8Array stdout - line split over multiple chunks, objectMode', testLines, 1, runOverChunks, simpleLines, simpleLines, true, true, true);135test('Split Uint8Array stdout - 0 newlines, big line, objectMode', testLines, 1, bigChunks, bigChunks, bigChunks, true, true, true);136test('Split Uint8Array stdout - 0 newlines, many chunks, objectMode', testLines, 1, manyChunks, manyLines, manyLines, true, true, true);137test('Split string stdout - n newlines, 1 chunk, preserveNewlines', testLines, 1, simpleChunks, noNewlinesChunks, simpleFullEnd, false, false, false);138test('Split string stderr - n newlines, 1 chunk, preserveNewlines', testLines, 2, simpleChunks, noNewlinesChunks, simpleFullEnd, false, false, false);139test('Split string stdio[*] - n newlines, 1 chunk, preserveNewlines', testLines, 3, simpleChunks, noNewlinesChunks, simpleFullEnd, false, false, false);140test('Split string stdout - preserveNewlines, n chunks, preserveNewlines', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesFullEnd, false, false, false);141test('Split string stdout - empty, 1 chunk, preserveNewlines', testLines, 1, emptyChunks, noLines, emptyFull, false, false, false);142test('Split string stdout - 0 newlines, 1 chunk, preserveNewlines', testLines, 1, singleChunks, singleChunks, singleFullEnd, false, false, false);143test('Split string stdout - Windows newlines, preserveNewlines', testLines, 1, windowsChunks, noNewlinesChunks, windowsFullEnd, false, false, false);144test('Split string stdout - chunk ends with newline, preserveNewlines', testLines, 1, simpleFullEndChunks, noNewlinesChunks, simpleFullEnd, false, false, false);145test('Split string stdout - single newline, preserveNewlines', testLines, 1, newlineChunks, emptyChunks, newlineFull, false, false, false);146test('Split string stdout - only newlines, preserveNewlines', testLines, 1, newlinesChunks, manyEmptyChunks, newlinesFull, false, false, false);147test('Split string stdout - only Windows newlines, preserveNewlines', testLines, 1, windowsNewlinesChunks, manyEmptyChunks, windowsNewlinesFull, false, false, false);148test('Split string stdout - line split over multiple chunks, preserveNewlines', testLines, 1, runOverChunks, noNewlinesChunks, simpleFullEnd, false, false, false);149test('Split string stdout - 0 newlines, big line, preserveNewlines', testLines, 1, bigChunks, bigChunks, bigFullEnd, false, false, false);150test('Split string stdout - 0 newlines, many chunks, preserveNewlines', testLines, 1, manyChunks, manyLines, manyFullEnd, false, false, false);151test('Split Uint8Array stdout - n newlines, 1 chunk, preserveNewlines', testLines, 1, simpleChunks, noNewlinesChunks, simpleFullEnd, true, false, false);152test('Split Uint8Array stderr - n newlines, 1 chunk, preserveNewlines', testLines, 2, simpleChunks, noNewlinesChunks, simpleFullEnd, true, false, false);153test('Split Uint8Array stdio[*] - n newlines, 1 chunk, preserveNewlines', testLines, 3, simpleChunks, noNewlinesChunks, simpleFullEnd, true, false, false);154test('Split Uint8Array stdout - preserveNewlines, n chunks, preserveNewlines', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesFullEnd, true, false, false);155test('Split Uint8Array stdout - empty, 1 chunk, preserveNewlines', testLines, 1, emptyChunks, noLines, emptyFull, true, false, false);156test('Split Uint8Array stdout - 0 newlines, 1 chunk, preserveNewlines', testLines, 1, singleChunks, singleChunks, singleFullEnd, true, false, false);157test('Split Uint8Array stdout - Windows newlines, preserveNewlines', testLines, 1, windowsChunks, noNewlinesChunks, windowsFullEnd, true, false, false);158test('Split Uint8Array stdout - chunk ends with newline, preserveNewlines', testLines, 1, simpleFullEndChunks, noNewlinesChunks, simpleFullEnd, true, false, false);159test('Split Uint8Array stdout - single newline, preserveNewlines', testLines, 1, newlineChunks, emptyChunks, newlineFull, true, false, false);160test('Split Uint8Array stdout - only newlines, preserveNewlines', testLines, 1, newlinesChunks, manyEmptyChunks, newlinesFull, true, false, false);161test('Split Uint8Array stdout - only Windows newlines, preserveNewlines', testLines, 1, windowsNewlinesChunks, manyEmptyChunks, windowsNewlinesFull, true, false, false);162test('Split Uint8Array stdout - line split over multiple chunks, preserveNewlines', testLines, 1, runOverChunks, noNewlinesChunks, simpleFullEnd, true, false, false);163test('Split Uint8Array stdout - 0 newlines, big line, preserveNewlines', testLines, 1, bigChunks, bigChunks, bigFullEnd, true, false, false);164test('Split Uint8Array stdout - 0 newlines, many chunks, preserveNewlines', testLines, 1, manyChunks, manyLines, manyFullEnd, true, false, false);165test('Split string stdout - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 1, simpleChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);166test('Split string stderr - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 2, simpleChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);167test('Split string stdio[*] - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 3, simpleChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);168test('Split string stdout - preserveNewlines, n chunks, objectMode, preserveNewlines', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesLines, false, true, false);169test('Split string stdout - empty, 1 chunk, objectMode, preserveNewlines', testLines, 1, emptyChunks, noLines, noLines, false, true, false);170test('Split string stdout - 0 newlines, 1 chunk, objectMode, preserveNewlines', testLines, 1, singleChunks, singleChunks, singleChunks, false, true, false);171test('Split string stdout - Windows newlines, objectMode, preserveNewlines', testLines, 1, windowsChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);172test('Split string stdout - chunk ends with newline, objectMode, preserveNewlines', testLines, 1, simpleFullEndChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);173test('Split string stdout - single newline, objectMode, preserveNewlines', testLines, 1, newlineChunks, emptyChunks, emptyChunks, false, true, false);174test('Split string stdout - only newlines, objectMode, preserveNewlines', testLines, 1, newlinesChunks, manyEmptyChunks, manyEmptyChunks, false, true, false);175test('Split string stdout - only Windows newlines, objectMode, preserveNewlines', testLines, 1, windowsNewlinesChunks, manyEmptyChunks, manyEmptyChunks, false, true, false);176test('Split string stdout - line split over multiple chunks, objectMode, preserveNewlines', testLines, 1, runOverChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);177test('Split string stdout - 0 newlines, big line, objectMode, preserveNewlines', testLines, 1, bigChunks, bigChunks, bigChunks, false, true, false);178test('Split string stdout - 0 newlines, many chunks, objectMode, preserveNewlines', testLines, 1, manyChunks, manyLines, manyLines, false, true, false);179test('Split Uint8Array stdout - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 1, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);180test('Split Uint8Array stderr - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 2, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);181test('Split Uint8Array stdio[*] - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 3, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);182test('Split Uint8Array stdout - preserveNewlines, n chunks, objectMode, preserveNewlines', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesLines, true, true, false);183test('Split Uint8Array stdout - empty, 1 chunk, objectMode, preserveNewlines', testLines, 1, emptyChunks, noLines, noLines, true, true, false);184test('Split Uint8Array stdout - 0 newlines, 1 chunk, objectMode, preserveNewlines', testLines, 1, singleChunks, singleChunks, singleChunks, true, true, false);185test('Split Uint8Array stdout - Windows newlines, objectMode, preserveNewlines', testLines, 1, windowsChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);186test('Split Uint8Array stdout - chunk ends with newline, objectMode, preserveNewlines', testLines, 1, simpleFullEndChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);187test('Split Uint8Array stdout - single newline, objectMode, preserveNewlines', testLines, 1, newlineChunks, emptyChunks, emptyChunks, true, true, false);188test('Split Uint8Array stdout - only newlines, objectMode, preserveNewlines', testLines, 1, newlinesChunks, manyEmptyChunks, manyEmptyChunks, true, true, false);189test('Split Uint8Array stdout - only Windows newlines, objectMode, preserveNewlines', testLines, 1, windowsNewlinesChunks, manyEmptyChunks, manyEmptyChunks, true, true, false);190test('Split Uint8Array stdout - line split over multiple chunks, objectMode, preserveNewlines', testLines, 1, runOverChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);191test('Split Uint8Array stdout - 0 newlines, big line, objectMode, preserveNewlines', testLines, 1, bigChunks, bigChunks, bigChunks, true, true, false);192test('Split Uint8Array stdout - 0 newlines, many chunks, objectMode, preserveNewlines', testLines, 1, manyChunks, manyLines, manyLines, true, true, false);193194// eslint-disable-next-line max-params142const testBinaryOption = async (t, binary, input, expectedLines, objectMode) => {195const testBinaryOption = async (t, binary, input, expectedLines, expectedOutput, objectMode, preserveNewlines) => {196 const lines = [];144 const {stdout} = await execa('noop-fd.js', ['1'], {197 const {stdout} = await execa('noop.js', {198 stdout: [146 {transform: inputGenerator.bind(undefined, input), objectMode},147 {transform: resultGenerator.bind(undefined, lines), objectMode, binary},199 getChunksGenerator(input, false, true),200 {transform: resultGenerator.bind(undefined, lines), binary, preserveNewlines, objectMode},201 ],202 stripFinalNewline: false,203 });150 t.is(input.join(''), objectMode ? stdout.join('') : stdout);204 t.deepEqual(lines, expectedLines);205 t.deepEqual(stdout, expectedOutput);206};207154test('Does not split lines when "binary" is true', testBinaryOption, true, simpleChunk, simpleChunk, false);155test('Splits lines when "binary" is false', testBinaryOption, false, simpleChunk, simpleLines, false);156test('Splits lines when "binary" is undefined', testBinaryOption, undefined, simpleChunk, simpleLines, false);157test('Does not split lines when "binary" is true, objectMode', testBinaryOption, true, simpleChunk, simpleChunk, true);158test('Splits lines when "binary" is false, objectMode', testBinaryOption, false, simpleChunk, simpleChunk, true);159test('Splits lines when "binary" is undefined, objectMode', testBinaryOption, undefined, simpleChunk, simpleChunk, true);208test('Does not split lines when "binary" is true', testBinaryOption, true, simpleChunks, simpleChunks, simpleFull, false, true);209test('Splits lines when "binary" is false', testBinaryOption, false, simpleChunks, simpleLines, simpleFull, false, true);210test('Splits lines when "binary" is undefined', testBinaryOption, undefined, simpleChunks, simpleLines, simpleFull, false, true);211test('Does not split lines when "binary" is true, objectMode', testBinaryOption, true, simpleChunks, simpleChunks, simpleChunks, true, true);212test('Splits lines when "binary" is false, objectMode', testBinaryOption, false, simpleChunks, simpleLines, simpleLines, true, true);213test('Splits lines when "binary" is undefined, objectMode', testBinaryOption, undefined, simpleChunks, simpleLines, simpleLines, true, true);214test('Does not split lines when "binary" is true, preserveNewlines', testBinaryOption, true, simpleChunks, simpleChunks, simpleFull, false, false);215test('Splits lines when "binary" is false, preserveNewlines', testBinaryOption, false, simpleChunks, noNewlinesChunks, simpleFullEnd, false, false);216test('Splits lines when "binary" is undefined, preserveNewlines', testBinaryOption, undefined, simpleChunks, noNewlinesChunks, simpleFullEnd, false, false);217test('Does not split lines when "binary" is true, objectMode, preserveNewlines', testBinaryOption, true, simpleChunks, simpleChunks, simpleChunks, true, false);218test('Splits lines when "binary" is false, objectMode, preserveNewlines', testBinaryOption, false, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, false);219test('Splits lines when "binary" is undefined, objectMode, preserveNewlines', testBinaryOption, undefined, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, false);220161// eslint-disable-next-line max-params162const testStreamLines = async (t, fdNumber, input, expectedLines, isUint8Array) => {163 const {stdio} = await execa('noop-fd.js', [`${fdNumber}`, input], {164 ...fullStdio,221const resultStringGenerator = function * (lines, chunk) {222 lines.push(chunk);223 yield new TextDecoder().decode(chunk);224};225226const testUint8ArrayToString = async (t, expectedOutput, objectMode, preserveNewlines) => {227 const lines = [];228 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {229 stdout: {230 transform: resultStringGenerator.bind(undefined, lines),231 objectMode,232 preserveNewlines,233 },234 encoding: 'buffer',235 lines: true,166 encoding: getEncoding(isUint8Array),236 });168 t.deepEqual(stdio[fdNumber], stringsToUint8Arrays(expectedLines, isUint8Array));237 t.deepEqual(lines, [foobarUint8Array]);238 t.deepEqual(stdout, expectedOutput);239};240171test('"lines: true" splits lines, stdout, string', testStreamLines, 1, simpleChunk[0], simpleLines, false);172test('"lines: true" splits lines, stdout, Uint8Array', testStreamLines, 1, simpleChunk[0], simpleLines, true);173test('"lines: true" splits lines, stderr, string', testStreamLines, 2, simpleChunk[0], simpleLines, false);174test('"lines: true" splits lines, stderr, Uint8Array', testStreamLines, 2, simpleChunk[0], simpleLines, true);175test('"lines: true" splits lines, stdio[*], string', testStreamLines, 3, simpleChunk[0], simpleLines, false);176test('"lines: true" splits lines, stdio[*], Uint8Array', testStreamLines, 3, simpleChunk[0], simpleLines, true);241test('Line splitting when converting from Uint8Array to string', testUint8ArrayToString, [foobarUint8Array], false, true);242test('Line splitting when converting from Uint8Array to string, objectMode', testUint8ArrayToString, [foobarString], true, true);243test('Line splitting when converting from Uint8Array to string, preserveNewlines', testUint8ArrayToString, [foobarUint8Array], false, false);244test('Line splitting when converting from Uint8Array to string, objectMode, preserveNewlines', testUint8ArrayToString, [foobarString], true, false);245178const testStreamLinesNoop = async (t, lines, execaMethod) => {179 const {stdout} = await execaMethod('noop-fd.js', ['1', simpleChunk[0]], {lines});180 t.is(stdout, simpleChunk[0]);246const resultUint8ArrayGenerator = function * (lines, chunk) {247 lines.push(chunk);248 yield new TextEncoder().encode(chunk);249};250183test('"lines: false" is a noop with execa()', testStreamLinesNoop, false, execa);184test('"lines: false" is a noop with execaSync()', testStreamLinesNoop, false, execaSync);185test('"lines: true" is a noop with execaSync()', testStreamLinesNoop, true, execaSync);251const testStringToUint8Array = async (t, expectedOutput, objectMode, preserveNewlines) => {252 const lines = [];253 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {254 stdout: {255 transform: resultUint8ArrayGenerator.bind(undefined, lines),256 objectMode,257 preserveNewlines,258 },259 lines: true,260 });261 t.deepEqual(lines, [foobarString]);262 t.deepEqual(stdout, expectedOutput);263};264187const bigArray = Array.from({length: 1e5}).fill('.\n');188const bigString = bigArray.join('');189const bigStringNoNewlines = '.'.repeat(1e6);265test('Line splitting when converting from string to Uint8Array', testStringToUint8Array, [foobarString], false, true);266test('Line splitting when converting from string to Uint8Array, objectMode', testStringToUint8Array, [foobarUint8Array], true, true);267test('Line splitting when converting from string to Uint8Array, preserveNewlines', testStringToUint8Array, [foobarString], false, false);268test('Line splitting when converting from string to Uint8Array, objectMode, preserveNewlines', testStringToUint8Array, [foobarUint8Array], true, false);269191// eslint-disable-next-line max-params192const testStreamLinesGenerator = async (t, input, expectedLines, objectMode, binary) => {193 const {stdout} = await execa('noop.js', {lines: true, stdout: getChunksGenerator(input, objectMode, binary)});194 t.deepEqual(stdout, expectedLines);270const testStripNewline = async (t, input, expectedOutput) => {271 const {stdout} = await execa('noop.js', {272 stdout: getChunksGenerator([input]),273 stripFinalNewline: false,274 });275 t.is(stdout, expectedOutput);276};277197test('"lines: true" works with strings generators', testStreamLinesGenerator, simpleChunk, simpleLines, false, false);198test('"lines: true" works with strings generators, objectMode', testStreamLinesGenerator, simpleChunk, simpleChunk, true, false);199test('"lines: true" works with strings generators, binary', testStreamLinesGenerator, simpleChunk, simpleLines, false, true);200test('"lines: true" works with strings generators, binary, objectMode', testStreamLinesGenerator, simpleChunk, simpleChunk, true, true);201test('"lines: true" works with big strings generators', testStreamLinesGenerator, [bigString], bigArray, false, false);202test('"lines: true" works with big strings generators, objectMode', testStreamLinesGenerator, [bigString], [bigString], true, false);203test('"lines: true" works with big strings generators without newlines', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlines], false, false);204test('"lines: true" works with big strings generators without newlines, objectMode', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlines], true, false);278test('Strips newline when user do not mistakenly yield one at the end', testStripNewline, singleFull, singleFullEnd);279test('Strips newline when user mistakenly yielded one at the end', testStripNewline, singleFullEnd, singleFullEnd);280test('Strips newline when user mistakenly yielded one at the end, Windows newline', testStripNewline, singleFullEndWindows, singleFullEndWindows);281206test('"lines: true" is a noop with objects generators, objectMode', async t => {207 const {stdout} = await execa('noop.js', {lines: true, stdout: getChunksGenerator([foobarObject], true)});208 t.deepEqual(stdout, [foobarObject]);209});282const testMixNewlines = async (t, generator) => {283 const {stdout} = await execa('noop-fd.js', ['1', mixedNewlines], {284 stdout: generator(),285 stripFinalNewline: false,286 });287 t.is(stdout, mixedNewlines);288};289211const singleLine = 'a\n';290test('Can mix Unix and Windows newlines', testMixNewlines, noopGenerator);291test('Can mix Unix and Windows newlines, async', testMixNewlines, noopAsyncGenerator);292213test('"lines: true" does not work with other encodings', async t => {214 const {stdout} = await execa('noop-fd.js', ['1', `${singleLine}${singleLine}`], {lines: true, encoding: 'base64'});215 t.deepEqual(stdout, [singleLine, singleLine]);216});217218const testAsyncIteration = async (t, isUint8Array) => {219 const subprocess = getSimpleChunkSubprocess(isUint8Array);220 const [stdout] = await Promise.all([subprocess.stdout.toArray(), subprocess]);221 t.deepEqual(stdout, stringsToUint8Arrays(simpleLines, isUint8Array));293const serializeResultGenerator = function * (lines, chunk) {294 lines.push(chunk);295 yield JSON.stringify(chunk);296};297224test('"lines: true" works with stream async iteration, string', testAsyncIteration, false);225test('"lines: true" works with stream async iteration, Uint8Array', testAsyncIteration, true);226227const testDataEvents = async (t, isUint8Array) => {228 const subprocess = getSimpleChunkSubprocess(isUint8Array);229 const [[firstLine]] = await Promise.all([once(subprocess.stdout, 'data'), subprocess]);230 t.deepEqual(firstLine, stringsToUint8Arrays(simpleLines, isUint8Array)[0]);298const testUnsetObjectMode = async (t, expectedOutput, preserveNewlines) => {299 const lines = [];300 const {stdout} = await execa('noop.js', {301 stdout: [302 getChunksGenerator([foobarObject], true),303 {transform: serializeResultGenerator.bind(undefined, lines), preserveNewlines, objectMode: false},304 ],305 stripFinalNewline: false,306 });307 t.deepEqual(lines, [foobarObject]);308 t.is(stdout, expectedOutput);309};310233test('"lines: true" works with stream "data" events, string', testDataEvents, false);234test('"lines: true" works with stream "data" events, Uint8Array', testDataEvents, true);311test('Can switch from objectMode to non-objectMode', testUnsetObjectMode, `${foobarObjectString}\n`, false);312test('Can switch from objectMode to non-objectMode, preserveNewlines', testUnsetObjectMode, foobarObjectString, true);313236const testWritableStream = async (t, isUint8Array) => {314const testYieldArray = async (t, input, expectedLines, expectedOutput) => {315 const lines = [];238 const writable = new Writable({239 write(line, encoding, done) {240 lines.push(line);241 done();242 },243 decodeStrings: false,316 const {stdout} = await execa('noop.js', {317 stdout: [318 getOutputsGenerator(input),319 resultGenerator.bind(undefined, lines),320 ],321 stripFinalNewline: false,322 });245 await getSimpleChunkSubprocess(isUint8Array, {stdout: ['pipe', writable]});246 t.deepEqual(lines, stringsToBuffers(simpleLines, isUint8Array));323 t.deepEqual(lines, expectedLines);324 t.deepEqual(stdout, expectedOutput);325};326249test('"lines: true" works with writable streams targets, string', testWritableStream, false);250test('"lines: true" works with writable streams targets, Uint8Array', testWritableStream, true);327test('Can use "yield* array" to produce multiple lines', testYieldArray, [foobarString, foobarString], [foobarString, foobarString], `${foobarString}\n${foobarString}\n`);328test('Can use "yield* array" to produce empty lines', testYieldArray, [foobarString, ''], [foobarString, ''], `${foobarString}\n\n`);
modified:test/stdio/transform.js+10 added-4 removed
30 unmodified lines
31test('Generators "final" can be used', testGeneratorFinal, 'noop.js');32test('Generators "final" is used even on empty streams', testGeneratorFinal, 'empty.js');3334const testFinalAlone = async (t, final) => {35 const {stdout} = await execa('noop-fd.js', ['1', '.'], {stdout: {final: final(foobarString)}});35 const {stdout} = await execa('noop-fd.js', ['1', '.'], {stdout: {final: final(foobarString), binary: true}});36 t.is(stdout, `.${foobarString}`);37};3839test('Generators "final" can be used without "transform"', testFinalAlone, identityGenerator);
60 unmodified lines
100};101102const testMultipleYields = async (t, final) => {103 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: convertTransformToFinal(multipleYieldGenerator, final)});104 t.is(stdout, `${prefix}${foobarString}${suffix}`);104 t.is(stdout, `${prefix}\n${foobarString}\n${suffix}`);105};106107test('Generator can yield "transform" multiple times at different moments', testMultipleYields, false);108test('Generator can yield "final" multiple times at different moments', testMultipleYields, true);
29 unmodified lines
138const maxBuffer = 10;139140test('Generators take "maxBuffer" into account', async t => {141 const bigString = '.'.repeat(maxBuffer);142 const {stdout} = await execa('noop.js', {maxBuffer, stdout: getOutputGenerator(bigString, false)});142 const {stdout} = await execa('noop.js', {143 maxBuffer,144 stdout: getOutputGenerator(bigString, false, true),145 });146 t.is(stdout, bigString);147148 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputGenerator(`${bigString}.`, false)}));149});150151test('Generators take "maxBuffer" into account, objectMode', async t => {152 const bigArray = Array.from({length: maxBuffer}).fill('.');150 const {stdout} = await execa('noop.js', {maxBuffer, stdout: getOutputsGenerator(bigArray, true)});153 const {stdout} = await execa('noop.js', {154 maxBuffer,155 stdout: getOutputsGenerator(bigArray, true, true),156 });157 t.is(stdout.length, maxBuffer);158159 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputsGenerator([...bigArray, ''], true)}));160});
64 unmodified lines
modified:test/stdio/type.js+3 added-7 removed
1import test from 'ava';2import {execa, execaSync} from '../../index.js';3import {getStdio} from '../helpers/stdio.js';4import {noopGenerator} from '../helpers/generator.js';4import {noopGenerator, uppercaseGenerator} from '../helpers/generator.js';5import {setFixtureDir} from '../helpers/fixtures-dir.js';67setFixtureDir();89const uppercaseGenerator = function * (line) {10 yield line.toUpperCase();11};129const testInvalidGenerator = (t, fdNumber, stdioOption) => {10 t.throws(() => {11 execa('empty.js', getStdio(fdNumber, {...noopGenerator(), ...stdioOption}));12 }, {message: /must be a generator/});
9 unmodified lines
22test('Cannot use invalid "final" with stdio[*]', testInvalidGenerator, 3, {final: true});2324const testInvalidBinary = (t, fdNumber, optionName) => {25 t.throws(() => {30 execa('empty.js', getStdio(fdNumber, {transform: uppercaseGenerator, [optionName]: 'true'}));26 execa('empty.js', getStdio(fdNumber, {...uppercaseGenerator(), [optionName]: 'true'}));27 }, {message: /a boolean/});28};2930test('Cannot use invalid "binary" with stdin', testInvalidBinary, 0, 'binary');
6 unmodified lines
37test('Cannot use invalid "objectMode" with stdio[*]', testInvalidBinary, 3, 'objectMode');3839const testSyncMethods = (t, fdNumber) => {40 t.throws(() => {45 execaSync('empty.js', getStdio(fdNumber, uppercaseGenerator));41 execaSync('empty.js', getStdio(fdNumber, uppercaseGenerator()));42 }, {message: /cannot be a generator/});43};4445test('Cannot use generators with sync methods and stdin', testSyncMethods, 0);
3 unmodified lines
modified:test/stdio/validate.js+1 added-1 removed
27 unmodified lines
28 return 'a string or an Uint8Array';29};3031const lastInputGenerator = (input, objectMode) => [foobarUint8Array, getOutputGenerator(input, objectMode)];32const inputGenerator = (input, objectMode) => [...lastInputGenerator(input, objectMode), serializeGenerator];32const inputGenerator = (input, objectMode) => [...lastInputGenerator(input, objectMode), serializeGenerator(true)];3334test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputGenerator, foobarObject, false, true);35test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputGenerator, foobarObject, false, true);36test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputGenerator, foobarObject, true, true);
24 unmodified lines
modified:index.d.ts+3 added
26 unmodified lines
27type StdioTransformFull = {28 transform: StdioTransform;29 final?: StdioFinal;30 binary?: boolean;31 preserveNewlines?: boolean;32 objectMode?: boolean;33};3435type CommonStdioOption<IsSync extends boolean = boolean> =
383 unmodified lines
419420 /**421 Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from the output.422423 If the `lines` option is true, this applies to each output line instead.424425 @default true426 */427 readonly stripFinalNewline?: boolean;428
1100 unmodified lines
modified:index.test-d.ts+11 added
1067 unmodified lines
1068execa('unicorns', {stdin: {transform: unknownGenerator, final: asyncFinal}});1069expectError(execaSync('unicorns', {stdin: {transform: unknownGenerator, final: asyncFinal}}));1070expectError(execa('unicorns', {stdin: {}}));1071expectError(execa('unicorns', {stdin: {binary: true}}));1072expectError(execa('unicorns', {stdin: {preserveNewlines: true}}));1073expectError(execa('unicorns', {stdin: {objectMode: true}}));1074expectError(execa('unicorns', {stdin: {final: unknownFinal}}));1075execa('unicorns', {stdin: {transform: unknownGenerator, binary: true}});1076expectError(execa('unicorns', {stdin: {transform: unknownGenerator, binary: 'true'}}));1077execa('unicorns', {stdin: {transform: unknownGenerator, preserveNewlines: true}});1078expectError(execa('unicorns', {stdin: {transform: unknownGenerator, preserveNewlines: 'true'}}));1079execa('unicorns', {stdin: {transform: unknownGenerator, objectMode: true}});1080expectError(execa('unicorns', {stdin: {transform: unknownGenerator, objectMode: 'true'}}));1081execa('unicorns', {stdin: undefined});1082execaSync('unicorns', {stdin: undefined});
86 unmodified lines
1169execa('unicorns', {stdout: {transform: unknownGenerator, final: asyncFinal}});1170expectError(execaSync('unicorns', {stdout: {transform: unknownGenerator, final: asyncFinal}}));1171expectError(execa('unicorns', {stdout: {}}));1172expectError(execa('unicorns', {stdout: {binary: true}}));1173expectError(execa('unicorns', {stdout: {preserveNewlines: true}}));1174expectError(execa('unicorns', {stdout: {objectMode: true}}));1175expectError(execa('unicorns', {stdout: {final: unknownFinal}}));1176execa('unicorns', {stdout: {transform: unknownGenerator, binary: true}});1177expectError(execa('unicorns', {stdout: {transform: unknownGenerator, binary: 'true'}}));1178execa('unicorns', {stdout: {transform: unknownGenerator, preserveNewlines: true}});1179expectError(execa('unicorns', {stdout: {transform: unknownGenerator, preserveNewlines: 'true'}}));1180execa('unicorns', {stdout: {transform: unknownGenerator, objectMode: true}});1181expectError(execa('unicorns', {stdout: {transform: unknownGenerator, objectMode: 'true'}}));1182execa('unicorns', {stdout: undefined});1183execaSync('unicorns', {stdout: undefined});
86 unmodified lines
1270execa('unicorns', {stderr: {transform: unknownGenerator, final: asyncFinal}});1271expectError(execaSync('unicorns', {stderr: {transform: unknownGenerator, final: asyncFinal}}));1272expectError(execa('unicorns', {stderr: {}}));1273expectError(execa('unicorns', {stderr: {binary: true}}));1274expectError(execa('unicorns', {stderr: {preserveNewlines: true}}));1275expectError(execa('unicorns', {stderr: {objectMode: true}}));1276expectError(execa('unicorns', {stderr: {final: unknownFinal}}));1277execa('unicorns', {stderr: {transform: unknownGenerator, binary: true}});1278expectError(execa('unicorns', {stderr: {transform: unknownGenerator, binary: 'true'}}));1279execa('unicorns', {stderr: {transform: unknownGenerator, preserveNewlines: true}});1280expectError(execa('unicorns', {stderr: {transform: unknownGenerator, preserveNewlines: 'true'}}));1281execa('unicorns', {stderr: {transform: unknownGenerator, objectMode: true}});1282expectError(execa('unicorns', {stderr: {transform: unknownGenerator, objectMode: 'true'}}));1283execa('unicorns', {stderr: undefined});1284execaSync('unicorns', {stderr: undefined});
87 unmodified lines
1372 1,1373 unknownGenerator,1374 {transform: unknownGenerator},1375 {transform: unknownGenerator, binary: true},1376 {transform: unknownGenerator, preserveNewlines: true},1377 {transform: unknownGenerator, objectMode: true},1378 {transform: unknownGenerator, final: unknownFinal},1379 undefined,1380 fileUrl,
42 unmodified lines
1423 [1],1424 [unknownGenerator],1425 [{transform: unknownGenerator}],1426 [{transform: unknownGenerator, binary: true}],1427 [{transform: unknownGenerator, preserveNewlines: true}],1428 [{transform: unknownGenerator, objectMode: true}],1429 [{transform: unknownGenerator, final: unknownFinal}],1430 [undefined],1431 [fileUrl],
211 unmodified lines
modified:readme.md+2 added
916 unmodified lines
917Default: `true`918919Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from the output.920921If the [`lines` option](#lines) is true, this applies to each output line instead.922923#### maxBuffer924925Type: `number`\926Default: `100_000_000` (100 MB)
234 unmodified lines