Skip to content
Myr

duplex.js

This file

Path

test/convert/duplex.js

215 lines · 7.9 KiB · JavaScript

Last changed by

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

History of this file

Take it

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–4why, one step back
1import assert from 'node:assert/strict';
2import {pipeline} from 'node:stream/promises';3import {text} from 'node:stream/consumers';4import test from 'node:test';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 5–10why, one step back
5import {
6 compose,7 Readable,8 Writable,9 PassThrough,10} from 'node:stream';

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 12–27why, one step back
12import {execa} from '../../index.js';
13import {setFixtureDirectory} from '../helpers/fixtures-directory.js';14import {15 finishedStream,16 assertReadableAborted,17 assertWritableAborted,18 assertProcessNormalExit,19 assertStreamOutput,20 assertStreamError,21 assertStreamReadError,22 assertSubprocessOutput,23 assertSubprocessError,24 assertPromiseError,25 getReadWriteSubprocess,26} from '../helpers/convert.js';27import {foobarString} from '../helpers/input.js';

Runs CI tests on Node 23 (#1169)ehmickybefore adoptione330bd8

line 28why, one step back
28import {majorNodeVersion} from '../helpers/node-version.js';

Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4

lines 29–34why, one step back
29import {
30 getStdio,31 prematureClose,32 fullStdio,33 fullReadableStdio,34} from '../helpers/stdio.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 35–38why, one step back
35import {defaultHighWaterMark} from '../helpers/stream.js';
3637setFixtureDirectory();38

TweaksSindre Sorhusbefore adoption698a1b8

line 39why, one step back
39test('.duplex() success', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 40–42why, one step back
40 const subprocess = getReadWriteSubprocess();
41 const stream = subprocess.duplex();42

TweaksSindre Sorhusbefore adoption698a1b8

lines 43–46why, one step back
43 assert.ok(stream instanceof Writable);
44 assert.equal(stream.writable, true);45 assert.ok(stream instanceof Readable);46 assert.equal(stream.readable, true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 47–49why, one step back
47
48 stream.end(foobarString);49

TweaksSindre Sorhusbefore adoption698a1b8

lines 50–51why, one step back
50 await assertStreamOutput(stream);
51 await assertSubprocessOutput(subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 52–53why, one step back
52});
53

TweaksSindre Sorhusbefore adoption698a1b8

line 54why, one step back
54const testReadableDuplexDefault = async (fdNumber, from, options, hasResult) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 55–58why, one step back
55 const subprocess = execa('noop-stdin-fd.js', [`${fdNumber}`], options);
56 const stream = subprocess.duplex({from});57 stream.end(foobarString);58

TweaksSindre Sorhusbefore adoption698a1b8

lines 59–60why, one step back
59 await assertStreamOutput(stream, hasResult ? foobarString : '');
60 await assertSubprocessOutput(subprocess, foobarString, fdNumber);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 61–62why, one step back
61};
62

TweaksSindre Sorhusbefore adoption698a1b8

lines 63–72why, one step back
63test('.duplex() can use stdout', () => testReadableDuplexDefault(1, 'stdout', {}, true));
64test('.duplex() can use stderr', () => testReadableDuplexDefault(2, 'stderr', {}, true));65test('.duplex() can use output stdio[*]', () => testReadableDuplexDefault(3, 'fd3', fullStdio, true));66test('.duplex() uses stdout by default', () => testReadableDuplexDefault(1, undefined, {}, true));67test('.duplex() does not use stderr by default', () => testReadableDuplexDefault(2, undefined, {}, false));68test('.duplex() does not use stdio[*] by default', () => testReadableDuplexDefault(3, undefined, fullStdio, false));69test('.duplex() uses stdout even if stderr is "ignore"', () => testReadableDuplexDefault(1, 'stdout', {stderr: 'ignore'}, true));70test('.duplex() uses stderr even if stdout is "ignore"', () => testReadableDuplexDefault(2, 'stderr', {stdout: 'ignore'}, true));71test('.duplex() uses stdout if "all" is used', () => testReadableDuplexDefault(1, 'all', {all: true}, true));72test('.duplex() uses stderr if "all" is used', () => testReadableDuplexDefault(2, 'all', {all: true}, true));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 73why, one step back
73

TweaksSindre Sorhusbefore adoption698a1b8

line 74why, one step back
74const testWritableDuplexDefault = async (fdNumber, to, options) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 75–79why, one step back
75 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], options);
76 const stream = subprocess.duplex({to});7778 stream.end(foobarString);79

TweaksSindre Sorhusbefore adoption698a1b8

lines 80–81why, one step back
80 await assertStreamOutput(stream, foobarString);
81 await assertSubprocessOutput(subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 84–87why, one step back
84test('.duplex() can use stdin', () => testWritableDuplexDefault(0, 'stdin', {}));
85test('.duplex() can use input stdio[*]', () => testWritableDuplexDefault(3, 'fd3', fullReadableStdio()));86test('.duplex() can use input stdio[*] with { value: "pipe", input: true }', () => testWritableDuplexDefault(3, 'fd3', getStdio(3, {value: 'pipe', input: true})));87test('.duplex() uses stdin by default', () => testWritableDuplexDefault(0, undefined, {}));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 88why, one step back
88

TweaksSindre Sorhusbefore adoption698a1b8

line 89why, one step back
89test('.duplex() abort -> subprocess fail', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 90–95why, one step back
90 const subprocess = getReadWriteSubprocess();
91 const stream = subprocess.duplex();92 const textPromise = text(stream);9394 stream.destroy();95

TweaksSindre Sorhusbefore adoption698a1b8

lines 96–102why, one step back
96 const error = await assertRejects(textPromise);
97 assertLike(error, prematureClose);98 assertProcessNormalExit(error);99 assertWritableAborted(subprocess.stdin);100 assertReadableAborted(subprocess.stdout);101 assert.equal(subprocess.stderr.readableEnded, true);102 await assertSubprocessError(subprocess, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

line 105why, one step back
105test('.duplex() error -> subprocess fail', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 106–111why, one step back
106 const subprocess = getReadWriteSubprocess();
107 const stream = subprocess.duplex();108109 const cause = new Error(foobarString);110 stream.destroy(cause);111

TweaksSindre Sorhusbefore adoption698a1b8

lines 112–117why, one step back
112 const error = await assertStreamError(stream, {cause});
113 assertProcessNormalExit(error);114 assert.equal(subprocess.stdin.errored, cause);115 assert.equal(subprocess.stdout.errored, cause);116 assert.equal(subprocess.stderr.readableEnded, true);117 await assertSubprocessError(subprocess, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 118–119why, one step back
118});
119

TweaksSindre Sorhusbefore adoption698a1b8

line 120why, one step back
120test('.duplex() can be used with Stream.pipeline()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 121–129why, one step back
121 const subprocess = getReadWriteSubprocess();
122 const inputStream = Readable.from([foobarString]);123 const stream = subprocess.duplex();124 const outputStream = new PassThrough();125126 await pipeline(inputStream, stream, outputStream);127128 await finishedStream(inputStream);129 await finishedStream(stream);

TweaksSindre Sorhusbefore adoption698a1b8

lines 130–131why, one step back
130 await assertStreamOutput(outputStream);
131 await assertSubprocessOutput(subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 132–133why, one step back
132});
133

TweaksSindre Sorhusbefore adoption698a1b8

line 134why, one step back
134test('.duplex() can error with Stream.pipeline()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 135–139why, one step back
135 const subprocess = execa('stdin-fail.js');
136 const inputStream = Readable.from([foobarString]);137 const stream = subprocess.duplex();138 const outputStream = new PassThrough();139

TweaksSindre Sorhusbefore adoption698a1b8

lines 140–142why, one step back
140 const error = await assertRejects(pipeline(inputStream, stream, outputStream));
141 assertProcessNormalExit(error, 2);142 assertLike(error, {stdout: foobarString});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 143–144why, one step back
143
144 await finishedStream(inputStream);

TweaksSindre Sorhusbefore adoption698a1b8

lines 145–147why, one step back
145 await assertStreamError(stream, error);
146 await assertStreamReadError(outputStream, error);147 await assertSubprocessError(subprocess, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 148–149why, one step back
148});
149

TweaksSindre Sorhusbefore adoption698a1b8

line 150why, one step back
150test('.duplex() can pipe to errored stream with Stream.pipeline()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 151–158why, one step back
151 const subprocess = execa('stdin-fail.js');
152 const inputStream = Readable.from([foobarString]);153 const stream = subprocess.duplex();154 const outputStream = new PassThrough();155156 const cause = new Error('test');157 outputStream.destroy(cause);158

Runs CI tests on Node 23 (#1169)ehmickybefore adoptione330bd8

lines 159–162why, one step back
159 // Node 23 does not allow calling `stream.pipeline()` with an already errored stream
160 if (majorNodeVersion >= 23) {161 outputStream.on('error', () => {});162 stream.on('error', () => {});

TweaksSindre Sorhusbefore adoption698a1b8

line 163why, one step back
163 await assertRejects(pipeline(stream, outputStream), {code: 'ERR_STREAM_UNABLE_TO_PIPE'});

Runs CI tests on Node 23 (#1169)ehmickybefore adoptione330bd8

lines 164–165why, one step back
164 stream.end();
165 } else {

TweaksSindre Sorhusbefore adoption698a1b8

lines 166–167why, one step back
166 await assertPromiseError(pipeline(inputStream, stream, outputStream), cause);
167 await assertRejects(finishedStream(stream));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 168why, one step back
168

TweaksSindre Sorhusbefore adoption698a1b8

lines 169–172why, one step back
169 await assertStreamError(inputStream, cause);
170 const error = await assertStreamError(stream, cause);171 await assertStreamReadError(outputStream, cause);172 await assertSubprocessError(subprocess, {cause: error});

Runs CI tests on Node 23 (#1169)ehmickybefore adoptione330bd8

line 173why, one step back
173 }

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 174–175why, one step back
174});
175

TweaksSindre Sorhusbefore adoption698a1b8

line 176why, one step back
176test('.duplex() can be piped to errored stream with Stream.pipeline()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 177–184why, one step back
177 const subprocess = execa('stdin-fail.js');
178 const inputStream = Readable.from([foobarString]);179 const stream = subprocess.duplex();180 const outputStream = new PassThrough();181182 const cause = new Error('test');183 inputStream.destroy(cause);184

TweaksSindre Sorhusbefore adoption698a1b8

lines 185–186why, one step back
185 await assertPromiseError(pipeline(inputStream, stream, outputStream), cause);
186 await assertRejects(finishedStream(stream));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 187why, one step back
187

TweaksSindre Sorhusbefore adoption698a1b8

lines 188–191why, one step back
188 await assertStreamError(inputStream, cause);
189 const error = await assertStreamError(stream, cause);190 await assertStreamReadError(outputStream, cause);191 await assertSubprocessError(subprocess, {cause: error});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 192–193why, one step back
192});
193

TweaksSindre Sorhusbefore adoption698a1b8

line 194why, one step back
194test('.duplex() can be used with Stream.compose()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 195–199why, one step back
195 const subprocess = getReadWriteSubprocess();
196 const inputStream = Readable.from([foobarString]);197 const stream = subprocess.duplex();198 const outputStream = new PassThrough();199

TweaksSindre Sorhusbefore adoption698a1b8

lines 200–201why, one step back
200 await assertStreamOutput(compose(inputStream, stream, outputStream));
201 await assertSubprocessOutput(subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 202–203why, one step back
202});
203

TweaksSindre Sorhusbefore adoption698a1b8

line 204why, one step back
204test('.duplex() has the right highWaterMark', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 205–206why, one step back
205 const subprocess = getReadWriteSubprocess();
206 const stream = subprocess.duplex();

TweaksSindre Sorhusbefore adoption698a1b8

lines 207–208why, one step back
207 assert.equal(stream.readableHighWaterMark, defaultHighWaterMark);
208 assert.equal(stream.writableHighWaterMark, defaultHighWaterMark);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 209–211why, one step back
209 stream.end();
210 await text(stream);211});

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 212–215why, one step back
212
213test('The "binary" duplex option must use a boolean', () => {214 assert.throws(() => execa('noop.js').duplex({binary: 'true'}), {message: /`binary` option must use a boolean/});215});