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 fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 2–4why, one step back2import {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 back5import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 6why, one step back6import tempfile from 'tempfile';TweaksSindre Sorhusbefore adoption698a1b8
line 7why, one step back7import {assertThrows, assertRejects} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 8–20why, one step back8import {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();20TweaksSindre Sorhusbefore adoption698a1b8
lines 21–22why, one step back21const testNoFileStreamSync = async (fdNumber, stream) => {22 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 23–26why, one step back23 execaSync('empty.js', getStdio(fdNumber, stream));24 }, {code: 'ERR_INVALID_ARG_VALUE'});25};26TweaksSindre Sorhusbefore adoption698a1b8
lines 27–35why, one step back27test('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 back36TweaksSindre Sorhusbefore adoption698a1b8
line 37why, one step back37test('input can be a Node.js Readable without a file descriptor', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 38why, one step back38 const {stdout} = await execa('stdin.js', {input: simpleReadable()});TweaksSindre Sorhusbefore adoption698a1b8
line 39why, one step back39 assert.equal(stdout, foobarString);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 40–41why, one step back40});41TweaksSindre Sorhusbefore adoption698a1b8
lines 42–43why, one step back42test('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 back44 execaSync('empty.js', {input: simpleReadable()});45 }, {message: 'The `input` option cannot be a Node.js stream with synchronous methods.'});46});47TweaksSindre Sorhusbefore adoption698a1b8
lines 48–49why, one step back48const 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 back50};51TweaksSindre Sorhusbefore adoption698a1b8
lines 52–60why, one step back52test('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 back6162const 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};76TweaksSindre Sorhusbefore adoption698a1b8
line 77why, one step back77const assertFileStreamError = async (subprocess, stream, filePath) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 78–80why, one step back78 const cause = new Error('test');79 stream.destroy(cause);80TweaksSindre Sorhusbefore adoption698a1b8
lines 81–84why, one step back81 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 back8586 await rm(filePath);87};88TweaksSindre Sorhusbefore adoption698a1b8
line 89why, one step back89const testFileReadable = async (fdNumber, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 90–93why, one step back90 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 back94 assert.equal(stdout, 'foobar');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 95–98why, one step back9596 await rm(filePath);97};98TweaksSindre Sorhusbefore adoption698a1b8
lines 99–103why, one step back99test('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 back104TweaksSindre Sorhusbefore adoption698a1b8
line 105why, one step back105const testFileReadableError = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 106–110why, one step back106 const {stream, filePath} = await createFileReadStream();107108 const fdNumberString = fdNumber === 'input' ? '0' : `${fdNumber}`;109 const subprocess = execa('stdin-fd.js', [fdNumberString], getStdio(fdNumber, stream));110TweaksSindre Sorhusbefore adoption698a1b8
line 111why, one step back111 await assertFileStreamError(subprocess, stream, filePath);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 112–113why, one step back112};113TweaksSindre Sorhusbefore adoption698a1b8
lines 114–116why, one step back114test('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 back117TweaksSindre Sorhusbefore adoption698a1b8
line 118why, one step back118const testFileReadableOpen = async (fdNumber, useSingle, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 119why, one step back119 const {stream, filePath} = await createFileReadStream();TweaksSindre Sorhusbefore adoption698a1b8
line 120why, one step back120 assert.deepEqual(stream.eventNames(), []);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 121–124why, one step back121122 const stdioOption = useSingle ? stream : [stream, 'pipe'];123 await execaMethod('empty.js', getStdio(fdNumber, stdioOption));124TweaksSindre Sorhusbefore adoption698a1b8
lines 125–126why, one step back125 assert.equal(stream.readable, useSingle && fdNumber !== 'input');126 assert.deepEqual(stream.eventNames(), []);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 127–130why, one step back127128 await rm(filePath);129};130TweaksSindre Sorhusbefore adoption698a1b8
lines 131–136why, one step back131test('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 back137TweaksSindre Sorhusbefore adoption698a1b8
line 138why, one step back138const testFileWritable = async (fdNumber, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 139–141why, one step back139 const {stream, filePath} = await createFileWriteStream();140141 await execaMethod('noop-fd.js', [`${fdNumber}`, 'foobar'], getStdio(fdNumber, stream));TweaksSindre Sorhusbefore adoption698a1b8
line 142why, one step back142 assert.equal(await readFile(filePath, 'utf8'), 'foobar');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 143–146why, one step back143144 await rm(filePath);145};146TweaksSindre Sorhusbefore adoption698a1b8
lines 147–152why, one step back147test('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 back153TweaksSindre Sorhusbefore adoption698a1b8
line 154why, one step back154const testFileWritableError = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 155–158why, one step back155 const {stream, filePath} = await createFileWriteStream();156157 const subprocess = execa('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, stream));158TweaksSindre Sorhusbefore adoption698a1b8
line 159why, one step back159 await assertFileStreamError(subprocess, stream, filePath);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 160–161why, one step back160};161TweaksSindre Sorhusbefore adoption698a1b8
lines 162–164why, one step back162test('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 back165TweaksSindre Sorhusbefore adoption698a1b8
line 166why, one step back166const testFileWritableOpen = async (fdNumber, useSingle, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 167why, one step back167 const {stream, filePath} = await createFileWriteStream();TweaksSindre Sorhusbefore adoption698a1b8
line 168why, one step back168 assert.deepEqual(stream.eventNames(), []);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 169–172why, one step back169170 const stdioOption = useSingle ? stream : [stream, 'pipe'];171 await execaMethod('empty.js', getStdio(fdNumber, stdioOption));172TweaksSindre Sorhusbefore adoption698a1b8
lines 173–174why, one step back173 assert.equal(stream.writable, useSingle);174 assert.deepEqual(stream.eventNames(), []);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 175–178why, one step back175176 await rm(filePath);177};178TweaksSindre Sorhusbefore adoption698a1b8
lines 179–187why, one step back179test('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));