lines.js
This file
Path
test/stdio/lines.js
131 lines · 9.5 KiB · JavaScript
Last changed by
Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
History of this fileTake it
Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 1–36why, one step back1import {scheduler} from 'node:timers/promises';2import test from 'ava';3import {execa} from '../../index.js';4import {setFixtureDir} from '../helpers/fixtures-dir.js';5import {getStdio} from '../helpers/stdio.js';67setFixtureDir();89const bigLine = '.'.repeat(1e5);10const manyChunks = Array.from({length: 1e3}).fill('.');1112const inputGenerator = async function * (input, chunks) {13 // eslint-disable-next-line no-unused-vars14 for await (const chunk of chunks) {15 for (const inputItem of input) {16 yield inputItem;17 // eslint-disable-next-line no-await-in-loop18 await scheduler.yield();19 }20 }21};2223const resultGenerator = async function * (lines, chunks) {24 for await (const chunk of chunks) {25 lines.push(chunk);26 yield chunk;27 }28};2930const textEncoder = new TextEncoder();31const textDecoder = new TextDecoder();3233const stringsToUint8Arrays = (strings, isUint8Array) => isUint8Array34 ? strings.map(string => textEncoder.encode(string))35 : strings;36Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 37–44why, one step back37const serializeResult = (result, isUint8Array, objectMode) => objectMode38 ? result.map(resultItem => serializeResultItem(resultItem, isUint8Array)).join('')39 : serializeResultItem(result, isUint8Array);4041const serializeResultItem = (resultItem, isUint8Array) => isUint8Array42 ? textDecoder.decode(resultItem)43 : resultItem;44Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 45why, one step back45// eslint-disable-next-line max-paramsAllow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 46why, one step back46const testLines = async (t, index, input, expectedLines, isUint8Array, objectMode) => {Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 47–49why, one step back47 const lines = [];48 const {stdio} = await execa('noop-fd.js', [`${index}`], {49 ...getStdio(index, [Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 50–51why, one step back50 {transform: inputGenerator.bind(undefined, stringsToUint8Arrays(input, isUint8Array)), objectMode},51 {transform: resultGenerator.bind(undefined, lines), objectMode},Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 52–55why, one step back52 ]),53 encoding: isUint8Array ? 'buffer' : 'utf8',54 stripFinalNewline: false,55 });Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 56why, one step back56 t.is(input.join(''), serializeResult(stdio[index], isUint8Array, objectMode));Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 57–59why, one step back57 t.deepEqual(lines, stringsToUint8Arrays(expectedLines, isUint8Array));58};59Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 60–111why, one step back60test('Split string stdout - n newlines, 1 chunk', testLines, 1, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], false, false);61test('Split string stderr - n newlines, 1 chunk', testLines, 2, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], false, false);62test('Split string stdio[*] - n newlines, 1 chunk', testLines, 3, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], false, false);63test('Split string stdout - no newline, n chunks', testLines, 1, ['aaa', 'bbb', 'ccc'], ['aaabbbccc'], false, false);64test('Split string stdout - 0 newlines, 1 chunk', testLines, 1, ['aaa'], ['aaa'], false, false);65test('Split string stdout - Windows newlines', testLines, 1, ['aaa\r\nbbb\r\nccc'], ['aaa\r\n', 'bbb\r\n', 'ccc'], false, false);66test('Split string stdout - chunk ends with newline', testLines, 1, ['aaa\nbbb\nccc\n'], ['aaa\n', 'bbb\n', 'ccc\n'], false, false);67test('Split string stdout - single newline', testLines, 1, ['\n'], ['\n'], false, false);68test('Split string stdout - only newlines', testLines, 1, ['\n\n\n'], ['\n', '\n', '\n'], false, false);69test('Split string stdout - only Windows newlines', testLines, 1, ['\r\n\r\n\r\n'], ['\r\n', '\r\n', '\r\n'], false, false);70test('Split string stdout - line split over multiple chunks', testLines, 1, ['aaa\nb', 'b', 'b\nccc'], ['aaa\n', 'bbb\n', 'ccc'], false, false);71test('Split string stdout - 0 newlines, big line', testLines, 1, [bigLine], [bigLine], false, false);72test('Split string stdout - 0 newlines, many chunks', testLines, 1, manyChunks, [manyChunks.join('')], false, false);73test('Split Uint8Array stdout - n newlines, 1 chunk', testLines, 1, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], true, false);74test('Split Uint8Array stderr - n newlines, 1 chunk', testLines, 2, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], true, false);75test('Split Uint8Array stdio[*] - n newlines, 1 chunk', testLines, 3, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], true, false);76test('Split Uint8Array stdout - no newline, n chunks', testLines, 1, ['aaa', 'bbb', 'ccc'], ['aaabbbccc'], true, false);77test('Split Uint8Array stdout - 0 newlines, 1 chunk', testLines, 1, ['aaa'], ['aaa'], true, false);78test('Split Uint8Array stdout - Windows newlines', testLines, 1, ['aaa\r\nbbb\r\nccc'], ['aaa\r\n', 'bbb\r\n', 'ccc'], true, false);79test('Split Uint8Array stdout - chunk ends with newline', testLines, 1, ['aaa\nbbb\nccc\n'], ['aaa\n', 'bbb\n', 'ccc\n'], true, false);80test('Split Uint8Array stdout - single newline', testLines, 1, ['\n'], ['\n'], true, false);81test('Split Uint8Array stdout - only newlines', testLines, 1, ['\n\n\n'], ['\n', '\n', '\n'], true, false);82test('Split Uint8Array stdout - only Windows newlines', testLines, 1, ['\r\n\r\n\r\n'], ['\r\n', '\r\n', '\r\n'], true, false);83test('Split Uint8Array stdout - line split over multiple chunks', testLines, 1, ['aaa\nb', 'b', 'b\nccc'], ['aaa\n', 'bbb\n', 'ccc'], true, false);84test('Split Uint8Array stdout - 0 newlines, big line', testLines, 1, [bigLine], [bigLine], true, false);85test('Split Uint8Array stdout - 0 newlines, many chunks', testLines, 1, manyChunks, [manyChunks.join('')], true, false);86test('Split string stdout - n newlines, 1 chunk, objectMode', testLines, 1, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], false, true);87test('Split string stderr - n newlines, 1 chunk, objectMode', testLines, 2, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], false, true);88test('Split string stdio[*] - n newlines, 1 chunk, objectMode', testLines, 3, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], false, true);89test('Split string stdout - no newline, n chunks, objectMode', testLines, 1, ['aaa', 'bbb', 'ccc'], ['aaabbbccc'], false, true);90test('Split string stdout - 0 newlines, 1 chunk, objectMode', testLines, 1, ['aaa'], ['aaa'], false, true);91test('Split string stdout - Windows newlines, objectMode', testLines, 1, ['aaa\r\nbbb\r\nccc'], ['aaa\r\n', 'bbb\r\n', 'ccc'], false, true);92test('Split string stdout - chunk ends with newline, objectMode', testLines, 1, ['aaa\nbbb\nccc\n'], ['aaa\n', 'bbb\n', 'ccc\n'], false, true);93test('Split string stdout - single newline, objectMode', testLines, 1, ['\n'], ['\n'], false, true);94test('Split string stdout - only newlines, objectMode', testLines, 1, ['\n\n\n'], ['\n', '\n', '\n'], false, true);95test('Split string stdout - only Windows newlines, objectMode', testLines, 1, ['\r\n\r\n\r\n'], ['\r\n', '\r\n', '\r\n'], false, true);96test('Split string stdout - line split over multiple chunks, objectMode', testLines, 1, ['aaa\nb', 'b', 'b\nccc'], ['aaa\n', 'bbb\n', 'ccc'], false, true);97test('Split string stdout - 0 newlines, big line, objectMode', testLines, 1, [bigLine], [bigLine], false, true);98test('Split string stdout - 0 newlines, many chunks, objectMode', testLines, 1, manyChunks, [manyChunks.join('')], false, true);99test('Split Uint8Array stdout - n newlines, 1 chunk, objectMode', testLines, 1, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], true, true);100test('Split Uint8Array stderr - n newlines, 1 chunk, objectMode', testLines, 2, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], true, true);101test('Split Uint8Array stdio[*] - n newlines, 1 chunk, objectMode', testLines, 3, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], true, true);102test('Split Uint8Array stdout - no newline, n chunks, objectMode', testLines, 1, ['aaa', 'bbb', 'ccc'], ['aaabbbccc'], true, true);103test('Split Uint8Array stdout - 0 newlines, 1 chunk, objectMode', testLines, 1, ['aaa'], ['aaa'], true, true);104test('Split Uint8Array stdout - Windows newlines, objectMode', testLines, 1, ['aaa\r\nbbb\r\nccc'], ['aaa\r\n', 'bbb\r\n', 'ccc'], true, true);105test('Split Uint8Array stdout - chunk ends with newline, objectMode', testLines, 1, ['aaa\nbbb\nccc\n'], ['aaa\n', 'bbb\n', 'ccc\n'], true, true);106test('Split Uint8Array stdout - single newline, objectMode', testLines, 1, ['\n'], ['\n'], true, true);107test('Split Uint8Array stdout - only newlines, objectMode', testLines, 1, ['\n\n\n'], ['\n', '\n', '\n'], true, true);108test('Split Uint8Array stdout - only Windows newlines, objectMode', testLines, 1, ['\r\n\r\n\r\n'], ['\r\n', '\r\n', '\r\n'], true, true);109test('Split Uint8Array stdout - line split over multiple chunks, objectMode', testLines, 1, ['aaa\nb', 'b', 'b\nccc'], ['aaa\n', 'bbb\n', 'ccc'], true, true);110test('Split Uint8Array stdout - 0 newlines, big line, objectMode', testLines, 1, [bigLine], [bigLine], true, true);111test('Split Uint8Array stdout - 0 newlines, many chunks, objectMode', testLines, 1, manyChunks, [manyChunks.join('')], true, true);Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 112why, one step back112Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 113–114why, one step back113// eslint-disable-next-line max-params114const testBinaryOption = async (t, binary, input, expectedLines, objectMode) => {Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 115–117why, one step back115 const lines = [];116 const {stdout} = await execa('noop-fd.js', ['1'], {117 stdout: [Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 118–119why, one step back118 {transform: inputGenerator.bind(undefined, input), objectMode},119 {transform: resultGenerator.bind(undefined, lines), objectMode, binary},Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 120–121why, one step back120 ],121 });Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 122why, one step back122 t.is(input.join(''), objectMode ? stdout.join('') : stdout);Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 123–125why, one step back123 t.deepEqual(lines, expectedLines);124};125Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 126–131why, one step back126test('Does not split lines when "binary" is true', testBinaryOption, true, ['aaa\nbbb\nccc'], ['aaa\nbbb\nccc'], false);127test('Splits lines when "binary" is false', testBinaryOption, false, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], false);128test('Splits lines when "binary" is undefined', testBinaryOption, undefined, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], false);129test('Does not split lines when "binary" is true, objectMode', testBinaryOption, true, ['aaa\nbbb\nccc'], ['aaa\nbbb\nccc'], true);130test('Splits lines when "binary" is false, objectMode', testBinaryOption, false, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], true);131test('Splits lines when "binary" is undefined, objectMode', testBinaryOption, undefined, ['aaa\nbbb\nccc'], ['aaa\n', 'bbb\n', 'ccc'], true);