Skip to content
Myr

generator-main.js

This file

Path

test/transform/generator-main.js

186 lines · 8.9 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

line 1why, one step back
1import assert from 'node:assert/strict';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 2–3why, one step back
2import {Buffer} from 'node:buffer';
3import {scheduler} from 'node:timers/promises';

TweaksSindre Sorhusbefore adoption698a1b8

line 4why, one step back
4import test from 'node:test';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 5why, one step back
5import {getStreamAsArray} from 'get-stream';

TweaksSindre Sorhusbefore adoption698a1b8

line 6why, one step back
6import {assertRejects} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 7–30why, one step back
7import {execa, execaSync} from '../../index.js';
8import {foobarString} from '../helpers/input.js';9import {10 noopGenerator,11 outputObjectGenerator,12 convertTransformToFinal,13 prefix,14 suffix,15} from '../helpers/generator.js';16import {generatorsMap} from '../helpers/map.js';17import {defaultHighWaterMark} from '../helpers/stream.js';18import {setFixtureDirectory} from '../helpers/fixtures-directory.js';19import {maxBuffer, assertErrorMessage} from '../helpers/max-buffer.js';2021setFixtureDirectory();2223const repeatCount = defaultHighWaterMark * 3;2425const writerGenerator = function * () {26 for (let index = 0; index < repeatCount; index += 1) {27 yield '\n';28 }29};30

TweaksSindre Sorhusbefore adoption698a1b8

lines 31–32why, one step back
31const getLengthGenerator = function * (chunk) {
32 assert.equal(chunk.length, 1);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 33–35why, one step back
33 yield chunk;
34};35

TweaksSindre Sorhusbefore adoption698a1b8

line 36why, one step back
36const testHighWaterMark = async (passThrough, binary, objectMode, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 37–41why, one step back
37 const {stdout} = await execaMethod('noop.js', {
38 stdout: [39 ...(objectMode ? [outputObjectGenerator()] : []),40 writerGenerator,41 ...(passThrough ? [noopGenerator(false, binary)] : []),

TweaksSindre Sorhusbefore adoption698a1b8

line 42why, one step back
42 {transform: getLengthGenerator.bind(undefined), preserveNewlines: true, objectMode: true},

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 43–44why, one step back
43 ],
44 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 45–46why, one step back
45 assert.equal(stdout.length, repeatCount);
46 assert.ok(stdout.every(chunk => chunk === '\n'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 47–48why, one step back
47};
48

TweaksSindre Sorhusbefore adoption698a1b8

lines 49–56why, one step back
49test('Synchronous yields are not buffered, no passThrough', () => testHighWaterMark(false, false, false, execa));
50test('Synchronous yields are not buffered, line-wise passThrough', () => testHighWaterMark(true, false, false, execa));51test('Synchronous yields are not buffered, binary passThrough', () => testHighWaterMark(true, true, false, execa));52test('Synchronous yields are not buffered, objectMode as input but not output', () => testHighWaterMark(false, false, true, execa));53test('Synchronous yields are not buffered, no passThrough, sync', () => testHighWaterMark(false, false, false, execaSync));54test('Synchronous yields are not buffered, line-wise passThrough, sync', () => testHighWaterMark(true, false, false, execaSync));55test('Synchronous yields are not buffered, binary passThrough, sync', () => testHighWaterMark(true, true, false, execaSync));56test('Synchronous yields are not buffered, objectMode as input but not output, sync', () => testHighWaterMark(false, false, true, execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 57–58why, one step back
57
58// eslint-disable-next-line max-params

TweaksSindre Sorhusbefore adoption698a1b8

line 59why, one step back
59const testNoYield = async (type, objectMode, final, output, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 60why, one step back
60 const {stdout} = await execaMethod('noop.js', {stdout: convertTransformToFinal(generatorsMap[type].noYield(objectMode), final)});

TweaksSindre Sorhusbefore adoption698a1b8

line 61why, one step back
61 assert.deepEqual(stdout, output);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 62–63why, one step back
62};
63

TweaksSindre Sorhusbefore adoption698a1b8

lines 64–75why, one step back
64test('Generator can filter "transform" by not calling yield', () => testNoYield('generator', false, false, '', execa));
65test('Generator can filter "transform" by not calling yield, objectMode', () => testNoYield('generator', true, false, [], execa));66test('Generator can filter "final" by not calling yield', () => testNoYield('generator', false, true, '', execa));67test('Generator can filter "final" by not calling yield, objectMode', () => testNoYield('generator', true, true, [], execa));68test('Generator can filter "transform" by not calling yield, sync', () => testNoYield('generator', false, false, '', execaSync));69test('Generator can filter "transform" by not calling yield, objectMode, sync', () => testNoYield('generator', true, false, [], execaSync));70test('Generator can filter "final" by not calling yield, sync', () => testNoYield('generator', false, true, '', execaSync));71test('Generator can filter "final" by not calling yield, objectMode, sync', () => testNoYield('generator', true, true, [], execaSync));72test('Duplex can filter by not calling push', () => testNoYield('duplex', false, false, '', execa));73test('Duplex can filter by not calling push, objectMode', () => testNoYield('duplex', true, false, [], execa));74test('WebTransform can filter by not calling push', () => testNoYield('webTransform', false, false, '', execa));75test('WebTransform can filter by not calling push, objectMode', () => testNoYield('webTransform', true, false, [], execa));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 76why, one step back
76

TweaksSindre Sorhusbefore adoption698a1b8

line 77why, one step back
77const testMultipleYields = async (type, final, binary) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 78–79why, one step back
78 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: convertTransformToFinal(generatorsMap[type].multipleYield(), final)});
79 const newline = binary ? '' : '\n';

TweaksSindre Sorhusbefore adoption698a1b8

line 80why, one step back
80 assert.equal(stdout, `${prefix}${newline}${foobarString}${newline}${suffix}`);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 81–82why, one step back
81};
82

TweaksSindre Sorhusbefore adoption698a1b8

lines 83–86why, one step back
83test('Generator can yield "transform" multiple times at different moments', () => testMultipleYields('generator', false, false));
84test('Generator can yield "final" multiple times at different moments', () => testMultipleYields('generator', true, false));85test('Duplex can push multiple times at different moments', () => testMultipleYields('duplex', false, true));86test('WebTransform can push multiple times at different moments', () => testMultipleYields('webTransform', false, true));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 87–104why, one step back
87
88const partsPerChunk = 4;89const chunksPerCall = 10;90const callCount = 5;91const fullString = '\n'.repeat(defaultHighWaterMark / partsPerChunk);9293const yieldFullStrings = function * () {94 yield * Array.from({length: partsPerChunk * chunksPerCall}).fill(fullString);95};9697const manyYieldGenerator = async function * () {98 for (let index = 0; index < callCount; index += 1) {99 yield * yieldFullStrings();100 // eslint-disable-next-line no-await-in-loop101 await scheduler.yield();102 }103};104

TweaksSindre Sorhusbefore adoption698a1b8

line 105why, one step back
105const testManyYields = async final => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 106–108why, one step back
106 const subprocess = execa('noop.js', {stdout: convertTransformToFinal(manyYieldGenerator, final), stripFinalNewline: false});
107 const [chunks, {stdout}] = await Promise.all([getStreamAsArray(subprocess.stdout), subprocess]);108 const expectedChunk = Buffer.from(fullString);

TweaksSindre Sorhusbefore adoption698a1b8

lines 109–110why, one step back
109 assert.deepEqual(chunks, Array.from({length: callCount * partsPerChunk * chunksPerCall}).fill(expectedChunk));
110 assert.equal(chunks.join(''), stdout);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 111–112why, one step back
111};
112

TweaksSindre Sorhusbefore adoption698a1b8

lines 113–114why, one step back
113test('Generator "transform" yields are sent right away', () => testManyYields(false));
114test('Generator "final" yields are sent right away', () => testManyYields(true));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 115why, one step back
115

TweaksSindre Sorhusbefore adoption698a1b8

line 116why, one step back
116const testMaxBuffer = async type => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 117–121why, one step back
117 const bigString = '.'.repeat(maxBuffer);
118 const {stdout} = await execa('noop.js', {119 maxBuffer,120 stdout: generatorsMap[type].getOutput(bigString)(false, true),121 });

TweaksSindre Sorhusbefore adoption698a1b8

line 122why, one step back
122 assert.equal(stdout, bigString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 123why, one step back
123

TweaksSindre Sorhusbefore adoption698a1b8

line 124why, one step back
124 const {isMaxBuffer, shortMessage} = await assertRejects(execa('noop.js', {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 125–127why, one step back
125 maxBuffer,
126 stdout: generatorsMap[type].getOutput(`${bigString}.`)(false, true),127 }));

TweaksSindre Sorhusbefore adoption698a1b8

lines 128–129why, one step back
128 assert.equal(isMaxBuffer, true);
129 assertErrorMessage(shortMessage);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 130–131why, one step back
130};
131

TweaksSindre Sorhusbefore adoption698a1b8

lines 132–134why, one step back
132test('Generators take "maxBuffer" into account', () => testMaxBuffer('generator'));
133test('Duplexes take "maxBuffer" into account', () => testMaxBuffer('duplex'));134test('WebTransforms take "maxBuffer" into account', () => testMaxBuffer('webTransform'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 135why, one step back
135

TweaksSindre Sorhusbefore adoption698a1b8

line 136why, one step back
136test('Generators does not take "maxBuffer" into account, sync', () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 137–141why, one step back
137 const bigString = '.'.repeat(maxBuffer);
138 const {isMaxBuffer, stdout} = execaSync('noop.js', {139 maxBuffer,140 stdout: generatorsMap.generator.getOutput(`${bigString}.`)(false, true),141 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 142–143why, one step back
142 assert.equal(isMaxBuffer, false);
143 assert.equal(stdout.length, maxBuffer + 1);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 144–145why, one step back
144});
145

TweaksSindre Sorhusbefore adoption698a1b8

line 146why, one step back
146const testMaxBufferObject = async type => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 147–151why, one step back
147 const bigArray = Array.from({length: maxBuffer}).fill('..');
148 const {stdout} = await execa('noop.js', {149 maxBuffer,150 stdout: generatorsMap[type].getOutputs(bigArray)(true, true),151 });

TweaksSindre Sorhusbefore adoption698a1b8

line 152why, one step back
152 assert.equal(stdout.length, maxBuffer);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 153why, one step back
153

TweaksSindre Sorhusbefore adoption698a1b8

line 154why, one step back
154 const {isMaxBuffer, shortMessage} = await assertRejects(execa('noop.js', {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 155–157why, one step back
155 maxBuffer,
156 stdout: generatorsMap[type].getOutputs([...bigArray, ''])(true, true),157 }));

TweaksSindre Sorhusbefore adoption698a1b8

lines 158–159why, one step back
158 assert.equal(isMaxBuffer, true);
159 assertErrorMessage(shortMessage, {unit: 'objects'});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 160–161why, one step back
160};
161

TweaksSindre Sorhusbefore adoption698a1b8

lines 162–164why, one step back
162test('Generators take "maxBuffer" into account, objectMode', () => testMaxBufferObject('generator'));
163test('Duplexes take "maxBuffer" into account, objectMode', () => testMaxBufferObject('duplex'));164test('WebTransforms take "maxBuffer" into account, objectMode', () => testMaxBufferObject('webTransform'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 165why, one step back
165

TweaksSindre Sorhusbefore adoption698a1b8

line 166why, one step back
166test('Generators does not take "maxBuffer" into account, objectMode, sync', () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 167–171why, one step back
167 const bigArray = Array.from({length: maxBuffer}).fill('..');
168 const {isMaxBuffer, stdout} = execaSync('noop.js', {169 maxBuffer,170 stdout: generatorsMap.generator.getOutputs([...bigArray, ''])(true, true),171 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 172–173why, one step back
172 assert.equal(isMaxBuffer, false);
173 assert.equal(stdout.length, maxBuffer + 1);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 174–175why, one step back
174});
175

TweaksSindre Sorhusbefore adoption698a1b8

line 176why, one step back
176const testAsyncGenerators = async (type, final) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 177–179why, one step back
177 const {stdout} = await execa('noop.js', {
178 stdout: convertTransformToFinal(generatorsMap[type].timeout(1e2)(), final),179 });

TweaksSindre Sorhusbefore adoption698a1b8

line 180why, one step back
180 assert.equal(stdout, foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 181–182why, one step back
181};
182

TweaksSindre Sorhusbefore adoption698a1b8

lines 183–186why, one step back
183test('Generators "transform" is awaited on success', () => testAsyncGenerators('generator', false));
184test('Generators "final" is awaited on success', () => testAsyncGenerators('generator', true));185test('Duplex is awaited on success', () => testAsyncGenerators('duplex', false));186test('WebTransform is awaited on success', () => testAsyncGenerators('webTransform', false));