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 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';Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 2–3why, one step back2import process from 'node:process';3import {Buffer} from 'node:buffer';TweaksSindre Sorhusbefore adoption698a1b8
line 4why, one step back4import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 5–14why, one step back5import {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';14TweaksSindre Sorhusbefore adoption698a1b8
line 15why, one step back15const testStripNewline = async (input, expectedOutput, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 16–19why, one step back16 const {stdout} = await execaMethod('noop.js', {17 stdout: getOutputsGenerator([input])(),18 stripFinalNewline: false,19 });TweaksSindre Sorhusbefore adoption698a1b8
line 20why, one step back20 assert.equal(stdout, expectedOutput);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 21–22why, one step back21};22TweaksSindre Sorhusbefore adoption698a1b8
lines 23–28why, one step back23test('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 back29TweaksSindre Sorhusbefore adoption698a1b8
line 30why, one step back30const testMixNewlines = async (generator, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 31–34why, one step back31 const {stdout} = await execaMethod('noop-fd.js', ['1', mixedNewlines], {32 stdout: generator(),33 stripFinalNewline: false,34 });TweaksSindre Sorhusbefore adoption698a1b8
line 35why, one step back35 assert.equal(stdout, mixedNewlines);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 36–37why, one step back36};37TweaksSindre Sorhusbefore adoption698a1b8
lines 38–40why, one step back38test('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 back4142// 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 back44test('Detects Windows newlines split across chunks', async () => {Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 45why, one step back45 const {stdout} = await execa('noop-progressive.js', ['aaa\r'], {lines: true});TweaksSindre Sorhusbefore adoption698a1b8
line 46why, one step back46 assert.deepEqual(stdout, ['aaa']);Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 47–48why, one step back47});48TweaksSindre Sorhusbefore adoption698a1b8
line 49why, one step back49test('Keeps Windows newlines split across chunks', async () => {Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 50why, one step back50 const {stdout} = await execa('noop-progressive.js', ['aaa\r'], {lines: true, stripFinalNewline: false});TweaksSindre Sorhusbefore adoption698a1b8
line 51why, one step back51 assert.deepEqual(stdout, ['aaa\r\n']);Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 52–53why, one step back52});53TweaksSindre Sorhusbefore adoption698a1b8
line 54why, one step back54test('Detects multiple Windows newlines split across chunks', async () => {Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 55why, one step back55 const {stdout} = await execa('noop-progressive.js', ['aaa\r\nbbb\r'], {lines: true});TweaksSindre Sorhusbefore adoption698a1b8
line 56why, one step back56 assert.deepEqual(stdout, ['aaa', 'bbb']);Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 57–58why, one step back57});58TweaksSindre Sorhusbefore adoption698a1b8
line 59why, one step back59test('Keeps lone carriage returns split across chunks', async () => {Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 60why, one step back60 const {stdout} = await execa('noop-progressive.js', ['aaa\rbbb'], {lines: true});TweaksSindre Sorhusbefore adoption698a1b8
line 61why, one step back61 assert.deepEqual(stdout, ['aaa\rbbb']);Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 62–64why, one step back62});6364/*TweaksSindre Sorhusbefore adoption698a1b8
line 65why, one step back65The 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 back66*/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};82TweaksSindre Sorhusbefore adoption698a1b8
lines 83–86why, one step back83// The subprocess writes its output with UTF-1684const 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 back87 encoding: 'utf16le',88 stripFinalNewline: false,89 });TweaksSindre Sorhusbefore adoption698a1b8
line 90why, one step back90 assert.equal(stdout, output);Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 91–92why, one step back91};92TweaksSindre Sorhusbefore adoption698a1b8
lines 93–98why, one step back93test('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 back99100// Control: UTF-8 bytes keep using UTF-8 newlines101const utf8YieldGenerator = {102 * transform(line) {103 yield Buffer.from(line, 'utf8');104 },105};106TweaksSindre Sorhusbefore adoption698a1b8
lines 107–108why, one step back107// 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 back109 const {stdout} = await execaMethod('stdin.js', {110 stdin: [[utf16Input], utf8YieldGenerator],TweaksSindre Sorhusbefore adoption698a1b8
line 111why, one step back111 encoding,Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 112–113why, one step back112 stripFinalNewline: false,113 });TweaksSindre Sorhusbefore adoption698a1b8
line 114why, one step back114 assert.equal(stdout, Buffer.from(utf16Input).toString(encoding));Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 115–116why, one step back115};116TweaksSindre Sorhusbefore adoption698a1b8
lines 117–120why, one step back117test('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 back121122// With the `binary` transform option, the transform deals with raw bytes: they are left untouchedTweaksSindre Sorhusbefore adoption698a1b8
line 123why, one step back123const testBinaryUtf16Le = async execaMethod => {Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 124–129why, one step back124 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 back130 assert.equal(stdout, utf16Input);Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 131–132why, one step back131};132TweaksSindre Sorhusbefore adoption698a1b8
lines 133–134why, one step back133test('"binary: true" leaves utf16le bytes untouched', () => testBinaryUtf16Le(execa));134test('"binary: true" leaves utf16le bytes untouched, sync', () => testBinaryUtf16Le(execaSync));