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
Provenance
- Commit
26e9150- Tree
bb4b386- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
98f403c
27 files changed+680 added−288 removed
modified:docs/transform.md+60 added
52 unmodified lines535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912068 unmodified lines52 unmodified lines- Text: But the transform works even if a line or word is split across multiple chunks.
Please note the [`lines`](../readme.md#lines) option is unrelated: it has no impact on transforms.
## Newlines
Unless [`{transform, binary: true}`](#binary-data) is used, the transform iterates over lines.By default, newlines are stripped from each `line` argument.
```js// `line`'s value never ends with '\n'.const transform = function * (line) { /* ... */ };
await execa('./run.js', {stdout: transform});```
However, if a `{transform, preserveNewlines: true}` plain object is passed, newlines are kept.
```js// `line`'s value ends with '\n'.// The output's last `line` might or might not end with '\n', depending on the output.const transform = function * (line) { /* ... */ };
await execa('./run.js', {stdout: {transform, preserveNewlines: true}});```
Each `yield` produces at least one line. Calling `yield` multiple times or calling `yield *` produces multiples lines.
```jsconst transform = function * (line) { yield 'Important note:'; yield 'Read the comments below.';
// Or: yield * [ 'Important note:', 'Read the comments below.', ];
// Is the same as: yield 'Important note:\nRead the comments below.\n';
yield line};
await execa('./run.js', {stdout: transform});```
However, if a `{transform, preserveNewlines: true}` plain object is passed, multiple `yield`s produce a single line instead.
```jsconst transform = function * (line) { yield 'Important note: '; yield 'Read the comments below.\n';
// Is the same as: yield 'Important note: Read the comments below.\n';
yield line};
await execa('./run.js', {stdout: {transform, preserveNewlines: true}});```
## Object mode
By default, `stdout` and `stderr`'s transforms must return a string or an `Uint8Array`. However, if a `{transform, objectMode: true}` plain object is passed, any type can be returned instead, except `null` or `undefined`. The subprocess' [`stdout`](../readme.md#stdout)/[`stderr`](../readme.md#stderr) will be an array of values.
68 unmodified linesmodified:lib/return/error.js+1 added-1 removed
178 unmodified lines179 return escapeLines(fixCwdError(originalMessage, cwd));180 };181 182 const 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);185 186 const serializeMessageItem = messageItem => {187 if (typeof messageItem === 'string') {9 unmodified linesmodified:lib/stdio/generator.js+7 added-5 removed
1 import {generatorsToTransform} from './transform.js';2 import {getEncodingTransformGenerator} from './encoding-transform.js';3-import {getLinesGenerator} from './split.js';3+import {getSplitLinesGenerator, getAppendNewlineGenerator} from './split.js';4 import {pipeStreams} from './pipeline.js';5 import {isGeneratorOptions, isAsyncGenerator} from './type.js';6 import {getValidateTransformReturn} from './validate.js';7 10 unmodified lines18 return [...nonGenerators, ...sortGenerators(newGenerators)];19 };20 21 const 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 };28 29 /*30 `objectMode` determines the return value's type, i.e. the `readableObjectMode`.36 unmodified lines67 68 Chunks are currently processed serially. We could add a `concurrency` option to parallelize in the future.69 */70 export 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 linesmodified:lib/stdio/handle.js+1 added-1 removed
2 unmodified lines3 import {addStreamDirection} from './direction.js';4 import {normalizeStdio} from './option.js';5 import {handleNativeStream} from './native.js';6 import {handleInputOptions} from './input.js';7-import {handleStreamsLines} from './split.js';7+import {handleStreamsLines} from './lines.js';8 import {handleStreamsEncoding} from './encoding-final.js';9 import {normalizeGenerators} from './generator.js';10 import {forwardStdio} from './forward.js';11 88 unmodified linesadded:lib/stdio/lines.js+23 added
1234567891011121314151617181920212223import {willPipeStreams} from './forward.js';
// Split chunks line-wise for streams exposed to users like `subprocess.stdout`.// Appending a noop transform in object mode is enough to do this, since every non-binary transform iterates line-wise.export const handleStreamsLines = (stdioStreams, {lines, stripFinalNewline}, isSync) => shouldSplitLines(stdioStreams, lines, isSync) ? [ ...stdioStreams, { ...stdioStreams[0], type: 'generator', value: {transform: linesEndGenerator, objectMode: true, preserveNewlines: !stripFinalNewline}, }, ] : stdioStreams;
const shouldSplitLines = (stdioStreams, lines, isSync) => stdioStreams[0].direction === 'output' && lines && !isSync && willPipeStreams(stdioStreams);
const linesEndGenerator = function * (chunk) { yield chunk;};modified:lib/stdio/split.js+48 added-35 removed
1 import {isUint8Array} from '../utils.js';2-import {willPipeStreams} from './forward.js';2 4-// 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.6-export 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;16-17-const shouldSplitLines = (stdioStreams, lines, isSync) => stdioStreams[0].direction === 'output'18- && lines19- && !isSync20- && willPipeStreams(stdioStreams);21-22-const linesEndGenerator = function * (chunk) {23- yield chunk;24-};25-3 // Split chunks line-wise for generators passed to the `std*` options27-export const getLinesGenerator = (encoding, writableObjectMode, binary) => {4+export const getSplitLinesGenerator = ({encoding, binary, preserveNewlines, writableObjectMode, state}) => {5 if (binary || writableObjectMode) {6 return;7 }8 9 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 lines22 };23 24 const 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 };33 34 const concatString = (firstChunk, secondChunk) => `${firstChunk}${secondChunk}`;35 const isString = chunk => typeof chunk === 'string';36 37 const 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 };46 47+const linesInfo = [linesStringInfo, linesUint8ArrayInfo];48+49 // This imperative logic is much faster than using `String.split()` and uses very low memory.50 // Also, it allows sharing it with `Uint8Array`.66-const linesGenerator = function * (state, {emptyValue, newline, concat}, chunk) {51+const splitGenerator = function * (state, preserveNewlines, {emptyValue, CR, LF, concatBytes}, chunk) {52 let {previousChunks} = state;53 let start = -1;54 55 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);59 60 if (previousChunks.length > 0) {75- line = concat(previousChunks, line);61+ line = concatBytes(previousChunks, line);62 previousChunks = emptyValue;63 }64 65 yield line;1 unmodified line67 }68 }69 70 if (start !== chunk.length - 1) {85- previousChunks = concat(previousChunks, chunk.slice(start + 1));71+ previousChunks = concatBytes(previousChunks, chunk.slice(start + 1));72 }73 74 state.previousChunks = previousChunks;75 };76 77+const getNewlineLength = ({chunk, end, CR, preserveNewlines, state}) => {78+ if (preserveNewlines) {79+ return 0;80+ }81+82+ state.isWindowsNewline = end !== 0 && chunk[end - 1] === CR;83+ return state.isWindowsNewline ? 2 : 1;84+};85+86 const linesFinal = function * ({previousChunks}) {87 if (previousChunks.length > 0) {88 yield previousChunks;89 }90 };91+92+// Unless `preserveNewlines: true` is used, we strip the newline of each line.93+// This re-adds them after the user `transform` code has run.94+export const getAppendNewlineGenerator = ({binary, preserveNewlines, readableObjectMode, state}) => binary || preserveNewlines || readableObjectMode95+ ? undefined96+ : {transform: appendNewlineGenerator.bind(undefined, state)};97+98+const appendNewlineGenerator = function * ({isWindowsNewline = false}, chunk) {99+ const {unixNewline, windowsNewline, LF, concatBytes} = linesInfo.find(({isValidType}) => isValidType(chunk));100+101+ if (chunk.at(-1) === LF) {102+ yield chunk;103+ return;104+ }105+106+ const newline = isWindowsNewline ? windowsNewline : unixNewline;107+ yield concatBytes(chunk, newline);108+};modified:lib/verbose/output.js+1 added-2 removed
1 import {inspect} from 'node:util';2-import stripFinalNewline from 'strip-final-newline';2 import {escapeLines} from '../arguments/escape.js';3 import {PIPED_STDIO_VALUES} from '../stdio/forward.js';4 import {verboseLog} from './log.js';5 50 unmodified lines56 const isPiping = stream => stream._readableState.pipes.length > 0;57 58 // When `verbose` is `full`, print stdout|stderr59 const 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 linesmodified:test/convert/readable.js+2 added-2 removed
316 unmodified lines317 for await (const line of subprocess.readable()) {318 lines.push(line);319 }320 321- 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 });325 326 test('.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();329 330 t.is(stream.read(), null);331 await once(stream, 'readable');123 unmodified linesmodified:test/convert/writable.js+4 added-4 removed
252 unmodified lines253 await assertSubprocessOutput(t, subprocess, foobarString, 2);254 });255 256 test('.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 lines264 await assertSubprocessOutput(t, subprocess, foobarObjectString);265 });266 267 test('.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 lines318 return '.'.repeat(size * 3);319 };320 321 test('.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();324 325 const expectedOutput = await writeUntilFull(t, stream, subprocess);326 327 await assertSubprocessOutput(t, subprocess, expectedOutput);328 });329 330 test('.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();333 334 const expectedOutput = await writeUntilFull(t, stream, subprocess);335 54 unmodified linesmodified:test/fixtures/nested-inherit.js+2 added-5 removed
1 #!/usr/bin/env node2 import {execa} from '../../index.js';3+import {uppercaseGenerator} from '../helpers/generator.js';4 4-const uppercaseGenerator = function * (line) {5- yield line.toUpperCase();6-};7-8-await execa('noop-fd.js', ['1'], {stdout: ['inherit', uppercaseGenerator]});5+await execa('noop-fd.js', ['1'], {stdout: ['inherit', uppercaseGenerator()]});modified:test/fixtures/nested-transform.js+1 added-1 removed
2 unmodified lines3 import {execa} from '../../index.js';4 import {uppercaseGenerator} from '../helpers/generator.js';5 6 const [options, file, ...args] = process.argv.slice(2);7-await execa(file, args, {stdout: uppercaseGenerator, ...JSON.parse(options)});7+await execa(file, args, {stdout: uppercaseGenerator(), ...JSON.parse(options)});modified:test/helpers/generator.js+16 added-8 removed
1 import {setImmediate, setInterval} from 'node:timers/promises';2 import {foobarObject} from './input.js';3 4-export const noopAsyncGenerator = () => ({4+export const noopAsyncGenerator = (objectMode, binary) => ({5 async * transform(line) {6 yield line;7 },8+ objectMode,9+ binary,10 });11 12 export const addNoopGenerator = (transform, addNoopTransform) => addNoopTransform13 ? [transform, noopGenerator(undefined, true)]6 unmodified lines20 objectMode,21 binary,22 });23 22-export const serializeGenerator = {24+export const serializeGenerator = (objectMode, binary) => ({25 * transform(object) {26 yield JSON.stringify(object);27 },26- objectMode: true,27-};28+ objectMode,29+ binary,30+});31 32 export const getOutputsGenerator = (inputs, objectMode) => ({33 * transform() {34 yield * inputs;8 unmodified lines43 export const identityAsyncGenerator = input => async function * () {44 yield input;45 };46 44-export const getOutputGenerator = (input, objectMode) => ({47+export const getOutputGenerator = (input, objectMode, binary) => ({48 transform: identityGenerator(input),49 objectMode,50+ binary,51 });52 53 export const outputObjectGenerator = getOutputGenerator(foobarObject, true);54 30 unmodified lines85 yield value;86 }87 };88 85-export const uppercaseGenerator = function * (line) {86- yield line.toUpperCase();87-};89+export const uppercaseGenerator = (objectMode, binary) => ({90+ * transform(line) {91+ yield line.toUpperCase();92+ },93+ objectMode,94+ binary,95+});96 97 // eslint-disable-next-line require-yield98 export const throwingGenerator = function * () {99 throw new Error('Generator error');3 unmodified linesadded:test/helpers/lines.js+28 added
1+import {Buffer} from 'node:buffer';2+3+export const simpleFull = 'aaa\nbbb\nccc';4+export const simpleChunks = [simpleFull];5+export const simpleLines = ['aaa\n', 'bbb\n', 'ccc'];6+export const simpleFullEndLines = ['aaa\n', 'bbb\n', 'ccc\n'];7+export const noNewlinesChunks = ['aaa', 'bbb', 'ccc'];8+9+const textEncoder = new TextEncoder();10+const textDecoder = new TextDecoder();11+12+export const getEncoding = isUint8Array => isUint8Array ? 'buffer' : 'utf8';13+14+export const stringsToUint8Arrays = (strings, isUint8Array) => isUint8Array15+ ? strings.map(string => textEncoder.encode(string))16+ : strings;17+18+export const stringsToBuffers = (strings, isUint8Array) => isUint8Array19+ ? strings.map(string => Buffer.from(string))20+ : strings;21+22+export const serializeResult = (result, isUint8Array) => Array.isArray(result)23+ ? result.map(resultItem => serializeResultItem(resultItem, isUint8Array))24+ : serializeResultItem(result, isUint8Array);25+26+const serializeResultItem = (resultItem, isUint8Array) => isUint8Array27+ ? textDecoder.decode(resultItem)28+ : resultItem;modified:test/pipe/streaming.js+1 added-1 removed
225 unmodified lines226 const secondPipePromise = secondSource.pipe(destination);227 228 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 linesmodified:test/return/error.js+16 added
1 unmodified line2 import test from 'ava';3 import {execa, execaSync} from '../../index.js';4 import {setFixtureDir} from '../helpers/fixtures-dir.js';5 import {fullStdio, getStdio} from '../helpers/stdio.js';6+import {foobarString} from '../helpers/input.js';7+import {QUOTE} from '../helpers/verbose.js';8 import {noopGenerator, outputObjectGenerator} from '../helpers/generator.js';9 10 const isWindows = process.platform === 'win32';11 64 unmodified lines76 test('error.message contains stdout/stderr/stdio even with encoding "buffer", objectMode', testStdioMessage, 'buffer', false, true);77 test('error.message contains all if available, objectMode', testStdioMessage, 'utf8', true, true);78 test('error.message contains all even with encoding "buffer", objectMode', testStdioMessage, 'buffer', true, true);79 80+const 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+};88+89+test('error.message handles "lines: true"', testLinesMessage, 'utf8', false);90+test('error.message handles "lines: true", stripFinalNewline', testLinesMessage, 'utf8', true);91+test('error.message handles "lines: true", buffer', testLinesMessage, 'buffer', false);92+test('error.message handles "lines: true", buffer, stripFinalNewline', testLinesMessage, 'buffer', true);93+94 const 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 linesmodified:test/return/output.js+1 added-1 removed
135 unmodified lines136 test('stripFinalNewline: true with stdio[*] - sync', testStripFinalNewline, 3, true, execaSync);137 test('stripFinalNewline: false with stdio[*] - sync', testStripFinalNewline, 3, false, execaSync);138 139 test('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 lines10 setFixtureDir();11 12 const textEncoder = new TextEncoder();13 14-const getTypeofGenerator = objectMode => ({14+const getTypeofGenerator = (objectMode, binary) => ({15 * transform(line) {16 yield Object.prototype.toString.call(line);17 },18 objectMode,19+ binary,20 });21 22 // eslint-disable-next-line max-params23 const 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 lines57 const 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 lines95 96 // eslint-disable-next-line max-params97 const 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 lines161 const breakingLength = multibyteUint8Array.length * 0.75;162 const brokenSymbol = '\uFFFD';163 164 const 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 lines176 const 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 linesmodified:test/stdio/generator.js+13 added-13 removed
22 unmodified lines23 24 const 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 };35 36 const getOutputObjectMode = (objectMode, addNoopTransform) => objectMode2 unmodified lines39 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 lines188 if (objectMode) {189 return outputObjectGenerator;190 }191 192- return encoding === 'buffer' ? uppercaseBufferGenerator : uppercaseGenerator;192+ return encoding === 'buffer' ? uppercaseBufferGenerator : uppercaseGenerator();193 };194 195 const getStdoutStderrOutput = (output, stdioOption, encoding, objectMode) => {196 if (objectMode && !stdioOption) {59 unmodified lines256 test('Can use generators with result.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, true, false);257 test('Can use generators with error.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, true, false);258 259 test('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 });263 264 const testInputFile = async (t, getOptions, reversed) => {5 unmodified lines270 t.is(stdout, foobarUppercase);271 await rm(filePath);272 };273 274-test('Can use generators with a file as input', testInputFile, filePath => ({stdin: [{file: filePath}, uppercaseGenerator]}), false);275-test('Can use generators with a file as input, reversed', testInputFile, filePath => ({stdin: [{file: filePath}, uppercaseGenerator]}), true);276-test('Can use generators with inputFile option', testInputFile, filePath => ({inputFile: filePath, stdin: uppercaseGenerator}), false);274+test('Can use generators with a file as input', testInputFile, filePath => ({stdin: [{file: filePath}, uppercaseGenerator()]}), false);275+test('Can use generators with a file as input, reversed', testInputFile, filePath => ({stdin: [{file: filePath}, uppercaseGenerator()]}), true);276+test('Can use generators with inputFile option', testInputFile, filePath => ({inputFile: filePath, stdin: uppercaseGenerator()}), false);277 278 const 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 lines290 291 test('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 });300 301 test('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 lines317 yield `${line}${casedSuffix}`;318 };319 320 const 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 lines328 test('Can use multiple generators as input', testAppendInput, false);329 test('Can use multiple generators as input, reversed', testAppendInput, true);330 331 const 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 linesmodified:test/stdio/iterable.js+1 added-1 removed
52 unmodified lines53 yield foobarObject;54 };55 56 const 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 };60 61 test('stdin option can be an array of objects', testObjectIterable, [foobarObject], 0);71 unmodified linesadded:test/stdio/lines.js+149 added
1+import {once} from 'node:events';2+import {Writable} from 'node:stream';3+import test from 'ava';4+import {execa, execaSync} from '../../index.js';5+import {setFixtureDir} from '../helpers/fixtures-dir.js';6+import {fullStdio} from '../helpers/stdio.js';7+import {getChunksGenerator} from '../helpers/generator.js';8+import {foobarObject} from '../helpers/input.js';9+import {10+ simpleFull,11+ simpleChunks,12+ simpleLines,13+ simpleFullEndLines,14+ noNewlinesChunks,15+ getEncoding,16+ stringsToUint8Arrays,17+ stringsToBuffers,18+ serializeResult,19+} from '../helpers/lines.js';20+21+setFixtureDir();22+23+// eslint-disable-next-line max-params24+const 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+};34+35+test('"lines: true" splits lines, stdout, string', testStreamLines, 1, simpleFull, simpleLines, false, false);36+test('"lines: true" splits lines, stdout, Uint8Array', testStreamLines, 1, simpleFull, simpleLines, true, false);37+test('"lines: true" splits lines, stderr, string', testStreamLines, 2, simpleFull, simpleLines, false, false);38+test('"lines: true" splits lines, stderr, Uint8Array', testStreamLines, 2, simpleFull, simpleLines, true, false);39+test('"lines: true" splits lines, stdio[*], string', testStreamLines, 3, simpleFull, simpleLines, false, false);40+test('"lines: true" splits lines, stdio[*], Uint8Array', testStreamLines, 3, simpleFull, simpleLines, true, false);41+test('"lines: true" splits lines, stdout, string, stripFinalNewline', testStreamLines, 1, simpleFull, noNewlinesChunks, false, true);42+test('"lines: true" splits lines, stdout, Uint8Array, stripFinalNewline', testStreamLines, 1, simpleFull, noNewlinesChunks, true, true);43+test('"lines: true" splits lines, stderr, string, stripFinalNewline', testStreamLines, 2, simpleFull, noNewlinesChunks, false, true);44+test('"lines: true" splits lines, stderr, Uint8Array, stripFinalNewline', testStreamLines, 2, simpleFull, noNewlinesChunks, true, true);45+test('"lines: true" splits lines, stdio[*], string, stripFinalNewline', testStreamLines, 3, simpleFull, noNewlinesChunks, false, true);46+test('"lines: true" splits lines, stdio[*], Uint8Array, stripFinalNewline', testStreamLines, 3, simpleFull, noNewlinesChunks, true, true);47+48+const testStreamLinesNoop = async (t, lines, execaMethod) => {49+ const {stdout} = await execaMethod('noop-fd.js', ['1', simpleFull], {lines});50+ t.is(stdout, simpleFull);51+};52+53+test('"lines: false" is a noop with execa()', testStreamLinesNoop, false, execa);54+test('"lines: false" is a noop with execaSync()', testStreamLinesNoop, false, execaSync);55+test('"lines: true" is a noop with execaSync()', testStreamLinesNoop, true, execaSync);56+57+const bigArray = Array.from({length: 1e5}).fill('.\n');58+const bigString = bigArray.join('');59+const bigStringNoNewlines = '.'.repeat(1e6);60+const bigStringNoNewlinesEnd = `${bigStringNoNewlines}\n`;61+62+// eslint-disable-next-line max-params63+const 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+};71+72+test('"lines: true" works with strings generators', testStreamLinesGenerator, simpleChunks, simpleFullEndLines, false, false);73+test('"lines: true" works with strings generators, objectMode', testStreamLinesGenerator, simpleChunks, simpleChunks, true, false);74+test('"lines: true" works with strings generators, binary', testStreamLinesGenerator, simpleChunks, simpleLines, false, true);75+test('"lines: true" works with strings generators, binary, objectMode', testStreamLinesGenerator, simpleChunks, simpleChunks, true, true);76+test('"lines: true" works with big strings generators', testStreamLinesGenerator, [bigString], bigArray, false, false);77+test('"lines: true" works with big strings generators, objectMode', testStreamLinesGenerator, [bigString], [bigString], true, false);78+test('"lines: true" works with big strings generators without newlines', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlinesEnd], false, false);79+test('"lines: true" works with big strings generators without newlines, objectMode', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlines], true, false);80+81+test('"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+});88+89+const singleLine = 'a\n';90+const singleLineStrip = 'a';91+92+const 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+};100+101+test('"lines: true" does not work with other encodings', testOtherEncoding, false, singleLine);102+test('"lines: true" does not work with other encodings, stripFinalNewline', testOtherEncoding, true, singleLineStrip);103+104+const getSimpleChunkSubprocess = (isUint8Array, stripFinalNewline, options) => execa('noop-fd.js', ['1', ...simpleChunks], {105+ lines: true,106+ encoding: getEncoding(isUint8Array),107+ stripFinalNewline,108+ ...options,109+});110+111+const 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+};116+117+test('"lines: true" works with stream async iteration, string', testAsyncIteration, simpleLines, false, false);118+test('"lines: true" works with stream async iteration, Uint8Array', testAsyncIteration, simpleLines, true, false);119+test('"lines: true" works with stream async iteration, string, stripFinalNewline', testAsyncIteration, noNewlinesChunks, false, true);120+test('"lines: true" works with stream async iteration, Uint8Array, stripFinalNewline', testAsyncIteration, noNewlinesChunks, true, true);121+122+const 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+};127+128+test('"lines: true" works with stream "data" events, string', testDataEvents, simpleLines, false, false);129+test('"lines: true" works with stream "data" events, Uint8Array', testDataEvents, simpleLines, true, false);130+test('"lines: true" works with stream "data" events, string, stripFinalNewline', testDataEvents, noNewlinesChunks, false, true);131+test('"lines: true" works with stream "data" events, Uint8Array, stripFinalNewline', testDataEvents, noNewlinesChunks, true, true);132+133+const 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+};145+146+test('"lines: true" works with writable streams targets, string', testWritableStream, simpleLines, false, false);147+test('"lines: true" works with writable streams targets, Uint8Array', testWritableStream, simpleLines, true, false);148+test('"lines: true" works with writable streams targets, string, stripFinalNewline', testWritableStream, noNewlinesChunks, false, true);149+test('"lines: true" works with writable streams targets, Uint8Array, stripFinalNewline', testWritableStream, noNewlinesChunks, true, true);modified:test/stdio/split.js+267 added-189 removed
1-import {Buffer} from 'node:buffer';2-import {once} from 'node:events';3-import {Writable} from 'node:stream';4-import {scheduler} from 'node:timers/promises';1 import test from 'ava';6-import {execa, execaSync} from '../../index.js';2+import {execa} from '../../index.js';3 import {setFixtureDir} from '../helpers/fixtures-dir.js';8-import {fullStdio, getStdio} from '../helpers/stdio.js';9-import {getChunksGenerator} from '../helpers/generator.js';10-import {foobarObject} from '../helpers/input.js';4+import {getStdio} from '../helpers/stdio.js';5+import {6+ getChunksGenerator,7+ getOutputsGenerator,8+ noopGenerator,9+ noopAsyncGenerator,10+} from '../helpers/generator.js';11+import {foobarString, foobarUint8Array, foobarObject, foobarObjectString} from '../helpers/input.js';12+import {13+ simpleFull,14+ simpleChunks,15+ simpleLines,16+ simpleFullEndLines,17+ noNewlinesChunks,18+ getEncoding,19+ stringsToUint8Arrays,20+ serializeResult,21+} from '../helpers/lines.js';22 23 setFixtureDir();24 14-const simpleChunk = ['aaa\nbbb\nccc'];15-const simpleLines = ['aaa\n', 'bbb\n', 'ccc'];16-const windowsChunk = ['aaa\r\nbbb\r\nccc'];25+const simpleFullEnd = `${simpleFull}\n`;26+const simpleFullEndChunks = [simpleFullEnd];27+const windowsFull = 'aaa\r\nbbb\r\nccc';28+const windowsFullEnd = `${windowsFull}\r\n`;29+const windowsChunks = [windowsFull];30 const windowsLines = ['aaa\r\n', 'bbb\r\n', 'ccc'];18-const newlineEndChunk = ['aaa\nbbb\nccc\n'];19-const newlineEndLines = ['aaa\n', 'bbb\n', 'ccc\n'];20-const noNewlinesChunk = ['aaa'];21-const noNewlinesChunks = ['aaa', 'bbb', 'ccc'];31+const noNewlinesFull = 'aaabbbccc';32+const noNewlinesFullEnd = `${noNewlinesFull}\n`;33 const noNewlinesLines = ['aaabbbccc'];23-const newlineChunk = ['\n'];24-const newlinesChunk = ['\n\n\n'];34+const singleFull = 'aaa';35+const singleFullEnd = `${singleFull}\n`;36+const singleFullEndWindows = `${singleFull}\r\n`;37+const singleChunks = [singleFull];38+const noLines = [];39+const emptyFull = '';40+const emptyChunks = [emptyFull];41+const manyEmptyChunks = [emptyFull, emptyFull, emptyFull];42+const newlineFull = '\n';43+const newlineChunks = [newlineFull];44+const newlinesFull = '\n\n\n';45+const newlinesChunks = [newlinesFull];46 const newlinesLines = ['\n', '\n', '\n'];26-const windowsNewlinesChunk = ['\r\n\r\n\r\n'];47+const windowsNewlinesFull = '\r\n\r\n\r\n';48+const windowsNewlinesChunks = [windowsNewlinesFull];49 const windowsNewlinesLines = ['\r\n', '\r\n', '\r\n'];50 const runOverChunks = ['aaa\nb', 'b', 'b\nccc'];29-30-const bigLine = '.'.repeat(1e5);51+const bigFull = '.'.repeat(1e5);52+const bigFullEnd = `${bigFull}\n`;53+const bigChunks = [bigFull];54 const manyChunks = Array.from({length: 1e3}).fill('.');55+const manyFull = manyChunks.join('');56+const manyFullEnd = `${manyFull}\n`;57+const manyLines = [manyFull];58+const mixedNewlines = '.\n.\r\n.\n.\r\n.\n';59 33-const 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-};40-60 const resultGenerator = function * (lines, chunk) {61 lines.push(chunk);62 yield chunk;63 };64 46-const textEncoder = new TextEncoder();47-const textDecoder = new TextDecoder();48-49-const getEncoding = isUint8Array => isUint8Array ? 'buffer' : 'utf8';50-51-const stringsToUint8Arrays = (strings, isUint8Array) => isUint8Array52- ? strings.map(string => textEncoder.encode(string))53- : strings;54-55-const stringsToBuffers = (strings, isUint8Array) => isUint8Array56- ? strings.map(string => Buffer.from(string))57- : strings;58-59-const getSimpleChunkSubprocess = (isUint8Array, options) => execa('noop-fd.js', ['1', ...simpleChunk], {60- lines: true,61- encoding: getEncoding(isUint8Array),62- ...options,63-});64-65-const serializeResult = (result, isUint8Array, objectMode) => objectMode66- ? result.map(resultItem => serializeResultItem(resultItem, isUint8Array)).join('')67- : serializeResultItem(result, isUint8Array);68-69-const serializeResultItem = (resultItem, isUint8Array) => isUint8Array70- ? textDecoder.decode(resultItem)71- : resultItem;72-65 // eslint-disable-next-line max-params74-const testLines = async (t, fdNumber, input, expectedLines, isUint8Array, objectMode) => {66+const 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 };80 88-test('Split string stdout - n newlines, 1 chunk', testLines, 1, simpleChunk, simpleLines, false, false);89-test('Split string stderr - n newlines, 1 chunk', testLines, 2, simpleChunk, simpleLines, false, false);90-test('Split string stdio[*] - n newlines, 1 chunk', testLines, 3, simpleChunk, simpleLines, false, false);91-test('Split string stdout - no newline, n chunks', testLines, 1, noNewlinesChunks, noNewlinesLines, false, false);92-test('Split string stdout - 0 newlines, 1 chunk', testLines, 1, noNewlinesChunk, noNewlinesChunk, false, false);93-test('Split string stdout - Windows newlines', testLines, 1, windowsChunk, windowsLines, false, false);94-test('Split string stdout - chunk ends with newline', testLines, 1, newlineEndChunk, newlineEndLines, false, false);95-test('Split string stdout - single newline', testLines, 1, newlineChunk, newlineChunk, false, false);96-test('Split string stdout - only newlines', testLines, 1, newlinesChunk, newlinesLines, false, false);97-test('Split string stdout - only Windows newlines', testLines, 1, windowsNewlinesChunk, windowsNewlinesLines, false, false);98-test('Split string stdout - line split over multiple chunks', testLines, 1, runOverChunks, simpleLines, false, false);99-test('Split string stdout - 0 newlines, big line', testLines, 1, [bigLine], [bigLine], false, false);100-test('Split string stdout - 0 newlines, many chunks', testLines, 1, manyChunks, [manyChunks.join('')], false, false);101-test('Split Uint8Array stdout - n newlines, 1 chunk', testLines, 1, simpleChunk, simpleLines, true, false);102-test('Split Uint8Array stderr - n newlines, 1 chunk', testLines, 2, simpleChunk, simpleLines, true, false);103-test('Split Uint8Array stdio[*] - n newlines, 1 chunk', testLines, 3, simpleChunk, simpleLines, true, false);104-test('Split Uint8Array stdout - no newline, n chunks', testLines, 1, noNewlinesChunks, noNewlinesLines, true, false);105-test('Split Uint8Array stdout - 0 newlines, 1 chunk', testLines, 1, noNewlinesChunk, noNewlinesChunk, true, false);106-test('Split Uint8Array stdout - Windows newlines', testLines, 1, windowsChunk, windowsLines, true, false);107-test('Split Uint8Array stdout - chunk ends with newline', testLines, 1, newlineEndChunk, newlineEndLines, true, false);108-test('Split Uint8Array stdout - single newline', testLines, 1, newlineChunk, newlineChunk, true, false);109-test('Split Uint8Array stdout - only newlines', testLines, 1, newlinesChunk, newlinesLines, true, false);110-test('Split Uint8Array stdout - only Windows newlines', testLines, 1, windowsNewlinesChunk, windowsNewlinesLines, true, false);111-test('Split Uint8Array stdout - line split over multiple chunks', testLines, 1, runOverChunks, simpleLines, true, false);112-test('Split Uint8Array stdout - 0 newlines, big line', testLines, 1, [bigLine], [bigLine], true, false);113-test('Split Uint8Array stdout - 0 newlines, many chunks', testLines, 1, manyChunks, [manyChunks.join('')], true, false);114-test('Split string stdout - n newlines, 1 chunk, objectMode', testLines, 1, simpleChunk, simpleChunk, false, true);115-test('Split string stderr - n newlines, 1 chunk, objectMode', testLines, 2, simpleChunk, simpleChunk, false, true);116-test('Split string stdio[*] - n newlines, 1 chunk, objectMode', testLines, 3, simpleChunk, simpleChunk, false, true);117-test('Split string stdout - no newline, n chunks, objectMode', testLines, 1, noNewlinesChunks, noNewlinesChunks, false, true);118-test('Split string stdout - 0 newlines, 1 chunk, objectMode', testLines, 1, noNewlinesChunk, noNewlinesChunk, false, true);119-test('Split string stdout - Windows newlines, objectMode', testLines, 1, windowsChunk, windowsChunk, false, true);120-test('Split string stdout - chunk ends with newline, objectMode', testLines, 1, newlineEndChunk, newlineEndChunk, false, true);121-test('Split string stdout - single newline, objectMode', testLines, 1, newlineChunk, newlineChunk, false, true);122-test('Split string stdout - only newlines, objectMode', testLines, 1, newlinesChunk, newlinesChunk, false, true);123-test('Split string stdout - only Windows newlines, objectMode', testLines, 1, windowsNewlinesChunk, windowsNewlinesChunk, false, true);124-test('Split string stdout - line split over multiple chunks, objectMode', testLines, 1, runOverChunks, runOverChunks, false, true);125-test('Split string stdout - 0 newlines, big line, objectMode', testLines, 1, [bigLine], [bigLine], false, true);126-test('Split string stdout - 0 newlines, many chunks, objectMode', testLines, 1, manyChunks, manyChunks, false, true);127-test('Split Uint8Array stdout - n newlines, 1 chunk, objectMode', testLines, 1, simpleChunk, simpleChunk, true, true);128-test('Split Uint8Array stderr - n newlines, 1 chunk, objectMode', testLines, 2, simpleChunk, simpleChunk, true, true);129-test('Split Uint8Array stdio[*] - n newlines, 1 chunk, objectMode', testLines, 3, simpleChunk, simpleChunk, true, true);130-test('Split Uint8Array stdout - no newline, n chunks, objectMode', testLines, 1, noNewlinesChunks, noNewlinesChunks, true, true);131-test('Split Uint8Array stdout - 0 newlines, 1 chunk, objectMode', testLines, 1, noNewlinesChunk, noNewlinesChunk, true, true);132-test('Split Uint8Array stdout - Windows newlines, objectMode', testLines, 1, windowsChunk, windowsChunk, true, true);133-test('Split Uint8Array stdout - chunk ends with newline, objectMode', testLines, 1, newlineEndChunk, newlineEndChunk, true, true);134-test('Split Uint8Array stdout - single newline, objectMode', testLines, 1, newlineChunk, newlineChunk, true, true);135-test('Split Uint8Array stdout - only newlines, objectMode', testLines, 1, newlinesChunk, newlinesChunk, true, true);136-test('Split Uint8Array stdout - only Windows newlines, objectMode', testLines, 1, windowsNewlinesChunk, windowsNewlinesChunk, true, true);137-test('Split Uint8Array stdout - line split over multiple chunks, objectMode', testLines, 1, runOverChunks, runOverChunks, true, true);138-test('Split Uint8Array stdout - 0 newlines, big line, objectMode', testLines, 1, [bigLine], [bigLine], true, true);139-test('Split Uint8Array stdout - 0 newlines, many chunks, objectMode', testLines, 1, manyChunks, manyChunks, true, true);81+test('Split string stdout - n newlines, 1 chunk', testLines, 1, simpleChunks, simpleLines, simpleFull, false, false, true);82+test('Split string stderr - n newlines, 1 chunk', testLines, 2, simpleChunks, simpleLines, simpleFull, false, false, true);83+test('Split string stdio[*] - n newlines, 1 chunk', testLines, 3, simpleChunks, simpleLines, simpleFull, false, false, true);84+test('Split string stdout - preserveNewlines, n chunks', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesFull, false, false, true);85+test('Split string stdout - 0 newlines, 1 chunk', testLines, 1, singleChunks, singleChunks, singleFull, false, false, true);86+test('Split string stdout - empty, 1 chunk', testLines, 1, emptyChunks, noLines, emptyFull, false, false, true);87+test('Split string stdout - Windows newlines', testLines, 1, windowsChunks, windowsLines, windowsFull, false, false, true);88+test('Split string stdout - chunk ends with newline', testLines, 1, simpleFullEndChunks, simpleFullEndLines, simpleFullEnd, false, false, true);89+test('Split string stdout - single newline', testLines, 1, newlineChunks, newlineChunks, newlineFull, false, false, true);90+test('Split string stdout - only newlines', testLines, 1, newlinesChunks, newlinesLines, newlinesFull, false, false, true);91+test('Split string stdout - only Windows newlines', testLines, 1, windowsNewlinesChunks, windowsNewlinesLines, windowsNewlinesFull, false, false, true);92+test('Split string stdout - line split over multiple chunks', testLines, 1, runOverChunks, simpleLines, simpleFull, false, false, true);93+test('Split string stdout - 0 newlines, big line', testLines, 1, bigChunks, bigChunks, bigFull, false, false, true);94+test('Split string stdout - 0 newlines, many chunks', testLines, 1, manyChunks, manyLines, manyFull, false, false, true);95+test('Split Uint8Array stdout - n newlines, 1 chunk', testLines, 1, simpleChunks, simpleLines, simpleFull, true, false, true);96+test('Split Uint8Array stderr - n newlines, 1 chunk', testLines, 2, simpleChunks, simpleLines, simpleFull, true, false, true);97+test('Split Uint8Array stdio[*] - n newlines, 1 chunk', testLines, 3, simpleChunks, simpleLines, simpleFull, true, false, true);98+test('Split Uint8Array stdout - preserveNewlines, n chunks', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesFull, true, false, true);99+test('Split Uint8Array stdout - empty, 1 chunk', testLines, 1, emptyChunks, noLines, emptyFull, true, false, true);100+test('Split Uint8Array stdout - 0 newlines, 1 chunk', testLines, 1, singleChunks, singleChunks, singleFull, true, false, true);101+test('Split Uint8Array stdout - Windows newlines', testLines, 1, windowsChunks, windowsLines, windowsFull, true, false, true);102+test('Split Uint8Array stdout - chunk ends with newline', testLines, 1, simpleFullEndChunks, simpleFullEndLines, simpleFullEnd, true, false, true);103+test('Split Uint8Array stdout - single newline', testLines, 1, newlineChunks, newlineChunks, newlineFull, true, false, true);104+test('Split Uint8Array stdout - only newlines', testLines, 1, newlinesChunks, newlinesLines, newlinesFull, true, false, true);105+test('Split Uint8Array stdout - only Windows newlines', testLines, 1, windowsNewlinesChunks, windowsNewlinesLines, windowsNewlinesFull, true, false, true);106+test('Split Uint8Array stdout - line split over multiple chunks', testLines, 1, runOverChunks, simpleLines, simpleFull, true, false, true);107+test('Split Uint8Array stdout - 0 newlines, big line', testLines, 1, bigChunks, bigChunks, bigFull, true, false, true);108+test('Split Uint8Array stdout - 0 newlines, many chunks', testLines, 1, manyChunks, manyLines, manyFull, true, false, true);109+test('Split string stdout - n newlines, 1 chunk, objectMode', testLines, 1, simpleChunks, simpleLines, simpleLines, false, true, true);110+test('Split string stderr - n newlines, 1 chunk, objectMode', testLines, 2, simpleChunks, simpleLines, simpleLines, false, true, true);111+test('Split string stdio[*] - n newlines, 1 chunk, objectMode', testLines, 3, simpleChunks, simpleLines, simpleLines, false, true, true);112+test('Split string stdout - preserveNewlines, n chunks, objectMode', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesLines, false, true, true);113+test('Split string stdout - empty, 1 chunk, objectMode', testLines, 1, emptyChunks, noLines, noLines, false, true, true);114+test('Split string stdout - 0 newlines, 1 chunk, objectMode', testLines, 1, singleChunks, singleChunks, singleChunks, false, true, true);115+test('Split string stdout - Windows newlines, objectMode', testLines, 1, windowsChunks, windowsLines, windowsLines, false, true, true);116+test('Split string stdout - chunk ends with newline, objectMode', testLines, 1, simpleFullEndChunks, simpleFullEndLines, simpleFullEndLines, false, true, true);117+test('Split string stdout - single newline, objectMode', testLines, 1, newlineChunks, newlineChunks, newlineChunks, false, true, true);118+test('Split string stdout - only newlines, objectMode', testLines, 1, newlinesChunks, newlinesLines, newlinesLines, false, true, true);119+test('Split string stdout - only Windows newlines, objectMode', testLines, 1, windowsNewlinesChunks, windowsNewlinesLines, windowsNewlinesLines, false, true, true);120+test('Split string stdout - line split over multiple chunks, objectMode', testLines, 1, runOverChunks, simpleLines, simpleLines, false, true, true);121+test('Split string stdout - 0 newlines, big line, objectMode', testLines, 1, bigChunks, bigChunks, bigChunks, false, true, true);122+test('Split string stdout - 0 newlines, many chunks, objectMode', testLines, 1, manyChunks, manyLines, manyLines, false, true, true);123+test('Split Uint8Array stdout - n newlines, 1 chunk, objectMode', testLines, 1, simpleChunks, simpleLines, simpleLines, true, true, true);124+test('Split Uint8Array stderr - n newlines, 1 chunk, objectMode', testLines, 2, simpleChunks, simpleLines, simpleLines, true, true, true);125+test('Split Uint8Array stdio[*] - n newlines, 1 chunk, objectMode', testLines, 3, simpleChunks, simpleLines, simpleLines, true, true, true);126+test('Split Uint8Array stdout - preserveNewlines, n chunks, objectMode', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesLines, true, true, true);127+test('Split Uint8Array stdout - empty, 1 chunk, objectMode', testLines, 1, emptyChunks, noLines, noLines, true, true, true);128+test('Split Uint8Array stdout - 0 newlines, 1 chunk, objectMode', testLines, 1, singleChunks, singleChunks, singleChunks, true, true, true);129+test('Split Uint8Array stdout - Windows newlines, objectMode', testLines, 1, windowsChunks, windowsLines, windowsLines, true, true, true);130+test('Split Uint8Array stdout - chunk ends with newline, objectMode', testLines, 1, simpleFullEndChunks, simpleFullEndLines, simpleFullEndLines, true, true, true);131+test('Split Uint8Array stdout - single newline, objectMode', testLines, 1, newlineChunks, newlineChunks, newlineChunks, true, true, true);132+test('Split Uint8Array stdout - only newlines, objectMode', testLines, 1, newlinesChunks, newlinesLines, newlinesLines, true, true, true);133+test('Split Uint8Array stdout - only Windows newlines, objectMode', testLines, 1, windowsNewlinesChunks, windowsNewlinesLines, windowsNewlinesLines, true, true, true);134+test('Split Uint8Array stdout - line split over multiple chunks, objectMode', testLines, 1, runOverChunks, simpleLines, simpleLines, true, true, true);135+test('Split Uint8Array stdout - 0 newlines, big line, objectMode', testLines, 1, bigChunks, bigChunks, bigChunks, true, true, true);136+test('Split Uint8Array stdout - 0 newlines, many chunks, objectMode', testLines, 1, manyChunks, manyLines, manyLines, true, true, true);137+test('Split string stdout - n newlines, 1 chunk, preserveNewlines', testLines, 1, simpleChunks, noNewlinesChunks, simpleFullEnd, false, false, false);138+test('Split string stderr - n newlines, 1 chunk, preserveNewlines', testLines, 2, simpleChunks, noNewlinesChunks, simpleFullEnd, false, false, false);139+test('Split string stdio[*] - n newlines, 1 chunk, preserveNewlines', testLines, 3, simpleChunks, noNewlinesChunks, simpleFullEnd, false, false, false);140+test('Split string stdout - preserveNewlines, n chunks, preserveNewlines', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesFullEnd, false, false, false);141+test('Split string stdout - empty, 1 chunk, preserveNewlines', testLines, 1, emptyChunks, noLines, emptyFull, false, false, false);142+test('Split string stdout - 0 newlines, 1 chunk, preserveNewlines', testLines, 1, singleChunks, singleChunks, singleFullEnd, false, false, false);143+test('Split string stdout - Windows newlines, preserveNewlines', testLines, 1, windowsChunks, noNewlinesChunks, windowsFullEnd, false, false, false);144+test('Split string stdout - chunk ends with newline, preserveNewlines', testLines, 1, simpleFullEndChunks, noNewlinesChunks, simpleFullEnd, false, false, false);145+test('Split string stdout - single newline, preserveNewlines', testLines, 1, newlineChunks, emptyChunks, newlineFull, false, false, false);146+test('Split string stdout - only newlines, preserveNewlines', testLines, 1, newlinesChunks, manyEmptyChunks, newlinesFull, false, false, false);147+test('Split string stdout - only Windows newlines, preserveNewlines', testLines, 1, windowsNewlinesChunks, manyEmptyChunks, windowsNewlinesFull, false, false, false);148+test('Split string stdout - line split over multiple chunks, preserveNewlines', testLines, 1, runOverChunks, noNewlinesChunks, simpleFullEnd, false, false, false);149+test('Split string stdout - 0 newlines, big line, preserveNewlines', testLines, 1, bigChunks, bigChunks, bigFullEnd, false, false, false);150+test('Split string stdout - 0 newlines, many chunks, preserveNewlines', testLines, 1, manyChunks, manyLines, manyFullEnd, false, false, false);151+test('Split Uint8Array stdout - n newlines, 1 chunk, preserveNewlines', testLines, 1, simpleChunks, noNewlinesChunks, simpleFullEnd, true, false, false);152+test('Split Uint8Array stderr - n newlines, 1 chunk, preserveNewlines', testLines, 2, simpleChunks, noNewlinesChunks, simpleFullEnd, true, false, false);153+test('Split Uint8Array stdio[*] - n newlines, 1 chunk, preserveNewlines', testLines, 3, simpleChunks, noNewlinesChunks, simpleFullEnd, true, false, false);154+test('Split Uint8Array stdout - preserveNewlines, n chunks, preserveNewlines', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesFullEnd, true, false, false);155+test('Split Uint8Array stdout - empty, 1 chunk, preserveNewlines', testLines, 1, emptyChunks, noLines, emptyFull, true, false, false);156+test('Split Uint8Array stdout - 0 newlines, 1 chunk, preserveNewlines', testLines, 1, singleChunks, singleChunks, singleFullEnd, true, false, false);157+test('Split Uint8Array stdout - Windows newlines, preserveNewlines', testLines, 1, windowsChunks, noNewlinesChunks, windowsFullEnd, true, false, false);158+test('Split Uint8Array stdout - chunk ends with newline, preserveNewlines', testLines, 1, simpleFullEndChunks, noNewlinesChunks, simpleFullEnd, true, false, false);159+test('Split Uint8Array stdout - single newline, preserveNewlines', testLines, 1, newlineChunks, emptyChunks, newlineFull, true, false, false);160+test('Split Uint8Array stdout - only newlines, preserveNewlines', testLines, 1, newlinesChunks, manyEmptyChunks, newlinesFull, true, false, false);161+test('Split Uint8Array stdout - only Windows newlines, preserveNewlines', testLines, 1, windowsNewlinesChunks, manyEmptyChunks, windowsNewlinesFull, true, false, false);162+test('Split Uint8Array stdout - line split over multiple chunks, preserveNewlines', testLines, 1, runOverChunks, noNewlinesChunks, simpleFullEnd, true, false, false);163+test('Split Uint8Array stdout - 0 newlines, big line, preserveNewlines', testLines, 1, bigChunks, bigChunks, bigFullEnd, true, false, false);164+test('Split Uint8Array stdout - 0 newlines, many chunks, preserveNewlines', testLines, 1, manyChunks, manyLines, manyFullEnd, true, false, false);165+test('Split string stdout - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 1, simpleChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);166+test('Split string stderr - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 2, simpleChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);167+test('Split string stdio[*] - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 3, simpleChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);168+test('Split string stdout - preserveNewlines, n chunks, objectMode, preserveNewlines', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesLines, false, true, false);169+test('Split string stdout - empty, 1 chunk, objectMode, preserveNewlines', testLines, 1, emptyChunks, noLines, noLines, false, true, false);170+test('Split string stdout - 0 newlines, 1 chunk, objectMode, preserveNewlines', testLines, 1, singleChunks, singleChunks, singleChunks, false, true, false);171+test('Split string stdout - Windows newlines, objectMode, preserveNewlines', testLines, 1, windowsChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);172+test('Split string stdout - chunk ends with newline, objectMode, preserveNewlines', testLines, 1, simpleFullEndChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);173+test('Split string stdout - single newline, objectMode, preserveNewlines', testLines, 1, newlineChunks, emptyChunks, emptyChunks, false, true, false);174+test('Split string stdout - only newlines, objectMode, preserveNewlines', testLines, 1, newlinesChunks, manyEmptyChunks, manyEmptyChunks, false, true, false);175+test('Split string stdout - only Windows newlines, objectMode, preserveNewlines', testLines, 1, windowsNewlinesChunks, manyEmptyChunks, manyEmptyChunks, false, true, false);176+test('Split string stdout - line split over multiple chunks, objectMode, preserveNewlines', testLines, 1, runOverChunks, noNewlinesChunks, noNewlinesChunks, false, true, false);177+test('Split string stdout - 0 newlines, big line, objectMode, preserveNewlines', testLines, 1, bigChunks, bigChunks, bigChunks, false, true, false);178+test('Split string stdout - 0 newlines, many chunks, objectMode, preserveNewlines', testLines, 1, manyChunks, manyLines, manyLines, false, true, false);179+test('Split Uint8Array stdout - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 1, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);180+test('Split Uint8Array stderr - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 2, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);181+test('Split Uint8Array stdio[*] - n newlines, 1 chunk, objectMode, preserveNewlines', testLines, 3, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);182+test('Split Uint8Array stdout - preserveNewlines, n chunks, objectMode, preserveNewlines', testLines, 1, noNewlinesChunks, noNewlinesLines, noNewlinesLines, true, true, false);183+test('Split Uint8Array stdout - empty, 1 chunk, objectMode, preserveNewlines', testLines, 1, emptyChunks, noLines, noLines, true, true, false);184+test('Split Uint8Array stdout - 0 newlines, 1 chunk, objectMode, preserveNewlines', testLines, 1, singleChunks, singleChunks, singleChunks, true, true, false);185+test('Split Uint8Array stdout - Windows newlines, objectMode, preserveNewlines', testLines, 1, windowsChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);186+test('Split Uint8Array stdout - chunk ends with newline, objectMode, preserveNewlines', testLines, 1, simpleFullEndChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);187+test('Split Uint8Array stdout - single newline, objectMode, preserveNewlines', testLines, 1, newlineChunks, emptyChunks, emptyChunks, true, true, false);188+test('Split Uint8Array stdout - only newlines, objectMode, preserveNewlines', testLines, 1, newlinesChunks, manyEmptyChunks, manyEmptyChunks, true, true, false);189+test('Split Uint8Array stdout - only Windows newlines, objectMode, preserveNewlines', testLines, 1, windowsNewlinesChunks, manyEmptyChunks, manyEmptyChunks, true, true, false);190+test('Split Uint8Array stdout - line split over multiple chunks, objectMode, preserveNewlines', testLines, 1, runOverChunks, noNewlinesChunks, noNewlinesChunks, true, true, false);191+test('Split Uint8Array stdout - 0 newlines, big line, objectMode, preserveNewlines', testLines, 1, bigChunks, bigChunks, bigChunks, true, true, false);192+test('Split Uint8Array stdout - 0 newlines, many chunks, objectMode, preserveNewlines', testLines, 1, manyChunks, manyLines, manyLines, true, true, false);193 194 // eslint-disable-next-line max-params142-const testBinaryOption = async (t, binary, input, expectedLines, objectMode) => {195+const 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 };207 154-test('Does not split lines when "binary" is true', testBinaryOption, true, simpleChunk, simpleChunk, false);155-test('Splits lines when "binary" is false', testBinaryOption, false, simpleChunk, simpleLines, false);156-test('Splits lines when "binary" is undefined', testBinaryOption, undefined, simpleChunk, simpleLines, false);157-test('Does not split lines when "binary" is true, objectMode', testBinaryOption, true, simpleChunk, simpleChunk, true);158-test('Splits lines when "binary" is false, objectMode', testBinaryOption, false, simpleChunk, simpleChunk, true);159-test('Splits lines when "binary" is undefined, objectMode', testBinaryOption, undefined, simpleChunk, simpleChunk, true);208+test('Does not split lines when "binary" is true', testBinaryOption, true, simpleChunks, simpleChunks, simpleFull, false, true);209+test('Splits lines when "binary" is false', testBinaryOption, false, simpleChunks, simpleLines, simpleFull, false, true);210+test('Splits lines when "binary" is undefined', testBinaryOption, undefined, simpleChunks, simpleLines, simpleFull, false, true);211+test('Does not split lines when "binary" is true, objectMode', testBinaryOption, true, simpleChunks, simpleChunks, simpleChunks, true, true);212+test('Splits lines when "binary" is false, objectMode', testBinaryOption, false, simpleChunks, simpleLines, simpleLines, true, true);213+test('Splits lines when "binary" is undefined, objectMode', testBinaryOption, undefined, simpleChunks, simpleLines, simpleLines, true, true);214+test('Does not split lines when "binary" is true, preserveNewlines', testBinaryOption, true, simpleChunks, simpleChunks, simpleFull, false, false);215+test('Splits lines when "binary" is false, preserveNewlines', testBinaryOption, false, simpleChunks, noNewlinesChunks, simpleFullEnd, false, false);216+test('Splits lines when "binary" is undefined, preserveNewlines', testBinaryOption, undefined, simpleChunks, noNewlinesChunks, simpleFullEnd, false, false);217+test('Does not split lines when "binary" is true, objectMode, preserveNewlines', testBinaryOption, true, simpleChunks, simpleChunks, simpleChunks, true, false);218+test('Splits lines when "binary" is false, objectMode, preserveNewlines', testBinaryOption, false, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, false);219+test('Splits lines when "binary" is undefined, objectMode, preserveNewlines', testBinaryOption, undefined, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, false);220 161-// eslint-disable-next-line max-params162-const testStreamLines = async (t, fdNumber, input, expectedLines, isUint8Array) => {163- const {stdio} = await execa('noop-fd.js', [`${fdNumber}`, input], {164- ...fullStdio,221+const resultStringGenerator = function * (lines, chunk) {222+ lines.push(chunk);223+ yield new TextDecoder().decode(chunk);224+};225+226+const 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 };240 171-test('"lines: true" splits lines, stdout, string', testStreamLines, 1, simpleChunk[0], simpleLines, false);172-test('"lines: true" splits lines, stdout, Uint8Array', testStreamLines, 1, simpleChunk[0], simpleLines, true);173-test('"lines: true" splits lines, stderr, string', testStreamLines, 2, simpleChunk[0], simpleLines, false);174-test('"lines: true" splits lines, stderr, Uint8Array', testStreamLines, 2, simpleChunk[0], simpleLines, true);175-test('"lines: true" splits lines, stdio[*], string', testStreamLines, 3, simpleChunk[0], simpleLines, false);176-test('"lines: true" splits lines, stdio[*], Uint8Array', testStreamLines, 3, simpleChunk[0], simpleLines, true);241+test('Line splitting when converting from Uint8Array to string', testUint8ArrayToString, [foobarUint8Array], false, true);242+test('Line splitting when converting from Uint8Array to string, objectMode', testUint8ArrayToString, [foobarString], true, true);243+test('Line splitting when converting from Uint8Array to string, preserveNewlines', testUint8ArrayToString, [foobarUint8Array], false, false);244+test('Line splitting when converting from Uint8Array to string, objectMode, preserveNewlines', testUint8ArrayToString, [foobarString], true, false);245 178-const testStreamLinesNoop = async (t, lines, execaMethod) => {179- const {stdout} = await execaMethod('noop-fd.js', ['1', simpleChunk[0]], {lines});180- t.is(stdout, simpleChunk[0]);246+const resultUint8ArrayGenerator = function * (lines, chunk) {247+ lines.push(chunk);248+ yield new TextEncoder().encode(chunk);249 };250 183-test('"lines: false" is a noop with execa()', testStreamLinesNoop, false, execa);184-test('"lines: false" is a noop with execaSync()', testStreamLinesNoop, false, execaSync);185-test('"lines: true" is a noop with execaSync()', testStreamLinesNoop, true, execaSync);251+const 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+};264 187-const bigArray = Array.from({length: 1e5}).fill('.\n');188-const bigString = bigArray.join('');189-const bigStringNoNewlines = '.'.repeat(1e6);265+test('Line splitting when converting from string to Uint8Array', testStringToUint8Array, [foobarString], false, true);266+test('Line splitting when converting from string to Uint8Array, objectMode', testStringToUint8Array, [foobarUint8Array], true, true);267+test('Line splitting when converting from string to Uint8Array, preserveNewlines', testStringToUint8Array, [foobarString], false, false);268+test('Line splitting when converting from string to Uint8Array, objectMode, preserveNewlines', testStringToUint8Array, [foobarUint8Array], true, false);269 191-// eslint-disable-next-line max-params192-const 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);270+const 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 };277 197-test('"lines: true" works with strings generators', testStreamLinesGenerator, simpleChunk, simpleLines, false, false);198-test('"lines: true" works with strings generators, objectMode', testStreamLinesGenerator, simpleChunk, simpleChunk, true, false);199-test('"lines: true" works with strings generators, binary', testStreamLinesGenerator, simpleChunk, simpleLines, false, true);200-test('"lines: true" works with strings generators, binary, objectMode', testStreamLinesGenerator, simpleChunk, simpleChunk, true, true);201-test('"lines: true" works with big strings generators', testStreamLinesGenerator, [bigString], bigArray, false, false);202-test('"lines: true" works with big strings generators, objectMode', testStreamLinesGenerator, [bigString], [bigString], true, false);203-test('"lines: true" works with big strings generators without newlines', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlines], false, false);204-test('"lines: true" works with big strings generators without newlines, objectMode', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlines], true, false);278+test('Strips newline when user do not mistakenly yield one at the end', testStripNewline, singleFull, singleFullEnd);279+test('Strips newline when user mistakenly yielded one at the end', testStripNewline, singleFullEnd, singleFullEnd);280+test('Strips newline when user mistakenly yielded one at the end, Windows newline', testStripNewline, singleFullEndWindows, singleFullEndWindows);281 206-test('"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-});282+const 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+};289 211-const singleLine = 'a\n';290+test('Can mix Unix and Windows newlines', testMixNewlines, noopGenerator);291+test('Can mix Unix and Windows newlines, async', testMixNewlines, noopAsyncGenerator);292 213-test('"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-});217-218-const 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));293+const serializeResultGenerator = function * (lines, chunk) {294+ lines.push(chunk);295+ yield JSON.stringify(chunk);296 };297 224-test('"lines: true" works with stream async iteration, string', testAsyncIteration, false);225-test('"lines: true" works with stream async iteration, Uint8Array', testAsyncIteration, true);226-227-const 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]);298+const 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 };310 233-test('"lines: true" works with stream "data" events, string', testDataEvents, false);234-test('"lines: true" works with stream "data" events, Uint8Array', testDataEvents, true);311+test('Can switch from objectMode to non-objectMode', testUnsetObjectMode, `${foobarObjectString}\n`, false);312+test('Can switch from objectMode to non-objectMode, preserveNewlines', testUnsetObjectMode, foobarObjectString, true);313 236-const testWritableStream = async (t, isUint8Array) => {314+const 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 };326 249-test('"lines: true" works with writable streams targets, string', testWritableStream, false);250-test('"lines: true" works with writable streams targets, Uint8Array', testWritableStream, true);327+test('Can use "yield* array" to produce multiple lines', testYieldArray, [foobarString, foobarString], [foobarString, foobarString], `${foobarString}\n${foobarString}\n`);328+test('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 lines31 test('Generators "final" can be used', testGeneratorFinal, 'noop.js');32 test('Generators "final" is used even on empty streams', testGeneratorFinal, 'empty.js');33 34 const 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 };38 39 test('Generators "final" can be used without "transform"', testFinalAlone, identityGenerator);60 unmodified lines100 };101 102 const 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 };106 107 test('Generator can yield "transform" multiple times at different moments', testMultipleYields, false);108 test('Generator can yield "final" multiple times at different moments', testMultipleYields, true);29 unmodified lines138 const maxBuffer = 10;139 140 test('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);147 148 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputGenerator(`${bigString}.`, false)}));149 });150 151 test('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);158 159 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputsGenerator([...bigArray, ''], true)}));160 });64 unmodified linesmodified:test/stdio/type.js+3 added-7 removed
1 import test from 'ava';2 import {execa, execaSync} from '../../index.js';3 import {getStdio} from '../helpers/stdio.js';4-import {noopGenerator} from '../helpers/generator.js';4+import {noopGenerator, uppercaseGenerator} from '../helpers/generator.js';5 import {setFixtureDir} from '../helpers/fixtures-dir.js';6 7 setFixtureDir();8 9-const uppercaseGenerator = function * (line) {10- yield line.toUpperCase();11-};12-9 const testInvalidGenerator = (t, fdNumber, stdioOption) => {10 t.throws(() => {11 execa('empty.js', getStdio(fdNumber, {...noopGenerator(), ...stdioOption}));12 }, {message: /must be a generator/});9 unmodified lines22 test('Cannot use invalid "final" with stdio[*]', testInvalidGenerator, 3, {final: true});23 24 const 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 };29 30 test('Cannot use invalid "binary" with stdin', testInvalidBinary, 0, 'binary');6 unmodified lines37 test('Cannot use invalid "objectMode" with stdio[*]', testInvalidBinary, 3, 'objectMode');38 39 const 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 };44 45 test('Cannot use generators with sync methods and stdin', testSyncMethods, 0);3 unmodified linesmodified:test/stdio/validate.js+1 added-1 removed
27 unmodified lines28 return 'a string or an Uint8Array';29 };30 31 const lastInputGenerator = (input, objectMode) => [foobarUint8Array, getOutputGenerator(input, objectMode)];32-const inputGenerator = (input, objectMode) => [...lastInputGenerator(input, objectMode), serializeGenerator];32+const inputGenerator = (input, objectMode) => [...lastInputGenerator(input, objectMode), serializeGenerator(true)];33 34 test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputGenerator, foobarObject, false, true);35 test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputGenerator, foobarObject, false, true);36 test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputGenerator, foobarObject, true, true);24 unmodified linesmodified:index.d.ts+3 added
26 unmodified lines27 type StdioTransformFull = {28 transform: StdioTransform;29 final?: StdioFinal;30 binary?: boolean;31+ preserveNewlines?: boolean;32 objectMode?: boolean;33 };34 35 type CommonStdioOption<IsSync extends boolean = boolean> =383 unmodified lines419 420 /**421 Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from the output.422 423+ If the `lines` option is true, this applies to each output line instead.424+425 @default true426 */427 readonly stripFinalNewline?: boolean;428 1100 unmodified linesmodified:index.test-d.ts+11 added
1067 unmodified lines1068 execa('unicorns', {stdin: {transform: unknownGenerator, final: asyncFinal}});1069 expectError(execaSync('unicorns', {stdin: {transform: unknownGenerator, final: asyncFinal}}));1070 expectError(execa('unicorns', {stdin: {}}));1071 expectError(execa('unicorns', {stdin: {binary: true}}));1072+expectError(execa('unicorns', {stdin: {preserveNewlines: true}}));1073 expectError(execa('unicorns', {stdin: {objectMode: true}}));1074 expectError(execa('unicorns', {stdin: {final: unknownFinal}}));1075 execa('unicorns', {stdin: {transform: unknownGenerator, binary: true}});1076 expectError(execa('unicorns', {stdin: {transform: unknownGenerator, binary: 'true'}}));1077+execa('unicorns', {stdin: {transform: unknownGenerator, preserveNewlines: true}});1078+expectError(execa('unicorns', {stdin: {transform: unknownGenerator, preserveNewlines: 'true'}}));1079 execa('unicorns', {stdin: {transform: unknownGenerator, objectMode: true}});1080 expectError(execa('unicorns', {stdin: {transform: unknownGenerator, objectMode: 'true'}}));1081 execa('unicorns', {stdin: undefined});1082 execaSync('unicorns', {stdin: undefined});86 unmodified lines1169 execa('unicorns', {stdout: {transform: unknownGenerator, final: asyncFinal}});1170 expectError(execaSync('unicorns', {stdout: {transform: unknownGenerator, final: asyncFinal}}));1171 expectError(execa('unicorns', {stdout: {}}));1172 expectError(execa('unicorns', {stdout: {binary: true}}));1173+expectError(execa('unicorns', {stdout: {preserveNewlines: true}}));1174 expectError(execa('unicorns', {stdout: {objectMode: true}}));1175 expectError(execa('unicorns', {stdout: {final: unknownFinal}}));1176 execa('unicorns', {stdout: {transform: unknownGenerator, binary: true}});1177 expectError(execa('unicorns', {stdout: {transform: unknownGenerator, binary: 'true'}}));1178+execa('unicorns', {stdout: {transform: unknownGenerator, preserveNewlines: true}});1179+expectError(execa('unicorns', {stdout: {transform: unknownGenerator, preserveNewlines: 'true'}}));1180 execa('unicorns', {stdout: {transform: unknownGenerator, objectMode: true}});1181 expectError(execa('unicorns', {stdout: {transform: unknownGenerator, objectMode: 'true'}}));1182 execa('unicorns', {stdout: undefined});1183 execaSync('unicorns', {stdout: undefined});86 unmodified lines1270 execa('unicorns', {stderr: {transform: unknownGenerator, final: asyncFinal}});1271 expectError(execaSync('unicorns', {stderr: {transform: unknownGenerator, final: asyncFinal}}));1272 expectError(execa('unicorns', {stderr: {}}));1273 expectError(execa('unicorns', {stderr: {binary: true}}));1274+expectError(execa('unicorns', {stderr: {preserveNewlines: true}}));1275 expectError(execa('unicorns', {stderr: {objectMode: true}}));1276 expectError(execa('unicorns', {stderr: {final: unknownFinal}}));1277 execa('unicorns', {stderr: {transform: unknownGenerator, binary: true}});1278 expectError(execa('unicorns', {stderr: {transform: unknownGenerator, binary: 'true'}}));1279+execa('unicorns', {stderr: {transform: unknownGenerator, preserveNewlines: true}});1280+expectError(execa('unicorns', {stderr: {transform: unknownGenerator, preserveNewlines: 'true'}}));1281 execa('unicorns', {stderr: {transform: unknownGenerator, objectMode: true}});1282 expectError(execa('unicorns', {stderr: {transform: unknownGenerator, objectMode: 'true'}}));1283 execa('unicorns', {stderr: undefined});1284 execaSync('unicorns', {stderr: undefined});87 unmodified lines1372 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 lines1423 [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 linesmodified:readme.md+2 added
916 unmodified lines917 Default: `true`918 919 Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from the output.920 921+If the [`lines` option](#lines) is true, this applies to each output line instead.922+923 #### maxBuffer924 925 Type: `number`\926 Default: `100_000_000` (100 MB)234 unmodified lines