Skip to content
Myr

lines.js

This file

Path

test/stdio/lines.js

221 lines · 12.0 KiB · JavaScript

Last changed by

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

History of this file

Take it

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 1–2why, one step back
1import {Writable} from 'node:stream';
2import test from 'ava';

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 3why, one step back
3import {MaxBufferError} from 'get-stream';

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 4–6why, one step back
4import {execa, execaSync} from '../../index.js';
5import {setFixtureDir} from '../helpers/fixtures-dir.js';6import {fullStdio} from '../helpers/stdio.js';

Refactor tests (#934)ehmickybefore adoption52c4bde

line 7why, one step back
7import {getOutputsGenerator} from '../helpers/generator.js';

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 8why, one step back
8import {foobarString, foobarObject} from '../helpers/input.js';

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 9why, one step back
9import {assertStreamOutput, assertStreamDataEvents, assertIterableChunks} from '../helpers/convert.js';

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 10–12why, one step back
10import {
11 simpleFull,12 simpleChunks,

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 13why, one step back
13 simpleFullUint8Array,

Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf

line 14why, one step back
14 simpleFullHex,

Fix using both `encoding: 'utf16le'` and `lines`/`verbose` options (#931)ehmickybefore adoptiona480e1d

line 15why, one step back
15 simpleFullUtf16Uint8Array,

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 16–22why, one step back
16 simpleLines,
17 simpleFullEndLines,18 noNewlinesChunks,19} from '../helpers/lines.js';2021setFixtureDir();22

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

lines 23–24why, one step back
23const getSimpleChunkSubprocessAsync = options => getSimpleChunkSubprocess(execa, options);
24const getSimpleChunkSubprocess = (execaMethod, options) => execaMethod('noop-fd.js', ['1', simpleFull], {lines: true, ...options});

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 25why, one step back
25

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 26why, one step back
26// eslint-disable-next-line max-params

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

lines 27–28why, one step back
27const testStreamLines = async (t, fdNumber, input, expectedOutput, stripFinalNewline, execaMethod) => {
28 const {stdio} = await execaMethod('noop-fd.js', [`${fdNumber}`, input], {

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 29–32why, one step back
29 ...fullStdio,
30 lines: true,31 stripFinalNewline,32 });

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 33why, one step back
33 t.deepEqual(stdio[fdNumber], expectedOutput);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 34–35why, one step back
34};
35

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

lines 36–47why, one step back
36test('"lines: true" splits lines, stdout, string', testStreamLines, 1, simpleFull, simpleLines, false, execa);
37test('"lines: true" splits lines, stderr, string', testStreamLines, 2, simpleFull, simpleLines, false, execa);38test('"lines: true" splits lines, stdio[*], string', testStreamLines, 3, simpleFull, simpleLines, false, execa);39test('"lines: true" splits lines, stdout, string, stripFinalNewline', testStreamLines, 1, simpleFull, noNewlinesChunks, true, execa);40test('"lines: true" splits lines, stderr, string, stripFinalNewline', testStreamLines, 2, simpleFull, noNewlinesChunks, true, execa);41test('"lines: true" splits lines, stdio[*], string, stripFinalNewline', testStreamLines, 3, simpleFull, noNewlinesChunks, true, execa);42test('"lines: true" splits lines, stdout, string, sync', testStreamLines, 1, simpleFull, simpleLines, false, execaSync);43test('"lines: true" splits lines, stderr, string, sync', testStreamLines, 2, simpleFull, simpleLines, false, execaSync);44test('"lines: true" splits lines, stdio[*], string, sync', testStreamLines, 3, simpleFull, simpleLines, false, execaSync);45test('"lines: true" splits lines, stdout, string, stripFinalNewline, sync', testStreamLines, 1, simpleFull, noNewlinesChunks, true, execaSync);46test('"lines: true" splits lines, stderr, string, stripFinalNewline, sync', testStreamLines, 2, simpleFull, noNewlinesChunks, true, execaSync);47test('"lines: true" splits lines, stdio[*], string, stripFinalNewline, sync', testStreamLines, 3, simpleFull, noNewlinesChunks, true, execaSync);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 48–62why, one step back
48
49const testStreamLinesNoop = async (t, lines, execaMethod) => {50 const {stdout} = await execaMethod('noop-fd.js', ['1', simpleFull], {lines});51 t.is(stdout, simpleFull);52};5354test('"lines: false" is a noop with execa()', testStreamLinesNoop, false, execa);55test('"lines: false" is a noop with execaSync()', testStreamLinesNoop, false, execaSync);5657const bigArray = Array.from({length: 1e5}).fill('.\n');58const bigString = bigArray.join('');59const bigStringNoNewlines = '.'.repeat(1e6);60const bigStringNoNewlinesEnd = `${bigStringNoNewlines}\n`;6162// eslint-disable-next-line max-params

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

lines 63–64why, one step back
63const testStreamLinesGenerator = async (t, input, expectedLines, objectMode, binary, execaMethod) => {
64 const {stdout} = await execaMethod('noop.js', {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 65why, one step back
65 stdout: getOutputsGenerator(input)(objectMode, binary),

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 66–71why, one step back
66 lines: true,
67 stripFinalNewline: false,68 });69 t.deepEqual(stdout, expectedLines);70};71

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

lines 72–87why, one step back
72test('"lines: true" works with strings generators', testStreamLinesGenerator, simpleChunks, simpleFullEndLines, false, false, execa);
73test('"lines: true" works with strings generators, binary', testStreamLinesGenerator, simpleChunks, simpleLines, false, true, execa);74test('"lines: true" works with big strings generators', testStreamLinesGenerator, [bigString], bigArray, false, false, execa);75test('"lines: true" works with big strings generators without newlines', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlinesEnd], false, false, execa);76test('"lines: true" is a noop with strings generators, objectMode', testStreamLinesGenerator, simpleChunks, simpleChunks, true, false, execa);77test('"lines: true" is a noop with strings generators, binary, objectMode', testStreamLinesGenerator, simpleChunks, simpleChunks, true, true, execa);78test('"lines: true" is a noop big strings generators, objectMode', testStreamLinesGenerator, [bigString], [bigString], true, false, execa);79test('"lines: true" is a noop big strings generators without newlines, objectMode', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlines], true, false, execa);80test('"lines: true" works with strings generators, sync', testStreamLinesGenerator, simpleChunks, simpleFullEndLines, false, false, execaSync);81test('"lines: true" works with strings generators, binary, sync', testStreamLinesGenerator, simpleChunks, simpleLines, false, true, execaSync);82test('"lines: true" works with big strings generators, sync', testStreamLinesGenerator, [bigString], bigArray, false, false, execaSync);83test('"lines: true" works with big strings generators without newlines, sync', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlinesEnd], false, false, execaSync);84test('"lines: true" is a noop with strings generators, objectMode, sync', testStreamLinesGenerator, simpleChunks, simpleChunks, true, false, execaSync);85test('"lines: true" is a noop with strings generators, binary, objectMode, sync', testStreamLinesGenerator, simpleChunks, simpleChunks, true, true, execaSync);86test('"lines: true" is a noop big strings generators, objectMode, sync', testStreamLinesGenerator, [bigString], [bigString], true, false, execaSync);87test('"lines: true" is a noop big strings generators without newlines, objectMode, sync', testStreamLinesGenerator, [bigStringNoNewlines], [bigStringNoNewlines], true, false, execaSync);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 88why, one step back
88

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

lines 89–90why, one step back
89const testLinesObjectMode = async (t, execaMethod) => {
90 const {stdout} = await execaMethod('noop.js', {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 91why, one step back
91 stdout: getOutputsGenerator([foobarObject])(true),

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 92–94why, one step back
92 lines: true,
93 });94 t.deepEqual(stdout, [foobarObject]);

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

line 95why, one step back
95};

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 96why, one step back
96

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

lines 97–102why, one step back
97test('"lines: true" is a noop with objects generators, objectMode', testLinesObjectMode, execa);
98test('"lines: true" is a noop with objects generators, objectMode, sync', testLinesObjectMode, execaSync);99100// eslint-disable-next-line max-params101const testBinaryEncoding = async (t, expectedOutput, encoding, stripFinalNewline, execaMethod) => {102 const {stdout} = await getSimpleChunkSubprocess(execaMethod, {encoding, stripFinalNewline});

Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf

line 103why, one step back
103 t.deepEqual(stdout, expectedOutput);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 104–105why, one step back
104};
105

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

lines 106–113why, one step back
106test('"lines: true" is a noop with "encoding: buffer"', testBinaryEncoding, simpleFullUint8Array, 'buffer', false, execa);
107test('"lines: true" is a noop with "encoding: buffer", stripFinalNewline', testBinaryEncoding, simpleFullUint8Array, 'buffer', false, execa);108test('"lines: true" is a noop with "encoding: hex"', testBinaryEncoding, simpleFullHex, 'hex', false, execa);109test('"lines: true" is a noop with "encoding: hex", stripFinalNewline', testBinaryEncoding, simpleFullHex, 'hex', true, execa);110test('"lines: true" is a noop with "encoding: buffer", sync', testBinaryEncoding, simpleFullUint8Array, 'buffer', false, execaSync);111test('"lines: true" is a noop with "encoding: buffer", stripFinalNewline, sync', testBinaryEncoding, simpleFullUint8Array, 'buffer', false, execaSync);112test('"lines: true" is a noop with "encoding: hex", sync', testBinaryEncoding, simpleFullHex, 'hex', false, execaSync);113test('"lines: true" is a noop with "encoding: hex", stripFinalNewline, sync', testBinaryEncoding, simpleFullHex, 'hex', true, execaSync);

Fix using both `encoding: 'utf16le'` and `lines`/`verbose` options (#931)ehmickybefore adoptiona480e1d

line 114why, one step back
114

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

lines 115–116why, one step back
115const testTextEncoding = async (t, expectedLines, stripFinalNewline, execaMethod) => {
116 const {stdout} = await execaMethod('stdin.js', {

Fix using both `encoding: 'utf16le'` and `lines`/`verbose` options (#931)ehmickybefore adoptiona480e1d

lines 117–124why, one step back
117 lines: true,
118 stripFinalNewline,119 encoding: 'utf16le',120 input: simpleFullUtf16Uint8Array,121 });122 t.deepEqual(stdout, expectedLines);123};124

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

lines 125–128why, one step back
125test('"lines: true" is a noop with "encoding: utf16"', testTextEncoding, simpleLines, false, execa);
126test('"lines: true" is a noop with "encoding: utf16", stripFinalNewline', testTextEncoding, noNewlinesChunks, true, execa);127test('"lines: true" is a noop with "encoding: utf16", sync', testTextEncoding, simpleLines, false, execaSync);128test('"lines: true" is a noop with "encoding: utf16", stripFinalNewline, sync', testTextEncoding, noNewlinesChunks, true, execaSync);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 129why, one step back
129

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

lines 130–131why, one step back
130const testLinesNoBuffer = async (t, execaMethod) => {
131 const {stdout} = await getSimpleChunkSubprocess(execaMethod, {buffer: false});

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 132why, one step back
132 t.is(stdout, undefined);

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

lines 133–136why, one step back
133};
134135test('"lines: true" is a noop with "buffer: false"', testLinesNoBuffer, execa);136test('"lines: true" is a noop with "buffer: false", sync', testLinesNoBuffer, execaSync);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 137why, one step back
137

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 138–139why, one step back
138test('"lines: true" can be below "maxBuffer"', async t => {
139 const maxBuffer = simpleLines.length;

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

line 140why, one step back
140 const {stdout} = await getSimpleChunkSubprocessAsync({maxBuffer});

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 141–145why, one step back
141 t.deepEqual(stdout, noNewlinesChunks);
142});143144test('"lines: true" can be above "maxBuffer"', async t => {145 const maxBuffer = simpleLines.length - 1;

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

line 146why, one step back
146 const {cause, stdout} = await t.throwsAsync(getSimpleChunkSubprocessAsync({maxBuffer}));

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 147–152why, one step back
147 t.true(cause instanceof MaxBufferError);
148 t.deepEqual(stdout, noNewlinesChunks.slice(0, maxBuffer));149});150151test('"lines: true" stops on stream error', async t => {152 const cause = new Error(foobarString);

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

line 153why, one step back
153 const error = await t.throwsAsync(getSimpleChunkSubprocessAsync({

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 154–165why, one step back
154 * stdout(line) {
155 if (line === noNewlinesChunks[2]) {156 throw cause;157 }158159 yield line;160 },161 }));162 t.is(error.cause, cause);163 t.deepEqual(error.stdout, noNewlinesChunks.slice(0, 2));164});165

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 166–174why, one step back
166test('"lines: true" stops on stream error event', async t => {
167 const cause = new Error(foobarString);168 const subprocess = getSimpleChunkSubprocessAsync();169 subprocess.stdout.emit('error', cause);170 const error = await t.throwsAsync(subprocess);171 t.is(error.cause, cause);172 t.deepEqual(error.stdout, []);173});174

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 175why, one step back
175const testAsyncIteration = async (t, expectedLines, stripFinalNewline) => {

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

line 176why, one step back
176 const subprocess = getSimpleChunkSubprocessAsync({stripFinalNewline});

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 177–180why, one step back
177 t.false(subprocess.stdout.readableObjectMode);
178 await assertStreamOutput(t, subprocess.stdout, simpleFull);179 const {stdout} = await subprocess;180 t.deepEqual(stdout, expectedLines);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 181–182why, one step back
181};
182

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 183–184why, one step back
183test('"lines: true" works with stream async iteration', testAsyncIteration, simpleLines, false);
184test('"lines: true" works with stream async iteration, stripFinalNewline', testAsyncIteration, noNewlinesChunks, true);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 185why, one step back
185

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 186why, one step back
186const testDataEvents = async (t, expectedLines, stripFinalNewline) => {

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

line 187why, one step back
187 const subprocess = getSimpleChunkSubprocessAsync({stripFinalNewline});

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 188why, one step back
188 await assertStreamDataEvents(t, subprocess.stdout, simpleFull);

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 189–190why, one step back
189 const {stdout} = await subprocess;
190 t.deepEqual(stdout, expectedLines);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 191–192why, one step back
191};
192

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 193–194why, one step back
193test('"lines: true" works with stream "data" events', testDataEvents, simpleLines, false);
194test('"lines: true" works with stream "data" events, stripFinalNewline', testDataEvents, noNewlinesChunks, true);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 195why, one step back
195

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 196why, one step back
196const testWritableStream = async (t, expectedLines, stripFinalNewline) => {

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 197why, one step back
197 let output = '';

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 198–199why, one step back
198 const writable = new Writable({
199 write(line, encoding, done) {

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 200why, one step back
200 output += line.toString();

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 201–204why, one step back
201 done();
202 },203 decodeStrings: false,204 });

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

line 205why, one step back
205 const {stdout} = await getSimpleChunkSubprocessAsync({stripFinalNewline, stdout: ['pipe', writable]});

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 206why, one step back
206 t.deepEqual(output, simpleFull);

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 207why, one step back
207 t.deepEqual(stdout, expectedLines);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 208–209why, one step back
208};
209

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 210–213why, one step back
210test('"lines: true" works with writable streams targets', testWritableStream, simpleLines, false);
211test('"lines: true" works with writable streams targets, stripFinalNewline', testWritableStream, noNewlinesChunks, true);212213const testIterable = async (t, expectedLines, stripFinalNewline) => {

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

line 214why, one step back
214 const subprocess = getSimpleChunkSubprocessAsync({stripFinalNewline});

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 215–221why, one step back
215 await assertIterableChunks(t, subprocess, noNewlinesChunks);
216 const {stdout} = await subprocess;217 t.deepEqual(stdout, expectedLines);218};219220test('"lines: true" works with subprocess.iterable()', testIterable, simpleLines, false);221test('"lines: true" works with subprocess.iterable(), stripFinalNewline', testIterable, noNewlinesChunks, true);