Skip to content
Myr

Fix `execaSync()` output transforms

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

With a binary encoding, no line is split, so transforms wrongly ran on the empty output when the subprocess produced none. That empty chunk is now not passed to them. A transform failing on one file descriptor also prevented the other ones from being redirected to their target. Those errors are now collected per file descriptor, so each one keeps writing its own files.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 004f3b3 · Browse files · 3 files+58 added−17 removed
This commit

State

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

Diff stats

3 files+58 added−17 removed

  • tests:+39 added−1 removed
  • other:+19 added−16 removed

3 files+58 added−17 removed

modified:lib/io/output-sync.js+19 added-16 removed
modified:test/transform/generator-error.js+22 added-1 removed
1import {once} from 'node:events';2import {readFile, rm} from 'node:fs/promises';3import test from 'ava';4import tempfile from 'tempfile';5import {execa, execaSync} from '../../index.js';6import {foobarString} from '../helpers/input.js';5import {noopGenerator, infiniteGenerator, convertTransformToFinal} from '../helpers/generator.js';7import {noopGenerator, infiniteGenerator, convertTransformToFinal, throwingGenerator} from '../helpers/generator.js';8import {generatorsMap} from '../helpers/map.js';9import {setFixtureDirectory} from '../helpers/fixtures-directory.js';10import {getEarlyErrorSubprocess, expectedEarlyError} from '../helpers/early-error.js';11
78 unmodified lines
90test('Generators are destroyed on early subprocess exit', async t => {91 const error = await t.throwsAsync(getEarlyErrorSubprocess({stdout: infiniteGenerator()}));92 t.like(error, expectedEarlyError);93});9495// A transform failing on one file descriptor must not prevent the other ones from being redirected to their target96const testTransformErrorOtherFd = async (t, execaMethod) => {97 const filePath = tempfile();98 const cause = new Error(foobarString);99 const runSubprocess = () => execaMethod('noop-both.js', [foobarString], {100 stdout: throwingGenerator(cause)(),101 stderr: {file: filePath},102 });103104 await (execaMethod === execa105 ? t.throwsAsync(runSubprocess())106 : t.throws(runSubprocess));107 t.is(await readFile(filePath, 'utf8'), `${foobarString}\n`);108 await rm(filePath);109};110111test('Transform errors on stdout do not skip the stderr file', testTransformErrorOtherFd, execa);112test('Transform errors on stdout do not skip the stderr file, sync', testTransformErrorOtherFd, execaSync);
modified:test/transform/split-binary.js+17 added
90 unmodified lines
91test('Does not split lines when "binary" is undefined, encoding "hex", preserveNewlines, sync', testBinaryOption, undefined, simpleChunks, simpleChunksUint8Array, simpleFullHex, false, false, 'hex', execaSync);92test('Does not split lines when "binary" is false, encoding "hex", preserveNewlines, sync', testBinaryOption, false, simpleChunks, simpleChunksUint8Array, simpleFullHex, false, false, 'hex', execaSync);93test('Splits lines when "binary" is false, objectMode, preserveNewlines, sync', testBinaryOption, false, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, false, 'utf8', execaSync);94test('Splits lines when "binary" is undefined, objectMode, preserveNewlines, sync', testBinaryOption, undefined, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, false, 'utf8', execaSync);9596// With a binary encoding, no line is split, so transforms must not run when the subprocess produced no output at all97const bracketsGenerator = function * () {98 yield '[]';99};100101const testNoOutputBinary = async (t, execaMethod) => {102 const {stdout} = await execaMethod('empty.js', {103 stdout: [bracketsGenerator],104 encoding: 'hex',105 stripFinalNewline: false,106 });107 t.is(stdout, '');108};109110test('Does not run transforms on empty output with a binary encoding', testNoOutputBinary, execa);111test('Does not run transforms on empty output with a binary encoding, sync', testNoOutputBinary, execaSync);