import {once} from 'node:events'; import {createReadStream, createWriteStream} from 'node:fs'; import {readFile, writeFile, rm} from 'node:fs/promises'; import {Writable, PassThrough} from 'node:stream'; import {text} from 'node:stream/consumers'; import {setImmediate} from 'node:timers/promises'; import {callbackify} from 'node:util'; import test from 'ava'; import tempfile from 'tempfile'; import {execa, execaSync} from '../../index.js'; import {setFixtureDir} from '../helpers/fixtures-dir.js'; import {getStdio} from '../helpers/stdio.js'; import {foobarString} from '../helpers/input.js'; import {noopReadable, noopWritable, noopDuplex, simpleReadable} from '../helpers/stream.js'; setFixtureDir(); const testNoFileStreamSync = async (t, index, stream) => { t.throws(() => { execaSync('empty.js', getStdio(index, stream)); }, {code: 'ERR_INVALID_ARG_VALUE'}); }; test('stdin cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 0, noopReadable()); test('stdin cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 0, noopDuplex()); test('stdout cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 1, noopWritable()); test('stdout cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 1, noopDuplex()); test('stderr cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 2, noopWritable()); test('stderr cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 2, noopDuplex()); test('stdio[*] cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 3, noopReadable()); test('stdio[*] cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 3, noopWritable()); test('stdio[*] cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 3, noopDuplex()); test('input can be a Node.js Readable without a file descriptor', async t => { const {stdout} = await execa('stdin.js', {input: simpleReadable()}); t.is(stdout, foobarString); }); test('input cannot be a Node.js Readable without a file descriptor - sync', t => { t.throws(() => { execaSync('empty.js', {input: simpleReadable()}); }, {message: 'The `input` option cannot be a Node.js stream in sync mode.'}); }); const testNoFileStream = async (t, index, stream) => { await t.throwsAsync(execa('empty.js', getStdio(index, stream)), {code: 'ERR_INVALID_ARG_VALUE'}); }; test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 0, noopReadable()); test('stdin cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 0, noopDuplex()); test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 1, noopWritable()); test('stdout cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 1, noopDuplex()); test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 2, noopWritable()); test('stderr cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 2, noopDuplex()); test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, noopReadable()); test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, noopWritable()); test('stdio[*] cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 3, noopDuplex()); const createFileReadStream = async () => { const filePath = tempfile(); await writeFile(filePath, 'foobar'); const stream = createReadStream(filePath); await once(stream, 'open'); return {stream, filePath}; }; const createFileWriteStream = async () => { const filePath = tempfile(); const stream = createWriteStream(filePath); await once(stream, 'open'); return {stream, filePath}; }; const assertFileStreamError = async (t, childProcess, stream, filePath) => { const error = new Error('test'); stream.destroy(error); t.is(await t.throwsAsync(childProcess), error); t.is(error.exitCode, 0); t.is(error.signal, undefined); await rm(filePath); }; const testFileReadable = async (t, index, execaMethod) => { const {stream, filePath} = await createFileReadStream(); const indexString = index === 'input' ? '0' : `${index}`; const {stdout} = await execaMethod('stdin-fd.js', [indexString], getStdio(index, stream)); t.is(stdout, 'foobar'); await rm(filePath); }; test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'input', execa); test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 0, execa); test('stdio[*] can be a Node.js Readable with a file descriptor', testFileReadable, 3, execa); test('stdin can be a Node.js Readable with a file descriptor - sync', testFileReadable, 0, execaSync); test('stdio[*] can be a Node.js Readable with a file descriptor - sync', testFileReadable, 3, execaSync); const testFileReadableError = async (t, index) => { const {stream, filePath} = await createFileReadStream(); const indexString = index === 'input' ? '0' : `${index}`; const childProcess = execa('stdin-fd.js', [indexString], getStdio(index, stream)); await assertFileStreamError(t, childProcess, stream, filePath); }; test.serial('input handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 'input'); test.serial('stdin handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 0); test.serial('stdio[*] handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 3); const testFileReadableOpen = async (t, index, useSingle, execaMethod) => { const {stream, filePath} = await createFileReadStream(); t.deepEqual(stream.eventNames(), []); const stdioOption = useSingle ? stream : [stream, 'pipe']; await execaMethod('empty.js', getStdio(index, stdioOption)); t.is(stream.readable, useSingle && index !== 'input'); t.deepEqual(stream.eventNames(), []); await rm(filePath); }; test('input closes a Node.js Readable with a file descriptor', testFileReadableOpen, 'input', true, execa); test('stdin leaves open a single Node.js Readable with a file descriptor', testFileReadableOpen, 0, true, execa); test('stdin closes a combined Node.js Readable with a file descriptor', testFileReadableOpen, 0, false, execa); test('stdio[*] leaves open a single Node.js Readable with a file descriptor', testFileReadableOpen, 3, true, execa); test('stdin leaves open a single Node.js Readable with a file descriptor - sync', testFileReadableOpen, 0, true, execaSync); test('stdio[*] leaves open a single Node.js Readable with a file descriptor - sync', testFileReadableOpen, 3, true, execaSync); const testFileWritable = async (t, index, execaMethod) => { const {stream, filePath} = await createFileWriteStream(); await execaMethod('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, stream)); t.is(await readFile(filePath, 'utf8'), 'foobar'); await rm(filePath); }; test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, 1, execa); test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, 2, execa); test('stdio[*] can be a Node.js Writable with a file descriptor', testFileWritable, 3, execa); test('stdout can be a Node.js Writable with a file descriptor - sync', testFileWritable, 1, execaSync); test('stderr can be a Node.js Writable with a file descriptor - sync', testFileWritable, 2, execaSync); test('stdio[*] can be a Node.js Writable with a file descriptor - sync', testFileWritable, 3, execaSync); const testFileWritableError = async (t, index) => { const {stream, filePath} = await createFileWriteStream(); const childProcess = execa('noop-stdin-fd.js', [`${index}`], getStdio(index, stream)); childProcess.stdin.end(foobarString); await assertFileStreamError(t, childProcess, stream, filePath); }; test.serial('stdout handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 1); test.serial('stderr handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 2); test.serial('stdio[*] handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 3); const testFileWritableOpen = async (t, index, useSingle, execaMethod) => { const {stream, filePath} = await createFileWriteStream(); t.deepEqual(stream.eventNames(), []); const stdioOption = useSingle ? stream : [stream, 'pipe']; await execaMethod('empty.js', getStdio(index, stdioOption)); t.is(stream.writable, useSingle); t.deepEqual(stream.eventNames(), []); await rm(filePath); }; test('stdout leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 1, true, execa); test('stdout closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 1, false, execa); test('stderr leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 2, true, execa); test('stderr closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 2, false, execa); test('stdio[*] leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 3, true, execa); test('stdio[*] closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 3, false, execa); test('stdout leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 1, true, execaSync); test('stderr leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 2, true, execaSync); test('stdio[*] leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 3, true, execaSync); const testLazyFileReadable = async (t, index) => { const filePath = tempfile(); await writeFile(filePath, 'foobar'); const stream = createReadStream(filePath); const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, [stream, 'pipe'])); t.is(stdout, 'foobar'); await rm(filePath); }; test('stdin can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 0); test('stdio[*] can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 3); const testLazyFileWritable = async (t, index) => { const filePath = tempfile(); const stream = createWriteStream(filePath); await execa('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, [stream, 'pipe'])); t.is(await readFile(filePath, 'utf8'), 'foobar'); await rm(filePath); }; test('stdout can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 1); test('stderr can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 2); test('stdio[*] can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 3); test('Waits for custom streams destroy on process errors', async t => { let waitedForDestroy = false; const stream = new Writable({ destroy: callbackify(async error => { await setImmediate(); waitedForDestroy = true; return error; }), }); const {timedOut} = await t.throwsAsync(execa('forever.js', {stdout: [stream, 'pipe'], timeout: 1})); t.true(timedOut); t.true(waitedForDestroy); }); test('Handles custom streams destroy errors on process success', async t => { const error = new Error('test'); const stream = new Writable({ destroy(destroyError, done) { done(destroyError ?? error); }, }); const thrownError = await t.throwsAsync(execa('empty.js', {stdout: [stream, 'pipe']})); t.is(thrownError, error); }); const testStreamEarlyExit = async (t, stream, streamName) => { await t.throwsAsync(execa('noop.js', {[streamName]: [stream, 'pipe'], uid: -1})); t.true(stream.destroyed); }; test('Input streams are canceled on early process exit', testStreamEarlyExit, noopReadable(), 'stdin'); test('Output streams are canceled on early process exit', testStreamEarlyExit, noopWritable(), 'stdout'); const testInputDuplexStream = async (t, index) => { const stream = new PassThrough(); stream.end(foobarString); const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, [stream, new Uint8Array()])); t.is(stdout, foobarString); }; test('Can pass Duplex streams to stdin', testInputDuplexStream, 0); test('Can pass Duplex streams to input stdio[*]', testInputDuplexStream, 3); const testOutputDuplexStream = async (t, index) => { const stream = new PassThrough(); const [output] = await Promise.all([ text(stream), execa('noop-fd.js', [`${index}`], getStdio(index, [stream, 'pipe'])), ]); t.is(output, foobarString); }; test('Can pass Duplex streams to stdout', testOutputDuplexStream, 1); test('Can pass Duplex streams to stderr', testOutputDuplexStream, 2); test('Can pass Duplex streams to output stdio[*]', testOutputDuplexStream, 3);