Skip to content
Myr

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 file

Take it

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 1–4why, one step back
1import {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 back
5import {setImmediate} from 'node:timers/promises';

Wait for streams completion on errors (#722)ehmickybefore adoption195369f

line 6why, one step back
6import {callbackify} from 'node:util';

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 7–10why, one step back
7import 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 back
11import {getStdio} from '../helpers/stdio.js';

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 12–21why, one step back
12
13setFixtureDir();1415const createNoFileReadable = value => {16 const stream = new PassThrough();17 stream.write(value);18 stream.end();19 return stream;20};21

Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf

line 22why, one step back
22const testNoFileStreamSync = async (t, index, StreamClass) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

line 23why, one step back
23 t.throws(() => {

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 24why, one step back
24 execaSync('empty.js', getStdio(index, new StreamClass()));

Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf

line 25why, one step back
25 }, {code: 'ERR_INVALID_ARG_VALUE'});

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 26–27why, one step back
26};
27

Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf

lines 28–32why, one step back
28test('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 back
33
34test('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});38

Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf

lines 39–44why, one step back
39test('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});44

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 45–46why, one step back
45const 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 back
47};
48

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 49–53why, one step back
49test('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 back
54

Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf

line 55why, one step back
55const testFileReadable = async (t, index, execaMethod) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 56–60why, one step back
56 const filePath = tempfile();
57 await writeFile(filePath, 'foobar');58 const stream = createReadStream(filePath);59 await once(stream, 'open');60

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 61why, one step back
61 const indexString = index === 'input' ? '0' : `${index}`;

Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf

line 62why, one step back
62 const {stdout} = await execaMethod('stdin-fd.js', [indexString], getStdio(index, stream));

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 63–67why, one step back
63 t.is(stdout, 'foobar');
6465 await rm(filePath);66};67

Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf

lines 68–72why, one step back
68test('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 back
73

Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf

line 74why, one step back
74const testFileWritable = async (t, index, execaMethod) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 75–78why, one step back
75 const filePath = tempfile();
76 const stream = createWriteStream(filePath);77 await once(stream, 'open');78

Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf

line 79why, one step back
79 await execaMethod('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, stream));

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 80why, one step back
80 t.is(await readFile(filePath, 'utf8'), 'foobar');

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 81–84why, one step back
81
82 await rm(filePath);83};84

Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf

lines 85–90why, one step back
85test('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 back
91

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 92why, one step back
92const testLazyFileReadable = async (t, index) => {

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 93–96why, one step back
93 const filePath = tempfile();
94 await writeFile(filePath, 'foobar');95 const stream = createReadStream(filePath);96

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 97why, one step back
97 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 back
98 t.is(stdout, 'foobar');
99100 await rm(filePath);101};102

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 103–104why, one step back
103test('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 back
105

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 106why, one step back
106const testLazyFileWritable = async (t, index) => {

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 107–109why, one step back
107 const filePath = tempfile();
108 const stream = createWriteStream(filePath);109

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 110–111why, one step back
110 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 back
112
113 await rm(filePath);114};115

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 116–118why, one step back
116test('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 back
119
120test('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 back
124 await setImmediate();

Wait for streams completion on errors (#722)ehmickybefore adoption195369f

lines 125–133why, one step back
125 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});