Skip to content
Myr

shared.js

This file

Path

test/convert/shared.js

167 lines · 7.5 KiB · JavaScript

Last changed by

Fix tests failing on WindowsSindre Sorhusbefore adoption5a8dd97

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

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

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

lines 2–3why, one step back
2import {once} from 'node:events';
3import {setImmediate} from 'node:timers/promises';

TweaksSindre Sorhusbefore adoption698a1b8

lines 4–5why, one step back
4import test from 'node:test';
5import {assertRejects, assertThrows} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 6–18why, one step back
6import {execa} from '../../index.js';
7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8import {9 finishedStream,10 assertWritableAborted,11 assertStreamError,12 assertSubprocessError,13 getReadWriteSubprocess,14} from '../helpers/convert.js';15import {foobarString} from '../helpers/input.js';1617setFixtureDirectory();18

TweaksSindre Sorhusbefore adoption698a1b8

line 19why, one step back
19const testSubprocessFail = async methodName => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 20–25why, one step back
20 const subprocess = getReadWriteSubprocess();
21 const stream = subprocess[methodName]();2223 const cause = new Error(foobarString);24 subprocess.kill(cause);25

TweaksSindre Sorhusbefore adoption698a1b8

lines 26–29why, one step back
26 const error = await assertStreamError(stream, {cause});
27 assertWritableAborted(subprocess.stdin);28 assert.equal(subprocess.stdout.readableEnded, true);29 assert.equal(subprocess.stderr.readableEnded, true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 30why, one step back
30

TweaksSindre Sorhusbefore adoption698a1b8

line 31why, one step back
31 await assertSubprocessError(subprocess, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 32–33why, one step back
32};
33

TweaksSindre Sorhusbefore adoption698a1b8

lines 34–36why, one step back
34test('subprocess fail -> .readable() error', () => testSubprocessFail('readable'));
35test('subprocess fail -> .writable() error', () => testSubprocessFail('writable'));36test('subprocess fail -> .duplex() error', () => testSubprocessFail('duplex'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 37why, one step back
37

TweaksSindre Sorhusbefore adoption698a1b8

line 38why, one step back
38const testErrorEvent = async methodName => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 39–40why, one step back
39 const subprocess = execa('empty.js');
40 const stream = subprocess[methodName]();

TweaksSindre Sorhusbefore adoption698a1b8

line 41why, one step back
41 assert.equal(stream.listenerCount('error'), 0);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 42why, one step back
42 stream.destroy();

TweaksSindre Sorhusbefore adoption698a1b8

line 43why, one step back
43 await assertRejects(finishedStream(stream));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 44–45why, one step back
44};
45

TweaksSindre Sorhusbefore adoption698a1b8

lines 46–48why, one step back
46test('.readable() requires listening to "error" event', () => testErrorEvent('readable'));
47test('.writable() requires listening to "error" event', () => testErrorEvent('writable'));48test('.duplex() requires listening to "error" event', () => testErrorEvent('duplex'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 49why, one step back
49

TweaksSindre Sorhusbefore adoption698a1b8

line 50why, one step back
50const testSubprocessError = async methodName => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 51–54why, one step back
51 const subprocess = getReadWriteSubprocess();
52 const stream = subprocess[methodName]();53 const cause = new Error(foobarString);54 subprocess.kill(cause);

TweaksSindre Sorhusbefore adoption698a1b8

line 55why, one step back
55 await assertStreamError(stream, {cause});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 56–57why, one step back
56};
57

TweaksSindre Sorhusbefore adoption698a1b8

lines 58–60why, one step back
58test('Do not need to await subprocess with .readable()', () => testSubprocessError('readable'));
59test('Do not need to await subprocess with .writable()', () => testSubprocessError('writable'));60test('Do not need to await subprocess with .duplex()', () => testSubprocessError('duplex'));

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

lines 61–113why, one step back
61
62// Once the subprocess has ended, the error passed to `stream.destroy(error)` must still be emitted, including when another stream uses the same file descriptor63const testDestroyAfterExit = async (methodName, fixtureName, otherMethodName) => {64 const subprocess = execa(fixtureName, {reject: false});65 const stream = subprocess[methodName]();66 if (otherMethodName !== undefined) {67 subprocess[otherMethodName]().on('error', () => {});68 }6970 await subprocess;71 const cause = new Error(foobarString);72 let errorEvent;73 stream.on('error', error => {74 errorEvent = error;75 });76 stream.destroy(cause);77 await assertRejects(finishedStream(stream));78 assert.equal(errorEvent, cause);79};8081test('.readable() destroy error after the subprocess ended', () => testDestroyAfterExit('readable', 'empty.js'));82test('.writable() destroy error after the subprocess ended', () => testDestroyAfterExit('writable', 'empty.js'));83test('.duplex() destroy error after the subprocess ended', () => testDestroyAfterExit('duplex', 'empty.js'));84test('.readable() destroy error after the subprocess ended, with .duplex()', () => testDestroyAfterExit('readable', 'empty.js', 'duplex'));85test('.writable() destroy error after the subprocess ended, with .duplex()', () => testDestroyAfterExit('writable', 'empty.js', 'duplex'));86test('.duplex() destroy error after the subprocess ended, with .writable()', () => testDestroyAfterExit('duplex', 'empty.js', 'writable'));87test('.duplex() destroy error after the subprocess ended, with .duplex()', () => testDestroyAfterExit('duplex', 'empty.js', 'duplex'));88test('.readable() destroy error after the subprocess failed, with .duplex()', () => testDestroyAfterExit('readable', 'fail.js', 'duplex'));89test('.writable() destroy error after the subprocess failed, with .duplex()', () => testDestroyAfterExit('writable', 'fail.js', 'duplex'));90test('.duplex() destroy error after the subprocess failed, with .writable()', () => testDestroyAfterExit('duplex', 'fail.js', 'writable'));9192// The pending write waits for the `drain` event, so it must fail instead of hanging93test('.writable() write fails when the subprocess fails to spawn', async () => {94 const subprocess = execa('nonexistent-command-for-execa');95 const stream = subprocess.writable();96 const chunk = '.'.repeat(1e5);97 const writeAll = async () => {98 for (let index = 0; index < 1e2; index += 1) {99 if (!stream.write(chunk)) {100 // eslint-disable-next-line no-await-in-loop101 await once(stream, 'drain');102 }103 }104105 stream.end();106 await finishedStream(stream);107 };108109 const error = await assertRejects(writeAll());110 assert.equal(error, await assertRejects(subprocess));111});112113// The subprocess error is more useful than the `EPIPE` error it causes on `stdin`

Fix tests failing on WindowsSindre Sorhusbefore adoption5a8dd97

line 114why, one step back
114// A nonexistent command cannot be used, since it is run by `cmd.exe` on Windows.

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

line 115why, one step back
115const testSpawnFailureWrite = async methodName => {

Fix tests failing on WindowsSindre Sorhusbefore adoption5a8dd97

line 116why, one step back
116 const subprocess = execa('empty.js', {cwd: 'does_not_exist'});

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

lines 117–153why, one step back
117 const stream = subprocess[methodName]();
118 stream.end(foobarString);119 const error = await assertRejects(finishedStream(stream));120 assert.equal(error, await assertRejects(subprocess));121 assert.equal(error.code, 'ENOENT');122};123124test('.writable() fails with the subprocess error when the subprocess fails to spawn', () => testSpawnFailureWrite('writable'));125test('.duplex() fails with the subprocess error when the subprocess fails to spawn', () => testSpawnFailureWrite('duplex'));126127// The pending write waits for the `drain` event of `stdin`, which has been destroyed, so the stream must be destroyed too, even though it is ending128const testWriteAfterExit = async (methodName, isSameTick) => {129 const subprocess = execa('empty.js');130 await subprocess;131 const stream = subprocess[methodName]();132 if (!isSameTick) {133 await setImmediate();134 }135136 stream.end(foobarString);137 const error = await assertRejects(finishedStream(stream));138 assert.equal(error.code, 'ERR_STREAM_PREMATURE_CLOSE');139};140141test('.writable() fails when ending it after the subprocess ended', () => testWriteAfterExit('writable', true));142test('.duplex() fails when ending it after the subprocess ended', () => testWriteAfterExit('duplex', true));143test('.writable() fails when ending it later after the subprocess ended', () => testWriteAfterExit('writable', false));144test('.duplex() fails when ending it later after the subprocess ended', () => testWriteAfterExit('duplex', false));145146test('.writable() fails with the subprocess error when the subprocess fails without reading stdin', async () => {147 const subprocess = execa('fail.js');148 const stream = subprocess.writable();149 stream.end('.'.repeat(1e6));150 const error = await assertRejects(finishedStream(stream));151 assert.equal(error, await assertRejects(subprocess));152 assert.equal(error.exitCode, 2);153});

TweaksSindre Sorhusbefore adoption698a1b8

lines 154–167why, one step back
154
155const testNullOptions = async methodName => {156 const subprocess = getReadWriteSubprocess();157 subprocess.stdin.end();158 assertThrows(() => {159 subprocess[methodName](null);160 }, {message: 'The options must be an object, not `null`.'});161 await subprocess;162};163164test('.readable() cannot use null options', () => testNullOptions('readable'));165test('.writable() cannot use null options', () => testNullOptions('writable'));166test('.duplex() cannot use null options', () => testNullOptions('duplex'));167test('.iterable() cannot use null options', () => testNullOptions('iterable'));