direction.js
This file
Path
test/stdio/direction.js
242 lines · 15.3 KiB · JavaScript
Last changed by
Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 2–3why, one step back2import {readFile, writeFile, rm} from 'node:fs/promises';3import process from 'node:process';TweaksSindre Sorhusbefore adoption698a1b8
line 4why, one step back4import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 5why, one step back5import tempfile from 'tempfile';TweaksSindre Sorhusbefore adoption698a1b8
line 6why, one step back6import {assertThrows} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 7–10why, one step back7import {execa, execaSync} from '../../index.js';8import {getStdio} from '../helpers/stdio.js';9import {setFixtureDirectory} from '../helpers/fixtures-directory.js';10import {foobarString} from '../helpers/input.js';Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
line 11why, one step back11import {noopReadable, noopWritable} from '../helpers/stream.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 12–14why, one step back1213setFixtureDirectory();14TweaksSindre Sorhusbefore adoption698a1b8
lines 15–16why, one step back15const testInputOutput = (stdioOption, execaMethod) => {16 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 17–20why, one step back17 execaMethod('empty.js', getStdio(3, [new ReadableStream(), stdioOption]));18 }, {message: /readable and writable/});19};20TweaksSindre Sorhusbefore adoption698a1b8
lines 21–30why, one step back21test('Cannot pass both readable and writable values to stdio[*] - WritableStream', () => testInputOutput(new WritableStream(), execa));22test('Cannot pass both readable and writable values to stdio[*] - 1', () => testInputOutput(1, execa));23test('Cannot pass both readable and writable values to stdio[*] - 2', () => testInputOutput(2, execa));24test('Cannot pass both readable and writable values to stdio[*] - process.stdout', () => testInputOutput(process.stdout, execa));25test('Cannot pass both readable and writable values to stdio[*] - process.stderr', () => testInputOutput(process.stderr, execa));26test('Cannot pass both readable and writable values to stdio[*] - WritableStream - sync', () => testInputOutput(new WritableStream(), execaSync));27test('Cannot pass both readable and writable values to stdio[*] - 1 - sync', () => testInputOutput(1, execaSync));28test('Cannot pass both readable and writable values to stdio[*] - 2 - sync', () => testInputOutput(2, execaSync));29test('Cannot pass both readable and writable values to stdio[*] - process.stdout - sync', () => testInputOutput(process.stdout, execaSync));30test('Cannot pass both readable and writable values to stdio[*] - process.stderr - sync', () => testInputOutput(process.stderr, execaSync));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 31why, one step back31TweaksSindre Sorhusbefore adoption698a1b8
line 32why, one step back32const testAmbiguousDirection = async execaMethod => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 33–34why, one step back33 const [filePathOne, filePathTwo] = [tempfile(), tempfile()];34 await execaMethod('noop-fd.js', ['3', foobarString], getStdio(3, [{file: filePathOne}, {file: filePathTwo}]));TweaksSindre Sorhusbefore adoption698a1b8
line 35why, one step back35 assert.deepEqual(100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 36–41why, one step back36 await Promise.all([readFile(filePathOne, 'utf8'), readFile(filePathTwo, 'utf8')]),37 [foobarString, foobarString],38 );39 await Promise.all([rm(filePathOne), rm(filePathTwo)]);40};41TweaksSindre Sorhusbefore adoption698a1b8
lines 42–43why, one step back42test('stdio[*] default direction is output', () => testAmbiguousDirection(execa));43test('stdio[*] default direction is output - sync', () => testAmbiguousDirection(execaSync));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 44why, one step back44TweaksSindre Sorhusbefore adoption698a1b8
line 45why, one step back45const testAmbiguousMultiple = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 46–48why, one step back46 const filePath = tempfile();47 await writeFile(filePath, foobarString);48 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [{file: filePath}, ['foo', 'bar']]));TweaksSindre Sorhusbefore adoption698a1b8
line 49why, one step back49 assert.equal(stdout, `${foobarString}${foobarString}`);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 50–52why, one step back50 await rm(filePath);51};52TweaksSindre Sorhusbefore adoption698a1b8
lines 53–54why, one step back53test('stdin ambiguous direction is influenced by other values', () => testAmbiguousMultiple(0));54test('stdio[*] ambiguous direction is influenced by other values', () => testAmbiguousMultiple(3));Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
line 55why, one step back55TweaksSindre Sorhusbefore adoption698a1b8
line 56why, one step back56const testDirectionInputPipe = async stdioOption => {Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 57–59why, one step back57 const subprocess = execa('stdin-fd.js', ['3'], getStdio(3, stdioOption));58 subprocess.stdio[3].end(foobarString);59 const {stdout} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 60why, one step back60 assert.equal(stdout, foobarString);Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 61–62why, one step back61};62TweaksSindre Sorhusbefore adoption698a1b8
lines 63–64why, one step back63test('stdio[*] { value: "pipe", input: true } sets the direction to input', () => testDirectionInputPipe({value: 'pipe', input: true}));64test('stdio[*] { value: "overlapped", input: true } sets the direction to input', () => testDirectionInputPipe({value: 'overlapped', input: true}));Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
line 65why, one step back65Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
lines 66–69why, one step back66const uppercaseGenerator = function * (line) {67 yield line.toUpperCase();68};69TweaksSindre Sorhusbefore adoption698a1b8
line 70why, one step back70test('stdio[*] { value: generator, input: true } sets the direction to input', async () => {Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
lines 71–73why, one step back71 const subprocess = execa('stdin-fd.js', ['3'], getStdio(3, {value: uppercaseGenerator, input: true}));72 subprocess.stdio[3].end(foobarString);73 const {stdout} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 74why, one step back74 assert.equal(stdout, foobarString.toUpperCase());Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
lines 75–76why, one step back75});76TweaksSindre Sorhusbefore adoption698a1b8
line 77why, one step back77test('stdio[*] { value: {file}, input: true } sets the direction to input', async () => {Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
lines 78–80why, one step back78 const filePath = tempfile();79 await writeFile(filePath, foobarString);80 const {stdout} = await execa('stdin-fd.js', ['3'], getStdio(3, {value: {file: filePath}, input: true}));TweaksSindre Sorhusbefore adoption698a1b8
line 81why, one step back81 assert.equal(stdout, foobarString);Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
lines 82–84why, one step back82 await rm(filePath);83});84TweaksSindre Sorhusbefore adoption698a1b8
lines 85–86why, one step back85const testInputFixedDirection = (stdioOption, execaMethod) => {86 assertThrows(() => {Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
lines 87–90why, one step back87 execaMethod('empty.js', getStdio(3, {value: stdioOption, input: true}));88 }, {message: /cannot be used with a writable value/});89};90TweaksSindre Sorhusbefore adoption698a1b8
lines 91–94why, one step back91test('stdio[*] { value: WritableStream, input: true } is invalid', () => testInputFixedDirection(new WritableStream(), execa));92test('stdio[*] { value: WritableStream, input: true } is invalid - sync', () => testInputFixedDirection(new WritableStream(), execaSync));93test('stdio[*] { value: process.stdout, input: true } is invalid', () => testInputFixedDirection(process.stdout, execa));94test('stdio[*] { value: process.stdout, input: true } is invalid - sync', () => testInputFixedDirection(process.stdout, execaSync));Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
line 95why, one step back95TweaksSindre Sorhusbefore adoption698a1b8
lines 96–97why, one step back96test('stdio[*] { value: "pipe", input: true } cannot be used in sync mode', () => {97 assertThrows(() => {Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 98–101why, one step back98 execaSync('empty.js', getStdio(3, {value: 'pipe', input: true}));99 }, {message: /can be an input pipe with synchronous methods/});100});101TweaksSindre Sorhusbefore adoption698a1b8
lines 102–103why, one step back102test('stdio[*] { value: "pipe", input: true } cannot be used in sync mode with buffer false', () => {103 assertThrows(() => {Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 104–107why, one step back104 execaSync('empty.js', {...getStdio(3, {value: 'pipe', input: true}), buffer: false});105 }, {message: /can be an input pipe with synchronous methods/});106});107Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 108–142why, one step back108// Nothing could write to that file descriptor after spawning, so the subprocess would hang reading it109const testInputTransformSync = stdioOption => {110 assertThrows(() => {111 execaSync('stdin-fd.js', ['3'], getStdio(3, stdioOption));112 }, {message: /Only the `stdin` option, not `stdio\[3\]`, can be an input transform with synchronous methods/});113};114115test('stdio[*] { value: generator, input: true } cannot be used in sync mode', () => testInputTransformSync({116 * value(line) {117 yield line;118 },119 input: true,120}));121test('stdio[*] { value: {transform}, input: true } cannot be used in sync mode', () => testInputTransformSync({122 value: {123 * transform(line) {124 yield line;125 },126 },127 input: true,128}));129130test('stdin { value: generator, input: true } can be used in sync mode', () => {131 const {stdout} = execaSync('stdin.js', {132 stdin: {133 * value(line) {134 yield line.toUpperCase();135 },136 input: true,137 },138 input: foobarString,139 });140 assert.equal(stdout, foobarString.toUpperCase());141});142TweaksSindre Sorhusbefore adoption698a1b8
line 143why, one step back143const testDirectionOutputPipe = async (stdioOption, execaMethod) => {Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
line 144why, one step back144 const {stdio} = await execaMethod('noop-fd.js', ['3', foobarString], getStdio(3, stdioOption));TweaksSindre Sorhusbefore adoption698a1b8
line 145why, one step back145 assert.equal(stdio[3], foobarString);Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 146–147why, one step back146};147TweaksSindre Sorhusbefore adoption698a1b8
lines 148–151why, one step back148test('stdio[*] { value: "pipe" } keeps the default output direction', () => testDirectionOutputPipe({value: 'pipe'}, execa));149test('stdio[*] { value: "pipe" } keeps the default output direction - sync', () => testDirectionOutputPipe({value: 'pipe'}, execaSync));150test('stdio[*] { value: "pipe", input: false } keeps the default output direction', () => testDirectionOutputPipe({value: 'pipe', input: false}, execa));151test('stdio[*] { value: "pipe", input: false } keeps the default output direction - sync', () => testDirectionOutputPipe({value: 'pipe', input: false}, execaSync));Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
line 152why, one step back152TweaksSindre Sorhusbefore adoption698a1b8
lines 153–154why, one step back153const testDirectionConflict = execaMethod => {154 assertThrows(() => {Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 155–158why, one step back155 execaMethod('empty.js', getStdio(3, [{value: 'pipe', input: true}, 1]));156 }, {message: /readable and writable/});157};158TweaksSindre Sorhusbefore adoption698a1b8
lines 159–160why, one step back159test('Cannot pass { value: "pipe", input: true } with a writable value to stdio[*]', () => testDirectionConflict(execa));160test('Cannot pass { value: "pipe", input: true } with a writable value to stdio[*] - sync', () => testDirectionConflict(execaSync));Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
line 161why, one step back161TweaksSindre Sorhusbefore adoption698a1b8
lines 162–163why, one step back162const testInputBoolean = execaMethod => {163 assertThrows(() => {Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 164–167why, one step back164 execaMethod('empty.js', getStdio(3, {value: 'pipe', input: 'yes'}));165 }, {message: /`stdio\[3\]\.input` option must use a boolean/});166};167TweaksSindre Sorhusbefore adoption698a1b8
lines 168–169why, one step back168test('stdio[*].input must be a boolean', () => testInputBoolean(execa));169test('stdio[*].input must be a boolean - sync', () => testInputBoolean(execaSync));Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
line 170why, one step back170TweaksSindre Sorhusbefore adoption698a1b8
lines 171–172why, one step back171test('stdout.input must be a boolean with buffer false - sync', () => {172 assertThrows(() => {Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 173–175why, one step back173 execaSync('empty.js', {stdout: {value: 'pipe', input: 'yes'}, buffer: false});174 }, {message: /`stdout\.input` option must use a boolean/});175});Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
lines 176–181why, one step back176177/*178`stdin`/`stdout`/`stderr` have a fixed direction, so values that are intrinsically the other direction179are invalid, like with additional file descriptors.180Otherwise, asynchronous methods pipe in the wrong direction and crash, e.g. with `dest.end is not a function`.181*/TweaksSindre Sorhusbefore adoption698a1b8
lines 182–183why, one step back182const testFixedDirection = (options, expectedMessage, execaMethod) => {183 assertThrows(() => {Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
lines 184–189why, one step back184 execaMethod('empty.js', options);185 }, {message: expectedMessage});186};187188const readableOutputMessage = /a readable value is always an input, but `std(?:out|err)` is an output/;189TweaksSindre Sorhusbefore adoption698a1b8
lines 190–195why, one step back190test('stdout cannot use a readable Node.js stream', () => testFixedDirection({stdout: [noopReadable(), 'pipe']}, readableOutputMessage, execa));191test('stdout cannot use a readable Node.js stream - sync', () => testFixedDirection({stdout: [noopReadable(), 'pipe']}, readableOutputMessage, execaSync));192test('stderr cannot use a readable Node.js stream', () => testFixedDirection({stderr: [noopReadable(), 'pipe']}, readableOutputMessage, execa));193test('stdout cannot use a readable web stream', () => testFixedDirection({stdout: new ReadableStream()}, readableOutputMessage, execa));194test('stdout cannot use a readable web stream - sync', () => testFixedDirection({stdout: new ReadableStream()}, readableOutputMessage, execaSync));195test('stdout { value: readable, input: true } is invalid', () => testFixedDirection({stdout: {value: noopReadable(), input: true}}, readableOutputMessage, execa));Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
line 196why, one step back196TweaksSindre Sorhusbefore adoption698a1b8
lines 197–200why, one step back197test('stdin cannot use a writable Node.js stream', () => testFixedDirection({stdin: [noopWritable(), 'pipe']}, /a writable value is always an output/, execa));198test('stdin cannot use a writable Node.js stream - sync', () => testFixedDirection({stdin: [noopWritable(), 'pipe']}, /a writable value is always an output/, execaSync));199test('stdin cannot use a writable web stream', () => testFixedDirection({stdin: new WritableStream()}, /a writable value is always an output/, execa));200test('stdin cannot use a writable web stream - sync', () => testFixedDirection({stdin: new WritableStream()}, /a writable value is always an output/, execaSync));Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
line 201why, one step back201TweaksSindre Sorhusbefore adoption698a1b8
lines 202–211why, one step back202test('stdout { value: writable, input: true } is invalid', () => testFixedDirection({stdout: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execa));203test('stdout { value: writable, input: true } is invalid - sync', () => testFixedDirection({stdout: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execaSync));204test('stdout { value: readable, input: true } is invalid - sync', () => testFixedDirection({stdout: {value: noopReadable(), input: true}}, readableOutputMessage, execaSync));205test('stderr cannot use a readable Node.js stream - sync', () => testFixedDirection({stderr: [noopReadable(), 'pipe']}, readableOutputMessage, execaSync));206test('stderr cannot use a readable web stream', () => testFixedDirection({stderr: new ReadableStream()}, readableOutputMessage, execa));207test('stderr cannot use a readable web stream - sync', () => testFixedDirection({stderr: new ReadableStream()}, readableOutputMessage, execaSync));208test('stderr { value: readable, input: true } is invalid', () => testFixedDirection({stderr: {value: noopReadable(), input: true}}, readableOutputMessage, execa));209test('stderr { value: readable, input: true } is invalid - sync', () => testFixedDirection({stderr: {value: noopReadable(), input: true}}, readableOutputMessage, execaSync));210test('stderr { value: web readable, input: true } is invalid', () => testFixedDirection({stderr: {value: new ReadableStream(), input: true}}, readableOutputMessage, execa));211test('stderr { value: web readable, input: true } is invalid - sync', () => testFixedDirection({stderr: {value: new ReadableStream(), input: true}}, readableOutputMessage, execaSync));Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
line 212why, one step back212TweaksSindre Sorhusbefore adoption698a1b8
lines 213–216why, one step back213test('stdin { value: writable, input: true } is invalid', () => testFixedDirection({stdin: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execa));214test('stdin { value: writable, input: true } is invalid - sync', () => testFixedDirection({stdin: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execaSync));215test('stdin { value: writable web stream, input: true } is invalid', () => testFixedDirection({stdin: {value: new WritableStream(), input: true}}, /cannot be used with a writable value/, execa));216test('stdin { value: writable web stream, input: true } is invalid - sync', () => testFixedDirection({stdin: {value: new WritableStream(), input: true}}, /cannot be used with a writable value/, execaSync));Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
lines 217–219why, one step back217218const writableStdinMessage = /a writable value is always an output/;219TweaksSindre Sorhusbefore adoption698a1b8
lines 220–225why, one step back220test('stdin cannot use a writable Node.js stream after another value', () => testFixedDirection({stdin: ['pipe', noopWritable()]}, writableStdinMessage, execa));221test('stdin cannot use a writable Node.js stream after another value - sync', () => testFixedDirection({stdin: ['pipe', noopWritable()]}, writableStdinMessage, execaSync));222test('stdin cannot use a writable web stream in an array', () => testFixedDirection({stdin: [new WritableStream(), 'pipe']}, writableStdinMessage, execa));223test('stdin cannot use a writable web stream in an array - sync', () => testFixedDirection({stdin: [new WritableStream(), 'pipe']}, writableStdinMessage, execaSync));224test('stdin cannot use both readable and writable values', () => testFixedDirection({stdin: [noopReadable(), noopWritable()]}, writableStdinMessage, execa));225test('stdin cannot use both readable and writable values - sync', () => testFixedDirection({stdin: [noopReadable(), noopWritable()]}, writableStdinMessage, execaSync));Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
line 226why, one step back226TweaksSindre Sorhusbefore adoption698a1b8
lines 227–231why, one step back227test('stdout cannot use a readable Node.js stream after another value', () => testFixedDirection({stdout: ['pipe', noopReadable()]}, readableOutputMessage, execa));228test('stdout cannot use a readable Node.js stream after another value - sync', () => testFixedDirection({stdout: ['pipe', noopReadable()]}, readableOutputMessage, execaSync));229test('stdout cannot use a readable web stream in an array', () => testFixedDirection({stdout: [new ReadableStream(), 'pipe']}, readableOutputMessage, execa));230test('stdout cannot use a readable web stream in an array - sync', () => testFixedDirection({stdout: [new ReadableStream(), 'pipe']}, readableOutputMessage, execaSync));231Report `null` pipe options as a rejectionSindre Sorhusbefore adoptionecda56b
line 232why, one step back232// A single standard file descriptor or stream is passed as is to `child_process.spawn()`, which handles it, so its direction is not checkedTweaksSindre Sorhusbefore adoption698a1b8
lines 233–242why, one step back233const testSingleNativeOtherDirection = async (options, execaMethod) => {234 const {exitCode} = await execaMethod('empty.js', options);235 assert.equal(exitCode, 0);236};237238test('stdin can be a single output file descriptor', () => testSingleNativeOtherDirection({stdin: 1}, execa));239test('stdin can be a single output file descriptor - sync', () => testSingleNativeOtherDirection({stdin: 1}, execaSync));240test('stdout can be a single input file descriptor', () => testSingleNativeOtherDirection({stdout: 0}, execa));241test('stdout can be a single input file descriptor - sync', () => testSingleNativeOtherDirection({stdout: 0}, execaSync));242test('stdout can be process.stdin', () => testSingleNativeOtherDirection({stdout: process.stdin}, execa));