Skip to content
Myr

split-transform.js

This file

Path

test/transform/split-transform.js

82 lines · 4.2 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–2why, one step back
1import assert from 'node:assert/strict';
2import test from 'node:test';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 3–19why, one step back
3import {execa, execaSync} from '../../index.js';
4import {setFixtureDirectory} from '../helpers/fixtures-directory.js';5import {getOutputsGenerator, resultGenerator} from '../helpers/generator.js';6import {7 foobarString,8 foobarUint8Array,9 foobarObject,10 foobarObjectString,11} from '../helpers/input.js';1213setFixtureDirectory();1415const resultUint8ArrayGenerator = function * (lines, chunk) {16 lines.push(chunk);17 yield new TextEncoder().encode(chunk);18};19

TweaksSindre Sorhusbefore adoption698a1b8

line 20why, one step back
20const testStringToUint8Array = async (expectedOutput, objectMode, preserveNewlines, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 21–29why, one step back
21 const lines = [];
22 const {stdout} = await execaMethod('noop-fd.js', ['1', foobarString], {23 stdout: {24 transform: resultUint8ArrayGenerator.bind(undefined, lines),25 objectMode,26 preserveNewlines,27 },28 lines: true,29 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 30–31why, one step back
30 assert.deepEqual(lines, [foobarString]);
31 assert.deepEqual(stdout, expectedOutput);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 32–33why, one step back
32};
33

TweaksSindre Sorhusbefore adoption698a1b8

lines 34–41why, one step back
34test('Line splitting when converting from string to Uint8Array', () => testStringToUint8Array([foobarString], false, true, execa));
35test('Line splitting when converting from string to Uint8Array, objectMode', () => testStringToUint8Array([foobarUint8Array], true, true, execa));36test('Line splitting when converting from string to Uint8Array, preserveNewlines', () => testStringToUint8Array([foobarString], false, false, execa));37test('Line splitting when converting from string to Uint8Array, objectMode, preserveNewlines', () => testStringToUint8Array([foobarUint8Array], true, false, execa));38test('Line splitting when converting from string to Uint8Array, sync', () => testStringToUint8Array([foobarString], false, true, execaSync));39test('Line splitting when converting from string to Uint8Array, objectMode, sync', () => testStringToUint8Array([foobarUint8Array], true, true, execaSync));40test('Line splitting when converting from string to Uint8Array, preserveNewlines, sync', () => testStringToUint8Array([foobarString], false, false, execaSync));41test('Line splitting when converting from string to Uint8Array, objectMode, preserveNewlines, sync', () => testStringToUint8Array([foobarUint8Array], true, false, execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 42–47why, one step back
42
43const serializeResultGenerator = function * (lines, chunk) {44 lines.push(chunk);45 yield JSON.stringify(chunk);46};47

TweaksSindre Sorhusbefore adoption698a1b8

line 48why, one step back
48const testUnsetObjectMode = async (expectedOutput, preserveNewlines, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 49–56why, one step back
49 const lines = [];
50 const {stdout} = await execaMethod('noop.js', {51 stdout: [52 getOutputsGenerator([foobarObject])(true),53 {transform: serializeResultGenerator.bind(undefined, lines), preserveNewlines, objectMode: false},54 ],55 stripFinalNewline: false,56 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 57–58why, one step back
57 assert.deepEqual(lines, [foobarObject]);
58 assert.equal(stdout, expectedOutput);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 59–60why, one step back
59};
60

TweaksSindre Sorhusbefore adoption698a1b8

lines 61–64why, one step back
61test('Can switch from objectMode to non-objectMode', () => testUnsetObjectMode(`${foobarObjectString}\n`, false, execa));
62test('Can switch from objectMode to non-objectMode, preserveNewlines', () => testUnsetObjectMode(foobarObjectString, true, execa));63test('Can switch from objectMode to non-objectMode, sync', () => testUnsetObjectMode(`${foobarObjectString}\n`, false, execaSync));64test('Can switch from objectMode to non-objectMode, preserveNewlines, sync', () => testUnsetObjectMode(foobarObjectString, true, execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 65why, one step back
65

TweaksSindre Sorhusbefore adoption698a1b8

line 66why, one step back
66const testYieldArray = async (input, expectedLines, expectedOutput, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 67–74why, one step back
67 const lines = [];
68 const {stdout} = await execaMethod('noop.js', {69 stdout: [70 getOutputsGenerator(input)(),71 resultGenerator(lines)(),72 ],73 stripFinalNewline: false,74 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 75–76why, one step back
75 assert.deepEqual(lines, expectedLines);
76 assert.deepEqual(stdout, expectedOutput);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 77–78why, one step back
77};
78

TweaksSindre Sorhusbefore adoption698a1b8

lines 79–82why, one step back
79test('Can use "yield* array" to produce multiple lines', () => testYieldArray([foobarString, foobarString], [foobarString, foobarString], `${foobarString}\n${foobarString}\n`, execa));
80test('Can use "yield* array" to produce empty lines', () => testYieldArray([foobarString, ''], [foobarString, ''], `${foobarString}\n\n`, execa));81test('Can use "yield* array" to produce multiple lines, sync', () => testYieldArray([foobarString, foobarString], [foobarString, foobarString], `${foobarString}\n${foobarString}\n`, execaSync));82test('Can use "yield* array" to produce empty lines, sync', () => testYieldArray([foobarString, ''], [foobarString, ''], `${foobarString}\n\n`, execaSync));