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
Provenance
- Commit
da7aec7- Tree
ae3be53- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
fc01815
1 file changed+38 added−15 removed
modified:test/stdio/lines.js+38 added-15 removed
1+import {Buffer} from 'node:buffer';2 import {once} from 'node:events';3 import {Writable} from 'node:stream';4 import {scheduler} from 'node:timers/promises';5 import test from 'ava';39 unmodified lines45 46 const textEncoder = new TextEncoder();47 const textDecoder = new TextDecoder();48 49+const getEncoding = isUint8Array => isUint8Array ? 'buffer' : 'utf8';50+51 const stringsToUint8Arrays = (strings, isUint8Array) => isUint8Array52 ? strings.map(string => textEncoder.encode(string))53 : strings;54 55+const stringsToBuffers = (strings, isUint8Array) => isUint8Array56+ ? strings.map(string => Buffer.from(string))57+ : strings;58+59+const getSimpleChunkSubprocess = (isUint8Array, options) => execa('noop-fd.js', ['1', ...simpleChunk], {60+ lines: true,61+ encoding: getEncoding(isUint8Array),62+ ...options,63+});64+65 const serializeResult = (result, isUint8Array, objectMode) => objectMode66 ? result.map(resultItem => serializeResultItem(resultItem, isUint8Array)).join('')67 : serializeResultItem(result, isUint8Array);68 8 unmodified lines77 ...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 lines162 const 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 lines216 t.not(btoa(`${singleLine}${singleLine}`), expectedLines.join(''));217 t.deepEqual(stdout, expectedLines);218 });219 207-test('"lines: true" works with stream async iteration', async t => {208- const subprocess = execa('noop.js', {lines: true, stdout: getChunksGenerator(simpleChunk), buffer: false});220+const 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+};225 213-test('"lines: true" works with stream "data" events', async t => {214- const subprocess = execa('noop.js', {lines: true, stdout: getChunksGenerator(simpleChunk), buffer: false});226+test('"lines: true" works with stream async iteration, string', testAsyncIteration, false);227+test('"lines: true" works with stream async iteration, Uint8Array', testAsyncIteration, true);228+229+const 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+};234 219-test('"lines: true" works with writable streams targets', async t => {235+test('"lines: true" works with stream "data" events, string', testDataEvents, false);236+test('"lines: true" works with stream "data" events, Uint8Array', testDataEvents, true);237+238+const 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+};250+251+test('"lines: true" works with writable streams targets, string', testWritableStream, false);252+test('"lines: true" works with writable streams targets, Uint8Array', testWritableStream, true);