Skip to content
Myr

split-newline.js

This file

Path

test/transform/split-newline.js

134 lines · 6.4 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';

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 2–3why, one step back
2import process from 'node:process';
3import {Buffer} from 'node:buffer';

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 5–14why, one step back
5import {execa, execaSync} from '../../index.js';
6import {setFixtureDirectory} from '../helpers/fixtures-directory.js';7import {getOutputsGenerator, noopGenerator, noopAsyncGenerator} from '../helpers/generator.js';8import {singleFull, singleFullEnd} from '../helpers/lines.js';910setFixtureDirectory();1112const singleFullEndWindows = `${singleFull}\r\n`;13const mixedNewlines = '.\n.\r\n.\n.\r\n.\n';14

TweaksSindre Sorhusbefore adoption698a1b8

line 15why, one step back
15const testStripNewline = async (input, expectedOutput, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 16–19why, one step back
16 const {stdout} = await execaMethod('noop.js', {
17 stdout: getOutputsGenerator([input])(),18 stripFinalNewline: false,19 });

TweaksSindre Sorhusbefore adoption698a1b8

line 20why, one step back
20 assert.equal(stdout, expectedOutput);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 21–22why, one step back
21};
22

TweaksSindre Sorhusbefore adoption698a1b8

lines 23–28why, one step back
23test('Strips newline when user do not mistakenly yield one at the end', () => testStripNewline(singleFull, singleFullEnd, execa));
24test('Strips newline when user mistakenly yielded one at the end', () => testStripNewline(singleFullEnd, singleFullEnd, execa));25test('Strips newline when user mistakenly yielded one at the end, Windows newline', () => testStripNewline(singleFullEndWindows, singleFullEndWindows, execa));26test('Strips newline when user do not mistakenly yield one at the end, sync', () => testStripNewline(singleFull, singleFullEnd, execaSync));27test('Strips newline when user mistakenly yielded one at the end, sync', () => testStripNewline(singleFullEnd, singleFullEnd, execaSync));28test('Strips newline when user mistakenly yielded one at the end, Windows newline, sync', () => testStripNewline(singleFullEndWindows, singleFullEndWindows, execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 29why, one step back
29

TweaksSindre Sorhusbefore adoption698a1b8

line 30why, one step back
30const testMixNewlines = async (generator, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 31–34why, one step back
31 const {stdout} = await execaMethod('noop-fd.js', ['1', mixedNewlines], {
32 stdout: generator(),33 stripFinalNewline: false,34 });

TweaksSindre Sorhusbefore adoption698a1b8

line 35why, one step back
35 assert.equal(stdout, mixedNewlines);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 36–37why, one step back
36};
37

TweaksSindre Sorhusbefore adoption698a1b8

lines 38–40why, one step back
38test('Can mix Unix and Windows newlines', () => testMixNewlines(noopGenerator, execa));
39test('Can mix Unix and Windows newlines, sync', () => testMixNewlines(noopGenerator, execaSync));40test('Can mix Unix and Windows newlines, async', () => testMixNewlines(noopAsyncGenerator, execa));

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 41–43why, one step back
41
42// A `\r\n` sequence can be split across two chunks, since chunk boundaries are arbitrary.43// `noop-progressive.js` writes each character as a separate chunk, then a final newline.

TweaksSindre Sorhusbefore adoption698a1b8

line 44why, one step back
44test('Detects Windows newlines split across chunks', async () => {

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 45why, one step back
45 const {stdout} = await execa('noop-progressive.js', ['aaa\r'], {lines: true});

TweaksSindre Sorhusbefore adoption698a1b8

line 46why, one step back
46 assert.deepEqual(stdout, ['aaa']);

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 47–48why, one step back
47});
48

TweaksSindre Sorhusbefore adoption698a1b8

line 49why, one step back
49test('Keeps Windows newlines split across chunks', async () => {

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 50why, one step back
50 const {stdout} = await execa('noop-progressive.js', ['aaa\r'], {lines: true, stripFinalNewline: false});

TweaksSindre Sorhusbefore adoption698a1b8

line 51why, one step back
51 assert.deepEqual(stdout, ['aaa\r\n']);

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 52–53why, one step back
52});
53

TweaksSindre Sorhusbefore adoption698a1b8

line 54why, one step back
54test('Detects multiple Windows newlines split across chunks', async () => {

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 55why, one step back
55 const {stdout} = await execa('noop-progressive.js', ['aaa\r\nbbb\r'], {lines: true});

TweaksSindre Sorhusbefore adoption698a1b8

line 56why, one step back
56 assert.deepEqual(stdout, ['aaa', 'bbb']);

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 57–58why, one step back
57});
58

TweaksSindre Sorhusbefore adoption698a1b8

line 59why, one step back
59test('Keeps lone carriage returns split across chunks', async () => {

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 60why, one step back
60 const {stdout} = await execa('noop-progressive.js', ['aaa\rbbb'], {lines: true});

TweaksSindre Sorhusbefore adoption698a1b8

line 61why, one step back
61 assert.deepEqual(stdout, ['aaa\rbbb']);

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 62–64why, one step back
62});
6364/*

TweaksSindre Sorhusbefore adoption698a1b8

line 65why, one step back
65The newline re-added after each stripped line must be encoded with the `encoding` option when the transform yields bytes, both for `\n` and `\r\n`.

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 66–82why, one step back
66*/
67const utf16Input = 'aaa\nbbb\nccc\n';68const utf16WindowsInput = 'aaa\r\nbbb\r\n';6970const utf16YieldGenerator = {71 * transform(line) {72 yield Buffer.from(line, 'utf16le');73 },74};7576// The transform yields bytes which already end with a newline: it must not be appended twice77const utf16YieldNewlineGenerator = {78 * transform(line) {79 yield Buffer.from(`${line}\n`, 'utf16le');80 },81};82

TweaksSindre Sorhusbefore adoption698a1b8

lines 83–86why, one step back
83// The subprocess writes its output with UTF-16
84const testUtf16LeNewlines = async (output, generator, execaMethod) => {85 const {stdout} = await execaMethod(process.execPath, ['-e', `process.stdout.write(${JSON.stringify(output)}, 'utf16le')`], {86 stdout: generator,

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 87–89why, one step back
87 encoding: 'utf16le',
88 stripFinalNewline: false,89 });

TweaksSindre Sorhusbefore adoption698a1b8

line 90why, one step back
90 assert.equal(stdout, output);

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 91–92why, one step back
91};
92

TweaksSindre Sorhusbefore adoption698a1b8

lines 93–98why, one step back
93test('Newlines re-added to bytes use encoding "utf16le"', () => testUtf16LeNewlines(utf16Input, utf16YieldGenerator, execa));
94test('Newlines re-added to bytes use encoding "utf16le", sync', () => testUtf16LeNewlines(utf16Input, utf16YieldGenerator, execaSync));95test('Windows newlines re-added to bytes use encoding "utf16le"', () => testUtf16LeNewlines(utf16WindowsInput, utf16YieldGenerator, execa));96test('Windows newlines re-added to bytes use encoding "utf16le", sync', () => testUtf16LeNewlines(utf16WindowsInput, utf16YieldGenerator, execaSync));97test('Bytes already ending with a newline are not appended to, utf16le', () => testUtf16LeNewlines(utf16Input, utf16YieldNewlineGenerator, execa));98test('Bytes already ending with a newline are not appended to, utf16le, sync', () => testUtf16LeNewlines(utf16Input, utf16YieldNewlineGenerator, execaSync));

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 99–106why, one step back
99
100// Control: UTF-8 bytes keep using UTF-8 newlines101const utf8YieldGenerator = {102 * transform(line) {103 yield Buffer.from(line, 'utf8');104 },105};106

TweaksSindre Sorhusbefore adoption698a1b8

lines 107–108why, one step back
107// With `stdin`, the newlines use UTF-8 whatever the text `encoding`, since that option only applies to the output. The subprocess echoes those bytes, which are then decoded with that option.
108const testUtf8InputNewlines = async (encoding, execaMethod) => {

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 109–110why, one step back
109 const {stdout} = await execaMethod('stdin.js', {
110 stdin: [[utf16Input], utf8YieldGenerator],

TweaksSindre Sorhusbefore adoption698a1b8

line 111why, one step back
111 encoding,

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 112–113why, one step back
112 stripFinalNewline: false,
113 });

TweaksSindre Sorhusbefore adoption698a1b8

line 114why, one step back
114 assert.equal(stdout, Buffer.from(utf16Input).toString(encoding));

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 115–116why, one step back
115};
116

TweaksSindre Sorhusbefore adoption698a1b8

lines 117–120why, one step back
117test('Newlines re-added to bytes use UTF-8 by default', () => testUtf8InputNewlines('utf8', execa));
118test('Newlines re-added to bytes use UTF-8 by default, sync', () => testUtf8InputNewlines('utf8', execaSync));119test('Newlines re-added to bytes use UTF-8 with encoding "utf16le", stdin', () => testUtf8InputNewlines('utf16le', execa));120test('Newlines re-added to bytes use UTF-8 with encoding "utf16le", stdin, sync', () => testUtf8InputNewlines('utf16le', execaSync));

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 121–122why, one step back
121
122// With the `binary` transform option, the transform deals with raw bytes: they are left untouched

TweaksSindre Sorhusbefore adoption698a1b8

line 123why, one step back
123const testBinaryUtf16Le = async execaMethod => {

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 124–129why, one step back
124 const bytes = new Uint8Array(Buffer.from(utf16Input, 'utf16le'));
125 const {stdout} = await execaMethod('stdin.js', {126 stdin: [[bytes], noopGenerator(false, true)],127 encoding: 'utf16le',128 stripFinalNewline: false,129 });

TweaksSindre Sorhusbefore adoption698a1b8

line 130why, one step back
130 assert.equal(stdout, utf16Input);

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 131–132why, one step back
131};
132

TweaksSindre Sorhusbefore adoption698a1b8

lines 133–134why, one step back
133test('"binary: true" leaves utf16le bytes untouched', () => testBinaryUtf16Le(execa));
134test('"binary: true" leaves utf16le bytes untouched, sync', () => testBinaryUtf16Le(execaSync));