Skip to content
Myr

input-option.js

This file

Path

test/io/input-option.js

129 lines · 7.1 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

line 2why, one step back
2import {Buffer} from 'node:buffer';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 3why, one step back
3import {Writable} from 'node:stream';

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 6–14why, one step back
6import {execa, execaSync} from '../../index.js';
7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8import {9 runExeca,10 runExecaSync,11 runScript,12 runScriptSync,13} from '../helpers/run.js';14import {

Fix input with inherited stdin (#1232)Puneet Dixitbefore adoption3ed0544

line 15why, one step back
15 foobarString,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 16–21why, one step back
16 foobarUint8Array,
17 foobarBuffer,18 foobarArrayBuffer,19 foobarUint16Array,20 foobarDataView,21} from '../helpers/input.js';

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

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 23–25why, one step back
23
24setFixtureDirectory();25

TweaksSindre Sorhusbefore adoption698a1b8

line 26why, one step back
26const testInput = async (input, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 27why, one step back
27 const {stdout} = await execaMethod('stdin.js', {input});

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 31–38why, one step back
31test('input option can be a String', () => testInput('foobar', runExeca));
32test('input option can be a Uint8Array', () => testInput(foobarUint8Array, runExeca));33test('input option can be a Buffer', () => testInput(foobarBuffer, runExeca));34test('input option can be a String - sync', () => testInput('foobar', runExecaSync));35test('input option can be a Uint8Array - sync', () => testInput(foobarUint8Array, runExecaSync));36test('input option can be a Buffer - sync', () => testInput(foobarBuffer, runExecaSync));37test('input option can be used with $', () => testInput('foobar', runScript));38test('input option can be used with $.sync', () => testInput('foobar', runScriptSync));

Fix input with inherited stdin (#1232)Puneet Dixitbefore adoption3ed0544

line 39why, one step back
39

TweaksSindre Sorhusbefore adoption698a1b8

line 40why, one step back
40const testInputIgnoresInheritedStdin = async (optionName, stdioOption, isSync) => {

Fix input with inherited stdin (#1232)Puneet Dixitbefore adoption3ed0544

line 41why, one step back
41 const {stdout} = await execa('nested-input-inherit.js', [optionName, JSON.stringify(stdioOption), `${isSync}`], {input: foobarString});

TweaksSindre Sorhusbefore adoption698a1b8

line 42why, one step back
42 assert.equal(stdout, foobarString);

Fix input with inherited stdin (#1232)Puneet Dixitbefore adoption3ed0544

lines 43–44why, one step back
43};
44

TweaksSindre Sorhusbefore adoption698a1b8

lines 45–52why, one step back
45test('input option ignores stdin "inherit"', () => testInputIgnoresInheritedStdin('stdin', 'inherit', false));
46test('input option ignores stdin ["inherit"]', () => testInputIgnoresInheritedStdin('stdin', ['inherit'], false));47test('input option ignores stdio "inherit"', () => testInputIgnoresInheritedStdin('stdio', 'inherit', false));48test('input option ignores stdio[0] "inherit"', () => testInputIgnoresInheritedStdin('stdio', ['inherit', 'inherit', 'pipe'], false));49test('input option ignores stdin "inherit" - sync', () => testInputIgnoresInheritedStdin('stdin', 'inherit', true));50test('input option ignores stdin ["inherit"] - sync', () => testInputIgnoresInheritedStdin('stdin', ['inherit'], true));51test('input option ignores stdio "inherit" - sync', () => testInputIgnoresInheritedStdin('stdio', 'inherit', true));52test('input option ignores stdio[0] "inherit" - sync', () => testInputIgnoresInheritedStdin('stdio', ['inherit', 'inherit', 'pipe'], true));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 53why, one step back
53

TweaksSindre Sorhusbefore adoption698a1b8

lines 54–55why, one step back
54const testInvalidInput = async (input, execaMethod) => {
55 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 56–59why, one step back
56 execaMethod('empty.js', {input});
57 }, {message: /a string, a Uint8Array/});58};59

TweaksSindre Sorhusbefore adoption698a1b8

lines 60–73why, one step back
60test('input option cannot be an ArrayBuffer', () => testInvalidInput(foobarArrayBuffer, execa));
61test('input option cannot be a DataView', () => testInvalidInput(foobarDataView, execa));62test('input option cannot be a Uint16Array', () => testInvalidInput(foobarUint16Array, execa));63test('input option cannot be 0', () => testInvalidInput(0, execa));64test('input option cannot be false', () => testInvalidInput(false, execa));65test('input option cannot be null', () => testInvalidInput(null, execa));66test('input option cannot be a non-Readable stream', () => testInvalidInput(new Writable(), execa));67test('input option cannot be an ArrayBuffer - sync', () => testInvalidInput(foobarArrayBuffer, execaSync));68test('input option cannot be a DataView - sync', () => testInvalidInput(foobarDataView, execaSync));69test('input option cannot be a Uint16Array - sync', () => testInvalidInput(foobarUint16Array, execaSync));70test('input option cannot be 0 - sync', () => testInvalidInput(0, execaSync));71test('input option cannot be false - sync', () => testInvalidInput(false, execaSync));72test('input option cannot be null - sync', () => testInvalidInput(null, execaSync));73test('input option cannot be a non-Readable stream - sync', () => testInvalidInput(new Writable(), execaSync));

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

lines 74–75why, one step back
74
75/*

TweaksSindre Sorhusbefore adoption698a1b8

lines 76–77why, one step back
76The `encoding` option only applies to the output: string input is always encoded with UTF-8, like with a binary encoding.
77Since the subprocess echoes its input, its output is those UTF-8 bytes decoded as UTF-16.

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

lines 78–80why, one step back
78This must not depend on whether a `stdin` transform happens to be present.
79*/80const nonAsciiString = '🦄🦄';

TweaksSindre Sorhusbefore adoption698a1b8

line 81why, one step back
81const getUtf16Output = input => Buffer.from(input).toString('utf16le');

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

line 82why, one step back
82

TweaksSindre Sorhusbefore adoption698a1b8

line 83why, one step back
83const testStringEncoding = async (options, execaMethod) => {

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

line 84why, one step back
84 const {stdout} = await execaMethod('stdin.js', {...options, input: nonAsciiString, encoding: 'utf16le'});

TweaksSindre Sorhusbefore adoption698a1b8

line 85why, one step back
85 assert.equal(stdout, getUtf16Output(nonAsciiString));

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

lines 86–87why, one step back
86};
87

TweaksSindre Sorhusbefore adoption698a1b8

lines 88–91why, one step back
88test('input option string is encoded with UTF-8 with encoding "utf16le" without transforms', () => testStringEncoding({}, execa));
89test('input option string is encoded with UTF-8 with encoding "utf16le" without transforms - sync', () => testStringEncoding({}, execaSync));90test('input option string is encoded with UTF-8 with encoding "utf16le" with transforms', () => testStringEncoding({stdin: noopGenerator()}, execa));91test('input option string is encoded with UTF-8 with encoding "utf16le" with transforms - sync', () => testStringEncoding({stdin: noopGenerator()}, execaSync));

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

line 92why, one step back
92

TweaksSindre Sorhusbefore adoption698a1b8

lines 93–95why, one step back
93// Sources other than the `input` option also pass strings to the subprocess, which are encoded with UTF-8 too
94const testIterableInputEncoding = async execaMethod => {95 const input = [`${nonAsciiString}aaa\n`, `${nonAsciiString}bbb\n`];

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

line 96why, one step back
96 const {stdout} = await execaMethod('stdin.js', {

TweaksSindre Sorhusbefore adoption698a1b8

line 97why, one step back
97 stdin: input.map(chunk => [chunk]),

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

lines 98–100why, one step back
98 encoding: 'utf16le',
99 stripFinalNewline: false,100 });

TweaksSindre Sorhusbefore adoption698a1b8

line 101why, one step back
101 assert.equal(stdout, getUtf16Output(input.join('')));

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

lines 102–103why, one step back
102};
103

TweaksSindre Sorhusbefore adoption698a1b8

lines 104–105why, one step back
104test('Iterable string input is encoded with UTF-8 with encoding "utf16le"', () => testIterableInputEncoding(execa));
105test('Iterable string input is encoded with UTF-8 with encoding "utf16le" - sync', () => testIterableInputEncoding(execaSync));

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

line 106why, one step back
106

TweaksSindre Sorhusbefore adoption698a1b8

line 107why, one step back
107const testAsyncIterableInputEncoding = async () => {

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

lines 108–112why, one step back
108 const chunks = async function * () {
109 yield nonAsciiString;110 };111112 const {stdout} = await execa('stdin.js', {stdin: [chunks()], encoding: 'utf16le'});

TweaksSindre Sorhusbefore adoption698a1b8

line 113why, one step back
113 assert.equal(stdout, getUtf16Output(nonAsciiString));

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

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

TweaksSindre Sorhusbefore adoption698a1b8

line 116why, one step back
116test('Async iterable string input is encoded with UTF-8 with encoding "utf16le"', () => testAsyncIterableInputEncoding());

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

lines 117–120why, one step back
117
118/*119The `encoding` option does not change how string input is encoded when it is a binary encoding: those describe how output bytes are serialized, so input keeps using UTF-8.120*/

TweaksSindre Sorhusbefore adoption698a1b8

line 121why, one step back
121const testBinaryEncodingInput = async (encoding, execaMethod) => {

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

line 122why, one step back
122 const {stdout} = await execaMethod('stdin.js', {input: nonAsciiString, encoding});

TweaksSindre Sorhusbefore adoption698a1b8

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

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

lines 124–125why, one step back
124};
125

TweaksSindre Sorhusbefore adoption698a1b8

lines 126–129why, one step back
126test('String input stays UTF-8 with encoding "latin1"', () => testBinaryEncodingInput('latin1', execa));
127test('String input stays UTF-8 with encoding "latin1" - sync', () => testBinaryEncodingInput('latin1', execaSync));128test('String input stays UTF-8 with encoding "hex"', () => testBinaryEncodingInput('hex', execa));129test('String input stays UTF-8 with encoding "hex" - sync', () => testBinaryEncodingInput('hex', execaSync));