Skip to content
Myr

generator-error.js

This file

Path

test/transform/generator-error.js

119 lines · 5.5 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

The walk stopped after 200 commits: lines older than 0bd0f0d point at it.

TweaksSindre Sorhusbefore adoption698a1b8

line 1why, one step back
1import assert from 'node:assert/strict';

Split type files (#978)ehmickybefore adoption0bd0f0d

line 2why, one step back
2import {once} from 'node:events';

Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc

line 3why, one step back
3import {readFile, rm} from 'node:fs/promises';

TweaksSindre Sorhusbefore adoption698a1b8

line 4why, one step back
4import test from 'node:test';

Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc

line 5why, one step back
5import tempfile from 'tempfile';

TweaksSindre Sorhusbefore adoption698a1b8

line 6why, one step back
6import {assertThrows, assertRejects, assertLike} from '../helpers/assert.js';

Split type files (#978)ehmickybefore adoption0bd0f0d

lines 7–8why, one step back
7import {execa, execaSync} from '../../index.js';
8import {foobarString} from '../helpers/input.js';

TweaksSindre Sorhusbefore adoption698a1b8

lines 9–14why, one step back
9import {
10 noopGenerator,11 infiniteGenerator,12 convertTransformToFinal,13 throwingGenerator,14} from '../helpers/generator.js';

Split type files (#978)ehmickybefore adoption0bd0f0d

line 15why, one step back
15import {generatorsMap} from '../helpers/map.js';

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 16why, one step back
16import {setFixtureDirectory} from '../helpers/fixtures-directory.js';

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 17why, one step back
17import {getEarlyErrorSubprocess, expectedEarlyError} from '../helpers/early-error.js';

Split type files (#978)ehmickybefore adoption0bd0f0d

line 18why, one step back
18

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 19why, one step back
19setFixtureDirectory();

Split type files (#978)ehmickybefore adoption0bd0f0d

line 20why, one step back
20

TweaksSindre Sorhusbefore adoption698a1b8

line 21why, one step back
21const assertProcessError = async (type, execaMethod, getSubprocess) => {

Split type files (#978)ehmickybefore adoption0bd0f0d

lines 22–24why, one step back
22 const cause = new Error(foobarString);
23 const transform = generatorsMap[type].throwing(cause)();24 const error = execaMethod === execa

TweaksSindre Sorhusbefore adoption698a1b8

lines 25–26why, one step back
25 ? await assertRejects(getSubprocess(transform))
26 : assertThrows(() => {

Split type files (#978)ehmickybefore adoption0bd0f0d

lines 27–28why, one step back
27 getSubprocess(transform);
28 });

TweaksSindre Sorhusbefore adoption698a1b8

line 29why, one step back
29 assert.equal(error.cause, cause);

Split type files (#978)ehmickybefore adoption0bd0f0d

lines 30–31why, one step back
30};
31

TweaksSindre Sorhusbefore adoption698a1b8

lines 32–33why, one step back
32const testThrowingGenerator = async (type, final, execaMethod) => {
33 await assertProcessError(type, execaMethod, transform => execaMethod('noop.js', {

Split type files (#978)ehmickybefore adoption0bd0f0d

lines 34–37why, one step back
34 stdout: convertTransformToFinal(transform, final),
35 }));36};37

TweaksSindre Sorhusbefore adoption698a1b8

lines 38–43why, one step back
38test('Generators "transform" errors make subprocess fail', () => testThrowingGenerator('generator', false, execa));
39test('Generators "final" errors make subprocess fail', () => testThrowingGenerator('generator', true, execa));40test('Generators "transform" errors make subprocess fail, sync', () => testThrowingGenerator('generator', false, execaSync));41test('Generators "final" errors make subprocess fail, sync', () => testThrowingGenerator('generator', true, execaSync));42test('Duplexes "transform" errors make subprocess fail', () => testThrowingGenerator('duplex', false, execa));43test('WebTransform "transform" errors make subprocess fail', () => testThrowingGenerator('webTransform', false, execa));

Split type files (#978)ehmickybefore adoption0bd0f0d

line 44why, one step back
44

TweaksSindre Sorhusbefore adoption698a1b8

lines 45–46why, one step back
45const testSingleErrorOutput = async (type, execaMethod) => {
46 await assertProcessError(type, execaMethod, transform => execaMethod('noop.js', {

Split type files (#978)ehmickybefore adoption0bd0f0d

lines 47–54why, one step back
47 stdout: [
48 generatorsMap[type].noop(false),49 transform,50 generatorsMap[type].noop(false),51 ],52 }));53};54

TweaksSindre Sorhusbefore adoption698a1b8

lines 55–58why, one step back
55test('Generators errors make subprocess fail even when other output generators do not throw', () => testSingleErrorOutput('generator', execa));
56test('Generators errors make subprocess fail even when other output generators do not throw, sync', () => testSingleErrorOutput('generator', execaSync));57test('Duplexes errors make subprocess fail even when other output generators do not throw', () => testSingleErrorOutput('duplex', execa));58test('WebTransform errors make subprocess fail even when other output generators do not throw', () => testSingleErrorOutput('webTransform', execa));

Split type files (#978)ehmickybefore adoption0bd0f0d

line 59why, one step back
59

TweaksSindre Sorhusbefore adoption698a1b8

lines 60–61why, one step back
60const testSingleErrorInput = async (type, execaMethod) => {
61 await assertProcessError(type, execaMethod, transform => execaMethod('stdin.js', {

Split type files (#978)ehmickybefore adoption0bd0f0d

lines 62–70why, one step back
62 stdin: [
63 ['foobar\n'],64 generatorsMap[type].noop(false),65 transform,66 generatorsMap[type].noop(false),67 ],68 }));69};70

TweaksSindre Sorhusbefore adoption698a1b8

lines 71–74why, one step back
71test('Generators errors make subprocess fail even when other input generators do not throw', () => testSingleErrorInput('generator', execa));
72test('Generators errors make subprocess fail even when other input generators do not throw, sync', () => testSingleErrorInput('generator', execaSync));73test('Duplexes errors make subprocess fail even when other input generators do not throw', () => testSingleErrorInput('duplex', execa));74test('WebTransform errors make subprocess fail even when other input generators do not throw', () => testSingleErrorInput('webTransform', execa));

Split type files (#978)ehmickybefore adoption0bd0f0d

line 75why, one step back
75

TweaksSindre Sorhusbefore adoption698a1b8

line 76why, one step back
76const testGeneratorCancel = async error => {

Split type files (#978)ehmickybefore adoption0bd0f0d

lines 77–79why, one step back
77 const subprocess = execa('noop.js', {stdout: infiniteGenerator()});
78 await once(subprocess.stdout, 'data');79 subprocess.stdout.destroy(error);

TweaksSindre Sorhusbefore adoption698a1b8

line 80why, one step back
80 await (error === undefined ? assert.doesNotReject(subprocess) : assertRejects(subprocess));

Split type files (#978)ehmickybefore adoption0bd0f0d

lines 81–82why, one step back
81};
82

TweaksSindre Sorhusbefore adoption698a1b8

lines 83–84why, one step back
83test('Running generators are canceled on subprocess abort', () => testGeneratorCancel(undefined));
84test('Running generators are canceled on subprocess error', () => testGeneratorCancel(new Error('test')));

Split type files (#978)ehmickybefore adoption0bd0f0d

line 85why, one step back
85

TweaksSindre Sorhusbefore adoption698a1b8

line 86why, one step back
86const testGeneratorDestroy = async transform => {

Split type files (#978)ehmickybefore adoption0bd0f0d

lines 87–90why, one step back
87 const subprocess = execa('forever.js', {stdout: transform});
88 const cause = new Error('test');89 subprocess.stdout.destroy(cause);90 subprocess.kill();

TweaksSindre Sorhusbefore adoption698a1b8

line 91why, one step back
91 assertLike(await assertRejects(subprocess), {cause});

Split type files (#978)ehmickybefore adoption0bd0f0d

lines 92–93why, one step back
92};
93

TweaksSindre Sorhusbefore adoption698a1b8

lines 94–95why, one step back
94test('Generators are destroyed on subprocess error, sync', () => testGeneratorDestroy(noopGenerator(false)));
95test('Generators are destroyed on subprocess error, async', () => testGeneratorDestroy(infiniteGenerator()));

Split type files (#978)ehmickybefore adoption0bd0f0d

line 96why, one step back
96

TweaksSindre Sorhusbefore adoption698a1b8

lines 97–99why, one step back
97test('Generators are destroyed on early subprocess exit', async () => {
98 const error = await assertRejects(getEarlyErrorSubprocess({stdout: infiniteGenerator()}));99 assertLike(error, expectedEarlyError);

Split type files (#978)ehmickybefore adoption0bd0f0d

line 100why, one step back
100});

Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc

lines 101–102why, one step back
101
102// A transform failing on one file descriptor must not prevent the other ones from being redirected to their target

TweaksSindre Sorhusbefore adoption698a1b8

line 103why, one step back
103const testTransformErrorOtherFd = async execaMethod => {

Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc

lines 104–111why, one step back
104 const filePath = tempfile();
105 const cause = new Error(foobarString);106 const runSubprocess = () => execaMethod('noop-both.js', [foobarString], {107 stdout: throwingGenerator(cause)(),108 stderr: {file: filePath},109 });110111 await (execaMethod === execa

TweaksSindre Sorhusbefore adoption698a1b8

lines 112–114why, one step back
112 ? assertRejects(runSubprocess())
113 : assertThrows(runSubprocess));114 assert.equal(await readFile(filePath, 'utf8'), `${foobarString}\n`);

Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc

lines 115–117why, one step back
115 await rm(filePath);
116};117

TweaksSindre Sorhusbefore adoption698a1b8

lines 118–119why, one step back
118test('Transform errors on stdout do not skip the stderr file', () => testTransformErrorOtherFd(execa));
119test('Transform errors on stdout do not skip the stderr file, sync', () => testTransformErrorOtherFd(execaSync));