Fix tests about iterables (#636)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 189d486 · Browse files · 3 files·+38 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+38 added−32 removed
- tests:+38 added−32 removed
Provenance
- Commit
6ea7cd7- Tree
a1e579f- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
189d486
3 files changed+38 added−32 removed
added:test/helpers/generator.js+23 added
1234567891011121314151617181920212223import {setTimeout} from 'node:timers/promises';
export const stringGenerator = function * () { yield * ['foo', 'bar'];};
const textEncoder = new TextEncoder();const binaryFoo = textEncoder.encode('foo');const binaryBar = textEncoder.encode('bar');
export const binaryGenerator = function * () { yield * [binaryFoo, binaryBar];};
export const asyncGenerator = async function * () { await setTimeout(0); yield * ['foo', 'bar'];};
// eslint-disable-next-line require-yieldexport const throwingGenerator = function * () { throw new Error('generator error');};modified:test/stdio/input.js+3 added-2 removed
2 unmodified lines345678910114 unmodified lines16171819192020212223242542 unmodified lines2 unmodified linesimport test from 'ava';import {execa, execaSync, $} from '../../index.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {getStdinOption, getPlainStdioOption, getScriptSync, identity} from '../helpers/stdio.js';import {stringGenerator} from '../helpers/generator.js';
setFixtureDir();
const textEncoder = new TextEncoder();4 unmodified lines execa('stdin.js', {stdin, [inputName]: 'foobar'}); }, {message: new RegExp(`\`${inputName}\` and \`stdin\` options`)});};
test('stdin option cannot be an iterable when "input" is used', testInputOptionError, ['foo', 'bar'], 'input');test('stdin option cannot be an iterable when "inputFile" is used', testInputOptionError, ['foo', 'bar'], 'inputFile');test('stdin option cannot be an iterable when "input" is used', testInputOptionError, stringGenerator(), 'input');test('stdin option cannot be an iterable when "inputFile" is used', testInputOptionError, stringGenerator(), 'inputFile');test('stdin option cannot be a file URL when "input" is used', testInputOptionError, pathToFileURL('unknown'), 'input');test('stdin option cannot be a file URL when "inputFile" is used', testInputOptionError, pathToFileURL('unknown'), 'inputFile');test('stdin option cannot be a file path when "input" is used', testInputOptionError, './unknown', 'input');test('stdin option cannot be a file path when "inputFile" is used', testInputOptionError, './unknown', 'inputFile');42 unmodified linesmodified:test/stdio/iterable.js+12 added-30 removed
12345678891011121314151617181920212223249101112133031323334353637141516171819202122232425264546474827282930313233343 unmodified lines383940416042434445463 unmodified linesimport test from 'ava';import {execa, execaSync} from '../../index.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption} from '../helpers/stdio.js';import {stringGenerator, binaryGenerator, asyncGenerator, throwingGenerator} from '../helpers/generator.js';
setFixtureDir();
const textEncoder = new TextEncoder();const binaryFoo = textEncoder.encode('foo');const binaryBar = textEncoder.encode('bar');
const stringGenerator = function * () { yield * ['foo', 'bar'];};
const binaryGenerator = function * () { yield * [binaryFoo, binaryBar];};
// eslint-disable-next-line require-yieldconst throwingGenerator = function * () { throw new Error('generator error');};
const testIterable = async (t, stdioOption, fixtureName, getOptions) => { const {stdout} = await execa(fixtureName, getOptions(stdioOption)); t.is(stdout, 'foobar');};
test('stdin option can be a sync iterable of strings', testIterable, ['foo', 'bar'], 'stdin.js', getStdinOption);test('stdio[*] option can be a sync iterable of strings', testIterable, ['foo', 'bar'], 'stdin-fd3.js', getStdioOption);test('stdin option can be a sync iterable of Uint8Arrays', testIterable, [binaryFoo, binaryBar], 'stdin.js', getStdinOption);test('stdio[*] option can be a sync iterable of Uint8Arrays', testIterable, [binaryFoo, binaryBar], 'stdin-fd3.js', getStdioOption);test('stdin option can be an sync iterable of strings', testIterable, stringGenerator(), 'stdin.js', getStdinOption);test('stdio[*] option can be an sync iterable of strings', testIterable, stringGenerator(), 'stdin-fd3.js', getStdioOption);test('stdin option can be an sync iterable of Uint8Arrays', testIterable, binaryGenerator(), 'stdin.js', getStdinOption);test('stdio[*] option can be an sync iterable of Uint8Arrays', testIterable, binaryGenerator(), 'stdin-fd3.js', getStdioOption);test('stdin option can be an iterable of strings', testIterable, stringGenerator(), 'stdin.js', getStdinOption);test('stdio[*] option can be an iterable of strings', testIterable, stringGenerator(), 'stdin-fd3.js', getStdioOption);test('stdin option can be an iterable of Uint8Arrays', testIterable, binaryGenerator(), 'stdin.js', getStdinOption);test('stdio[*] option can be an iterable of Uint8Arrays', testIterable, binaryGenerator(), 'stdin-fd3.js', getStdioOption);test('stdin option can be an async iterable', testIterable, asyncGenerator(), 'stdin.js', getStdinOption);test('stdio[*] option can be an async iterable', testIterable, asyncGenerator(), 'stdin-fd3.js', getStdioOption);
const testIterableSync = (t, stdioOption, fixtureName, getOptions) => { t.throws(() => { execaSync(fixtureName, getOptions(stdioOption)); }, {message: /an iterable in sync mode/});};
test('stdin option cannot be a sync iterable - sync', testIterableSync, ['foo', 'bar'], 'stdin.js', getStdinOption);test('stdio[*] option cannot be a sync iterable - sync', testIterableSync, ['foo', 'bar'], 'stdin-fd3.js', getStdioOption);test('stdin option cannot be an async iterable - sync', testIterableSync, stringGenerator(), 'stdin.js', getStdinOption);test('stdio[*] option cannot be an async iterable - sync', testIterableSync, stringGenerator(), 'stdin-fd3.js', getStdioOption);test('stdin option cannot be a sync iterable - sync', testIterableSync, stringGenerator(), 'stdin.js', getStdinOption);test('stdio[*] option cannot be a sync iterable - sync', testIterableSync, stringGenerator(), 'stdin-fd3.js', getStdioOption);test('stdin option cannot be an async iterable - sync', testIterableSync, asyncGenerator(), 'stdin.js', getStdinOption);test('stdio[*] option cannot be an async iterable - sync', testIterableSync, asyncGenerator(), 'stdin-fd3.js', getStdioOption);
const testIterableError = async (t, fixtureName, getOptions) => { const {originalMessage} = await t.throwsAsync(execa(fixtureName, getOptions(throwingGenerator()))); t.is(originalMessage, 'generator error');3 unmodified linestest('stdio[*] option handles errors in iterables', testIterableError, 'stdin-fd3.js', getStdioOption);
const testNoIterableOutput = (t, getOptions, execaMethod) => { t.throws(() => { execaMethod('noop.js', getOptions(['foo', 'bar'])); execaMethod('noop.js', getOptions(stringGenerator())); }, {message: /cannot be an iterable/});};
test('stdout option cannot be an iterable', testNoIterableOutput, getStdoutOption, execa);3 unmodified lines