Skip to content
Myr

Add more tests for the `lines` option (#914)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent fc01815 · Browse files · 1 file+38 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+38 added−15 removed

  • tests:+38 added−15 removed

1 file+38 added−15 removed

modified:test/stdio/lines.js+38 added-15 removed
1import {Buffer} from 'node:buffer';2import {once} from 'node:events';3import {Writable} from 'node:stream';4import {scheduler} from 'node:timers/promises';5import test from 'ava';
39 unmodified lines
4546const textEncoder = new TextEncoder();47const textDecoder = new TextDecoder();4849const getEncoding = isUint8Array => isUint8Array ? 'buffer' : 'utf8';5051const stringsToUint8Arrays = (strings, isUint8Array) => isUint8Array52 ? strings.map(string => textEncoder.encode(string))53 : strings;5455const stringsToBuffers = (strings, isUint8Array) => isUint8Array56 ? strings.map(string => Buffer.from(string))57 : strings;5859const getSimpleChunkSubprocess = (isUint8Array, options) => execa('noop-fd.js', ['1', ...simpleChunk], {60 lines: true,61 encoding: getEncoding(isUint8Array),62 ...options,63});6465const serializeResult = (result, isUint8Array, objectMode) => objectMode66 ? result.map(resultItem => serializeResultItem(resultItem, isUint8Array)).join('')67 : serializeResultItem(result, isUint8Array);68
8 unmodified lines
77 ...getStdio(fdNumber, [78 {transform: inputGenerator.bind(undefined, stringsToUint8Arrays(input, isUint8Array)), objectMode},79 {transform: resultGenerator.bind(undefined, lines), objectMode},80 ]),68 encoding: isUint8Array ? 'buffer' : 'utf8',81 encoding: getEncoding(isUint8Array),82 stripFinalNewline: false,83 });84 t.is(input.join(''), serializeResult(stdio[fdNumber], isUint8Array, objectMode));85 t.deepEqual(lines, stringsToUint8Arrays(expectedLines, isUint8Array));
76 unmodified lines
162const testStreamLines = async (t, fdNumber, input, expectedLines, isUint8Array) => {163 const {stdio} = await execa('noop-fd.js', [`${fdNumber}`, input], {164 ...fullStdio,165 lines: true,153 encoding: isUint8Array ? 'buffer' : 'utf8',166 encoding: getEncoding(isUint8Array),167 });168 t.deepEqual(stdio[fdNumber], stringsToUint8Arrays(expectedLines, isUint8Array));169};170
45 unmodified lines
216 t.not(btoa(`${singleLine}${singleLine}`), expectedLines.join(''));217 t.deepEqual(stdout, expectedLines);218});219207test('"lines: true" works with stream async iteration', async t => {208 const subprocess = execa('noop.js', {lines: true, stdout: getChunksGenerator(simpleChunk), buffer: false});220const testAsyncIteration = async (t, isUint8Array) => {221 const subprocess = getSimpleChunkSubprocess(isUint8Array);222 const [stdout] = await Promise.all([subprocess.stdout.toArray(), subprocess]);210 t.deepEqual(stdout, simpleLines);211});223 t.deepEqual(stdout, stringsToUint8Arrays(simpleLines, isUint8Array));224};225213test('"lines: true" works with stream "data" events', async t => {214 const subprocess = execa('noop.js', {lines: true, stdout: getChunksGenerator(simpleChunk), buffer: false});226test('"lines: true" works with stream async iteration, string', testAsyncIteration, false);227test('"lines: true" works with stream async iteration, Uint8Array', testAsyncIteration, true);228229const testDataEvents = async (t, isUint8Array) => {230 const subprocess = getSimpleChunkSubprocess(isUint8Array);231 const [[firstLine]] = await Promise.all([once(subprocess.stdout, 'data'), subprocess]);216 t.is(firstLine, simpleLines[0]);217});232 t.deepEqual(firstLine, stringsToUint8Arrays(simpleLines, isUint8Array)[0]);233};234219test('"lines: true" works with writable streams targets', async t => {235test('"lines: true" works with stream "data" events, string', testDataEvents, false);236test('"lines: true" works with stream "data" events, Uint8Array', testDataEvents, true);237238const testWritableStream = async (t, isUint8Array) => {239 const lines = [];240 const writable = new Writable({241 write(line, encoding, done) {223 lines.push(line.toString());242 lines.push(line);243 done();244 },245 decodeStrings: false,246 });227 await execa('noop.js', {lines: true, stdout: [getChunksGenerator(simpleChunk), writable]});228 t.deepEqual(lines, simpleLines);229});247 await getSimpleChunkSubprocess(isUint8Array, {stdout: ['pipe', writable]});248 t.deepEqual(lines, stringsToBuffers(simpleLines, isUint8Array));249};250251test('"lines: true" works with writable streams targets, string', testWritableStream, false);252test('"lines: true" works with writable streams targets, Uint8Array', testWritableStream, true);