validate.js
This file
Path
test/transform/validate.js
74 lines · 6.1 KiB · JavaScript
Last changed by
Allow passing `Buffer` to the `input` option (#1029)ehmickybefore adoption40fdc75
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–2why, one step back1import test from 'ava';2import {execa, execaSync} from '../../index.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
line 3why, one step back3import {setFixtureDirectory} from '../helpers/fixtures-directory.js';Rename files (#977)ehmickybefore adoption4b2e316
line 4why, one step back4import {getStdio} from '../helpers/stdio.js';Allow passing `Buffer` to the `input` option (#1029)ehmickybefore adoption40fdc75
line 5why, one step back5import {foobarUint8Array, foobarObject} from '../helpers/input.js';Rename files (#977)ehmickybefore adoption4b2e316
lines 6–7why, one step back6import {serializeGenerator, getOutputGenerator, convertTransformToFinal} from '../helpers/generator.js';7Upgrade xo (#980)ehmickybefore adoptionccfa412
line 8why, one step back8setFixtureDirectory();Rename files (#977)ehmickybefore adoption4b2e316
line 9why, one step back9Allow passing `Buffer` to the `input` option (#1029)ehmickybefore adoption40fdc75
lines 10–12why, one step back10const getMessage = input => input === null || input === undefined11 ? 'not be called at all'12 : 'a string or an Uint8Array';Rename files (#977)ehmickybefore adoption4b2e316
lines 13–74why, one step back1314const lastInputGenerator = input => objectMode => [foobarUint8Array, getOutputGenerator(input)(objectMode)];15const inputGenerator = input => objectMode => [...lastInputGenerator(input)(objectMode), serializeGenerator(true)];1617// eslint-disable-next-line max-params18const testGeneratorReturn = async (t, fdNumber, generator, input, objectMode, isInput) => {19 const fixtureName = isInput ? 'stdin-fd.js' : 'noop-fd.js';20 const {message} = await t.throwsAsync(execa(fixtureName, [`${fdNumber}`], getStdio(fdNumber, generator(input)(objectMode))));21 t.true(message.includes(getMessage(input)));22};2324test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputGenerator, foobarObject, false, true);25test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputGenerator, foobarObject, false, true);26test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputGenerator, foobarObject, true, true);27test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputGenerator, foobarObject, true, true);28test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, foobarObject, false, false);29test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, getOutputGenerator, foobarObject, false, false);30test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, getOutputGenerator, foobarObject, false, false);31test('Generators with result.stdin cannot return null if not in objectMode', testGeneratorReturn, 0, inputGenerator, null, false, true);32test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputGenerator, null, true, true);33test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, null, false, false);34test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, getOutputGenerator, null, true, false);35test('Generators with result.stdin cannot return undefined if not in objectMode', testGeneratorReturn, 0, inputGenerator, undefined, false, true);36test('Generators with result.stdin cannot return undefined if in objectMode', testGeneratorReturn, 0, inputGenerator, undefined, true, true);37test('Generators with result.stdout cannot return undefined if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, undefined, false, false);38test('Generators with result.stdout cannot return undefined if in objectMode', testGeneratorReturn, 1, getOutputGenerator, undefined, true, false);3940// eslint-disable-next-line max-params41const testGeneratorReturnSync = (t, fdNumber, generator, input, objectMode, isInput) => {42 const fixtureName = isInput ? 'stdin-fd.js' : 'noop-fd.js';43 const {message} = t.throws(() => {44 execaSync(fixtureName, [`${fdNumber}`], getStdio(fdNumber, generator(input)(objectMode)));45 });46 t.true(message.includes(getMessage(input)));47};4849test('Generators with result.stdin cannot return an object if not in objectMode, sync', testGeneratorReturnSync, 0, inputGenerator, foobarObject, false, true);50test('The last generator with result.stdin cannot return an object even in objectMode, sync', testGeneratorReturnSync, 0, lastInputGenerator, foobarObject, true, true);51test('Generators with result.stdout cannot return an object if not in objectMode, sync', testGeneratorReturnSync, 1, getOutputGenerator, foobarObject, false, false);52test('Generators with result.stderr cannot return an object if not in objectMode, sync', testGeneratorReturnSync, 2, getOutputGenerator, foobarObject, false, false);53test('Generators with result.stdio[*] as output cannot return an object if not in objectMode, sync', testGeneratorReturnSync, 3, getOutputGenerator, foobarObject, false, false);54test('Generators with result.stdin cannot return null if not in objectMode, sync', testGeneratorReturnSync, 0, inputGenerator, null, false, true);55test('Generators with result.stdin cannot return null if in objectMode, sync', testGeneratorReturnSync, 0, inputGenerator, null, true, true);56test('Generators with result.stdout cannot return null if not in objectMode, sync', testGeneratorReturnSync, 1, getOutputGenerator, null, false, false);57test('Generators with result.stdout cannot return null if in objectMode, sync', testGeneratorReturnSync, 1, getOutputGenerator, null, true, false);58test('Generators with result.stdin cannot return undefined if not in objectMode, sync', testGeneratorReturnSync, 0, inputGenerator, undefined, false, true);59test('Generators with result.stdin cannot return undefined if in objectMode, sync', testGeneratorReturnSync, 0, inputGenerator, undefined, true, true);60test('Generators with result.stdout cannot return undefined if not in objectMode, sync', testGeneratorReturnSync, 1, getOutputGenerator, undefined, false, false);61test('Generators with result.stdout cannot return undefined if in objectMode, sync', testGeneratorReturnSync, 1, getOutputGenerator, undefined, true, false);6263test('Generators "final" return value is validated', async t => {64 await t.throwsAsync(65 execa('noop.js', {stdout: convertTransformToFinal(getOutputGenerator(null)(true), true)}),66 {message: /not be called at all/},67 );68});6970test('Generators "final" return value is validated, sync', t => {71 t.throws(() => {72 execaSync('noop.js', {stdout: convertTransformToFinal(getOutputGenerator(null)(true), true)});73 }, {message: /not be called at all/});74});