Skip to content
Myr

Fix synchronous input transforms and `inputFile` errors

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 758768f · Browse files · 5 files+110 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+110 added−11 removed

  • tests:+97 added−2 removed
  • other:+13 added−9 removed

5 files+110 added−11 removed

modified:lib/io/input-sync.js+6 added-5 removed
modified:lib/stdio/handle-sync.js+5 added-3 removed
modified:lib/stdio/native.js+2 added-1 removed
54 unmodified lines
55 if (tty.isatty(targetFdNumber)) {56 throw new TypeError(`The \`${optionName}: ${serializeOptionValue(value)}\` option is invalid: it cannot be a TTY with synchronous methods.`);57 }5859 return {type: 'uint8Array', value: bufferToUint8Array(readFileSync(targetFdNumber)), optionName};59 // The file descriptor is read lazily, like `inputFile`, so that a read failure is reported like any other early error, instead of being thrown as a raw error60 return {type: 'uint8Array', contents: () => [bufferToUint8Array(readFileSync(targetFdNumber))], optionName};61};6263const getTargetFdNumber = (value, fdNumber) => {64 if (value === 'inherit') {
43 unmodified lines
modified:test/io/input-sync.js+81 added-1 removed
modified:test/return/early-error.js+16 added-1 removed
1import {arch} from 'node:os';2import process from 'node:process';3import {finished} from 'node:stream/promises';4import test from 'ava';5import {execa, execaSync, $} from '../../index.js';5import {execa, execaSync, $, ExecaError, ExecaSyncError} from '../../index.js';6import {setFixtureDirectory} from '../helpers/fixtures-directory.js';7import {foobarString, foobarUint8Array} from '../helpers/input.js';8import {fullStdio} from '../helpers/stdio.js';9import {
223 unmodified lines
233test('child_process.spawn() early errors can use .stderr', testEarlyErrorStream, ({stderr}) => stderr);234test('child_process.spawn() early errors can use .stdio[1]', testEarlyErrorStream, ({stdio}) => stdio[1]);235test('child_process.spawn() early errors can use .stdio[3]', testEarlyErrorStream, ({stdio}) => stdio[3], fullStdio);236test('child_process.spawn() early errors can use .all', testEarlyErrorStream, ({all}) => all, {all: true});237238// Reading the `inputFile` happens after the file descriptors are set up, so a missing file is reported as an early error, like with asynchronous methods239test('inputFile which does not exist is an early error', async t => {240 const error = await t.throwsAsync(execa('stdin.js', {inputFile: 'does_not_exist'}));241 t.true(error instanceof ExecaError);242 t.is(error.cause.code, 'ENOENT');243});244245test('inputFile which does not exist is an early error, sync', t => {246 const error = t.throws(() => {247 execaSync('stdin.js', {inputFile: 'does_not_exist'});248 });249 t.true(error instanceof ExecaSyncError);250 t.is(error.cause.code, 'ENOENT');251});