output-async.js
This file
Path
test/io/output-async.js
80 lines · 4.1 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
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
lines 2–4why, one step back2import {once, defaultMaxListeners} from 'node:events';3import process from 'node:process';4import {setImmediate} from 'node:timers/promises';TweaksSindre Sorhusbefore adoption698a1b8
line 5why, one step back5import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 6–10why, one step back6import {execa} from '../../index.js';7import {STANDARD_STREAMS} from '../helpers/stdio.js';8import {foobarString} from '../helpers/input.js';9import {setFixtureDirectory} from '../helpers/fixtures-directory.js';10import {assertMaxListeners} from '../helpers/listeners.js';Refactor parallel tests (#1084)ehmickybefore adoption5cf6312
line 11why, one step back11import {PARALLEL_COUNT} from '../helpers/parallel.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 12–25why, one step back1213setFixtureDirectory();1415const getStandardStreamListeners = stream => Object.fromEntries(stream.eventNames().map(eventName => [eventName, stream.listeners(eventName)]));16const getStandardStreamsListeners = () => STANDARD_STREAMS.map(stream => getStandardStreamListeners(stream));1718const getComplexStdio = isMultiple => ({19 stdin: ['pipe', 'inherit', ...(isMultiple ? [0, process.stdin] : [])],20 stdout: ['pipe', 'inherit', ...(isMultiple ? [1, process.stdout] : [])],21 stderr: ['pipe', 'inherit', ...(isMultiple ? [2, process.stderr] : [])],22});2324const onStdinRemoveListener = () => once(process.stdin, 'removeListener');25TweaksSindre Sorhusbefore adoption698a1b8
line 26why, one step back26const testListenersCleanup = async isMultiple => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 27–28why, one step back27 const streamsPreviousListeners = getStandardStreamsListeners();28 const subprocess = execa('empty.js', getComplexStdio(isMultiple));TweaksSindre Sorhusbefore adoption698a1b8
line 29why, one step back29 assert.notDeepEqual(getStandardStreamsListeners(), streamsPreviousListeners);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 30–36why, one step back30 await Promise.all([subprocess, onStdinRemoveListener()]);31 if (isMultiple) {32 await onStdinRemoveListener();33 }3435 for (const [fdNumber, streamNewListeners] of Object.entries(getStandardStreamsListeners())) {36 const defaultListeners = Object.fromEntries(Reflect.ownKeys(streamNewListeners).map(eventName => [eventName, []]));TweaksSindre Sorhusbefore adoption698a1b8
line 37why, one step back37 assert.deepEqual(streamNewListeners, {...defaultListeners, ...streamsPreviousListeners[fdNumber]});100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 38–40why, one step back38 }39};40TweaksSindre Sorhusbefore adoption698a1b8
lines 41–42why, one step back41test('process.std* listeners are cleaned up on success with a single input', () => testListenersCleanup(false));42test('process.std* listeners are cleaned up on success with multiple inputs', () => testListenersCleanup(true));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 43why, one step back43TweaksSindre Sorhusbefore adoption698a1b8
line 44why, one step back44test('Can spawn many subprocesses in parallel', async () => {Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 45why, one step back45 const results = await Promise.all(Array.from({length: PARALLEL_COUNT}, () => execa('noop.js', [foobarString])));TweaksSindre Sorhusbefore adoption698a1b8
line 46why, one step back46 assert.ok(results.every(({stdout}) => stdout === foobarString));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 47–48why, one step back47});48TweaksSindre Sorhusbefore adoption698a1b8
lines 49–50why, one step back49const testMaxListeners = async (isMultiple, maxListenersCount) => {50 const checkMaxListeners = assertMaxListeners();100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 51–56why, one step back5152 for (const standardStream of STANDARD_STREAMS) {53 standardStream.setMaxListeners(maxListenersCount);54 }5556 try {Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 57why, one step back57 const results = await Promise.all(Array.from({length: PARALLEL_COUNT}, () => execa('empty.js', getComplexStdio(isMultiple))));TweaksSindre Sorhusbefore adoption698a1b8
line 58why, one step back58 assert.ok(results.every(({exitCode}) => exitCode === 0));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 59–64why, one step back59 } finally {60 await setImmediate();61 await setImmediate();62 checkMaxListeners();6364 for (const standardStream of STANDARD_STREAMS) {TweaksSindre Sorhusbefore adoption698a1b8
line 65why, one step back65 assert.equal(standardStream.getMaxListeners(), maxListenersCount);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 66–70why, one step back66 standardStream.setMaxListeners(defaultMaxListeners);67 }68 }69};70TweaksSindre Sorhusbefore adoption698a1b8
lines 71–80why, one step back71test('No warning with maxListeners 1 and ["pipe", "inherit"]', () => testMaxListeners(false, 1));72test('No warning with maxListeners default and ["pipe", "inherit"]', () => testMaxListeners(false, defaultMaxListeners));73test('No warning with maxListeners 100 and ["pipe", "inherit"]', () => testMaxListeners(false, 100));74test('No warning with maxListeners Infinity and ["pipe", "inherit"]', () => testMaxListeners(false, Infinity));75test('No warning with maxListeners 0 and ["pipe", "inherit"]', () => testMaxListeners(false, 0));76test('No warning with maxListeners 1 and ["pipe", "inherit"], multiple inputs', () => testMaxListeners(true, 1));77test('No warning with maxListeners default and ["pipe", "inherit"], multiple inputs', () => testMaxListeners(true, defaultMaxListeners));78test('No warning with maxListeners 100 and ["pipe", "inherit"], multiple inputs', () => testMaxListeners(true, 100));79test('No warning with maxListeners Infinity and ["pipe", "inherit"], multiple inputs', () => testMaxListeners(true, Infinity));80test('No warning with maxListeners 0 and ["pipe", "inherit"], multiple inputs', () => testMaxListeners(true, 0));