Skip to content
Myr

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 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';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 2–7why, one step back
2import {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 back
8import test from 'node:test';

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 11–15why, one step back
11import {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 back
16import {getEarlyErrorSubprocess, expectedEarlyError} from '../helpers/early-error.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 17–19why, one step back
17
18setFixtureDirectory();19

TweaksSindre Sorhusbefore adoption698a1b8

line 20why, one step back
20const testLazyFileReadable = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 21–25why, one step back
21 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 back
26 assert.equal(stdout, 'foobar');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 27–30why, one step back
27
28 await rm(filePath);29};30

TweaksSindre Sorhusbefore adoption698a1b8

lines 31–32why, one step back
31test('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 back
33

TweaksSindre Sorhusbefore adoption698a1b8

lines 34–35why, one step back
34const testLazyFileReadableSync = fdNumber => {
35 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 36–39why, one step back
36 execaSync('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [noopReadable(), 'pipe']));
37 }, {message: /cannot both be an array and include a stream/});38};39

TweaksSindre Sorhusbefore adoption698a1b8

lines 40–41why, one step back
40test('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 back
42

TweaksSindre Sorhusbefore adoption698a1b8

line 43why, one step back
43const testLazyFileWritable = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 44–47why, one step back
44 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 back
48 assert.equal(await readFile(filePath, 'utf8'), 'foobar');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 49–52why, one step back
49
50 await rm(filePath);51};52

TweaksSindre Sorhusbefore adoption698a1b8

lines 53–55why, one step back
53test('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 back
56

TweaksSindre Sorhusbefore adoption698a1b8

lines 57–58why, one step back
57const testLazyFileWritableSync = fdNumber => {
58 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 59–62why, one step back
59 execaSync('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, [noopWritable(), 'pipe']));
60 }, {message: /cannot both be an array and include a stream/});61};62

TweaksSindre Sorhusbefore adoption698a1b8

lines 63–65why, one step back
63test('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 back
66

Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3

lines 67–71why, one step back
67/*
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 back
72const testUnconsumedOutputStream = async fdNumber => {

Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3

lines 73–74why, one step back
73 const stream = new PassThrough();
74 const {exitCode} = await execa('empty.js', {...getStdio(fdNumber, [stream, 'pipe']), timeout: 1e3});

TweaksSindre Sorhusbefore adoption698a1b8

line 75why, one step back
75 assert.equal(exitCode, 0);

Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3

lines 76–77why, one step back
76};
77

TweaksSindre Sorhusbefore adoption698a1b8

lines 78–80why, one step back
78test('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 back
81

TweaksSindre Sorhusbefore adoption698a1b8

line 82why, one step back
82test('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 back
83 const stream = new PassThrough();

TweaksSindre Sorhusbefore adoption698a1b8

lines 84–86why, one step back
84 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 back
87});
8889// The stream is handed back to the user with its data still readable

TweaksSindre Sorhusbefore adoption698a1b8

line 90why, one step back
90test('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 back
91 const stream = new PassThrough();
92 await execa('noop.js', [foobarString], {...getStdio(1, [stream, 'pipe'])});

TweaksSindre Sorhusbefore adoption698a1b8

line 93why, one step back
93 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 back
94});
95

TweaksSindre Sorhusbefore adoption698a1b8

line 96why, one step back
96test('Waits for custom streams destroy on subprocess errors', async () => {

Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d

line 97why, one step back
97 let isWaitedForDestroy = false;

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 98–100why, one step back
98 const stream = new Writable({
99 destroy: callbackify(async error => {100 await setImmediate();

Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d

line 101why, one step back
101 isWaitedForDestroy = true;

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 102–104why, one step back
102 return error;
103 }),104 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 105–107why, one step back
105 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 back
108});
109

TweaksSindre Sorhusbefore adoption698a1b8

line 110why, one step back
110test('Handles custom streams destroy errors on subprocess success', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 111–116why, one step back
111 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 back
117 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 back
119});
120

TweaksSindre Sorhusbefore adoption698a1b8

lines 121–124why, one step back
121const 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 back
125};
126

TweaksSindre Sorhusbefore adoption698a1b8

lines 127–128why, one step back
127test('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 back
129

TweaksSindre Sorhusbefore adoption698a1b8

line 130why, one step back
130const testInputDuplexStream = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 131–133why, one step back
131 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 back
134 assert.equal(stdout, foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 135–136why, one step back
135};
136

TweaksSindre Sorhusbefore adoption698a1b8

lines 137–138why, one step back
137test('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 back
139

TweaksSindre Sorhusbefore adoption698a1b8

line 140why, one step back
140const testOutputDuplexStream = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 141–145why, one step back
141 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 back
146 assert.equal(output, foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 147–148why, one step back
147};
148

TweaksSindre Sorhusbefore adoption698a1b8

lines 149–151why, one step back
149test('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 back
152

TweaksSindre Sorhusbefore adoption698a1b8

line 153why, one step back
153const testInputStreamAbort = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 154–158why, one step back
154 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 back
159 assert.ok(subprocess.stdio[fdNumber].writableEnded);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 160–161why, one step back
160};
161

TweaksSindre Sorhusbefore adoption698a1b8

lines 162–163why, one step back
162test('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 back
164

TweaksSindre Sorhusbefore adoption698a1b8

line 165why, one step back
165const testInputStreamError = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 166–170why, one step back
166 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 back
171 assertLike(await assertRejects(subprocess), {cause});
172 assert.ok(subprocess.stdio[fdNumber].writableEnded);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 173–174why, one step back
173};
174

TweaksSindre Sorhusbefore adoption698a1b8

lines 175–176why, one step back
175test('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 back
177

TweaksSindre Sorhusbefore adoption698a1b8

line 178why, one step back
178const testOutputStreamError = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 179–183why, one step back
179 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 back
184 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 back
187};
188

TweaksSindre Sorhusbefore adoption698a1b8

lines 189–191why, one step back
189test('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));