Skip to content
Myr

web-stream.js

This file

Path

test/stdio/web-stream.js

111 lines · 4.7 KiB · JavaScript

Last changed by

Fix CISindre Sorhusbefore adoptioncfac59f

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–3why, one step back
2import {Readable} from 'node:stream';
3import {setImmediate} from 'node:timers/promises';

TweaksSindre Sorhusbefore adoption698a1b8

lines 4–5why, one step back
4import test from 'node:test';
5import {assertThrows, assertRejects} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 6–11why, one step back
6import {execa, execaSync} from '../../index.js';
7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8import {getStdio} from '../helpers/stdio.js';910setFixtureDirectory();11

TweaksSindre Sorhusbefore adoption698a1b8

line 12why, one step back
12const testReadableStream = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 13–14why, one step back
13 const readableStream = Readable.toWeb(Readable.from('foobar'));
14 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, readableStream));

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 16–17why, one step back
16};
17

TweaksSindre Sorhusbefore adoption698a1b8

lines 18–19why, one step back
18test('stdin can be a ReadableStream', () => testReadableStream(0));
19test('stdio[*] can be a ReadableStream', () => testReadableStream(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 20why, one step back
20

TweaksSindre Sorhusbefore adoption698a1b8

line 21why, one step back
21const testWritableStream = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 22–28why, one step back
22 const result = [];
23 const writableStream = new WritableStream({24 write(chunk) {25 result.push(chunk);26 },27 });28 await execa('noop-fd.js', [`${fdNumber}`, 'foobar'], getStdio(fdNumber, writableStream));

TweaksSindre Sorhusbefore adoption698a1b8

line 29why, one step back
29 assert.equal(result.join(''), 'foobar');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 30–31why, one step back
30};
31

TweaksSindre Sorhusbefore adoption698a1b8

lines 32–34why, one step back
32test('stdout can be a WritableStream', () => testWritableStream(1));
33test('stderr can be a WritableStream', () => testWritableStream(2));34test('stdio[*] can be a WritableStream', () => testWritableStream(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 35why, one step back
35

TweaksSindre Sorhusbefore adoption698a1b8

lines 36–37why, one step back
36const testWebStreamSync = (StreamClass, fdNumber, optionName) => {
37 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 38–41why, one step back
38 execaSync('empty.js', getStdio(fdNumber, new StreamClass()));
39 }, {message: `The \`${optionName}\` option cannot be a web stream with synchronous methods.`});40};41

TweaksSindre Sorhusbefore adoption698a1b8

lines 42–46why, one step back
42test('stdin cannot be a ReadableStream - sync', () => testWebStreamSync(ReadableStream, 0, 'stdin'));
43test('stdio[*] cannot be a ReadableStream - sync', () => testWebStreamSync(ReadableStream, 3, 'stdio[3]'));44test('stdout cannot be a WritableStream - sync', () => testWebStreamSync(WritableStream, 1, 'stdout'));45test('stderr cannot be a WritableStream - sync', () => testWebStreamSync(WritableStream, 2, 'stderr'));46test('stdio[*] cannot be a WritableStream - sync', () => testWebStreamSync(WritableStream, 3, 'stdio[3]'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 47why, one step back
47

TweaksSindre Sorhusbefore adoption698a1b8

line 48why, one step back
48const testLongWritableStream = async fdNumber => {

Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d

line 49why, one step back
49 let isResult = false;

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 50–52why, one step back
50 const writableStream = new WritableStream({
51 async close() {52 await setImmediate();

Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d

line 53why, one step back
53 isResult = true;

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 54–56why, one step back
54 },
55 });56 await execa('empty.js', getStdio(fdNumber, writableStream));

TweaksSindre Sorhusbefore adoption698a1b8

line 57why, one step back
57 assert.equal(isResult, true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 58–59why, one step back
58};
59

TweaksSindre Sorhusbefore adoption698a1b8

lines 60–62why, one step back
60test('stdout waits for WritableStream completion', () => testLongWritableStream(1));
61test('stderr waits for WritableStream completion', () => testLongWritableStream(2));62test('stdio[*] waits for WritableStream completion', () => testLongWritableStream(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 63why, one step back
63

TweaksSindre Sorhusbefore adoption698a1b8

line 64why, one step back
64const testWritableStreamError = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 65–70why, one step back
65 const cause = new Error('foobar');
66 const writableStream = new WritableStream({67 start(controller) {68 controller.error(cause);69 },70 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 71–72why, one step back
71 const error = await assertRejects(execa('noop.js', getStdio(fdNumber, writableStream)));
72 assert.equal(error.cause, cause);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 73–74why, one step back
73};
74

TweaksSindre Sorhusbefore adoption698a1b8

lines 75–77why, one step back
75test('stdout option handles errors in WritableStream', () => testWritableStreamError(1));
76test('stderr option handles errors in WritableStream', () => testWritableStreamError(2));77test('stdio[*] option handles errors in WritableStream', () => testWritableStreamError(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 78why, one step back
78

TweaksSindre Sorhusbefore adoption698a1b8

line 79why, one step back
79const testReadableStreamError = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 80–85why, one step back
80 const cause = new Error('foobar');
81 const readableStream = new ReadableStream({82 start(controller) {83 controller.error(cause);84 },85 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 86–87why, one step back
86 const error = await assertRejects(execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, readableStream)));
87 assert.equal(error.cause, cause);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 88–89why, one step back
88};
89

TweaksSindre Sorhusbefore adoption698a1b8

lines 90–91why, one step back
90test('stdin option handles errors in ReadableStream', () => testReadableStreamError(0));
91test('stdio[*] option handles errors in ReadableStream', () => testReadableStreamError(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 92why, one step back
92

TweaksSindre Sorhusbefore adoption698a1b8

line 93why, one step back
93test('ReadableStream with stdin is canceled on subprocess exit', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 94–97why, one step back
94 let readableStream;
95 const promise = new Promise(resolve => {96 readableStream = new ReadableStream({cancel: resolve});97 });

TweaksSindre Sorhusbefore adoption698a1b8

line 98why, one step back
98 await assertRejects(execa('stdin.js', {stdin: readableStream, timeout: 1}), {message: /timed out/});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 99–100why, one step back
99 await promise;
100});

Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a

lines 101–103why, one step back
101
102// A web stream can only be used once, but like Node.js streams, the same instance103// passed to multiple file descriptors should send its data to each of them.

TweaksSindre Sorhusbefore adoption698a1b8

line 104why, one step back
104const testSharedReadableStream = async (fixtureArguments, stdio) => {

Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a

line 105why, one step back
105 const readableStream = Readable.toWeb(Readable.from('foobar'));

Fix CISindre Sorhusbefore adoptioncfac59f

lines 106–107why, one step back
106 const {stdout} = await execa('stdin-fds.js', fixtureArguments, {stdio: stdio.map(value => value === 'shared' ? readableStream : value)});
107 assert.equal(stdout, 'foobarfoobar');

Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a

lines 108–109why, one step back
108};
109

Fix CISindre Sorhusbefore adoptioncfac59f

lines 110–111why, one step back
110test('stdin can share a ReadableStream with stdio[*]', () => testSharedReadableStream(['0', '3'], ['shared', 'pipe', 'pipe', 'shared']));
111test('stdio[*] can share a ReadableStream with another stdio[*]', () => testSharedReadableStream(['3', '4'], ['pipe', 'pipe', 'pipe', 'shared', 'shared']));