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 fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
lines 1–4why, one step back1import 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 back5import {6 compose,7 Readable,8 Writable,9 PassThrough,10} from 'node:stream';TweaksSindre Sorhusbefore adoption698a1b8
line 11why, one step back11import {assertRejects, assertLike} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 12–27why, one step back12import {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 back28import {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 back29import {30 getStdio,31 prematureClose,32 fullStdio,33 fullReadableStdio,34} from '../helpers/stdio.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 35–38why, one step back35import {defaultHighWaterMark} from '../helpers/stream.js';3637setFixtureDirectory();38TweaksSindre Sorhusbefore adoption698a1b8
line 39why, one step back39test('.duplex() success', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 40–42why, one step back40 const subprocess = getReadWriteSubprocess();41 const stream = subprocess.duplex();42TweaksSindre Sorhusbefore adoption698a1b8
lines 43–46why, one step back43 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 back4748 stream.end(foobarString);49TweaksSindre Sorhusbefore adoption698a1b8
lines 50–51why, one step back50 await assertStreamOutput(stream);51 await assertSubprocessOutput(subprocess);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 52–53why, one step back52});53TweaksSindre Sorhusbefore adoption698a1b8
line 54why, one step back54const testReadableDuplexDefault = async (fdNumber, from, options, hasResult) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 55–58why, one step back55 const subprocess = execa('noop-stdin-fd.js', [`${fdNumber}`], options);56 const stream = subprocess.duplex({from});57 stream.end(foobarString);58TweaksSindre Sorhusbefore adoption698a1b8
lines 59–60why, one step back59 await assertStreamOutput(stream, hasResult ? foobarString : '');60 await assertSubprocessOutput(subprocess, foobarString, fdNumber);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 61–62why, one step back61};62TweaksSindre Sorhusbefore adoption698a1b8
lines 63–72why, one step back63test('.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 back73TweaksSindre Sorhusbefore adoption698a1b8
line 74why, one step back74const testWritableDuplexDefault = async (fdNumber, to, options) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 75–79why, one step back75 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], options);76 const stream = subprocess.duplex({to});7778 stream.end(foobarString);79TweaksSindre Sorhusbefore adoption698a1b8
lines 80–81why, one step back80 await assertStreamOutput(stream, foobarString);81 await assertSubprocessOutput(subprocess);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 82–83why, one step back82};83TweaksSindre Sorhusbefore adoption698a1b8
lines 84–87why, one step back84test('.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 back88TweaksSindre Sorhusbefore adoption698a1b8
line 89why, one step back89test('.duplex() abort -> subprocess fail', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 90–95why, one step back90 const subprocess = getReadWriteSubprocess();91 const stream = subprocess.duplex();92 const textPromise = text(stream);9394 stream.destroy();95TweaksSindre Sorhusbefore adoption698a1b8
lines 96–102why, one step back96 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 back103});104TweaksSindre Sorhusbefore adoption698a1b8
line 105why, one step back105test('.duplex() error -> subprocess fail', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 106–111why, one step back106 const subprocess = getReadWriteSubprocess();107 const stream = subprocess.duplex();108109 const cause = new Error(foobarString);110 stream.destroy(cause);111TweaksSindre Sorhusbefore adoption698a1b8
lines 112–117why, one step back112 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 back118});119TweaksSindre Sorhusbefore adoption698a1b8
line 120why, one step back120test('.duplex() can be used with Stream.pipeline()', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 121–129why, one step back121 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 back130 await assertStreamOutput(outputStream);131 await assertSubprocessOutput(subprocess);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 132–133why, one step back132});133TweaksSindre Sorhusbefore adoption698a1b8
line 134why, one step back134test('.duplex() can error with Stream.pipeline()', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 135–139why, one step back135 const subprocess = execa('stdin-fail.js');136 const inputStream = Readable.from([foobarString]);137 const stream = subprocess.duplex();138 const outputStream = new PassThrough();139TweaksSindre Sorhusbefore adoption698a1b8
lines 140–142why, one step back140 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 back143144 await finishedStream(inputStream);TweaksSindre Sorhusbefore adoption698a1b8
lines 145–147why, one step back145 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 back148});149TweaksSindre Sorhusbefore adoption698a1b8
line 150why, one step back150test('.duplex() can pipe to errored stream with Stream.pipeline()', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 151–158why, one step back151 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);158Runs CI tests on Node 23 (#1169)ehmickybefore adoptione330bd8
lines 159–162why, one step back159 // Node 23 does not allow calling `stream.pipeline()` with an already errored stream160 if (majorNodeVersion >= 23) {161 outputStream.on('error', () => {});162 stream.on('error', () => {});TweaksSindre Sorhusbefore adoption698a1b8
line 163why, one step back163 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 back164 stream.end();165 } else {TweaksSindre Sorhusbefore adoption698a1b8
lines 166–167why, one step back166 await assertPromiseError(pipeline(inputStream, stream, outputStream), cause);167 await assertRejects(finishedStream(stream));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 168why, one step back168TweaksSindre Sorhusbefore adoption698a1b8
lines 169–172why, one step back169 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 back173 }100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 174–175why, one step back174});175TweaksSindre Sorhusbefore adoption698a1b8
line 176why, one step back176test('.duplex() can be piped to errored stream with Stream.pipeline()', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 177–184why, one step back177 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);184TweaksSindre Sorhusbefore adoption698a1b8
lines 185–186why, one step back185 await assertPromiseError(pipeline(inputStream, stream, outputStream), cause);186 await assertRejects(finishedStream(stream));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 187why, one step back187TweaksSindre Sorhusbefore adoption698a1b8
lines 188–191why, one step back188 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 back192});193TweaksSindre Sorhusbefore adoption698a1b8
line 194why, one step back194test('.duplex() can be used with Stream.compose()', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 195–199why, one step back195 const subprocess = getReadWriteSubprocess();196 const inputStream = Readable.from([foobarString]);197 const stream = subprocess.duplex();198 const outputStream = new PassThrough();199TweaksSindre Sorhusbefore adoption698a1b8
lines 200–201why, one step back200 await assertStreamOutput(compose(inputStream, stream, outputStream));201 await assertSubprocessOutput(subprocess);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 202–203why, one step back202});203TweaksSindre Sorhusbefore adoption698a1b8
line 204why, one step back204test('.duplex() has the right highWaterMark', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 205–206why, one step back205 const subprocess = getReadWriteSubprocess();206 const stream = subprocess.duplex();TweaksSindre Sorhusbefore adoption698a1b8
lines 207–208why, one step back207 assert.equal(stream.readableHighWaterMark, defaultHighWaterMark);208 assert.equal(stream.writableHighWaterMark, defaultHighWaterMark);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 209–211why, one step back209 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 back212213test('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});