file-path-mixed.js
This file
Path
test/stdio/file-path-mixed.js
95 lines · 6.2 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596import assert from 'node:assert/strict';import {readFile, writeFile, rm} from 'node:fs/promises';import {pathToFileURL} from 'node:url';import test from 'node:test';import tempfile from 'tempfile';import {execa, execaSync} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {getStdio} from '../helpers/stdio.js';import {foobarString, foobarUppercase} from '../helpers/input.js';import {uppercaseGenerator} from '../helpers/generator.js';import {getAbsolutePath} from '../helpers/file-path.js';
setFixtureDirectory();
const testInputFileTransform = async (fdNumber, mapFile, execaMethod) => { const filePath = tempfile(); await writeFile(filePath, foobarString); const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [ new Uint8Array(), mapFile(filePath), uppercaseGenerator(), ])); assert.equal(stdout, foobarUppercase); await rm(filePath);};
test('stdin can use generators together with input file paths', () => testInputFileTransform(0, getAbsolutePath, execa));test('stdin can use generators together with input file URLs', () => testInputFileTransform(0, pathToFileURL, execa));test('stdio[*] can use generators together with input file paths', () => testInputFileTransform(3, getAbsolutePath, execa));test('stdio[*] can use generators together with input file URLs', () => testInputFileTransform(3, pathToFileURL, execa));test('stdin can use generators together with input file paths, sync', () => testInputFileTransform(0, getAbsolutePath, execaSync));test('stdin can use generators together with input file URLs, sync', () => testInputFileTransform(0, pathToFileURL, execaSync));
const testOutputFileTransform = async (fdNumber, mapFile, execaMethod) => { const filePath = tempfile(); await execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, [ uppercaseGenerator(), mapFile(filePath), ])); assert.equal(await readFile(filePath, 'utf8'), `${foobarUppercase}\n`); await rm(filePath);};
test('stdout can use generators together with output file paths', () => testOutputFileTransform(1, getAbsolutePath, execa));test('stdout can use generators together with output file URLs', () => testOutputFileTransform(1, pathToFileURL, execa));test('stderr can use generators together with output file paths', () => testOutputFileTransform(2, getAbsolutePath, execa));test('stderr can use generators together with output file URLs', () => testOutputFileTransform(2, pathToFileURL, execa));test('stdio[*] can use generators together with output file paths', () => testOutputFileTransform(3, getAbsolutePath, execa));test('stdio[*] can use generators together with output file URLs', () => testOutputFileTransform(3, pathToFileURL, execa));test('stdout can use generators together with output file paths, sync', () => testOutputFileTransform(1, getAbsolutePath, execaSync));test('stdout can use generators together with output file URLs, sync', () => testOutputFileTransform(1, pathToFileURL, execaSync));test('stderr can use generators together with output file paths, sync', () => testOutputFileTransform(2, getAbsolutePath, execaSync));test('stderr can use generators together with output file URLs, sync', () => testOutputFileTransform(2, pathToFileURL, execaSync));test('stdio[*] can use generators together with output file paths, sync', () => testOutputFileTransform(3, getAbsolutePath, execaSync));test('stdio[*] can use generators together with output file URLs, sync', () => testOutputFileTransform(3, pathToFileURL, execaSync));
const testOutputFileLines = async (fdNumber, mapFile, execaMethod) => { const filePath = tempfile(); const {stdio} = await execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], { ...getStdio(fdNumber, mapFile(filePath)), lines: true, }); assert.deepEqual(stdio[fdNumber], [foobarString]); assert.equal(await readFile(filePath, 'utf8'), foobarString); await rm(filePath);};
test('stdout can use "lines: true" together with output file paths', () => testOutputFileLines(1, getAbsolutePath, execa));test('stdout can use "lines: true" together with output file URLs', () => testOutputFileLines(1, pathToFileURL, execa));test('stderr can use "lines: true" together with output file paths', () => testOutputFileLines(2, getAbsolutePath, execa));test('stderr can use "lines: true" together with output file URLs', () => testOutputFileLines(2, pathToFileURL, execa));test('stdio[*] can use "lines: true" together with output file paths', () => testOutputFileLines(3, getAbsolutePath, execa));test('stdio[*] can use "lines: true" together with output file URLs', () => testOutputFileLines(3, pathToFileURL, execa));test('stdout can use "lines: true" together with output file paths, sync', () => testOutputFileLines(1, getAbsolutePath, execaSync));test('stdout can use "lines: true" together with output file URLs, sync', () => testOutputFileLines(1, pathToFileURL, execaSync));test('stderr can use "lines: true" together with output file paths, sync', () => testOutputFileLines(2, getAbsolutePath, execaSync));test('stderr can use "lines: true" together with output file URLs, sync', () => testOutputFileLines(2, pathToFileURL, execaSync));test('stdio[*] can use "lines: true" together with output file paths, sync', () => testOutputFileLines(3, getAbsolutePath, execaSync));test('stdio[*] can use "lines: true" together with output file URLs, sync', () => testOutputFileLines(3, pathToFileURL, execaSync));
const testOutputFileNoBuffer = async (buffer, execaMethod) => { const filePath = tempfile(); const {stdout} = await execaMethod('noop-fd.js', ['1', foobarString], { stdout: getAbsolutePath(filePath), buffer, }); assert.equal(stdout, undefined); assert.equal(await readFile(filePath, 'utf8'), foobarString); await rm(filePath);};
test('stdout can use "buffer: false" together with output file paths', () => testOutputFileNoBuffer(false, execa));test('stdout can use "buffer: false" together with output file paths, fd-specific', () => testOutputFileNoBuffer({stdout: false}, execa));test('stdout can use "buffer: false" together with output file paths, sync', () => testOutputFileNoBuffer(false, execaSync));test('stdout can use "buffer: false" together with output file paths, fd-specific, sync', () => testOutputFileNoBuffer({stdout: false}, execaSync));