input-sync.js
This file
Path
test/io/input-sync.js
116 lines · 5.1 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117import assert from 'node:assert/strict';import {openSync, closeSync} from 'node:fs';import process from 'node:process';import test from 'node:test';import {assertThrows} from '../helpers/assert.js';import {execa, execaSync} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {getStdio} from '../helpers/stdio.js';import {noopGenerator, serializeGenerator} from '../helpers/generator.js';import {foobarObject, foobarObjectString} from '../helpers/input.js';import {nestedSubprocess} from '../helpers/nested.js';
setFixtureDirectory();
const getFd3InputMessage = type => `not \`stdio[3]\`, can be ${type}`;
const testFd3InputSync = (stdioOption, expectedMessage) => { const {message} = assertThrows(() => { execaSync('empty.js', getStdio(3, stdioOption)); }); assert.ok(message.includes(expectedMessage));};
test('Cannot use Uint8Array with stdio[*], sync', () => testFd3InputSync(new Uint8Array(), getFd3InputMessage('a Uint8Array')));test('Cannot use iterable with stdio[*], sync', () => testFd3InputSync([[]], getFd3InputMessage('an iterable')));
/*With asynchronous methods, all `stdin` sources are merged into a single stream before being transformed, so transform state (including line splitting) spans all sources.Synchronous methods must apply the transform pipeline once over the merged sources, not once per source.*/const testMultipleSources = async execaMethod => { const {stdout} = await execaMethod('stdin.js', { stdin: [['a'], ['b\n'], noopGenerator()], }); assert.equal(stdout, 'ab');};
test('Multiple stdin sources are transformed as a single stream', () => testMultipleSources(execa));test('Multiple stdin sources are transformed as a single stream, sync', () => testMultipleSources(execaSync));
// The generator's `final` function must run once overall, not once per sourceconst finalCountGenerator = { * transform(value) { yield value; }, * final() { yield '[F]'; },};
const testMultipleSourcesFinal = async execaMethod => { const {stdout} = await execaMethod('stdin.js', { stdin: [['a'], ['b\n'], finalCountGenerator], }); assert.equal(stdout, 'ab\n[F]');};
test('Generator "final" runs once with multiple stdin sources', () => testMultipleSourcesFinal(execa));test('Generator "final" runs once with multiple stdin sources, sync', () => testMultipleSourcesFinal(execaSync));
// Without transforms, multiple sources are simply concatenated, like a single source with multiple chunksconst testMultipleSourcesNoTransform = async execaMethod => { const {stdout} = await execaMethod('stdin.js', {stdin: [['a'], ['b\n']]}); assert.equal(stdout, 'ab');};
test('Multiple stdin sources are concatenated without transforms', () => testMultipleSourcesNoTransform(execa));test('Multiple stdin sources are concatenated without transforms, sync', () => testMultipleSourcesNoTransform(execaSync));
// A `\r\n` sequence split across two sources must still be detected, like across two chunks of a single sourceconst testWindowsNewlineSpanningSources = async execaMethod => { const {stdout} = await execaMethod('stdin.js', { stdin: [['x\r'], ['\ny'], noopGenerator()], stripFinalNewline: false, }); assert.equal(stdout, 'x\r\ny\r\n');};
test('Windows newlines can span multiple stdin sources', () => testWindowsNewlineSpanningSources(execa));test('Windows newlines can span multiple stdin sources, sync', () => testWindowsNewlineSpanningSources(execaSync));
// The `input` option is one more source going through the same transforms, in the order the sources were passedconst testInputOptionWithStdinSource = async execaMethod => { const {stdout} = await execaMethod('stdin.js', { stdin: [['a'], noopGenerator()], input: 'b', }); assert.equal(stdout, 'ab');};
test('The input option is transformed together with stdin sources', () => testInputOptionWithStdinSource(execa));test('The input option is transformed together with stdin sources, sync', () => testInputOptionWithStdinSource(execaSync));
// Object inputs are serialized no matter how many sources they come fromconst testMultipleObjectSources = async execaMethod => { const {stdout} = await execaMethod('stdin.js', { stdin: [[foobarObject], [foobarObject], serializeGenerator(true)], }); assert.equal(stdout, `${foobarObjectString}\n${foobarObjectString}`);};
test('Multiple object stdin sources are serialized', () => testMultipleObjectSources(execa));test('Multiple object stdin sources are serialized, sync', () => testMultipleObjectSources(execaSync));
// Like `inputFile`, the inherited file descriptor is read lazily, so that a read failure is reported like any other error, including with `reject: false`if (process.platform !== 'win32') { test('Failing to read stdin: ["inherit", "pipe"] is reported as an Execa error, sync', async () => { const directoryFd = openSync('.', 'r'); try { const {nestedResult} = await nestedSubprocess('empty.js', {isSync: true, stdin: ['inherit', 'pipe'], reject: false}, {stdin: directoryFd}); assert.ok(nestedResult.message.startsWith('Command failed with EISDIR')); } finally { closeSync(directoryFd); } });}