Add support for `lines` option with `execaSync()` (#951)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 6233874 · Browse files · 10 files·+188 added−95 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+188 added−95 removed
- tests:+139 added−66 removed
- other:+49 added−29 removed
Provenance
- Commit
88bff1f- Tree
5f38cf7- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
6233874
10 files changed+188 added−95 removed
modified:lib/stdio/lines.js+6 added-7 removed
12345667899101112131414151617161719201819202122236 unmodified linesimport {MaxBufferError} from 'get-stream';import stripFinalNewlineFunction from 'strip-final-newline';
// 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 = ({options: {lines, stripFinalNewline, maxBuffer}, isSync, direction, optionName, objectMode, outputLines}) => shouldSplitLines({lines, isSync, direction, objectMode})export const handleStreamsLines = ({options: {lines, stripFinalNewline, maxBuffer}, isSync, direction, optionName, objectMode, outputLines}) => shouldSplitLines(lines, direction, objectMode) ? [{ type: 'generator', value: {transform: linesEndGenerator.bind(undefined, {outputLines, stripFinalNewline, maxBuffer}), preserveNewlines: true}, value: {transform: linesEndGenerator.bind(undefined, {outputLines, stripFinalNewline, maxBuffer, isSync}), preserveNewlines: true}, optionName, }] : [];
const shouldSplitLines = ({lines, isSync, direction, objectMode}) => direction === 'output'const shouldSplitLines = (lines, direction, objectMode) => direction === 'output' && lines && !objectMode && !isSync; && !objectMode;
const linesEndGenerator = function * ({outputLines, stripFinalNewline, maxBuffer}, line) { if (outputLines.length >= maxBuffer) {const linesEndGenerator = function * ({outputLines, stripFinalNewline, maxBuffer, isSync}, line) { if (!isSync && outputLines.length >= maxBuffer) { const error = new MaxBufferError(); error.bufferedData = outputLines; throw error; }6 unmodified linesmodified:lib/stdio/output-sync.js+14 added-12 removed
8 unmodified lines91011121313141516171718192021222324222324252627282829303132323334353536373838394041424 unmodified lines474849505151525353545556565758596061625 unmodified lines8 unmodified lines }
const state = {}; const transformedOutput = output.map((result, fdNumber) => transformOutputResultSync({result, fileDescriptors, fdNumber, options, state})); transformOutputResultSync({result, fileDescriptors, fdNumber, state}, options)); return {output: transformedOutput, ...state};};
const transformOutputResultSync = ({result, fileDescriptors, fdNumber, options, state}) => {const transformOutputResultSync = ({result, fileDescriptors, fdNumber, state}, {encoding, lines}) => { if (result === null) { return result; }
const allStdioItems = fileDescriptors .filter(fileDescriptor => fileDescriptor.fdNumber === fdNumber && fileDescriptor.direction === 'output') .flatMap(({stdioItems}) => stdioItems); const selectedFileDescriptors = fileDescriptors.filter(fileDescriptor => fileDescriptor.fdNumber === fdNumber && fileDescriptor.direction === 'output'); const allStdioItems = selectedFileDescriptors.flatMap(({stdioItems}) => stdioItems); const allOutputLines = selectedFileDescriptors.map(({outputLines}) => outputLines); const uint8ArrayResult = bufferToUint8Array(result); const generators = getGenerators(allStdioItems); const chunks = runOutputGeneratorsSync([uint8ArrayResult], generators, state); const transformedResult = serializeChunks(chunks, generators, options); const {serializedResult, finalResult} = serializeChunks({chunks, generators, allOutputLines, encoding, lines});
try { if (state.error === undefined) { writeToFiles(transformedResult, allStdioItems); writeToFiles(serializedResult, allStdioItems); }
return transformedResult; return finalResult; } catch (error) { state.error = error; return transformedResult; return finalResult; }};
const runOutputGeneratorsSync = (chunks, generators, state) => {4 unmodified lines return chunks; }};
const serializeChunks = (chunks, generators, {encoding}) => {const serializeChunks = ({chunks, generators, allOutputLines, encoding, lines}) => { if (generators.at(-1)?.value?.readableObjectMode) { return chunks; return {finalResult: chunks}; }
return encoding === 'buffer' ? joinToUint8Array(chunks) : joinToString(chunks, true); const serializedResult = encoding === 'buffer' ? joinToUint8Array(chunks) : joinToString(chunks, true); const finalResult = lines ? allOutputLines.flat() : serializedResult; return {serializedResult, finalResult};};
const writeToFiles = (transformedResult, allStdioItems) => { for (const {type, path} of allStdioItems) {5 unmodified linesmodified:test/return/error.js+13 added-7 removed
75 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`], {80+const testLinesMessage = async (t, encoding, stripFinalNewline, execaMethod) => {81+ const {failed, message} = await execaMethod('noop-fail.js', ['1', `${foobarString}\n${foobarString}\n`], {82 lines: true,83 encoding,84 stripFinalNewline,85+ reject: false,85- }));86+ });87+ t.true(failed);88 t.true(message.endsWith(`noop-fail.js 1 ${QUOTE}${foobarString}\\n${foobarString}\\n${QUOTE}\n\n${foobarString}\n${foobarString}`));89 };90 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);91+test('error.message handles "lines: true"', testLinesMessage, 'utf8', false, execa);92+test('error.message handles "lines: true", stripFinalNewline', testLinesMessage, 'utf8', true, execa);93+test('error.message handles "lines: true", buffer', testLinesMessage, 'buffer', false, execa);94+test('error.message handles "lines: true", buffer, stripFinalNewline', testLinesMessage, 'buffer', true, execa);95+test('error.message handles "lines: true", sync', testLinesMessage, 'utf8', false, execaSync);96+test('error.message handles "lines: true", stripFinalNewline, sync', testLinesMessage, 'utf8', true, execaSync);97+test('error.message handles "lines: true", buffer, sync', testLinesMessage, 'buffer', false, execaSync);98+test('error.message handles "lines: true", buffer, stripFinalNewline, sync', testLinesMessage, 'buffer', true, execaSync);99 100 const testPartialIgnoreMessage = async (t, fdNumber, stdioOption, output) => {101 const {message} = await t.throwsAsync(execa('echo-fail.js', getStdio(fdNumber, stdioOption, 4)));102 t.true(message.endsWith(`echo-fail.js\n\n${output}\n\nfd3`));109 unmodified linesmodified:test/stdio/encoding-final.js+26 added-12 removed
155 unmodified lines156 test('Is not ignored with other encodings and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false, base64Options, execa);157 test('Is not ignored with other encodings and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, base64Options, execa);158 test('Is not ignored with other encodings and undefined', testIgnoredEncoding, undefined, false, base64Options, execa);159 test('Is not ignored with other encodings and null', testIgnoredEncoding, null, false, base64Options, execa);160-test('Is ignored with other encodings and "ignore", sync', testIgnoredEncoding, 'ignore', true, base64Options, execaSync);161-test('Is ignored with other encodings and ["ignore"], sync', testIgnoredEncoding, ['ignore'], true, base64Options, execaSync);162-test('Is ignored with other encodings and "inherit", sync', testIgnoredEncoding, 'inherit', true, base64Options, execaSync);163-test('Is ignored with other encodings and ["inherit"], sync', testIgnoredEncoding, ['inherit'], true, base64Options, execaSync);164-test('Is ignored with other encodings and 1, sync', testIgnoredEncoding, 1, true, base64Options, execaSync);165-test('Is ignored with other encodings and [1], sync', testIgnoredEncoding, [1], true, base64Options, execaSync);166-test('Is ignored with other encodings and process.stdout, sync', testIgnoredEncoding, process.stdout, true, base64Options, execaSync);167-test('Is ignored with other encodings and [process.stdout], sync', testIgnoredEncoding, [process.stdout], true, base64Options, execaSync);168-test('Is not ignored with other encodings and "pipe", sync', testIgnoredEncoding, 'pipe', false, base64Options, execaSync);169-test('Is not ignored with other encodings and ["pipe"], sync', testIgnoredEncoding, ['pipe'], false, base64Options, execaSync);170-test('Is not ignored with other encodings and undefined, sync', testIgnoredEncoding, undefined, false, base64Options, execaSync);171-test('Is not ignored with other encodings and null, sync', testIgnoredEncoding, null, false, base64Options, execaSync);160 test('Is ignored with "lines: true" and "ignore"', testIgnoredEncoding, 'ignore', true, linesOptions, execa);161 test('Is ignored with "lines: true" and ["ignore"]', testIgnoredEncoding, ['ignore'], true, linesOptions, execa);162 test('Is ignored with "lines: true" and "ipc"', testIgnoredEncoding, 'ipc', true, linesOptions, execa);163 test('Is ignored with "lines: true" and ["ipc"]', testIgnoredEncoding, ['ipc'], true, linesOptions, execa);11 unmodified lines175 test('Is not ignored with "lines: true" and undefined', testIgnoredEncoding, undefined, false, linesOptions, execa);176 test('Is not ignored with "lines: true" and null', testIgnoredEncoding, null, false, linesOptions, execa);177 test('Is ignored with "lines: true", other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, {...base64Options, ...linesOptions}, execa);178 test('Is not ignored with "lines: true", other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, {...base64Options, ...linesOptions}, execa);179+test('Is ignored with other encodings and "ignore", sync', testIgnoredEncoding, 'ignore', true, base64Options, execaSync);180+test('Is ignored with other encodings and ["ignore"], sync', testIgnoredEncoding, ['ignore'], true, base64Options, execaSync);181+test('Is ignored with other encodings and "inherit", sync', testIgnoredEncoding, 'inherit', true, base64Options, execaSync);182+test('Is ignored with other encodings and ["inherit"], sync', testIgnoredEncoding, ['inherit'], true, base64Options, execaSync);183+test('Is ignored with other encodings and 1, sync', testIgnoredEncoding, 1, true, base64Options, execaSync);184+test('Is ignored with other encodings and [1], sync', testIgnoredEncoding, [1], true, base64Options, execaSync);185+test('Is ignored with other encodings and process.stdout, sync', testIgnoredEncoding, process.stdout, true, base64Options, execaSync);186+test('Is ignored with other encodings and [process.stdout], sync', testIgnoredEncoding, [process.stdout], true, base64Options, execaSync);187+test('Is not ignored with other encodings and "pipe", sync', testIgnoredEncoding, 'pipe', false, base64Options, execaSync);188+test('Is not ignored with other encodings and ["pipe"], sync', testIgnoredEncoding, ['pipe'], false, base64Options, execaSync);189+test('Is not ignored with other encodings and undefined, sync', testIgnoredEncoding, undefined, false, base64Options, execaSync);190+test('Is not ignored with other encodings and null, sync', testIgnoredEncoding, null, false, base64Options, execaSync);191+test('Is ignored with "lines: true" and "ignore", sync', testIgnoredEncoding, 'ignore', true, linesOptions, execaSync);192+test('Is ignored with "lines: true" and ["ignore"], sync', testIgnoredEncoding, ['ignore'], true, linesOptions, execaSync);193+test('Is ignored with "lines: true" and "inherit", sync', testIgnoredEncoding, 'inherit', true, linesOptions, execaSync);194+test('Is ignored with "lines: true" and ["inherit"], sync', testIgnoredEncoding, ['inherit'], true, linesOptions, execaSync);195+test('Is ignored with "lines: true" and 1, sync', testIgnoredEncoding, 1, true, linesOptions, execaSync);196+test('Is ignored with "lines: true" and [1], sync', testIgnoredEncoding, [1], true, linesOptions, execaSync);197+test('Is ignored with "lines: true" and process.stdout, sync', testIgnoredEncoding, process.stdout, true, linesOptions, execaSync);198+test('Is ignored with "lines: true" and [process.stdout], sync', testIgnoredEncoding, [process.stdout], true, linesOptions, execaSync);199+test('Is not ignored with "lines: true" and "pipe", sync', testIgnoredEncoding, 'pipe', false, linesOptions, execaSync);200+test('Is not ignored with "lines: true" and ["pipe"], sync', testIgnoredEncoding, ['pipe'], false, linesOptions, execaSync);201+test('Is not ignored with "lines: true" and undefined, sync', testIgnoredEncoding, undefined, false, linesOptions, execaSync);202+test('Is not ignored with "lines: true" and null, sync', testIgnoredEncoding, null, false, linesOptions, execaSync);203+test('Is ignored with "lines: true", other encodings and "ignore", sync', testIgnoredEncoding, 'ignore', true, {...base64Options, ...linesOptions}, execaSync);204+test('Is not ignored with "lines: true", other encodings and "pipe", sync', testIgnoredEncoding, 'pipe', false, {...base64Options, ...linesOptions}, execaSync);205 206 // eslint-disable-next-line max-params207 const testEncodingInput = async (t, input, expectedStdout, encoding, execaMethod) => {208 const {stdout} = await execaMethod('stdin.js', {input, encoding});15 unmodified linesmodified:test/stdio/file-path.js+24 added
296 unmodified lines297 test('stderr can use generators together with output file URLs, sync', testOutputFileTransform, 2, pathToFileURL, execaSync);298 test('stdio[*] can use generators together with output file paths, sync', testOutputFileTransform, 3, getAbsolutePath, execaSync);299 test('stdio[*] can use generators together with output file URLs, sync', testOutputFileTransform, 3, pathToFileURL, execaSync);300 301+const testOutputFileLines = async (t, fdNumber, mapFile, execaMethod) => {302+ const filePath = tempfile();303+ const {stdio} = await execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], {304+ ...getStdio(fdNumber, mapFile(filePath)),305+ lines: true,306+ });307+ t.deepEqual(stdio[fdNumber], [foobarString]);308+ t.is(await readFile(filePath, 'utf8'), foobarString);309+ await rm(filePath);310+};311+312+test('stdout can use "lines: true" together with output file paths', testOutputFileLines, 1, getAbsolutePath, execa);313+test('stdout can use "lines: true" together with output file URLs', testOutputFileLines, 1, pathToFileURL, execa);314+test('stderr can use "lines: true" together with output file paths', testOutputFileLines, 2, getAbsolutePath, execa);315+test('stderr can use "lines: true" together with output file URLs', testOutputFileLines, 2, pathToFileURL, execa);316+test('stdio[*] can use "lines: true" together with output file paths', testOutputFileLines, 3, getAbsolutePath, execa);317+test('stdio[*] can use "lines: true" together with output file URLs', testOutputFileLines, 3, pathToFileURL, execa);318+test('stdout can use "lines: true" together with output file paths, sync', testOutputFileLines, 1, getAbsolutePath, execaSync);319+test('stdout can use "lines: true" together with output file URLs, sync', testOutputFileLines, 1, pathToFileURL, execaSync);320+test('stderr can use "lines: true" together with output file paths, sync', testOutputFileLines, 2, getAbsolutePath, execaSync);321+test('stderr can use "lines: true" together with output file URLs, sync', testOutputFileLines, 2, pathToFileURL, execaSync);322+test('stdio[*] can use "lines: true" together with output file paths, sync', testOutputFileLines, 3, getAbsolutePath, execaSync);323+test('stdio[*] can use "lines: true" together with output file URLs, sync', testOutputFileLines, 3, pathToFileURL, execaSync);324+325 const testOutputFileObject = async (t, fdNumber, mapFile, execaMethod) => {326 const filePath = tempfile();327 t.throws(() => {328 execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, [52 unmodified linesmodified:test/stdio/lines.js+65 added-41 removed
20 unmodified lines21 } from '../helpers/lines.js';22 23 setFixtureDir();24 25-const getSimpleChunkSubprocess = options => execa('noop-fd.js', ['1', simpleFull], {lines: true, ...options});25+const getSimpleChunkSubprocessAsync = options => getSimpleChunkSubprocess(execa, options);26+const getSimpleChunkSubprocess = (execaMethod, options) => execaMethod('noop-fd.js', ['1', simpleFull], {lines: true, ...options});27 28 // eslint-disable-next-line max-params28-const testStreamLines = async (t, fdNumber, input, expectedOutput, stripFinalNewline) => {29- const {stdio} = await execa('noop-fd.js', [`${fdNumber}`, input], {29+const testStreamLines = async (t, fdNumber, input, expectedOutput, stripFinalNewline, execaMethod) => {30+ const {stdio} = await execaMethod('noop-fd.js', [`${fdNumber}`, input], {31 ...fullStdio,32 lines: true,33 stripFinalNewline,34 });35 t.deepEqual(stdio[fdNumber], expectedOutput);36 };37 37-test('"lines: true" splits lines, stdout, string', testStreamLines, 1, simpleFull, simpleLines, false);38-test('"lines: true" splits lines, stderr, string', testStreamLines, 2, simpleFull, simpleLines, false);39-test('"lines: true" splits lines, stdio[*], string', testStreamLines, 3, simpleFull, simpleLines, false);40-test('"lines: true" splits lines, stdout, string, stripFinalNewline', testStreamLines, 1, simpleFull, noNewlinesChunks, true);41-test('"lines: true" splits lines, stderr, string, stripFinalNewline', testStreamLines, 2, simpleFull, noNewlinesChunks, true);42-test('"lines: true" splits lines, stdio[*], string, stripFinalNewline', testStreamLines, 3, simpleFull, noNewlinesChunks, true);38+test('"lines: true" splits lines, stdout, string', testStreamLines, 1, simpleFull, simpleLines, false, execa);39+test('"lines: true" splits lines, stderr, string', testStreamLines, 2, simpleFull, simpleLines, false, execa);40+test('"lines: true" splits lines, stdio[*], string', testStreamLines, 3, simpleFull, simpleLines, false, execa);41+test('"lines: true" splits lines, stdout, string, stripFinalNewline', testStreamLines, 1, simpleFull, noNewlinesChunks, true, execa);42+test('"lines: true" splits lines, stderr, string, stripFinalNewline', testStreamLines, 2, simpleFull, noNewlinesChunks, true, execa);43+test('"lines: true" splits lines, stdio[*], string, stripFinalNewline', testStreamLines, 3, simpleFull, noNewlinesChunks, true, execa);44+test('"lines: true" splits lines, stdout, string, sync', testStreamLines, 1, simpleFull, simpleLines, false, execaSync);45+test('"lines: true" splits lines, stderr, string, sync', testStreamLines, 2, simpleFull, simpleLines, false, execaSync);46+test('"lines: true" splits lines, stdio[*], string, sync', testStreamLines, 3, simpleFull, simpleLines, false, execaSync);47+test('"lines: true" splits lines, stdout, string, stripFinalNewline, sync', testStreamLines, 1, simpleFull, noNewlinesChunks, true, execaSync);48+test('"lines: true" splits lines, stderr, string, stripFinalNewline, sync', testStreamLines, 2, simpleFull, noNewlinesChunks, true, execaSync);49+test('"lines: true" splits lines, stdio[*], string, stripFinalNewline, sync', testStreamLines, 3, simpleFull, noNewlinesChunks, true, execaSync);50 51 const testStreamLinesNoop = async (t, lines, execaMethod) => {52 const {stdout} = await execaMethod('noop-fd.js', ['1', simpleFull], {lines});53 t.is(stdout, simpleFull);54 };55 56 test('"lines: false" is a noop with execa()', testStreamLinesNoop, false, execa);57 test('"lines: false" is a noop with execaSync()', testStreamLinesNoop, false, execaSync);51-test('"lines: true" is a noop with execaSync()', testStreamLinesNoop, true, execaSync);58 59 const bigArray = Array.from({length: 1e5}).fill('.\n');60 const bigString = bigArray.join('');61 const bigStringNoNewlines = '.'.repeat(1e6);62 const bigStringNoNewlinesEnd = `${bigStringNoNewlines}\n`;63 64 // eslint-disable-next-line max-params59-const testStreamLinesGenerator = async (t, input, expectedLines, objectMode, binary) => {60- const {stdout} = await execa('noop.js', {65+const testStreamLinesGenerator = async (t, input, expectedLines, objectMode, binary, execaMethod) => {66+ const {stdout} = await execaMethod('noop.js', {67 stdout: getOutputsGenerator(input)(objectMode, binary),68 lines: true,69 stripFinalNewline: false,70 });71 t.deepEqual(stdout, expectedLines);72 };73 68-test('"lines: true" works with strings generators', testStreamLinesGenerator, simpleChunks, simpleFullEndLines, false, false);69-test('"lines: true" works with strings generators, binary', testStreamLinesGenerator, simpleChunks, simpleLines, false, true);70-test('"lines: true" works with big strings generators', testStreamLinesGenerator, [bigString], bigArray, false, false);71-test('"lines: true" works with big strings generators without newlines', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlinesEnd], false, false);72-test('"lines: true" is a noop with strings generators, objectMode', testStreamLinesGenerator, simpleChunks, simpleChunks, true, false);73-test('"lines: true" is a noop with strings generators, binary, objectMode', testStreamLinesGenerator, simpleChunks, simpleChunks, true, true);74-test('"lines: true" is a noop big strings generators, objectMode', testStreamLinesGenerator, [bigString], [bigString], true, false);75-test('"lines: true" is a noop big strings generators without newlines, objectMode', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlines], true, false);74+test('"lines: true" works with strings generators', testStreamLinesGenerator, simpleChunks, simpleFullEndLines, false, false, execa);75+test('"lines: true" works with strings generators, binary', testStreamLinesGenerator, simpleChunks, simpleLines, false, true, execa);76+test('"lines: true" works with big strings generators', testStreamLinesGenerator, [bigString], bigArray, false, false, execa);77+test('"lines: true" works with big strings generators without newlines', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlinesEnd], false, false, execa);78+test('"lines: true" is a noop with strings generators, objectMode', testStreamLinesGenerator, simpleChunks, simpleChunks, true, false, execa);79+test('"lines: true" is a noop with strings generators, binary, objectMode', testStreamLinesGenerator, simpleChunks, simpleChunks, true, true, execa);80+test('"lines: true" is a noop big strings generators, objectMode', testStreamLinesGenerator, [bigString], [bigString], true, false, execa);81+test('"lines: true" is a noop big strings generators without newlines, objectMode', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlines], true, false, execa);82+test('"lines: true" works with strings generators, sync', testStreamLinesGenerator, simpleChunks, simpleFullEndLines, false, false, execaSync);83+test('"lines: true" works with strings generators, binary, sync', testStreamLinesGenerator, simpleChunks, simpleLines, false, true, execaSync);84+test('"lines: true" works with big strings generators, sync', testStreamLinesGenerator, [bigString], bigArray, false, false, execaSync);85+test('"lines: true" works with big strings generators without newlines, sync', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlinesEnd], false, false, execaSync);86+test('"lines: true" is a noop with strings generators, objectMode, sync', testStreamLinesGenerator, simpleChunks, simpleChunks, true, false, execaSync);87+test('"lines: true" is a noop with strings generators, binary, objectMode, sync', testStreamLinesGenerator, simpleChunks, simpleChunks, true, true, execaSync);88+test('"lines: true" is a noop big strings generators, objectMode, sync', testStreamLinesGenerator, [bigString], [bigString], true, false, execaSync);89+test('"lines: true" is a noop big strings generators without newlines, objectMode, sync', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlines], true, false, execaSync);90 77-test('"lines: true" is a noop with objects generators, objectMode', async t => {78- const {stdout} = await execa('noop.js', {91+const testLinesObjectMode = async (t, execaMethod) => {92+ const {stdout} = await execaMethod('noop.js', {93 stdout: getOutputsGenerator([foobarObject])(true),94 lines: true,95 });96 t.deepEqual(stdout, [foobarObject]);83-});97+};98 99+test('"lines: true" is a noop with objects generators, objectMode', testLinesObjectMode, execa);100+test('"lines: true" is a noop with objects generators, objectMode, sync', testLinesObjectMode, execaSync);101+102+// eslint-disable-next-line max-params85-const testBinaryEncoding = async (t, expectedOutput, encoding, stripFinalNewline) => {86- const {stdout} = await getSimpleChunkSubprocess({encoding, stripFinalNewline});103+const testBinaryEncoding = async (t, expectedOutput, encoding, stripFinalNewline, execaMethod) => {104+ const {stdout} = await getSimpleChunkSubprocess(execaMethod, {encoding, stripFinalNewline});105 t.deepEqual(stdout, expectedOutput);106 };107 90-test('"lines: true" is a noop with "encoding: buffer"', testBinaryEncoding, simpleFullUint8Array, 'buffer', false);91-test('"lines: true" is a noop with "encoding: buffer", stripFinalNewline', testBinaryEncoding, simpleFullUint8Array, 'buffer', false);92-test('"lines: true" is a noop with "encoding: hex"', testBinaryEncoding, simpleFullHex, 'hex', false);93-test('"lines: true" is a noop with "encoding: hex", stripFinalNewline', testBinaryEncoding, simpleFullHex, 'hex', true);108+test('"lines: true" is a noop with "encoding: buffer"', testBinaryEncoding, simpleFullUint8Array, 'buffer', false, execa);109+test('"lines: true" is a noop with "encoding: buffer", stripFinalNewline', testBinaryEncoding, simpleFullUint8Array, 'buffer', false, execa);110+test('"lines: true" is a noop with "encoding: hex"', testBinaryEncoding, simpleFullHex, 'hex', false, execa);111+test('"lines: true" is a noop with "encoding: hex", stripFinalNewline', testBinaryEncoding, simpleFullHex, 'hex', true, execa);112+test('"lines: true" is a noop with "encoding: buffer", sync', testBinaryEncoding, simpleFullUint8Array, 'buffer', false, execaSync);113+test('"lines: true" is a noop with "encoding: buffer", stripFinalNewline, sync', testBinaryEncoding, simpleFullUint8Array, 'buffer', false, execaSync);114+test('"lines: true" is a noop with "encoding: hex", sync', testBinaryEncoding, simpleFullHex, 'hex', false, execaSync);115+test('"lines: true" is a noop with "encoding: hex", stripFinalNewline, sync', testBinaryEncoding, simpleFullHex, 'hex', true, execaSync);116 95-const testTextEncoding = async (t, expectedLines, stripFinalNewline) => {96- const {stdout} = await execa('stdin.js', {117+const testTextEncoding = async (t, expectedLines, stripFinalNewline, execaMethod) => {118+ const {stdout} = await execaMethod('stdin.js', {119 lines: true,120 stripFinalNewline,121 encoding: 'utf16le',122 input: simpleFullUtf16Uint8Array,123 });124 t.deepEqual(stdout, expectedLines);125 };126 105-test('"lines: true" is a noop with "encoding: utf16"', testTextEncoding, simpleLines, false);106-test('"lines: true" is a noop with "encoding: utf16", stripFinalNewline', testTextEncoding, noNewlinesChunks, true);127+test('"lines: true" is a noop with "encoding: utf16"', testTextEncoding, simpleLines, false, execa);128+test('"lines: true" is a noop with "encoding: utf16", stripFinalNewline', testTextEncoding, noNewlinesChunks, true, execa);129+test('"lines: true" is a noop with "encoding: utf16", sync', testTextEncoding, simpleLines, false, execaSync);130+test('"lines: true" is a noop with "encoding: utf16", stripFinalNewline, sync', testTextEncoding, noNewlinesChunks, true, execaSync);131 132 test('"lines: true" is a noop with "buffer: false"', async t => {109- const {stdout} = await getSimpleChunkSubprocess({buffer: false});133+ const {stdout} = await getSimpleChunkSubprocessAsync({buffer: false});134 t.is(stdout, undefined);135 });136 137 test('"lines: true" can be below "maxBuffer"', async t => {138 const maxBuffer = simpleLines.length;115- const {stdout} = await getSimpleChunkSubprocess({maxBuffer});139+ const {stdout} = await getSimpleChunkSubprocessAsync({maxBuffer});140 t.deepEqual(stdout, noNewlinesChunks);141 });142 143 test('"lines: true" can be above "maxBuffer"', async t => {144 const maxBuffer = simpleLines.length - 1;121- const {cause, stdout} = await t.throwsAsync(getSimpleChunkSubprocess({maxBuffer}));145+ const {cause, stdout} = await t.throwsAsync(getSimpleChunkSubprocessAsync({maxBuffer}));146 t.true(cause instanceof MaxBufferError);147 t.deepEqual(stdout, noNewlinesChunks.slice(0, maxBuffer));148 });149 150 test('"lines: true" stops on stream error', async t => {151 const cause = new Error(foobarString);128- const error = await t.throwsAsync(getSimpleChunkSubprocess({152+ const error = await t.throwsAsync(getSimpleChunkSubprocessAsync({153 * stdout(line) {154 if (line === noNewlinesChunks[2]) {155 throw cause;156 }5 unmodified lines162 t.deepEqual(error.stdout, noNewlinesChunks.slice(0, 2));163 });164 165 const testAsyncIteration = async (t, expectedLines, stripFinalNewline) => {142- const subprocess = getSimpleChunkSubprocess({stripFinalNewline});166+ const subprocess = getSimpleChunkSubprocessAsync({stripFinalNewline});167 t.false(subprocess.stdout.readableObjectMode);168 await assertStreamOutput(t, subprocess.stdout, simpleFull);169 const {stdout} = await subprocess;170 t.deepEqual(stdout, expectedLines);2 unmodified lines173 test('"lines: true" works with stream async iteration', testAsyncIteration, simpleLines, false);174 test('"lines: true" works with stream async iteration, stripFinalNewline', testAsyncIteration, noNewlinesChunks, true);175 176 const testDataEvents = async (t, expectedLines, stripFinalNewline) => {153- const subprocess = getSimpleChunkSubprocess({stripFinalNewline});177+ const subprocess = getSimpleChunkSubprocessAsync({stripFinalNewline});178 const [firstLine] = await once(subprocess.stdout, 'data');179 t.deepEqual(firstLine, Buffer.from(simpleLines[0]));180 const {stdout} = await subprocess;181 t.deepEqual(stdout, expectedLines);10 unmodified lines192 done();193 },194 decodeStrings: false,195 });172- const {stdout} = await getSimpleChunkSubprocess({stripFinalNewline, stdout: ['pipe', writable]});196+ const {stdout} = await getSimpleChunkSubprocessAsync({stripFinalNewline, stdout: ['pipe', writable]});197 t.deepEqual(lines, simpleLines);198 t.deepEqual(stdout, expectedLines);199 };200 201 test('"lines: true" works with writable streams targets', testWritableStream, simpleLines, false);202 test('"lines: true" works with writable streams targets, stripFinalNewline', testWritableStream, noNewlinesChunks, true);203 204 const testIterable = async (t, expectedLines, stripFinalNewline) => {181- const subprocess = getSimpleChunkSubprocess({stripFinalNewline});205+ const subprocess = getSimpleChunkSubprocessAsync({stripFinalNewline});206 await assertIterableChunks(t, subprocess, noNewlinesChunks);207 const {stdout} = await subprocess;208 t.deepEqual(stdout, expectedLines);209 };3 unmodified linesmodified:test/stdio/split.js+11 added-6 removed
243 unmodified lines244 lines.push(chunk);245 yield new TextEncoder().encode(chunk);246 };247 248+// eslint-disable-next-line max-params248-const testStringToUint8Array = async (t, expectedOutput, objectMode, preserveNewlines) => {249+const testStringToUint8Array = async (t, expectedOutput, objectMode, preserveNewlines, execaMethod) => {250 const lines = [];250- const {stdout} = await execa('noop-fd.js', ['1', foobarString], {251+ const {stdout} = await execaMethod('noop-fd.js', ['1', foobarString], {252 stdout: {253 transform: resultUint8ArrayGenerator.bind(undefined, lines),254 objectMode,255 preserveNewlines,3 unmodified lines259 t.deepEqual(lines, [foobarString]);260 t.deepEqual(stdout, expectedOutput);261 };262 262-test('Line splitting when converting from string to Uint8Array', testStringToUint8Array, [foobarString], false, true);263-test('Line splitting when converting from string to Uint8Array, objectMode', testStringToUint8Array, [foobarUint8Array], true, true);264-test('Line splitting when converting from string to Uint8Array, preserveNewlines', testStringToUint8Array, [foobarString], false, false);265-test('Line splitting when converting from string to Uint8Array, objectMode, preserveNewlines', testStringToUint8Array, [foobarUint8Array], true, false);263+test('Line splitting when converting from string to Uint8Array', testStringToUint8Array, [foobarString], false, true, execa);264+test('Line splitting when converting from string to Uint8Array, objectMode', testStringToUint8Array, [foobarUint8Array], true, true, execa);265+test('Line splitting when converting from string to Uint8Array, preserveNewlines', testStringToUint8Array, [foobarString], false, false, execa);266+test('Line splitting when converting from string to Uint8Array, objectMode, preserveNewlines', testStringToUint8Array, [foobarUint8Array], true, false, execa);267+test('Line splitting when converting from string to Uint8Array, sync', testStringToUint8Array, [foobarString], false, true, execaSync);268+test('Line splitting when converting from string to Uint8Array, objectMode, sync', testStringToUint8Array, [foobarUint8Array], true, true, execaSync);269+test('Line splitting when converting from string to Uint8Array, preserveNewlines, sync', testStringToUint8Array, [foobarString], false, false, execaSync);270+test('Line splitting when converting from string to Uint8Array, objectMode, preserveNewlines, sync', testStringToUint8Array, [foobarUint8Array], true, false, execaSync);271 272 const testStripNewline = async (t, input, expectedOutput, execaMethod) => {273 const {stdout} = await execaMethod('noop.js', {274 stdout: getOutputsGenerator([input])(),65 unmodified linesmodified:index.d.ts+3 added-3 removed
482 unmodified lines483 This cannot be used if the `encoding` option is binary.484 485 @default false486 */487- readonly lines?: Unless<IsSync, boolean>;487+ readonly lines?: boolean;488 489 /**490 Setting this to `false` resolves the promise with the error instead of rejecting it.491 916 unmodified lines1408 Same as `execa()` but synchronous.1409 1410 Returns or throws a `subprocessResult`. The `subprocess` is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), `.pipe()`, `.iterable()`, `.readable()`, `.writable()`, `.duplex()` and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.1411 1412-Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal`, `forceKillAfterDelay` and `lines`. Also, the `stdin`, `stdout`, `stderr` and `stdio` options cannot be a `['pipe', 'inherit']` array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async transform, a `Duplex`, or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.1412+Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal` and `forceKillAfterDelay`. Also, the `stdin`, `stdout`, `stderr` and `stdio` options cannot be a `['pipe', 'inherit']` array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async transform, a `Duplex`, or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.1413 1414 @param file - The program/script to execute, as a string or file URL1415 @param arguments - Arguments to pass to `file` on execution.1416 @returns A `subprocessResult` object105 unmodified lines1522 Same as `execaCommand()` but synchronous.1523 1524 Returns or throws a `subprocessResult`. The `subprocess` is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), `.pipe()`, `.iterable()`, `.readable()`, `.writable()`, `.duplex()` and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.1525 1526-Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal`, `forceKillAfterDelay` and `lines`. Also, the `stdin`, `stdout`, `stderr` and `stdio` options cannot be a `['pipe', 'inherit']` array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async transform, a `Duplex`, or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.1526+Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal` and `forceKillAfterDelay`. Also, the `stdin`, `stdout`, `stderr` and `stdio` options cannot be a `['pipe', 'inherit']` array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async transform, a `Duplex`, or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.1527 1528 @param command - The program/script to execute and its arguments.1529 @returns A `subprocessResult` object1530 @throws A `subprocessResult` error128 unmodified linesmodified:index.test-d.ts+25 added-6 removed
160 unmodified lines161 const preserveNewlinesOnly = {preserveNewlines: true} as const;162 const objectModeOnly = {objectMode: true} as const;163 const finalOnly = {final: unknownFinal} as const;164 165-type AnySyncChunk = string | Uint8Array | unknown[] | undefined;166-type AnyChunk = AnySyncChunk | string[];165+type AnyChunk = string | Uint8Array | string[] | unknown[] | undefined;166 expectType<Writable | null>({} as ExecaSubprocess['stdin']);167 expectType<Readable | null>({} as ExecaSubprocess['stdout']);168 expectType<Readable | null>({} as ExecaSubprocess['stderr']);169 expectType<Readable | undefined>({} as ExecaSubprocess['all']);170 expectType<AnyChunk>({} as ExecaResult['stdout']);171 expectType<AnyChunk>({} as ExecaResult['stderr']);172 expectType<AnyChunk>({} as ExecaResult['all']);173 expectAssignable<[undefined, AnyChunk, AnyChunk, ...AnyChunk[]]>({} as ExecaResult['stdio']);175-expectType<AnySyncChunk>({} as ExecaSyncResult['stdout']);176-expectType<AnySyncChunk>({} as ExecaSyncResult['stderr']);177-expectAssignable<[undefined, AnySyncChunk, AnySyncChunk, ...AnySyncChunk[]]>({} as ExecaSyncResult['stdio']);174+expectType<AnyChunk>({} as ExecaSyncResult['stdout']);175+expectType<AnyChunk>({} as ExecaSyncResult['stderr']);176+expectAssignable<[undefined, AnyChunk, AnyChunk, ...AnyChunk[]]>({} as ExecaSyncResult['stdio']);177 178 try {179 const execaPromise = execa('unicorns', {all: true});180 const unicornsResult = await execaPromise;940 unmodified lines1121 expectType<Uint8Array>(bufferResult.stderr);1122 expectType<Uint8Array>(bufferResult.stdio[2]);1123 expectError(bufferResult.all.toString());1124 1125+ const linesResult = execaSync('unicorns', {lines: true});1126+ expectType<string[]>(linesResult.stdout);1127+ expectType<string[]>(linesResult.stderr);1128+1129+ const linesBufferResult = execaSync('unicorns', {lines: true, encoding: 'buffer'});1130+ expectType<Uint8Array>(linesBufferResult.stdout);1131+ expectType<Uint8Array>(linesBufferResult.stderr);1132+1133+ const linesHexResult = execaSync('unicorns', {lines: true, encoding: 'hex'});1134+ expectType<string>(linesHexResult.stdout);1135+ expectType<string>(linesHexResult.stderr);1136+1137 const ignoreStdoutResult = execaSync('unicorns', {stdout: 'ignore'});1138 expectType<undefined>(ignoreStdoutResult.stdout);1139 expectType<undefined>(ignoreStdoutResult.stdio[1]);1140 expectType<string>(ignoreStdoutResult.stderr);59 unmodified lines1200 expectType<Uint8Array>(execaBufferError.stderr);1201 expectType<Uint8Array>(execaBufferError.stdio[2]);1202 expectError(execaBufferError.all.toString());1203 1204+ const execaLinesError = error as ExecaSyncError<{lines: true}>;1205+ expectType<string[]>(execaLinesError.stdout);1206+ expectType<string[]>(execaLinesError.stderr);1207+1208+ const execaLinesBufferError = error as ExecaSyncError<{lines: true; encoding: 'buffer'}>;1209+ expectType<Uint8Array>(execaLinesBufferError.stdout);1210+ expectType<Uint8Array>(execaLinesBufferError.stderr);1211+1212 const ignoreStdoutError = error as ExecaSyncError<{stdout: 'ignore'}>;1213 expectType<undefined>(ignoreStdoutError.stdout);1214 expectType<undefined>(ignoreStdoutError.stdio[1]);1215 expectType<string>(ignoreStdoutError.stderr);83 unmodified lines1299 execaSync('unicorns', {inputFile: fileUrl});1300 expectError(execa('unicorns', {inputFile: false}));1301 expectError(execaSync('unicorns', {inputFile: false}));1302 execa('unicorns', {lines: false});1284-expectError(execaSync('unicorns', {lines: false}));1303+execaSync('unicorns', {lines: false});1304 expectError(execa('unicorns', {lines: 'false'}));1305 expectError(execaSync('unicorns', {lines: 'false'}));1306 execa('unicorns', {reject: false});1307 execaSync('unicorns', {reject: false});2409 unmodified linesmodified:readme.md+1 added-1 removed
317 unmodified lines318 Same as [`execa()`](#execafile-arguments-options) but synchronous.319 320 Returns or throws a [`subprocessResult`](#subprocessResult). The [`subprocess`](#subprocess) is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), [`.pipe()`](#pipefile-arguments-options), [`.iterable()`](#iterablereadableoptions), [`.readable()`](#readablereadableoptions), [`.writable()`](#writablewritableoptions), [`.duplex()`](#duplexduplexoptions) and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.321 322-Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`ipc`](#ipc), [`serialization`](#serialization), [`cancelSignal`](#cancelsignal), [`forceKillAfterDelay`](#forcekillafterdelay) and [`lines`](#lines). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1) and [`stdio`](#stdio-1) cannot be a [`['pipe', 'inherit']`](#redirect-stdinstdoutstderr-to-multiple-destinations) array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async [transform](docs/transform.md), a [`Duplex`](docs/transform.md#duplextransform-streams), or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.322+Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`ipc`](#ipc), [`serialization`](#serialization), [`cancelSignal`](#cancelsignal) and [`forceKillAfterDelay`](#forcekillafterdelay). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1) and [`stdio`](#stdio-1) cannot be a [`['pipe', 'inherit']`](#redirect-stdinstdoutstderr-to-multiple-destinations) array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async [transform](docs/transform.md), a [`Duplex`](docs/transform.md#duplextransform-streams), or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.323 324 #### $(file, arguments?, options?)325 326 `file`: `string | URL`\899 unmodified lines