Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–5why, one step back
1import 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 back
6import {execa, execaSync} from '../../index.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 7–8why, one step back
7import {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 back
9import {noopGenerator, serializeGenerator} from '../helpers/generator.js';
10import {foobarObject, foobarObjectString} from '../helpers/input.js';

TweaksSindre Sorhusbefore adoption698a1b8

line 11why, one step back
11import {nestedSubprocess} from '../helpers/nested.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 12–16why, one step back
12
13setFixtureDirectory();1415const getFd3InputMessage = type => `not \`stdio[3]\`, can be ${type}`;16

TweaksSindre Sorhusbefore adoption698a1b8

lines 17–18why, one step back
17const testFd3InputSync = (stdioOption, expectedMessage) => {
18 const {message} = assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 19–20why, one step back
19 execaSync('empty.js', getStdio(3, stdioOption));
20 });

TweaksSindre Sorhusbefore adoption698a1b8

line 21why, one step back
21 assert.ok(message.includes(expectedMessage));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 22–23why, one step back
22};
23

TweaksSindre Sorhusbefore adoption698a1b8

lines 24–25why, one step back
24test('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 back
26
27/*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 back
31const testMultipleSources = async execaMethod => {

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 32–34why, one step back
32 const {stdout} = await execaMethod('stdin.js', {
33 stdin: [['a'], ['b\n'], noopGenerator()],34 });

TweaksSindre Sorhusbefore adoption698a1b8

line 35why, one step back
35 assert.equal(stdout, 'ab');

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 36–37why, one step back
36};
37

TweaksSindre Sorhusbefore adoption698a1b8

lines 38–39why, one step back
38test('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 back
40
41// 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};50

TweaksSindre Sorhusbefore adoption698a1b8

line 51why, one step back
51const testMultipleSourcesFinal = async execaMethod => {

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 52–54why, one step back
52 const {stdout} = await execaMethod('stdin.js', {
53 stdin: [['a'], ['b\n'], finalCountGenerator],54 });

TweaksSindre Sorhusbefore adoption698a1b8

line 55why, one step back
55 assert.equal(stdout, 'ab\n[F]');

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 56–57why, one step back
56};
57

TweaksSindre Sorhusbefore adoption698a1b8

lines 58–59why, one step back
58test('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 back
60
61// Without transforms, multiple sources are simply concatenated, like a single source with multiple chunks

TweaksSindre Sorhusbefore adoption698a1b8

line 62why, one step back
62const testMultipleSourcesNoTransform = async execaMethod => {

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

line 63why, one step back
63 const {stdout} = await execaMethod('stdin.js', {stdin: [['a'], ['b\n']]});

TweaksSindre Sorhusbefore adoption698a1b8

line 64why, one step back
64 assert.equal(stdout, 'ab');

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 65–66why, one step back
65};
66

TweaksSindre Sorhusbefore adoption698a1b8

lines 67–68why, one step back
67test('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 back
69
70// A `\r\n` sequence split across two sources must still be detected, like across two chunks of a single source

TweaksSindre Sorhusbefore adoption698a1b8

line 71why, one step back
71const testWindowsNewlineSpanningSources = async execaMethod => {

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 72–75why, one step back
72 const {stdout} = await execaMethod('stdin.js', {
73 stdin: [['x\r'], ['\ny'], noopGenerator()],74 stripFinalNewline: false,75 });

TweaksSindre Sorhusbefore adoption698a1b8

line 76why, one step back
76 assert.equal(stdout, 'x\r\ny\r\n');

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 77–78why, one step back
77};
78

TweaksSindre Sorhusbefore adoption698a1b8

lines 79–80why, one step back
79test('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 back
81
82// The `input` option is one more source going through the same transforms, in the order the sources were passed

TweaksSindre Sorhusbefore adoption698a1b8

line 83why, one step back
83const testInputOptionWithStdinSource = async execaMethod => {

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 84–87why, one step back
84 const {stdout} = await execaMethod('stdin.js', {
85 stdin: [['a'], noopGenerator()],86 input: 'b',87 });

TweaksSindre Sorhusbefore adoption698a1b8

line 88why, one step back
88 assert.equal(stdout, 'ab');

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 89–90why, one step back
89};
90

TweaksSindre Sorhusbefore adoption698a1b8

lines 91–92why, one step back
91test('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 back
93
94// Object inputs are serialized no matter how many sources they come from

TweaksSindre Sorhusbefore adoption698a1b8

line 95why, one step back
95const testMultipleObjectSources = async execaMethod => {

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 96–98why, one step back
96 const {stdout} = await execaMethod('stdin.js', {
97 stdin: [[foobarObject], [foobarObject], serializeGenerator(true)],98 });

TweaksSindre Sorhusbefore adoption698a1b8

line 99why, one step back
99 assert.equal(stdout, `${foobarObjectString}\n${foobarObjectString}`);

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 100–101why, one step back
100};
101

TweaksSindre Sorhusbefore adoption698a1b8

lines 102–116why, one step back
102test('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}