node-stream.js
This file
Path
test/stdio/node-stream.js
133 lines · 5.7 KiB · JavaScript
Last changed by
Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf
History of this fileTake it
Refactor tests (#628)ehmickybefore adoption30ecc81
lines 1–4why, one step back1import {once} from 'node:events';2import {createReadStream, createWriteStream} from 'node:fs';3import {readFile, writeFile, rm} from 'node:fs/promises';4import {Readable, Writable, PassThrough} from 'node:stream';Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf
line 5why, one step back5import {setImmediate} from 'node:timers/promises';Wait for streams completion on errors (#722)ehmickybefore adoption195369f
line 6why, one step back6import {callbackify} from 'node:util';Refactor tests (#628)ehmickybefore adoption30ecc81
lines 7–10why, one step back7import test from 'ava';8import tempfile from 'tempfile';9import {execa, execaSync} from '../../index.js';10import {setFixtureDir} from '../helpers/fixtures-dir.js';Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 11why, one step back11import {getStdio} from '../helpers/stdio.js';Refactor tests (#628)ehmickybefore adoption30ecc81
lines 12–21why, one step back1213setFixtureDir();1415const createNoFileReadable = value => {16 const stream = new PassThrough();17 stream.write(value);18 stream.end();19 return stream;20};21Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
line 22why, one step back22const testNoFileStreamSync = async (t, index, StreamClass) => {Refactor tests (#628)ehmickybefore adoption30ecc81
line 23why, one step back23 t.throws(() => {Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 24why, one step back24 execaSync('empty.js', getStdio(index, new StreamClass()));Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
line 25why, one step back25 }, {code: 'ERR_INVALID_ARG_VALUE'});Refactor tests (#628)ehmickybefore adoption30ecc81
lines 26–27why, one step back26};27Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
lines 28–32why, one step back28test('stdin cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 0, Readable);29test('stdout cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 1, Writable);30test('stderr cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 2, Writable);31test('stdio[*] cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 3, Readable);32test('stdio[*] cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 3, Writable);Refactor tests (#628)ehmickybefore adoption30ecc81
lines 33–38why, one step back3334test('input can be a Node.js Readable without a file descriptor', async t => {35 const {stdout} = await execa('stdin.js', {input: createNoFileReadable('foobar')});36 t.is(stdout, 'foobar');37});38Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
lines 39–44why, one step back39test('input cannot be a Node.js Readable without a file descriptor - sync', t => {40 t.throws(() => {41 execaSync('empty.js', {input: createNoFileReadable('foobar')});42 }, {message: 'The `input` option cannot be a Node.js stream in sync mode.'});43});44Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 45–46why, one step back45const testNoFileStream = async (t, index, StreamClass) => {46 await t.throwsAsync(execa('empty.js', getStdio(index, new StreamClass())), {code: 'ERR_INVALID_ARG_VALUE'});Refactor tests (#628)ehmickybefore adoption30ecc81
lines 47–48why, one step back47};48Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 49–53why, one step back49test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 0, Readable);50test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 1, Writable);51test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 2, Writable);52test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, Readable);53test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, Writable);Refactor tests (#628)ehmickybefore adoption30ecc81
line 54why, one step back54Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
line 55why, one step back55const testFileReadable = async (t, index, execaMethod) => {Refactor tests (#628)ehmickybefore adoption30ecc81
lines 56–60why, one step back56 const filePath = tempfile();57 await writeFile(filePath, 'foobar');58 const stream = createReadStream(filePath);59 await once(stream, 'open');60Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 61why, one step back61 const indexString = index === 'input' ? '0' : `${index}`;Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
line 62why, one step back62 const {stdout} = await execaMethod('stdin-fd.js', [indexString], getStdio(index, stream));Refactor tests (#628)ehmickybefore adoption30ecc81
lines 63–67why, one step back63 t.is(stdout, 'foobar');6465 await rm(filePath);66};67Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
lines 68–72why, one step back68test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'input', execa);69test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 0, execa);70test('stdio[*] can be a Node.js Readable with a file descriptor', testFileReadable, 3, execa);71test('stdin can be a Node.js Readable with a file descriptor - sync', testFileReadable, 0, execaSync);72test('stdio[*] can be a Node.js Readable with a file descriptor - sync', testFileReadable, 3, execaSync);Refactor tests (#628)ehmickybefore adoption30ecc81
line 73why, one step back73Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
line 74why, one step back74const testFileWritable = async (t, index, execaMethod) => {Refactor tests (#628)ehmickybefore adoption30ecc81
lines 75–78why, one step back75 const filePath = tempfile();76 const stream = createWriteStream(filePath);77 await once(stream, 'open');78Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
line 79why, one step back79 await execaMethod('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, stream));Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 80why, one step back80 t.is(await readFile(filePath, 'utf8'), 'foobar');Refactor tests (#628)ehmickybefore adoption30ecc81
lines 81–84why, one step back8182 await rm(filePath);83};84Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
lines 85–90why, one step back85test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, 1, execa);86test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, 2, execa);87test('stdio[*] can be a Node.js Writable with a file descriptor', testFileWritable, 3, execa);88test('stdout can be a Node.js Writable with a file descriptor - sync', testFileWritable, 1, execaSync);89test('stderr can be a Node.js Writable with a file descriptor - sync', testFileWritable, 2, execaSync);90test('stdio[*] can be a Node.js Writable with a file descriptor - sync', testFileWritable, 3, execaSync);Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 91why, one step back91Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 92why, one step back92const testLazyFileReadable = async (t, index) => {Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 93–96why, one step back93 const filePath = tempfile();94 await writeFile(filePath, 'foobar');95 const stream = createReadStream(filePath);96Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 97why, one step back97 const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, [stream, 'pipe']));Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 98–102why, one step back98 t.is(stdout, 'foobar');99100 await rm(filePath);101};102Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 103–104why, one step back103test('stdin can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 0);104test('stdio[*] can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 3);Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 105why, one step back105Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 106why, one step back106const testLazyFileWritable = async (t, index) => {Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 107–109why, one step back107 const filePath = tempfile();108 const stream = createWriteStream(filePath);109Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 110–111why, one step back110 await execa('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, [stream, 'pipe']));111 t.is(await readFile(filePath, 'utf8'), 'foobar');Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 112–115why, one step back112113 await rm(filePath);114};115Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 116–118why, one step back116test('stdout can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 1);117test('stderr can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 2);118test('stdio[*] can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 3);Wait for streams completion on errors (#722)ehmickybefore adoption195369f
lines 119–123why, one step back119120test('Wait for custom streams destroy on process errors', async t => {121 let waitedForDestroy = false;122 const stream = new Writable({123 destroy: callbackify(async error => {Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf
line 124why, one step back124 await setImmediate();Wait for streams completion on errors (#722)ehmickybefore adoption195369f
lines 125–133why, one step back125 waitedForDestroy = true;126 return error;127 }),128 });129 const childProcess = execa('forever.js', {stdout: [stream, 'pipe'], timeout: 1});130 const {timedOut} = await t.throwsAsync(childProcess);131 t.true(timedOut);132 t.true(waitedForDestroy);133});