node-stream-custom.js
This file
Path
test/stdio/node-stream-custom.js
191 lines · 8.2 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–7why, one step back2import {createReadStream, createWriteStream} from 'node:fs';3import {readFile, writeFile, rm} from 'node:fs/promises';4import {Writable, PassThrough} from 'node:stream';5import {text} from 'node:stream/consumers';6import {setImmediate} from 'node:timers/promises';7import {callbackify} from 'node:util';TweaksSindre Sorhusbefore adoption698a1b8
line 8why, one step back8import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 9why, one step back9import tempfile from 'tempfile';TweaksSindre Sorhusbefore adoption698a1b8
line 10why, one step back10import {assertThrows, assertRejects, assertLike} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 11–15why, one step back11import {execa, execaSync} from '../../index.js';12import {setFixtureDirectory} from '../helpers/fixtures-directory.js';13import {getStdio} from '../helpers/stdio.js';14import {foobarString} from '../helpers/input.js';15import {noopReadable, noopWritable} from '../helpers/stream.js';Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 16why, one step back16import {getEarlyErrorSubprocess, expectedEarlyError} from '../helpers/early-error.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 17–19why, one step back1718setFixtureDirectory();19TweaksSindre Sorhusbefore adoption698a1b8
line 20why, one step back20const testLazyFileReadable = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 21–25why, one step back21 const filePath = tempfile();22 await writeFile(filePath, 'foobar');23 const stream = createReadStream(filePath);2425 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, 'pipe']));TweaksSindre Sorhusbefore adoption698a1b8
line 26why, one step back26 assert.equal(stdout, 'foobar');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 27–30why, one step back2728 await rm(filePath);29};30TweaksSindre Sorhusbefore adoption698a1b8
lines 31–32why, one step back31test('stdin can be [Readable, "pipe"] without a file descriptor', () => testLazyFileReadable(0));32test('stdio[*] can be [Readable, "pipe"] without a file descriptor', () => testLazyFileReadable(3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 33why, one step back33TweaksSindre Sorhusbefore adoption698a1b8
lines 34–35why, one step back34const testLazyFileReadableSync = fdNumber => {35 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 36–39why, one step back36 execaSync('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [noopReadable(), 'pipe']));37 }, {message: /cannot both be an array and include a stream/});38};39TweaksSindre Sorhusbefore adoption698a1b8
lines 40–41why, one step back40test('stdin cannot be [Readable, "pipe"] without a file descriptor, sync', () => testLazyFileReadableSync(0));41test('stdio[*] cannot be [Readable, "pipe"] without a file descriptor, sync', () => testLazyFileReadableSync(3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 42why, one step back42TweaksSindre Sorhusbefore adoption698a1b8
line 43why, one step back43const testLazyFileWritable = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 44–47why, one step back44 const filePath = tempfile();45 const stream = createWriteStream(filePath);4647 await execa('noop-fd.js', [`${fdNumber}`, 'foobar'], getStdio(fdNumber, [stream, 'pipe']));TweaksSindre Sorhusbefore adoption698a1b8
line 48why, one step back48 assert.equal(await readFile(filePath, 'utf8'), 'foobar');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 49–52why, one step back4950 await rm(filePath);51};52TweaksSindre Sorhusbefore adoption698a1b8
lines 53–55why, one step back53test('stdout can be [Writable, "pipe"] without a file descriptor', () => testLazyFileWritable(1));54test('stderr can be [Writable, "pipe"] without a file descriptor', () => testLazyFileWritable(2));55test('stdio[*] can be [Writable, "pipe"] without a file descriptor', () => testLazyFileWritable(3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 56why, one step back56TweaksSindre Sorhusbefore adoption698a1b8
lines 57–58why, one step back57const testLazyFileWritableSync = fdNumber => {58 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 59–62why, one step back59 execaSync('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, [noopWritable(), 'pipe']));60 }, {message: /cannot both be an array and include a stream/});61};62TweaksSindre Sorhusbefore adoption698a1b8
lines 63–65why, one step back63test('stdout cannot be [Writable, "pipe"] without a file descriptor, sync', () => testLazyFileWritableSync(1));64test('stderr cannot be [Writable, "pipe"] without a file descriptor, sync', () => testLazyFileWritableSync(2));65test('stdio[*] cannot be [Writable, "pipe"] without a file descriptor, sync', () => testLazyFileWritableSync(3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 66why, one step back66Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3
lines 67–71why, one step back67/*68An output stream is written to, then handed back to the user.69We must only wait for our writes to be flushed, not for the user to consume its readable side,70otherwise the subprocess promise hangs until they do.71*/TweaksSindre Sorhusbefore adoption698a1b8
line 72why, one step back72const testUnconsumedOutputStream = async fdNumber => {Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3
lines 73–74why, one step back73 const stream = new PassThrough();74 const {exitCode} = await execa('empty.js', {...getStdio(fdNumber, [stream, 'pipe']), timeout: 1e3});TweaksSindre Sorhusbefore adoption698a1b8
line 75why, one step back75 assert.equal(exitCode, 0);Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3
lines 76–77why, one step back76};77TweaksSindre Sorhusbefore adoption698a1b8
lines 78–80why, one step back78test('stdout does not wait for the user to consume the output stream', () => testUnconsumedOutputStream(1));79test('stderr does not wait for the user to consume the output stream', () => testUnconsumedOutputStream(2));80test('stdio[*] does not wait for the user to consume the output stream', () => testUnconsumedOutputStream(3));Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3
line 81why, one step back81TweaksSindre Sorhusbefore adoption698a1b8
line 82why, one step back82test('stdout does not wait for the user to consume the output stream on subprocess errors', async () => {Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3
line 83why, one step back83 const stream = new PassThrough();TweaksSindre Sorhusbefore adoption698a1b8
lines 84–86why, one step back84 const error = await assertRejects(execa('fail.js', {...getStdio(1, [stream, 'pipe']), timeout: 1e3}));85 assert.equal(error.timedOut, false);86 assert.equal(error.exitCode, 2);Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3
lines 87–89why, one step back87});8889// The stream is handed back to the user with its data still readableTweaksSindre Sorhusbefore adoption698a1b8
line 90why, one step back90test('output stream data is readable once the subprocess is done', async () => {Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3
lines 91–92why, one step back91 const stream = new PassThrough();92 await execa('noop.js', [foobarString], {...getStdio(1, [stream, 'pipe'])});TweaksSindre Sorhusbefore adoption698a1b8
line 93why, one step back93 assert.equal(await text(stream), `${foobarString}\n`);Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3
lines 94–95why, one step back94});95TweaksSindre Sorhusbefore adoption698a1b8
line 96why, one step back96test('Waits for custom streams destroy on subprocess errors', async () => {Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
line 97why, one step back97 let isWaitedForDestroy = false;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 98–100why, one step back98 const stream = new Writable({99 destroy: callbackify(async error => {100 await setImmediate();Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
line 101why, one step back101 isWaitedForDestroy = true;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 102–104why, one step back102 return error;103 }),104 });TweaksSindre Sorhusbefore adoption698a1b8
lines 105–107why, one step back105 const {timedOut} = await assertRejects(execa('forever.js', {stdout: [stream, 'pipe'], timeout: 1}));106 assert.equal(timedOut, true);107 assert.equal(isWaitedForDestroy, true);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 108–109why, one step back108});109TweaksSindre Sorhusbefore adoption698a1b8
line 110why, one step back110test('Handles custom streams destroy errors on subprocess success', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 111–116why, one step back111 const cause = new Error('test');112 const stream = new Writable({113 destroy(destroyError, done) {114 done(destroyError ?? cause);115 },116 });TweaksSindre Sorhusbefore adoption698a1b8
lines 117–118why, one step back117 const error = await assertRejects(execa('empty.js', {stdout: [stream, 'pipe']}));118 assert.equal(error.cause, cause);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 119–120why, one step back119});120TweaksSindre Sorhusbefore adoption698a1b8
lines 121–124why, one step back121const testStreamEarlyExit = async (stream, streamName) => {122 const error = await assertRejects(getEarlyErrorSubprocess({[streamName]: [stream, 'pipe']}));123 assertLike(error, expectedEarlyError);124 assert.equal(stream.destroyed, true);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 125–126why, one step back125};126TweaksSindre Sorhusbefore adoption698a1b8
lines 127–128why, one step back127test('Input streams are canceled on early subprocess exit', () => testStreamEarlyExit(noopReadable(), 'stdin'));128test('Output streams are canceled on early subprocess exit', () => testStreamEarlyExit(noopWritable(), 'stdout'));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 129why, one step back129TweaksSindre Sorhusbefore adoption698a1b8
line 130why, one step back130const testInputDuplexStream = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 131–133why, one step back131 const stream = new PassThrough();132 stream.end(foobarString);133 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, new Uint8Array()]));TweaksSindre Sorhusbefore adoption698a1b8
line 134why, one step back134 assert.equal(stdout, foobarString);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 135–136why, one step back135};136TweaksSindre Sorhusbefore adoption698a1b8
lines 137–138why, one step back137test('Can pass Duplex streams to stdin', () => testInputDuplexStream(0));138test('Can pass Duplex streams to input stdio[*]', () => testInputDuplexStream(3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 139why, one step back139TweaksSindre Sorhusbefore adoption698a1b8
line 140why, one step back140const testOutputDuplexStream = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 141–145why, one step back141 const stream = new PassThrough();142 const [output] = await Promise.all([143 text(stream),144 execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, 'pipe'])),145 ]);TweaksSindre Sorhusbefore adoption698a1b8
line 146why, one step back146 assert.equal(output, foobarString);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 147–148why, one step back147};148TweaksSindre Sorhusbefore adoption698a1b8
lines 149–151why, one step back149test('Can pass Duplex streams to stdout', () => testOutputDuplexStream(1));150test('Can pass Duplex streams to stderr', () => testOutputDuplexStream(2));151test('Can pass Duplex streams to output stdio[*]', () => testOutputDuplexStream(3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 152why, one step back152TweaksSindre Sorhusbefore adoption698a1b8
line 153why, one step back153const testInputStreamAbort = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 154–158why, one step back154 const stream = new PassThrough();155 stream.destroy();156157 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, new Uint8Array()]));158 await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 159why, one step back159 assert.ok(subprocess.stdio[fdNumber].writableEnded);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 160–161why, one step back160};161TweaksSindre Sorhusbefore adoption698a1b8
lines 162–163why, one step back162test('subprocess.stdin is ended when an input stream aborts', () => testInputStreamAbort(0));163test('subprocess.stdio[*] is ended when an input stream aborts', () => testInputStreamAbort(3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 164why, one step back164TweaksSindre Sorhusbefore adoption698a1b8
line 165why, one step back165const testInputStreamError = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 166–170why, one step back166 const stream = new PassThrough();167 const cause = new Error(foobarString);168 stream.destroy(cause);169170 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, new Uint8Array()]));TweaksSindre Sorhusbefore adoption698a1b8
lines 171–172why, one step back171 assertLike(await assertRejects(subprocess), {cause});172 assert.ok(subprocess.stdio[fdNumber].writableEnded);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 173–174why, one step back173};174TweaksSindre Sorhusbefore adoption698a1b8
lines 175–176why, one step back175test('subprocess.stdin is ended when an input stream errors', () => testInputStreamError(0));176test('subprocess.stdio[*] is ended when an input stream errors', () => testInputStreamError(3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 177why, one step back177TweaksSindre Sorhusbefore adoption698a1b8
line 178why, one step back178const testOutputStreamError = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 179–183why, one step back179 const stream = new PassThrough();180 const cause = new Error(foobarString);181 stream.destroy(cause);182183 const subprocess = execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, 'pipe']));TweaksSindre Sorhusbefore adoption698a1b8
lines 184–186why, one step back184 assertLike(await assertRejects(subprocess), {cause});185 assert.ok(subprocess.stdio[fdNumber].readableAborted);186 assert.equal(subprocess.stdio[fdNumber].errored, null);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 187–188why, one step back187};188TweaksSindre Sorhusbefore adoption698a1b8
lines 189–191why, one step back189test('subprocess.stdout is aborted when an output stream errors', () => testOutputStreamError(1));190test('subprocess.stderr is aborted when an output stream errors', () => testOutputStreamError(2));191test('subprocess.stdio[*] is aborted when an output stream errors', () => testOutputStreamError(3));