generator-error.js
This file
Path
test/transform/generator-error.js
164 lines · 7.8 KiB · JavaScript
Last changed by
Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 2why, one step back2import {once} from 'node:events';Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
line 3why, one step back3import {setTimeout} from 'node:timers/promises';Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
line 4why, one step back4import {readFile, rm} from 'node:fs/promises';TweaksSindre Sorhusbefore adoption698a1b8
line 5why, one step back5import test from 'node:test';Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
line 6why, one step back6import tempfile from 'tempfile';TweaksSindre Sorhusbefore adoption698a1b8
line 7why, one step back7import {assertThrows, assertRejects, assertLike} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 8–9why, one step back8import {execa, execaSync} from '../../index.js';9import {foobarString} from '../helpers/input.js';TweaksSindre Sorhusbefore adoption698a1b8
lines 10–15why, one step back10import {11 noopGenerator,12 infiniteGenerator,13 convertTransformToFinal,14 throwingGenerator,15} from '../helpers/generator.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 16–17why, one step back16import {generatorsMap} from '../helpers/map.js';17import {setFixtureDirectory} from '../helpers/fixtures-directory.js';Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 18why, one step back18import {getEarlyErrorSubprocess, expectedEarlyError} from '../helpers/early-error.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 19–21why, one step back1920setFixtureDirectory();21TweaksSindre Sorhusbefore adoption698a1b8
line 22why, one step back22const assertProcessError = async (type, execaMethod, getSubprocess) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 23–25why, one step back23 const cause = new Error(foobarString);24 const transform = generatorsMap[type].throwing(cause)();25 const error = execaMethod === execaTweaksSindre Sorhusbefore adoption698a1b8
lines 26–27why, one step back26 ? await assertRejects(getSubprocess(transform))27 : assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 28–29why, one step back28 getSubprocess(transform);29 });TweaksSindre Sorhusbefore adoption698a1b8
line 30why, one step back30 assert.equal(error.cause, cause);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 31–32why, one step back31};32TweaksSindre Sorhusbefore adoption698a1b8
lines 33–34why, one step back33const testThrowingGenerator = async (type, final, execaMethod) => {34 await assertProcessError(type, execaMethod, transform => execaMethod('noop.js', {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 35–38why, one step back35 stdout: convertTransformToFinal(transform, final),36 }));37};38TweaksSindre Sorhusbefore adoption698a1b8
lines 39–44why, one step back39test('Generators "transform" errors make subprocess fail', () => testThrowingGenerator('generator', false, execa));40test('Generators "final" errors make subprocess fail', () => testThrowingGenerator('generator', true, execa));41test('Generators "transform" errors make subprocess fail, sync', () => testThrowingGenerator('generator', false, execaSync));42test('Generators "final" errors make subprocess fail, sync', () => testThrowingGenerator('generator', true, execaSync));43test('Duplexes "transform" errors make subprocess fail', () => testThrowingGenerator('duplex', false, execa));44test('WebTransform "transform" errors make subprocess fail', () => testThrowingGenerator('webTransform', false, execa));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 45why, one step back45TweaksSindre Sorhusbefore adoption698a1b8
lines 46–47why, one step back46const testSingleErrorOutput = async (type, execaMethod) => {47 await assertProcessError(type, execaMethod, transform => execaMethod('noop.js', {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 48–55why, one step back48 stdout: [49 generatorsMap[type].noop(false),50 transform,51 generatorsMap[type].noop(false),52 ],53 }));54};55TweaksSindre Sorhusbefore adoption698a1b8
lines 56–59why, one step back56test('Generators errors make subprocess fail even when other output generators do not throw', () => testSingleErrorOutput('generator', execa));57test('Generators errors make subprocess fail even when other output generators do not throw, sync', () => testSingleErrorOutput('generator', execaSync));58test('Duplexes errors make subprocess fail even when other output generators do not throw', () => testSingleErrorOutput('duplex', execa));59test('WebTransform errors make subprocess fail even when other output generators do not throw', () => testSingleErrorOutput('webTransform', execa));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 60why, one step back60TweaksSindre Sorhusbefore adoption698a1b8
lines 61–62why, one step back61const testSingleErrorInput = async (type, execaMethod) => {62 await assertProcessError(type, execaMethod, transform => execaMethod('stdin.js', {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 63–71why, one step back63 stdin: [64 ['foobar\n'],65 generatorsMap[type].noop(false),66 transform,67 generatorsMap[type].noop(false),68 ],69 }));70};71TweaksSindre Sorhusbefore adoption698a1b8
lines 72–75why, one step back72test('Generators errors make subprocess fail even when other input generators do not throw', () => testSingleErrorInput('generator', execa));73test('Generators errors make subprocess fail even when other input generators do not throw, sync', () => testSingleErrorInput('generator', execaSync));74test('Duplexes errors make subprocess fail even when other input generators do not throw', () => testSingleErrorInput('duplex', execa));75test('WebTransform errors make subprocess fail even when other input generators do not throw', () => testSingleErrorInput('webTransform', execa));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 76why, one step back76TweaksSindre Sorhusbefore adoption698a1b8
line 77why, one step back77const testGeneratorCancel = async error => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 78–80why, one step back78 const subprocess = execa('noop.js', {stdout: infiniteGenerator()});79 await once(subprocess.stdout, 'data');80 subprocess.stdout.destroy(error);TweaksSindre Sorhusbefore adoption698a1b8
line 81why, one step back81 await (error === undefined ? assert.doesNotReject(subprocess) : assertRejects(subprocess));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 82–83why, one step back82};83TweaksSindre Sorhusbefore adoption698a1b8
lines 84–85why, one step back84test('Running generators are canceled on subprocess abort', () => testGeneratorCancel(undefined));85test('Running generators are canceled on subprocess error', () => testGeneratorCancel(new Error('test')));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 86why, one step back86TweaksSindre Sorhusbefore adoption698a1b8
line 87why, one step back87const testGeneratorDestroy = async transform => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 88–91why, one step back88 const subprocess = execa('forever.js', {stdout: transform});89 const cause = new Error('test');90 subprocess.stdout.destroy(cause);91 subprocess.kill();TweaksSindre Sorhusbefore adoption698a1b8
line 92why, one step back92 assertLike(await assertRejects(subprocess), {cause});100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 93–94why, one step back93};94TweaksSindre Sorhusbefore adoption698a1b8
lines 95–96why, one step back95test('Generators are destroyed on subprocess error, sync', () => testGeneratorDestroy(noopGenerator(false)));96test('Generators are destroyed on subprocess error, async', () => testGeneratorDestroy(infiniteGenerator()));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 97why, one step back97TweaksSindre Sorhusbefore adoption698a1b8
lines 98–100why, one step back98test('Generators are destroyed on early subprocess exit', async () => {99 const error = await assertRejects(getEarlyErrorSubprocess({stdout: infiniteGenerator()}));100 assertLike(error, expectedEarlyError);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 101why, one step back101});Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
lines 102–103why, one step back102103// A transform failing on one file descriptor must not prevent the other ones from being redirected to their targetTweaksSindre Sorhusbefore adoption698a1b8
line 104why, one step back104const testTransformErrorOtherFd = async execaMethod => {Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
lines 105–112why, one step back105 const filePath = tempfile();106 const cause = new Error(foobarString);107 const runSubprocess = () => execaMethod('noop-both.js', [foobarString], {108 stdout: throwingGenerator(cause)(),109 stderr: {file: filePath},110 });111112 await (execaMethod === execaTweaksSindre Sorhusbefore adoption698a1b8
lines 113–115why, one step back113 ? assertRejects(runSubprocess())114 : assertThrows(runSubprocess));115 assert.equal(await readFile(filePath, 'utf8'), `${foobarString}\n`);Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
lines 116–118why, one step back116 await rm(filePath);117};118TweaksSindre Sorhusbefore adoption698a1b8
lines 119–120why, one step back119test('Transform errors on stdout do not skip the stderr file', () => testTransformErrorOtherFd(execa));120test('Transform errors on stdout do not skip the stderr file, sync', () => testTransformErrorOtherFd(execaSync));Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
lines 121–164why, one step back121122// Many signal-aware Node.js APIs reject with an `AbortError`, which must not be mistaken for the stream being aborted123const testAbortErrorTransform = async abortedLine => {124 const controller = new AbortController();125 const {failed, cause} = await execa('noop-fd.js', ['1', 'a\nb\nc'], {126 async * stdout(line) {127 if (line === abortedLine) {128 controller.abort();129 }130131 await setTimeout(0, undefined, {signal: controller.signal});132 yield line;133 },134 reject: false,135 });136 assert.equal(failed, true);137 assert.equal(cause.code, 'ABORT_ERR');138};139140test('Output generators throwing an AbortError make the subprocess fail', () => testAbortErrorTransform('a'));141test('Output generators throwing an AbortError after some output make the subprocess fail', () => testAbortErrorTransform('b'));142143const abortErrorGenerator = async function * (line) {144 await setTimeout(0, undefined, {signal: AbortSignal.abort()});145 yield line;146};147148const testAbortErrorConversion = async consumeOutput => {149 const subprocess = execa('noop-fd.js', ['1', foobarString], {stdout: abortErrorGenerator});150 const error = await assertRejects(consumeOutput(subprocess));151 assert.equal(error.cause.code, 'ABORT_ERR');152 await assertRejects(subprocess);153};154155test('Output generators throwing an AbortError make the subprocess fail, with .readable()', () => testAbortErrorConversion(subprocess => subprocess.readable().toArray()));156test('Output generators throwing an AbortError make the subprocess fail, with .duplex()', () => testAbortErrorConversion(subprocess => subprocess.duplex().toArray()));157test('Output generators throwing an AbortError make the subprocess fail, with .iterable()', () => testAbortErrorConversion(subprocess => Array.fromAsync(subprocess.iterable())));158test('Output generators throwing an AbortError make the subprocess fail, with .readableStream()', () => testAbortErrorConversion(subprocess => Array.fromAsync(subprocess.readableStream())));159160test('Input generators throwing an AbortError make the subprocess fail', async () => {161 const {failed, cause} = await execa('stdin.js', {stdin: abortErrorGenerator, input: foobarString, reject: false});162 assert.equal(failed, true);163 assert.equal(cause.code, 'ABORT_ERR');164});