Skip to content
Myr

node-stream.js

This file

Path

test/stdio/node-stream.js

106 lines · 4.5 KiB · JavaScript

Last changed by

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

History of this file

Take it

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 1–8why, 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';5import test from 'ava';6import tempfile from 'tempfile';7import {execa, execaSync} from '../../index.js';8import {setFixtureDir} from '../helpers/fixtures-dir.js';

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

line 9why, one step back
9import {getStdio} from '../helpers/stdio.js';

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 10–19why, one step back
10
11setFixtureDir();1213const createNoFileReadable = value => {14 const stream = new PassThrough();15 stream.write(value);16 stream.end();17 return stream;18};19

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

line 20why, one step back
20const testNodeStreamSync = (t, StreamClass, index, optionName) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

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

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

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

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 23–25why, one step back
23 }, {message: `The \`${optionName}\` option cannot be a Node.js stream in sync mode.`});
24};25

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

lines 26–31why, one step back
26test('input cannot be a Node.js Readable - sync', testNodeStreamSync, Readable, 'input', 'input');
27test('stdin cannot be a Node.js Readable - sync', testNodeStreamSync, Readable, 0, 'stdin');28test('stdio[*] cannot be a Node.js Readable - sync', testNodeStreamSync, Readable, 3, 'stdio[3]');29test('stdout cannot be a Node.js Writable - sync', testNodeStreamSync, Writable, 1, 'stdout');30test('stderr cannot be a Node.js Writable - sync', testNodeStreamSync, Writable, 2, 'stderr');31test('stdio[*] cannot be a Node.js Writable - sync', testNodeStreamSync, Writable, 3, 'stdio[3]');

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 32–37why, one step back
32
33test('input can be a Node.js Readable without a file descriptor', async t => {34 const {stdout} = await execa('stdin.js', {input: createNoFileReadable('foobar')});35 t.is(stdout, 'foobar');36});37

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

lines 38–39why, one step back
38const testNoFileStream = async (t, index, StreamClass) => {
39 await t.throwsAsync(execa('empty.js', getStdio(index, new StreamClass())), {code: 'ERR_INVALID_ARG_VALUE'});

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 40–41why, one step back
40};
41

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

lines 42–46why, one step back
42test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 0, Readable);
43test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 1, Writable);44test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 2, Writable);45test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, Readable);46test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, Writable);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 47why, one step back
47

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

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

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 49–53why, one step back
49 const filePath = tempfile();
50 await writeFile(filePath, 'foobar');51 const stream = createReadStream(filePath);52 await once(stream, 'open');53

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

lines 54–55why, one step back
54 const indexString = index === 'input' ? '0' : `${index}`;
55 const {stdout} = await execa('stdin-fd.js', [indexString], getStdio(index, stream));

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 56–60why, one step back
56 t.is(stdout, 'foobar');
5758 await rm(filePath);59};60

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

lines 61–63why, one step back
61test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'input');
62test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 0);63test('stdio[*] can be a Node.js Readable with a file descriptor', testFileReadable, 3);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 64why, one step back
64

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

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

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 66–69why, one step back
66 const filePath = tempfile();
67 const stream = createWriteStream(filePath);68 await once(stream, 'open');69

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

lines 70–71why, one step back
70 await execa('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, stream));
71 t.is(await readFile(filePath, 'utf8'), 'foobar');

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 72–75why, one step back
72
73 await rm(filePath);74};75

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

lines 76–78why, one step back
76test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, 1);
77test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, 2);78test('stdio[*] can be a Node.js Writable with a file descriptor', testFileWritable, 3);

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

line 79why, one step back
79

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

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

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

lines 81–84why, one step back
81 const filePath = tempfile();
82 await writeFile(filePath, 'foobar');83 const stream = createReadStream(filePath);84

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

line 85why, one step back
85 const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, [stream, 'pipe']));

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

lines 86–90why, one step back
86 t.is(stdout, 'foobar');
8788 await rm(filePath);89};90

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

lines 91–92why, one step back
91test('stdin can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 0);
92test('stdio[*] can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 3);

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

line 93why, one step back
93

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

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

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

lines 95–97why, one step back
95 const filePath = tempfile();
96 const stream = createWriteStream(filePath);97

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

lines 98–99why, one step back
98 await execa('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, [stream, 'pipe']));
99 t.is(await readFile(filePath, 'utf8'), 'foobar');

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

lines 100–103why, one step back
100
101 await rm(filePath);102};103

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

lines 104–106why, one step back
104test('stdout can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 1);
105test('stderr can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 2);106test('stdio[*] can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 3);