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
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
lines 1–5why, one step back1import assert from 'node:assert/strict';2import {openSync, closeSync} from 'node:fs';3import process from 'node:process';4import test from 'node:test';5import {assertThrows} from '../helpers/assert.js';Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
line 6why, one step back6import {execa, execaSync} from '../../index.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 7–8why, one step back7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8import {getStdio} from '../helpers/stdio.js';Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 9–10why, one step back9import {noopGenerator, serializeGenerator} from '../helpers/generator.js';10import {foobarObject, foobarObjectString} from '../helpers/input.js';TweaksSindre Sorhusbefore adoption698a1b8
line 11why, one step back11import {nestedSubprocess} from '../helpers/nested.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 12–16why, one step back1213setFixtureDirectory();1415const getFd3InputMessage = type => `not \`stdio[3]\`, can be ${type}`;16TweaksSindre Sorhusbefore adoption698a1b8
lines 17–18why, one step back17const testFd3InputSync = (stdioOption, expectedMessage) => {18 const {message} = assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 19–20why, one step back19 execaSync('empty.js', getStdio(3, stdioOption));20 });TweaksSindre Sorhusbefore adoption698a1b8
line 21why, one step back21 assert.ok(message.includes(expectedMessage));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 22–23why, one step back22};23TweaksSindre Sorhusbefore adoption698a1b8
lines 24–25why, one step back24test('Cannot use Uint8Array with stdio[*], sync', () => testFd3InputSync(new Uint8Array(), getFd3InputMessage('a Uint8Array')));25test('Cannot use iterable with stdio[*], sync', () => testFd3InputSync([[]], getFd3InputMessage('an iterable')));Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 26–30why, one step back2627/*28With asynchronous methods, all `stdin` sources are merged into a single stream before being transformed, so transform state (including line splitting) spans all sources.29Synchronous methods must apply the transform pipeline once over the merged sources, not once per source.30*/TweaksSindre Sorhusbefore adoption698a1b8
line 31why, one step back31const testMultipleSources = async execaMethod => {Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 32–34why, one step back32 const {stdout} = await execaMethod('stdin.js', {33 stdin: [['a'], ['b\n'], noopGenerator()],34 });TweaksSindre Sorhusbefore adoption698a1b8
line 35why, one step back35 assert.equal(stdout, 'ab');Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 36–37why, one step back36};37TweaksSindre Sorhusbefore adoption698a1b8
lines 38–39why, one step back38test('Multiple stdin sources are transformed as a single stream', () => testMultipleSources(execa));39test('Multiple stdin sources are transformed as a single stream, sync', () => testMultipleSources(execaSync));Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 40–50why, one step back4041// The generator's `final` function must run once overall, not once per source42const finalCountGenerator = {43 * transform(value) {44 yield value;45 },46 * final() {47 yield '[F]';48 },49};50TweaksSindre Sorhusbefore adoption698a1b8
line 51why, one step back51const testMultipleSourcesFinal = async execaMethod => {Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 52–54why, one step back52 const {stdout} = await execaMethod('stdin.js', {53 stdin: [['a'], ['b\n'], finalCountGenerator],54 });TweaksSindre Sorhusbefore adoption698a1b8
line 55why, one step back55 assert.equal(stdout, 'ab\n[F]');Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 56–57why, one step back56};57TweaksSindre Sorhusbefore adoption698a1b8
lines 58–59why, one step back58test('Generator "final" runs once with multiple stdin sources', () => testMultipleSourcesFinal(execa));59test('Generator "final" runs once with multiple stdin sources, sync', () => testMultipleSourcesFinal(execaSync));Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 60–61why, one step back6061// Without transforms, multiple sources are simply concatenated, like a single source with multiple chunksTweaksSindre Sorhusbefore adoption698a1b8
line 62why, one step back62const testMultipleSourcesNoTransform = async execaMethod => {Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
line 63why, one step back63 const {stdout} = await execaMethod('stdin.js', {stdin: [['a'], ['b\n']]});TweaksSindre Sorhusbefore adoption698a1b8
line 64why, one step back64 assert.equal(stdout, 'ab');Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 65–66why, one step back65};66TweaksSindre Sorhusbefore adoption698a1b8
lines 67–68why, one step back67test('Multiple stdin sources are concatenated without transforms', () => testMultipleSourcesNoTransform(execa));68test('Multiple stdin sources are concatenated without transforms, sync', () => testMultipleSourcesNoTransform(execaSync));Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 69–70why, one step back6970// A `\r\n` sequence split across two sources must still be detected, like across two chunks of a single sourceTweaksSindre Sorhusbefore adoption698a1b8
line 71why, one step back71const testWindowsNewlineSpanningSources = async execaMethod => {Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 72–75why, one step back72 const {stdout} = await execaMethod('stdin.js', {73 stdin: [['x\r'], ['\ny'], noopGenerator()],74 stripFinalNewline: false,75 });TweaksSindre Sorhusbefore adoption698a1b8
line 76why, one step back76 assert.equal(stdout, 'x\r\ny\r\n');Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 77–78why, one step back77};78TweaksSindre Sorhusbefore adoption698a1b8
lines 79–80why, one step back79test('Windows newlines can span multiple stdin sources', () => testWindowsNewlineSpanningSources(execa));80test('Windows newlines can span multiple stdin sources, sync', () => testWindowsNewlineSpanningSources(execaSync));Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 81–82why, one step back8182// The `input` option is one more source going through the same transforms, in the order the sources were passedTweaksSindre Sorhusbefore adoption698a1b8
line 83why, one step back83const testInputOptionWithStdinSource = async execaMethod => {Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 84–87why, one step back84 const {stdout} = await execaMethod('stdin.js', {85 stdin: [['a'], noopGenerator()],86 input: 'b',87 });TweaksSindre Sorhusbefore adoption698a1b8
line 88why, one step back88 assert.equal(stdout, 'ab');Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 89–90why, one step back89};90TweaksSindre Sorhusbefore adoption698a1b8
lines 91–92why, one step back91test('The input option is transformed together with stdin sources', () => testInputOptionWithStdinSource(execa));92test('The input option is transformed together with stdin sources, sync', () => testInputOptionWithStdinSource(execaSync));Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 93–94why, one step back9394// Object inputs are serialized no matter how many sources they come fromTweaksSindre Sorhusbefore adoption698a1b8
line 95why, one step back95const testMultipleObjectSources = async execaMethod => {Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 96–98why, one step back96 const {stdout} = await execaMethod('stdin.js', {97 stdin: [[foobarObject], [foobarObject], serializeGenerator(true)],98 });TweaksSindre Sorhusbefore adoption698a1b8
line 99why, one step back99 assert.equal(stdout, `${foobarObjectString}\n${foobarObjectString}`);Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 100–101why, one step back100};101TweaksSindre Sorhusbefore adoption698a1b8
lines 102–116why, one step back102test('Multiple object stdin sources are serialized', () => testMultipleObjectSources(execa));103test('Multiple object stdin sources are serialized, sync', () => testMultipleObjectSources(execaSync));104105// Like `inputFile`, the inherited file descriptor is read lazily, so that a read failure is reported like any other error, including with `reject: false`106if (process.platform !== 'win32') {107 test('Failing to read stdin: ["inherit", "pipe"] is reported as an Execa error, sync', async () => {108 const directoryFd = openSync('.', 'r');109 try {110 const {nestedResult} = await nestedSubprocess('empty.js', {isSync: true, stdin: ['inherit', 'pipe'], reject: false}, {stdin: directoryFd});111 assert.ok(nestedResult.message.startsWith('Command failed with EISDIR'));112 } finally {113 closeSync(directoryFd);114 }115 });116}