iterate.js
This file
Path
test/io/iterate.js
207 lines · 15.6 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–3why, one step back2import {once} from 'node:events';3import {getDefaultHighWaterMark} from 'node:stream';TweaksSindre Sorhusbefore adoption698a1b8
line 4why, one step back4import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 5–54why, one step back5import {setFixtureDirectory} from '../helpers/fixtures-directory.js';6import {7 assertStreamOutput,8 assertIterableChunks,9 assertStreamChunks,10 assertSubprocessOutput,11 getReadableSubprocess,12 getReadWriteSubprocess,13} from '../helpers/convert.js';14import {15 stringToUint8Arrays,16 simpleFull,17 simpleChunks,18 simpleChunksBuffer,19 simpleChunksUint8Array,20 simpleLines,21 noNewlinesFull,22 complexFull,23 complexFullUtf16,24 complexFullUtf16Uint8Array,25 singleComplexBuffer,26 singleComplexUtf16Buffer,27 singleComplexUint8Array,28 singleComplexHex,29 complexChunks,30 complexChunksEnd,31} from '../helpers/lines.js';32import {outputObjectGenerator, getOutputGenerator} from '../helpers/generator.js';33import {foobarString, foobarObject} from '../helpers/input.js';34import {35 multibyteChar,36 multibyteUint8Array,37 breakingLength,38 brokenSymbol,39} from '../helpers/encoding.js';4041setFixtureDirectory();4243const foobarObjectChunks = [foobarObject, foobarObject, foobarObject];4445const getSubprocess = (methodName, output, options) => {46 if (methodName !== 'duplex') {47 return getReadableSubprocess(output, options);48 }4950 const subprocess = getReadWriteSubprocess(options);51 subprocess.stdin.end(output);52 return subprocess;53};54TweaksSindre Sorhusbefore adoption698a1b8
line 55why, one step back55const assertChunks = async (streamOrIterable, expectedChunks, methodName) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 56why, one step back56 const assertMethod = methodName === 'iterable' ? assertIterableChunks : assertStreamChunks;TweaksSindre Sorhusbefore adoption698a1b8
line 57why, one step back57 await assertMethod(streamOrIterable, expectedChunks);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 58–60why, one step back58};5960// eslint-disable-next-line max-paramsTweaksSindre Sorhusbefore adoption698a1b8
line 61why, one step back61const testText = async (expectedChunks, methodName, binary, preserveNewlines, encoding) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 62–66why, one step back62 const subprocess = getReadWriteSubprocess({encoding});63 const input = encoding === 'utf16le' ? complexFullUtf16 : complexFull;64 subprocess.stdin.end(input);65 const stream = subprocess[methodName]({binary, preserveNewlines});66TweaksSindre Sorhusbefore adoption698a1b8
line 67why, one step back67 await assertChunks(stream, expectedChunks, methodName);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 68–70why, one step back68 const expectedOutput = encoding === 'hex'69 ? singleComplexHex70 : stringToUint8Arrays(complexFull, encoding === 'buffer');TweaksSindre Sorhusbefore adoption698a1b8
line 71why, one step back71 await assertSubprocessOutput(subprocess, expectedOutput);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 72–73why, one step back72};73TweaksSindre Sorhusbefore adoption698a1b8
lines 74–115why, one step back74test('.iterable() can use "binary: true"', () => testText([singleComplexUint8Array], 'iterable', true, undefined, 'utf8'));75test('.iterable() can use "binary: true" + "encoding: utf16le"', () => testText([complexFullUtf16Uint8Array], 'iterable', true, undefined, 'utf16le'));76test('.iterable() can use "binary: true" + "encoding: "buffer"', () => testText([singleComplexUint8Array], 'iterable', true, undefined, 'buffer'));77test('.iterable() can use "binary: true" + "encoding: "hex"', () => testText([singleComplexUint8Array], 'iterable', true, undefined, 'hex'));78test('.iterable() can use "binary: undefined"', () => testText(complexChunks, 'iterable', undefined, undefined, 'utf8'));79test('.iterable() can use "binary: undefined" + "encoding: utf16le"', () => testText(complexChunks, 'iterable', undefined, undefined, 'utf16le'));80test('.iterable() can use "binary: undefined" + "encoding: buffer"', () => testText([singleComplexUint8Array], 'iterable', undefined, undefined, 'buffer'));81test('.iterable() can use "binary: undefined" + "encoding: hex"', () => testText([singleComplexUint8Array], 'iterable', undefined, undefined, 'hex'));82test('.iterable() can use "binary: false"', () => testText(complexChunks, 'iterable', false, undefined, 'utf8'));83test('.iterable() can use "binary: false" + "encoding: utf16le"', () => testText(complexChunks, 'iterable', false, undefined, 'utf16le'));84test('.iterable() can use "binary: false" + "encoding: buffer"', () => testText([singleComplexUint8Array], 'iterable', false, undefined, 'buffer'));85test('.iterable() can use "binary: false" + "encoding: hex"', () => testText([singleComplexUint8Array], 'iterable', false, undefined, 'hex'));86test('.iterable() can use "binary: false" + "preserveNewlines: true"', () => testText(complexChunksEnd, 'iterable', false, true, 'utf8'));87test('.iterable() can use "binary: false" + "preserveNewlines: false"', () => testText(complexChunks, 'iterable', false, false, 'utf8'));88test('.readable() can use "binary: true"', () => testText(singleComplexBuffer, 'readable', true, undefined, 'utf8'));89test('.readable() can use "binary: true" + "encoding: utf16le"', () => testText(singleComplexUtf16Buffer, 'readable', true, undefined, 'utf16le'));90test('.readable() can use "binary: true" + "encoding: buffer"', () => testText(singleComplexBuffer, 'readable', true, undefined, 'buffer'));91test('.readable() can use "binary: true" + "encoding: hex"', () => testText(singleComplexBuffer, 'readable', true, undefined, 'hex'));92test('.readable() can use "binary: undefined"', () => testText(singleComplexBuffer, 'readable', undefined, undefined, 'utf8'));93test('.readable() can use "binary: undefined" + "encoding: utf16le"', () => testText(singleComplexUtf16Buffer, 'readable', undefined, undefined, 'utf16le'));94test('.readable() can use "binary: undefined" + "encoding: buffer"', () => testText(singleComplexBuffer, 'readable', undefined, undefined, 'buffer'));95test('.readable() can use "binary: undefined" + "encoding: hex"', () => testText(singleComplexBuffer, 'readable', undefined, undefined, 'hex'));96test('.readable() can use "binary: false"', () => testText(complexChunksEnd, 'readable', false, undefined, 'utf8'));97test('.readable() can use "binary: false" + "encoding: utf16le"', () => testText(complexChunksEnd, 'readable', false, undefined, 'utf16le'));98test('.readable() can use "binary: false" + "encoding: buffer"', () => testText(singleComplexBuffer, 'readable', false, undefined, 'buffer'));99test('.readable() can use "binary: false" + "encoding: hex"', () => testText(singleComplexBuffer, 'readable', false, undefined, 'hex'));100test('.readable() can use "binary: false" + "preserveNewlines: true"', () => testText(complexChunksEnd, 'readable', false, true, 'utf8'));101test('.readable() can use "binary: false" + "preserveNewlines: false"', () => testText(complexChunks, 'readable', false, false, 'utf8'));102test('.duplex() can use "binary: true"', () => testText(singleComplexBuffer, 'duplex', true, undefined, 'utf8'));103test('.duplex() can use "binary: true" + "encoding: utf16le"', () => testText(singleComplexUtf16Buffer, 'duplex', true, undefined, 'utf16le'));104test('.duplex() can use "binary: true" + "encoding: buffer"', () => testText(singleComplexBuffer, 'duplex', true, undefined, 'buffer'));105test('.duplex() can use "binary: true" + "encoding: hex"', () => testText(singleComplexBuffer, 'duplex', true, undefined, 'hex'));106test('.duplex() can use "binary: undefined"', () => testText(singleComplexBuffer, 'duplex', undefined, undefined, 'utf8'));107test('.duplex() can use "binary: undefined" + "encoding: utf16le"', () => testText(singleComplexUtf16Buffer, 'duplex', undefined, undefined, 'utf16le'));108test('.duplex() can use "binary: undefined" + "encoding: "buffer"', () => testText(singleComplexBuffer, 'duplex', undefined, undefined, 'buffer'));109test('.duplex() can use "binary: undefined" + "encoding: "hex"', () => testText(singleComplexBuffer, 'duplex', undefined, undefined, 'hex'));110test('.duplex() can use "binary: false"', () => testText(complexChunksEnd, 'duplex', false, undefined, 'utf8'));111test('.duplex() can use "binary: false" + "encoding: utf16le"', () => testText(complexChunksEnd, 'duplex', false, undefined, 'utf16le'));112test('.duplex() can use "binary: false" + "encoding: buffer"', () => testText(singleComplexBuffer, 'duplex', false, undefined, 'buffer'));113test('.duplex() can use "binary: false" + "encoding: hex"', () => testText(singleComplexBuffer, 'duplex', false, undefined, 'hex'));114test('.duplex() can use "binary: false" + "preserveNewlines: true"', () => testText(complexChunksEnd, 'duplex', false, true, 'utf8'));115test('.duplex() can use "binary: false" + "preserveNewlines: false"', () => testText(complexChunks, 'duplex', false, false, 'utf8'));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 116why, one step back116TweaksSindre Sorhusbefore adoption698a1b8
line 117why, one step back117const testTextOutput = async (expectedOutput, methodName, preserveNewlines) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 118–120why, one step back118 const subprocess = getSubprocess(methodName, complexFull);119 const stream = subprocess[methodName]({binary: false, preserveNewlines});120TweaksSindre Sorhusbefore adoption698a1b8
lines 121–122why, one step back121 await assertStreamOutput(stream, expectedOutput);122 await assertSubprocessOutput(subprocess, complexFull);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 123–124why, one step back123};124TweaksSindre Sorhusbefore adoption698a1b8
lines 125–130why, one step back125test('.readable() "binary: false" keeps output as is', () => testTextOutput(complexFull, 'readable', undefined));126test('.readable() "binary: false" + "preserveNewlines: true" keeps output as is', () => testTextOutput(complexFull, 'readable', true));127test('.readable() "binary: false" + "preserveNewlines: false" removes all newlines', () => testTextOutput(noNewlinesFull, 'readable', false));128test('.duplex() "binary: false" keeps output as is', () => testTextOutput(complexFull, 'duplex', undefined));129test('.duplex() "binary: false" + "preserveNewlines: true" keeps output as is', () => testTextOutput(complexFull, 'duplex', true));130test('.duplex() "binary: false" + "preserveNewlines: false" removes all newlines', () => testTextOutput(noNewlinesFull, 'duplex', false));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 131–132why, one step back131132// eslint-disable-next-line max-paramsTweaksSindre Sorhusbefore adoption698a1b8
line 133why, one step back133const testObjectMode = async (expectedChunks, methodName, encoding, initialObjectMode, finalObjectMode, binary, options) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 134–138why, one step back134 const subprocess = getSubprocess(methodName, simpleFull, options);135 if (encoding !== null) {136 subprocess.stdout.setEncoding(encoding);137 }138TweaksSindre Sorhusbefore adoption698a1b8
lines 139–141why, one step back139 assert.equal(subprocess.stdout.readableEncoding, encoding);140 assert.equal(subprocess.stdout.readableObjectMode, initialObjectMode);141 assert.equal(subprocess.stdout.readableHighWaterMark, getDefaultHighWaterMark(initialObjectMode));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 142–145why, one step back142143 const stream = subprocess[methodName]({binary, preserveNewlines: true});144145 if (methodName !== 'iterable') {TweaksSindre Sorhusbefore adoption698a1b8
lines 146–148why, one step back146 assert.equal(stream.readableEncoding, encoding);147 assert.equal(stream.readableObjectMode, finalObjectMode);148 assert.equal(stream.readableHighWaterMark, getDefaultHighWaterMark(finalObjectMode));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 149–150why, one step back149 }150TweaksSindre Sorhusbefore adoption698a1b8
lines 151–153why, one step back151 assert.equal(subprocess.stdout.readableEncoding, encoding);152 assert.equal(subprocess.stdout.readableObjectMode, initialObjectMode);153 assert.equal(subprocess.stdout.readableHighWaterMark, getDefaultHighWaterMark(initialObjectMode));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 154why, one step back154TweaksSindre Sorhusbefore adoption698a1b8
line 155why, one step back155 await assertChunks(stream, expectedChunks, methodName);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 156–158why, one step back156 await subprocess;157};158TweaksSindre Sorhusbefore adoption698a1b8
lines 159–182why, one step back159test('.iterable() uses Uint8Arrays with "binary: true"', () => testObjectMode(simpleChunksUint8Array, 'iterable', null, false, false, true));160test('.iterable() uses Uint8Arrays with "binary: true" and .setEncoding("utf8")', () => testObjectMode(simpleChunksUint8Array, 'iterable', 'utf8', false, false, true));161test('.iterable() uses Uint8Arrays with "binary: true", .setEncoding("utf8") and "encoding: buffer"', () => testObjectMode(simpleChunksUint8Array, 'iterable', 'utf8', false, false, true, {encoding: 'buffer'}));162test('.iterable() uses strings in objectMode with "binary: true" and object transforms', () => testObjectMode(foobarObjectChunks, 'iterable', null, true, true, true, {stdout: outputObjectGenerator()}));163test('.iterable() uses strings in objectMode with "binary: false"', () => testObjectMode(simpleLines, 'iterable', null, false, true, false));164test('.iterable() uses strings in objectMode with "binary: false" and .setEncoding("utf8")', () => testObjectMode(simpleLines, 'iterable', 'utf8', false, true, false));165test('.iterable() uses Uint8Arrays in objectMode with "binary: false", .setEncoding("utf8") and "encoding: buffer"', () => testObjectMode(simpleChunksUint8Array, 'iterable', 'utf8', false, true, false, {encoding: 'buffer'}));166test('.iterable() uses strings in objectMode with "binary: false" and object transforms', () => testObjectMode(foobarObjectChunks, 'iterable', null, true, true, false, {stdout: outputObjectGenerator()}));167test('.readable() uses Buffers with "binary: true"', () => testObjectMode(simpleChunksBuffer, 'readable', null, false, false, true));168test('.readable() uses strings with "binary: true" and .setEncoding("utf8")', () => testObjectMode(simpleChunks, 'readable', 'utf8', false, false, true));169test('.readable() uses strings with "binary: true", .setEncoding("utf8") and "encoding: buffer"', () => testObjectMode(simpleChunks, 'readable', 'utf8', false, false, true, {encoding: 'buffer'}));170test('.readable() uses strings in objectMode with "binary: true" and object transforms', () => testObjectMode(foobarObjectChunks, 'readable', null, true, true, true, {stdout: outputObjectGenerator()}));171test('.readable() uses strings in objectMode with "binary: false"', () => testObjectMode(simpleLines, 'readable', null, false, true, false));172test('.readable() uses strings in objectMode with "binary: false" and .setEncoding("utf8")', () => testObjectMode(simpleLines, 'readable', 'utf8', false, true, false));173test('.readable() uses strings in objectMode with "binary: false", .setEncoding("utf8") and "encoding: buffer"', () => testObjectMode(simpleChunks, 'readable', 'utf8', false, false, false, {encoding: 'buffer'}));174test('.readable() uses strings in objectMode with "binary: false" and object transforms', () => testObjectMode(foobarObjectChunks, 'readable', null, true, true, false, {stdout: outputObjectGenerator()}));175test('.duplex() uses Buffers with "binary: true"', () => testObjectMode(simpleChunksBuffer, 'duplex', null, false, false, true));176test('.duplex() uses strings with "binary: true" and .setEncoding("utf8")', () => testObjectMode(simpleChunks, 'duplex', 'utf8', false, false, true));177test('.duplex() uses strings with "binary: true", .setEncoding("utf8") and "encoding: buffer"', () => testObjectMode(simpleChunks, 'duplex', 'utf8', false, false, true, {encoding: 'buffer'}));178test('.duplex() uses strings in objectMode with "binary: true" and object transforms', () => testObjectMode(foobarObjectChunks, 'duplex', null, true, true, true, {stdout: outputObjectGenerator()}));179test('.duplex() uses strings in objectMode with "binary: false"', () => testObjectMode(simpleLines, 'duplex', null, false, true, false));180test('.duplex() uses strings in objectMode with "binary: false" and .setEncoding("utf8")', () => testObjectMode(simpleLines, 'duplex', 'utf8', false, true, false));181test('.duplex() uses strings in objectMode with "binary: false", .setEncoding("utf8") and "encoding: buffer"', () => testObjectMode(simpleChunks, 'duplex', 'utf8', false, false, false, {encoding: 'buffer'}));182test('.duplex() uses strings in objectMode with "binary: false" and object transforms', () => testObjectMode(foobarObjectChunks, 'duplex', null, true, true, false, {stdout: outputObjectGenerator()}));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 183why, one step back183TweaksSindre Sorhusbefore adoption698a1b8
line 184why, one step back184const testObjectSplit = async methodName => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 185–186why, one step back185 const subprocess = getSubprocess(methodName, foobarString, {stdout: getOutputGenerator(simpleFull)(true)});186 const stream = subprocess[methodName]({binary: false});TweaksSindre Sorhusbefore adoption698a1b8
line 187why, one step back187 await assertChunks(stream, [simpleFull], methodName);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 188–190why, one step back188 await subprocess;189};190TweaksSindre Sorhusbefore adoption698a1b8
lines 191–193why, one step back191test('.iterable() "binary: false" does not split lines of strings produced by object transforms', () => testObjectSplit('iterable'));192test('.readable() "binary: false" does not split lines of strings produced by object transforms', () => testObjectSplit('readable'));193test('.duplex() "binary: false" does not split lines of strings produced by object transforms', () => testObjectSplit('duplex'));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 194why, one step back194TweaksSindre Sorhusbefore adoption698a1b8
line 195why, one step back195const testMultibyteCharacters = async methodName => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 196–197why, one step back196 const subprocess = getReadWriteSubprocess();197 const stream = subprocess[methodName]({binary: false});TweaksSindre Sorhusbefore adoption698a1b8
line 198why, one step back198 const assertPromise = assertChunks(stream, [`${multibyteChar}${brokenSymbol}`], methodName);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 199–204why, one step back199 subprocess.stdin.write(multibyteUint8Array.slice(0, breakingLength));200 await once(subprocess.stdout, 'data');201 subprocess.stdin.end();202 await assertPromise;203};204TweaksSindre Sorhusbefore adoption698a1b8
lines 205–207why, one step back205test('.iterable() "binary: false" handles partial multibyte characters', () => testMultibyteCharacters('iterable'));206test('.readable() "binary: false" handles partial multibyte characters', () => testMultibyteCharacters('readable'));207test('.duplex() "binary: false" handles partial multibyte characters', () => testMultibyteCharacters('duplex'));