all.js
This file
Path
test/resolve/all.js
284 lines · 21.3 KiB · JavaScript
Last changed by
Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285import {Buffer} from 'node:buffer';import assert from 'node:assert/strict';import test from 'node:test';import {execa, execaSync} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {defaultHighWaterMark} from '../helpers/stream.js';import {foobarString, foobarObject} from '../helpers/input.js';import {outputObjectGenerator} from '../helpers/generator.js';
setFixtureDirectory();
const textEncoder = new TextEncoder();const foobarStringFull = `${foobarString}\n`;const doubleFoobarStringFull = `${foobarStringFull}${foobarStringFull}`;const doubleFoobarString = `${foobarStringFull}${foobarString}`;const doubleFoobarUint8ArrayFull = textEncoder.encode(doubleFoobarStringFull);const doubleFoobarUint8Array = textEncoder.encode(doubleFoobarString);const doubleFoobarArrayFull = [foobarStringFull, foobarStringFull];const doubleFoobarArray = [foobarString, foobarString];
// eslint-disable-next-line max-paramsconst testAllBoth = async (expectedOutput, encoding, lines, stripFinalNewline, isFailure, execaMethod) => { const fixtureName = isFailure ? 'noop-both-fail.js' : 'noop-both.js'; const {exitCode, all} = await execaMethod(fixtureName, [foobarString], { all: true, encoding, lines, stripFinalNewline, reject: !isFailure, }); assert.equal(exitCode, isFailure ? 1 : 0); assert.deepEqual(all, expectedOutput);};
// With encodings like base64, the bytes must be merged before being encoded, otherwise the padding of the first output would end up in the middleconst testAllBinaryEncoding = (encoding, execaMethod) => testAllBoth(Buffer.from(doubleFoobarStringFull).toString(encoding), encoding, false, false, false, execaMethod);
test('result.all is defined, encoding "base64"', () => testAllBinaryEncoding('base64', execa));test('result.all is defined, encoding "base64url"', () => testAllBinaryEncoding('base64url', execa));test('result.all is defined, encoding "hex"', () => testAllBinaryEncoding('hex', execa));test('result.all is defined, encoding "latin1"', () => testAllBinaryEncoding('latin1', execa));test('result.all is defined, encoding "base64", sync', () => testAllBinaryEncoding('base64', execaSync));test('result.all is defined, encoding "base64url", sync', () => testAllBinaryEncoding('base64url', execaSync));test('result.all is defined, encoding "hex", sync', () => testAllBinaryEncoding('hex', execaSync));test('result.all is defined, encoding "latin1", sync', () => testAllBinaryEncoding('latin1', execaSync));
// An empty output is not interleaved with the objects of the other file descriptorconst testAllObjectModeEmpty = async (fixtureName, commandArguments, options, expectedAll) => { const {all} = await execa(fixtureName, commandArguments, {...options, all: true}); assert.deepEqual(all, expectedAll); const {all: allSync} = execaSync(fixtureName, commandArguments, {...options, all: true}); assert.deepEqual(allSync, expectedAll);};
test('result.all omits empty stderr with objectMode stdout', () => testAllObjectModeEmpty('noop.js', [foobarString], {stdout: outputObjectGenerator()}, [foobarObject]));test('result.all omits empty stdout with objectMode stderr', () => testAllObjectModeEmpty('noop-fd.js', ['2', foobarString], {stderr: outputObjectGenerator()}, [foobarObject]));test('result.all omits empty stderr with objectMode stdout, encoding "buffer"', () => testAllObjectModeEmpty('noop.js', [foobarString], {stdout: outputObjectGenerator(), encoding: 'buffer'}, [foobarObject]));test('result.all keeps non-empty stderr with objectMode stdout', () => testAllObjectModeEmpty('noop-both.js', [foobarString], {stdout: outputObjectGenerator()}, [foobarObject, `${foobarString}\n`]));
const fdOne = {stderr: true};const fdBoth = {stdout: true, stderr: true};
test('result.all is defined', () => testAllBoth(doubleFoobarStringFull, 'utf8', false, false, false, execa));test('result.all is defined, encoding "buffer"', () => testAllBoth(doubleFoobarUint8ArrayFull, 'buffer', false, false, false, execa));test('result.all is defined, lines', () => testAllBoth(doubleFoobarArrayFull, 'utf8', true, false, false, execa));test('result.all is defined, lines, fd-specific one', () => testAllBoth(doubleFoobarArrayFull, 'utf8', fdOne, false, false, execa));test('result.all is defined, lines, fd-specific both', () => testAllBoth(doubleFoobarArrayFull, 'utf8', fdBoth, false, false, execa));test('result.all is defined, stripFinalNewline', () => testAllBoth(doubleFoobarString, 'utf8', false, true, false, execa));test('result.all is defined, stripFinalNewline, fd-specific one', () => testAllBoth(doubleFoobarString, 'utf8', false, fdOne, false, execa));test('result.all is defined, stripFinalNewline, fd-specific both', () => testAllBoth(doubleFoobarString, 'utf8', false, fdBoth, false, execa));test('result.all is defined, encoding "buffer", stripFinalNewline', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, true, false, execa));test('result.all is defined, encoding "buffer", stripFinalNewline, fd-specific one', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, fdOne, false, execa));test('result.all is defined, encoding "buffer", stripFinalNewline, fd-specific both', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, fdBoth, false, execa));test('result.all is defined, lines, stripFinalNewline', () => testAllBoth(doubleFoobarArray, 'utf8', true, true, false, execa));test('result.all is defined, lines, fd-specific one, stripFinalNewline', () => testAllBoth(doubleFoobarArray, 'utf8', fdOne, true, false, execa));test('result.all is defined, lines, fd-specific both, stripFinalNewline', () => testAllBoth(doubleFoobarArray, 'utf8', fdBoth, true, false, execa));test('result.all is defined, lines, stripFinalNewline, fd-specific one', () => testAllBoth(doubleFoobarArray, 'utf8', true, fdOne, false, execa));test('result.all is defined, lines, stripFinalNewline, fd-specific both', () => testAllBoth(doubleFoobarArray, 'utf8', true, fdBoth, false, execa));test('result.all is defined, failure', () => testAllBoth(doubleFoobarStringFull, 'utf8', false, false, true, execa));test('result.all is defined, encoding "buffer", failure', () => testAllBoth(doubleFoobarUint8ArrayFull, 'buffer', false, false, true, execa));test('result.all is defined, lines, failure', () => testAllBoth(doubleFoobarArrayFull, 'utf8', true, false, true, execa));test('result.all is defined, lines, fd-specific one, failure', () => testAllBoth(doubleFoobarArrayFull, 'utf8', fdOne, false, true, execa));test('result.all is defined, lines, fd-specific both, failure', () => testAllBoth(doubleFoobarArrayFull, 'utf8', fdBoth, false, true, execa));test('result.all is defined, stripFinalNewline, failure', () => testAllBoth(doubleFoobarString, 'utf8', false, true, true, execa));test('result.all is defined, stripFinalNewline, fd-specific one, failure', () => testAllBoth(doubleFoobarString, 'utf8', false, fdOne, true, execa));test('result.all is defined, stripFinalNewline, fd-specific both, failure', () => testAllBoth(doubleFoobarString, 'utf8', false, fdBoth, true, execa));test('result.all is defined, encoding "buffer", stripFinalNewline, failure', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, true, true, execa));test('result.all is defined, encoding "buffer", stripFinalNewline, fd-specific one, failure', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, fdOne, true, execa));test('result.all is defined, encoding "buffer", stripFinalNewline, fd-specific both, failure', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, fdBoth, true, execa));test('result.all is defined, lines, stripFinalNewline, failure', () => testAllBoth(doubleFoobarArray, 'utf8', true, true, true, execa));test('result.all is defined, lines, fd-specific one, stripFinalNewline, failure', () => testAllBoth(doubleFoobarArray, 'utf8', fdOne, true, true, execa));test('result.all is defined, lines, fd-specific both, stripFinalNewline, failure', () => testAllBoth(doubleFoobarArray, 'utf8', fdBoth, true, true, execa));test('result.all is defined, lines, stripFinalNewline, fd-specific one, failure', () => testAllBoth(doubleFoobarArray, 'utf8', true, fdOne, true, execa));test('result.all is defined, lines, stripFinalNewline, fd-specific both, failure', () => testAllBoth(doubleFoobarArray, 'utf8', true, fdBoth, true, execa));test('result.all is defined, sync', () => testAllBoth(doubleFoobarStringFull, 'utf8', false, false, false, execaSync));test('result.all is defined, encoding "buffer", sync', () => testAllBoth(doubleFoobarUint8ArrayFull, 'buffer', false, false, false, execaSync));test('result.all is defined, lines, sync', () => testAllBoth(doubleFoobarArrayFull, 'utf8', true, false, false, execaSync));test('result.all is defined, lines, fd-specific one, sync', () => testAllBoth(doubleFoobarArrayFull, 'utf8', fdOne, false, false, execaSync));test('result.all is defined, lines, fd-specific both, sync', () => testAllBoth(doubleFoobarArrayFull, 'utf8', fdBoth, false, false, execaSync));test('result.all is defined, stripFinalNewline, sync', () => testAllBoth(doubleFoobarString, 'utf8', false, true, false, execaSync));test('result.all is defined, stripFinalNewline, fd-specific one, sync', () => testAllBoth(doubleFoobarString, 'utf8', false, fdOne, false, execaSync));test('result.all is defined, stripFinalNewline, fd-specific both, sync', () => testAllBoth(doubleFoobarString, 'utf8', false, fdBoth, false, execaSync));test('result.all is defined, encoding "buffer", stripFinalNewline, sync', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, true, false, execaSync));test('result.all is defined, encoding "buffer", stripFinalNewline, fd-specific one, sync', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, fdOne, false, execaSync));test('result.all is defined, encoding "buffer", stripFinalNewline, fd-specific both, sync', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, fdBoth, false, execaSync));test('result.all is defined, lines, stripFinalNewline, sync', () => testAllBoth(doubleFoobarArray, 'utf8', true, true, false, execaSync));test('result.all is defined, lines, fd-specific one, stripFinalNewline, sync', () => testAllBoth(doubleFoobarArray, 'utf8', fdOne, true, false, execaSync));test('result.all is defined, lines, fd-specific both, stripFinalNewline, sync', () => testAllBoth(doubleFoobarArray, 'utf8', fdBoth, true, false, execaSync));test('result.all is defined, lines, stripFinalNewline, fd-specific one, sync', () => testAllBoth(doubleFoobarArray, 'utf8', true, fdOne, false, execaSync));test('result.all is defined, lines, stripFinalNewline, fd-specific both, sync', () => testAllBoth(doubleFoobarArray, 'utf8', true, fdBoth, false, execaSync));test('result.all is defined, failure, sync', () => testAllBoth(doubleFoobarStringFull, 'utf8', false, false, true, execaSync));test('result.all is defined, encoding "buffer", failure, sync', () => testAllBoth(doubleFoobarUint8ArrayFull, 'buffer', false, false, true, execaSync));test('result.all is defined, lines, failure, sync', () => testAllBoth(doubleFoobarArrayFull, 'utf8', true, false, true, execaSync));test('result.all is defined, lines, fd-specific one, failure, sync', () => testAllBoth(doubleFoobarArrayFull, 'utf8', fdOne, false, true, execaSync));test('result.all is defined, lines, fd-specific both, failure, sync', () => testAllBoth(doubleFoobarArrayFull, 'utf8', fdBoth, false, true, execaSync));test('result.all is defined, stripFinalNewline, failure, sync', () => testAllBoth(doubleFoobarString, 'utf8', false, true, true, execaSync));test('result.all is defined, stripFinalNewline, fd-specific one, failure, sync', () => testAllBoth(doubleFoobarString, 'utf8', false, fdOne, true, execaSync));test('result.all is defined, stripFinalNewline, fd-specific both, failure, sync', () => testAllBoth(doubleFoobarString, 'utf8', false, fdBoth, true, execaSync));test('result.all is defined, encoding "buffer", stripFinalNewline, failure, sync', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, true, true, execaSync));test('result.all is defined, encoding "buffer", stripFinalNewline, fd-specific one, failure, sync', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, fdOne, true, execaSync));test('result.all is defined, encoding "buffer", stripFinalNewline, fd-specific both, failure, sync', () => testAllBoth(doubleFoobarUint8Array, 'buffer', false, fdBoth, true, execaSync));test('result.all is defined, lines, stripFinalNewline, failure, sync', () => testAllBoth(doubleFoobarArray, 'utf8', true, true, true, execaSync));test('result.all is defined, lines, fd-specific one, stripFinalNewline, failure, sync', () => testAllBoth(doubleFoobarArray, 'utf8', fdOne, true, true, execaSync));test('result.all is defined, lines, fd-specific both, stripFinalNewline, failure, sync', () => testAllBoth(doubleFoobarArray, 'utf8', fdBoth, true, true, execaSync));test('result.all is defined, lines, stripFinalNewline, fd-specific one, failure, sync', () => testAllBoth(doubleFoobarArray, 'utf8', true, fdOne, true, execaSync));test('result.all is defined, lines, stripFinalNewline, fd-specific both, failure, sync', () => testAllBoth(doubleFoobarArray, 'utf8', true, fdBoth, true, execaSync));
test('result.all shows both `stdout` and `stderr` intermixed', async () => { const {all} = await execa('noop-132.js', {all: true}); assert.equal(all, '1\n2\n3');});
test('result.all shows both `stdout` and `stderr` not intermixed, sync', () => { const {all} = execaSync('noop-132.js', {all: true}); assert.equal(all, '1\n3\n2');});
const testAllIgnored = async (options, execaMethod) => { const {all} = await execaMethod('noop.js'); assert.equal(all, undefined);};
test('result.all is undefined unless opts.all is true', () => testAllIgnored({}, execa));test('result.all is undefined if opts.all is false', () => testAllIgnored({all: false}, execa));test('result.all is undefined if ignored', () => testAllIgnored({stdio: 'ignore', all: true}, execa));test('result.all is undefined unless opts.all is true, sync', () => testAllIgnored({}, execaSync));test('result.all is undefined if opts.all is false, sync', () => testAllIgnored({all: false}, execaSync));test('result.all is undefined if ignored, sync', () => testAllIgnored({stdio: 'ignore', all: true}, execaSync));
const testAllProperties = async options => { const subprocess = execa('empty.js', {...options, all: true}); assert.equal(subprocess.all.readableObjectMode, false); assert.equal(subprocess.all.readableHighWaterMark, defaultHighWaterMark); await subprocess;};
test('subprocess.all has the right objectMode and highWaterMark - stdout + stderr', () => testAllProperties({}));test('subprocess.all has the right objectMode and highWaterMark - stdout only', () => testAllProperties({stderr: 'ignore'}));test('subprocess.all has the right objectMode and highWaterMark - stderr only', () => testAllProperties({stdout: 'ignore'}));
const testAllIgnore = async (streamName, otherStreamName) => { const subprocess = execa('noop-both.js', {[otherStreamName]: 'ignore', all: true}); assert.equal(subprocess[otherStreamName], null); assert.notEqual(subprocess[streamName], null); assert.notEqual(subprocess.all, null); assert.equal(subprocess.all.readableObjectMode, subprocess[streamName].readableObjectMode); assert.equal(subprocess.all.readableHighWaterMark, subprocess[streamName].readableHighWaterMark);
const result = await subprocess; assert.equal(result[otherStreamName], undefined); assert.equal(result[streamName], foobarString); assert.equal(result.all, foobarString);};
test('can use all: true with stdout: ignore', () => testAllIgnore('stderr', 'stdout'));test('can use all: true with stderr: ignore', () => testAllIgnore('stdout', 'stderr'));
const testAllIgnoreSync = (streamName, otherStreamName) => { const result = execaSync('noop-both.js', {[otherStreamName]: 'ignore', all: true}); assert.equal(result[otherStreamName], undefined); assert.equal(result[streamName], foobarString); assert.equal(result.all, foobarString);};
test('can use all: true with stdout: ignore, sync', () => testAllIgnoreSync('stderr', 'stdout'));test('can use all: true with stderr: ignore, sync', () => testAllIgnoreSync('stdout', 'stderr'));
test('can use all: true with stdout: ignore + stderr: ignore', async () => { const subprocess = execa('noop-both.js', {stdout: 'ignore', stderr: 'ignore', all: true}); assert.equal(subprocess.stdout, null); assert.equal(subprocess.stderr, null); assert.equal(subprocess.all, undefined);
const {stdout, stderr, all} = await subprocess; assert.equal(stdout, undefined); assert.equal(stderr, undefined); assert.equal(all, undefined);});
test('can use all: true with stdout: ignore + stderr: ignore, sync', () => { const {stdout, stderr, all} = execaSync('noop-both.js', {stdout: 'ignore', stderr: 'ignore', all: true}); assert.equal(stdout, undefined); assert.equal(stderr, undefined); assert.equal(all, undefined);});
// `all` is split into lines when the `lines` option applies to either file descriptor, even when only one of them uses itconst foobarTwoLines = `${foobarString}\n${foobarString}`;// The fixture writes the same two lines to both `stdout` and `stderr`const foobarTwoLinesArray = Array.from({length: 4}, () => `${foobarString}\n`);const testAllBothLines = async (lines, execaMethod) => { const {all} = await execaMethod('noop-both.js', [foobarTwoLines], {all: true, lines, stripFinalNewline: false}); assert.deepEqual(all, foobarTwoLinesArray);};
test('result.all is split into lines when only stdout uses the lines option', () => testAllBothLines({stdout: true}, execa));test('result.all is split into lines when only stderr uses the lines option', () => testAllBothLines({stderr: true}, execa));test('result.all is split into lines when only stdout uses the lines option, sync', () => testAllBothLines({stdout: true}, execaSync));test('result.all is split into lines when only stderr uses the lines option, sync', () => testAllBothLines({stderr: true}, execaSync));
// When only one file descriptor is interleaved into `all`, it must follow that one exactly, including its `stripFinalNewline` valueconst testAllOneFd = async (lines, execaMethod) => { const {stderr, all} = await execaMethod('noop-both.js', [foobarString], { all: true, stdout: 'ignore', lines, stripFinalNewline: {stderr: false}, }); assert.deepEqual(stderr, lines ? [`${foobarString}\n`] : `${foobarString}\n`); assert.deepEqual(all, stderr);};
test('result.all follows stderr when stdout is ignored', () => testAllOneFd(false, execa));test('result.all follows stderr when stdout is ignored, lines', () => testAllOneFd(true, execa));test('result.all follows stderr when stdout is ignored, sync', () => testAllOneFd(false, execaSync));test('result.all follows stderr when stdout is ignored, lines, sync', () => testAllOneFd(true, execaSync));
// `all` is split into lines as soon as the `lines` option applies to either file descriptor, even the one which is not interleaved into `all`const testAllOneFdOtherLines = async (linesFdName, ignoredFdName, stripFinalNewline, execaMethod) => { const {all} = await execaMethod('noop-both.js', [foobarString], { all: true, [ignoredFdName]: 'ignore', lines: {[linesFdName]: true}, stripFinalNewline, }); assert.deepEqual(all, [stripFinalNewline ? foobarString : `${foobarString}\n`]);};
test('result.all is split into lines when only stdout uses the lines option and stderr is ignored', () => testAllOneFdOtherLines('stdout', 'stderr', true, execa));test('result.all is split into lines when only stdout uses the lines option and stderr is ignored, stripFinalNewline false', () => testAllOneFdOtherLines('stdout', 'stderr', false, execa));test('result.all is split into lines when only stderr uses the lines option and stdout is ignored', () => testAllOneFdOtherLines('stderr', 'stdout', true, execa));test('result.all is split into lines when only stdout uses the lines option and stderr is ignored, sync', () => testAllOneFdOtherLines('stdout', 'stderr', true, execaSync));test('result.all is split into lines when only stderr uses the lines option and stdout is ignored, sync', () => testAllOneFdOtherLines('stderr', 'stdout', true, execaSync));
// `splitLinesSync()` passes a non-string chunk straight through, so an absent file descriptor// must not be split, or it would be reported as a line holding `undefined`.const testAllNoFd = async (lines, execaMethod) => { const {all} = await execaMethod('noop.js', [], { all: true, lines, stdout: 'ignore', stderr: 'ignore', }); assert.equal(all, undefined);};
test('result.all is undefined when no file descriptor is buffered', () => testAllNoFd(false, execa));test('result.all is undefined when no file descriptor is buffered, lines', () => testAllNoFd(true, execa));test('result.all is undefined when no file descriptor is buffered, sync', () => testAllNoFd(false, execaSync));test('result.all is undefined when no file descriptor is buffered, lines, sync', () => testAllNoFd(true, execaSync));
const testUtf16LinesAll = async execaMethod => { const script = 'process.stdout.write(Buffer.from(\'a\\r\\nb\\n\', \'utf16le\')); process.stderr.write(Buffer.from(\'c\\n\', \'utf16le\'))'; const {all} = await execaMethod('node', ['-e', script], { all: true, lines: true, encoding: 'utf16le', stripFinalNewline: false, }); assert.deepEqual(all, ['a\r\n', 'b\n', 'c\n']);};
test('result.all splits lines with utf16le encoding', () => testUtf16LinesAll(execa));test('result.all splits lines with utf16le encoding, sync', () => testUtf16LinesAll(execaSync));