Skip to content
Myr

node-stream-native.js

This file

Path

test/stdio/node-stream-native.js

187 lines · 9.8 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

line 1why, one step back
1import assert from 'node:assert/strict';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 2–4why, one step back
2import {once} from 'node:events';
3import {createReadStream, createWriteStream} from 'node:fs';4import {readFile, writeFile, rm} from 'node:fs/promises';

TweaksSindre Sorhusbefore adoption698a1b8

line 5why, one step back
5import test from 'node:test';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 6why, one step back
6import tempfile from 'tempfile';

TweaksSindre Sorhusbefore adoption698a1b8

line 7why, one step back
7import {assertThrows, assertRejects} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 8–20why, one step back
8import {execa, execaSync} from '../../index.js';
9import {setFixtureDirectory} from '../helpers/fixtures-directory.js';10import {getStdio} from '../helpers/stdio.js';11import {foobarString} from '../helpers/input.js';12import {13 noopReadable,14 noopWritable,15 noopDuplex,16 simpleReadable,17} from '../helpers/stream.js';1819setFixtureDirectory();20

TweaksSindre Sorhusbefore adoption698a1b8

lines 21–22why, one step back
21const testNoFileStreamSync = async (fdNumber, stream) => {
22 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 23–26why, one step back
23 execaSync('empty.js', getStdio(fdNumber, stream));
24 }, {code: 'ERR_INVALID_ARG_VALUE'});25};26

TweaksSindre Sorhusbefore adoption698a1b8

lines 27–35why, one step back
27test('stdin cannot be a Node.js Readable without a file descriptor - sync', () => testNoFileStreamSync(0, noopReadable()));
28test('stdin cannot be a Node.js Duplex without a file descriptor - sync', () => testNoFileStreamSync(0, noopDuplex()));29test('stdout cannot be a Node.js Writable without a file descriptor - sync', () => testNoFileStreamSync(1, noopWritable()));30test('stdout cannot be a Node.js Duplex without a file descriptor - sync', () => testNoFileStreamSync(1, noopDuplex()));31test('stderr cannot be a Node.js Writable without a file descriptor - sync', () => testNoFileStreamSync(2, noopWritable()));32test('stderr cannot be a Node.js Duplex without a file descriptor - sync', () => testNoFileStreamSync(2, noopDuplex()));33test('stdio[*] cannot be a Node.js Readable without a file descriptor - sync', () => testNoFileStreamSync(3, noopReadable()));34test('stdio[*] cannot be a Node.js Writable without a file descriptor - sync', () => testNoFileStreamSync(3, noopWritable()));35test('stdio[*] cannot be a Node.js Duplex without a file descriptor - sync', () => testNoFileStreamSync(3, noopDuplex()));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 36why, one step back
36

TweaksSindre Sorhusbefore adoption698a1b8

line 37why, one step back
37test('input can be a Node.js Readable without a file descriptor', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 38why, one step back
38 const {stdout} = await execa('stdin.js', {input: simpleReadable()});

TweaksSindre Sorhusbefore adoption698a1b8

line 39why, one step back
39 assert.equal(stdout, foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 42–43why, one step back
42test('input cannot be a Node.js Readable without a file descriptor - sync', () => {
43 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 44–47why, one step back
44 execaSync('empty.js', {input: simpleReadable()});
45 }, {message: 'The `input` option cannot be a Node.js stream with synchronous methods.'});46});47

TweaksSindre Sorhusbefore adoption698a1b8

lines 48–49why, one step back
48const testNoFileStream = async (fdNumber, stream) => {
49 await assertRejects(execa('empty.js', getStdio(fdNumber, stream)), {code: 'ERR_INVALID_ARG_VALUE'});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 50–51why, one step back
50};
51

TweaksSindre Sorhusbefore adoption698a1b8

lines 52–60why, one step back
52test('stdin cannot be a Node.js Readable without a file descriptor', () => testNoFileStream(0, noopReadable()));
53test('stdin cannot be a Node.js Duplex without a file descriptor', () => testNoFileStream(0, noopDuplex()));54test('stdout cannot be a Node.js Writable without a file descriptor', () => testNoFileStream(1, noopWritable()));55test('stdout cannot be a Node.js Duplex without a file descriptor', () => testNoFileStream(1, noopDuplex()));56test('stderr cannot be a Node.js Writable without a file descriptor', () => testNoFileStream(2, noopWritable()));57test('stderr cannot be a Node.js Duplex without a file descriptor', () => testNoFileStream(2, noopDuplex()));58test('stdio[*] cannot be a Node.js Readable without a file descriptor', () => testNoFileStream(3, noopReadable()));59test('stdio[*] cannot be a Node.js Writable without a file descriptor', () => testNoFileStream(3, noopWritable()));60test('stdio[*] cannot be a Node.js Duplex without a file descriptor', () => testNoFileStream(3, noopDuplex()));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 61–76why, one step back
61
62const createFileReadStream = async () => {63 const filePath = tempfile();64 await writeFile(filePath, 'foobar');65 const stream = createReadStream(filePath);66 await once(stream, 'open');67 return {stream, filePath};68};6970const createFileWriteStream = async () => {71 const filePath = tempfile();72 const stream = createWriteStream(filePath);73 await once(stream, 'open');74 return {stream, filePath};75};76

TweaksSindre Sorhusbefore adoption698a1b8

line 77why, one step back
77const assertFileStreamError = async (subprocess, stream, filePath) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 78–80why, one step back
78 const cause = new Error('test');
79 stream.destroy(cause);80

TweaksSindre Sorhusbefore adoption698a1b8

lines 81–84why, one step back
81 const error = await assertRejects(subprocess);
82 assert.equal(error.cause, cause);83 assert.equal(error.exitCode, 0);84 assert.equal(error.signal, undefined);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 85–88why, one step back
85
86 await rm(filePath);87};88

TweaksSindre Sorhusbefore adoption698a1b8

line 89why, one step back
89const testFileReadable = async (fdNumber, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 90–93why, one step back
90 const {stream, filePath} = await createFileReadStream();
9192 const fdNumberString = fdNumber === 'input' ? '0' : `${fdNumber}`;93 const {stdout} = await execaMethod('stdin-fd.js', [fdNumberString], getStdio(fdNumber, stream));

TweaksSindre Sorhusbefore adoption698a1b8

line 94why, one step back
94 assert.equal(stdout, 'foobar');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 95–98why, one step back
95
96 await rm(filePath);97};98

TweaksSindre Sorhusbefore adoption698a1b8

lines 99–103why, one step back
99test('input can be a Node.js Readable with a file descriptor', () => testFileReadable('input', execa));
100test('stdin can be a Node.js Readable with a file descriptor', () => testFileReadable(0, execa));101test('stdio[*] can be a Node.js Readable with a file descriptor', () => testFileReadable(3, execa));102test('stdin can be a Node.js Readable with a file descriptor - sync', () => testFileReadable(0, execaSync));103test('stdio[*] can be a Node.js Readable with a file descriptor - sync', () => testFileReadable(3, execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 104why, one step back
104

TweaksSindre Sorhusbefore adoption698a1b8

line 105why, one step back
105const testFileReadableError = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 106–110why, one step back
106 const {stream, filePath} = await createFileReadStream();
107108 const fdNumberString = fdNumber === 'input' ? '0' : `${fdNumber}`;109 const subprocess = execa('stdin-fd.js', [fdNumberString], getStdio(fdNumber, stream));110

TweaksSindre Sorhusbefore adoption698a1b8

line 111why, one step back
111 await assertFileStreamError(subprocess, stream, filePath);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 112–113why, one step back
112};
113

TweaksSindre Sorhusbefore adoption698a1b8

lines 114–116why, one step back
114test('input handles errors from a Node.js Readable with a file descriptor', () => testFileReadableError('input'));
115test('stdin handles errors from a Node.js Readable with a file descriptor', () => testFileReadableError(0));116test('stdio[*] handles errors from a Node.js Readable with a file descriptor', () => testFileReadableError(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 117why, one step back
117

TweaksSindre Sorhusbefore adoption698a1b8

line 118why, one step back
118const testFileReadableOpen = async (fdNumber, useSingle, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 119why, one step back
119 const {stream, filePath} = await createFileReadStream();

TweaksSindre Sorhusbefore adoption698a1b8

line 120why, one step back
120 assert.deepEqual(stream.eventNames(), []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 121–124why, one step back
121
122 const stdioOption = useSingle ? stream : [stream, 'pipe'];123 await execaMethod('empty.js', getStdio(fdNumber, stdioOption));124

TweaksSindre Sorhusbefore adoption698a1b8

lines 125–126why, one step back
125 assert.equal(stream.readable, useSingle && fdNumber !== 'input');
126 assert.deepEqual(stream.eventNames(), []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 127–130why, one step back
127
128 await rm(filePath);129};130

TweaksSindre Sorhusbefore adoption698a1b8

lines 131–136why, one step back
131test('input closes a Node.js Readable with a file descriptor', () => testFileReadableOpen('input', true, execa));
132test('stdin leaves open a single Node.js Readable with a file descriptor', () => testFileReadableOpen(0, true, execa));133test('stdin closes a combined Node.js Readable with a file descriptor', () => testFileReadableOpen(0, false, execa));134test('stdio[*] leaves open a single Node.js Readable with a file descriptor', () => testFileReadableOpen(3, true, execa));135test('stdin leaves open a single Node.js Readable with a file descriptor - sync', () => testFileReadableOpen(0, true, execaSync));136test('stdio[*] leaves open a single Node.js Readable with a file descriptor - sync', () => testFileReadableOpen(3, true, execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 137why, one step back
137

TweaksSindre Sorhusbefore adoption698a1b8

line 138why, one step back
138const testFileWritable = async (fdNumber, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 139–141why, one step back
139 const {stream, filePath} = await createFileWriteStream();
140141 await execaMethod('noop-fd.js', [`${fdNumber}`, 'foobar'], getStdio(fdNumber, stream));

TweaksSindre Sorhusbefore adoption698a1b8

line 142why, one step back
142 assert.equal(await readFile(filePath, 'utf8'), 'foobar');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 143–146why, one step back
143
144 await rm(filePath);145};146

TweaksSindre Sorhusbefore adoption698a1b8

lines 147–152why, one step back
147test('stdout can be a Node.js Writable with a file descriptor', () => testFileWritable(1, execa));
148test('stderr can be a Node.js Writable with a file descriptor', () => testFileWritable(2, execa));149test('stdio[*] can be a Node.js Writable with a file descriptor', () => testFileWritable(3, execa));150test('stdout can be a Node.js Writable with a file descriptor - sync', () => testFileWritable(1, execaSync));151test('stderr can be a Node.js Writable with a file descriptor - sync', () => testFileWritable(2, execaSync));152test('stdio[*] can be a Node.js Writable with a file descriptor - sync', () => testFileWritable(3, execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 153why, one step back
153

TweaksSindre Sorhusbefore adoption698a1b8

line 154why, one step back
154const testFileWritableError = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 155–158why, one step back
155 const {stream, filePath} = await createFileWriteStream();
156157 const subprocess = execa('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, stream));158

TweaksSindre Sorhusbefore adoption698a1b8

line 159why, one step back
159 await assertFileStreamError(subprocess, stream, filePath);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 160–161why, one step back
160};
161

TweaksSindre Sorhusbefore adoption698a1b8

lines 162–164why, one step back
162test('stdout handles errors from a Node.js Writable with a file descriptor', () => testFileWritableError(1));
163test('stderr handles errors from a Node.js Writable with a file descriptor', () => testFileWritableError(2));164test('stdio[*] handles errors from a Node.js Writable with a file descriptor', () => testFileWritableError(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 165why, one step back
165

TweaksSindre Sorhusbefore adoption698a1b8

line 166why, one step back
166const testFileWritableOpen = async (fdNumber, useSingle, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 167why, one step back
167 const {stream, filePath} = await createFileWriteStream();

TweaksSindre Sorhusbefore adoption698a1b8

line 168why, one step back
168 assert.deepEqual(stream.eventNames(), []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 169–172why, one step back
169
170 const stdioOption = useSingle ? stream : [stream, 'pipe'];171 await execaMethod('empty.js', getStdio(fdNumber, stdioOption));172

TweaksSindre Sorhusbefore adoption698a1b8

lines 173–174why, one step back
173 assert.equal(stream.writable, useSingle);
174 assert.deepEqual(stream.eventNames(), []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 175–178why, one step back
175
176 await rm(filePath);177};178

TweaksSindre Sorhusbefore adoption698a1b8

lines 179–187why, one step back
179test('stdout leaves open a single Node.js Writable with a file descriptor', () => testFileWritableOpen(1, true, execa));
180test('stdout closes a combined Node.js Writable with a file descriptor', () => testFileWritableOpen(1, false, execa));181test('stderr leaves open a single Node.js Writable with a file descriptor', () => testFileWritableOpen(2, true, execa));182test('stderr closes a combined Node.js Writable with a file descriptor', () => testFileWritableOpen(2, false, execa));183test('stdio[*] leaves open a single Node.js Writable with a file descriptor', () => testFileWritableOpen(3, true, execa));184test('stdio[*] closes a combined Node.js Writable with a file descriptor', () => testFileWritableOpen(3, false, execa));185test('stdout leaves open a single Node.js Writable with a file descriptor - sync', () => testFileWritableOpen(1, true, execaSync));186test('stderr leaves open a single Node.js Writable with a file descriptor - sync', () => testFileWritableOpen(2, true, execaSync));187test('stdio[*] leaves open a single Node.js Writable with a file descriptor - sync', () => testFileWritableOpen(3, true, execaSync));