file-path-error.js
This file
Path
test/stdio/file-path-error.js
173 lines · 9.2 KiB · JavaScript
Last changed by
Upgrade xo (#980)ehmickybefore adoptionccfa412
History of this fileTake it
Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 1–6why, one step back1import {readFile, writeFile, rm} from 'node:fs/promises';2import {pathToFileURL} from 'node:url';3import test from 'ava';4import {pathExists} from 'path-exists';5import tempfile from 'tempfile';6import {execa, execaSync} from '../../index.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
line 7why, one step back7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 8–9why, one step back8import {identity, getStdio} from '../helpers/stdio.js';9import {foobarString, foobarUppercase} from '../helpers/input.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 10–15why, one step back10import {11 outputObjectGenerator,12 uppercaseGenerator,13 serializeGenerator,14 throwingGenerator,15} from '../helpers/generator.js';Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 16–17why, one step back16import {getAbsolutePath} from '../helpers/file-path.js';17Upgrade xo (#980)ehmickybefore adoptionccfa412
line 18why, one step back18setFixtureDirectory();Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 19–173why, one step back1920const nonFileUrl = new URL('https://example.com');2122const testStdioNonFileUrl = (t, fdNumber, execaMethod) => {23 t.throws(() => {24 execaMethod('empty.js', getStdio(fdNumber, nonFileUrl));25 }, {message: /pathToFileURL/});26};2728test('inputFile cannot be a non-file URL', testStdioNonFileUrl, 'inputFile', execa);29test('stdin cannot be a non-file URL', testStdioNonFileUrl, 0, execa);30test('stdout cannot be a non-file URL', testStdioNonFileUrl, 1, execa);31test('stderr cannot be a non-file URL', testStdioNonFileUrl, 2, execa);32test('stdio[*] cannot be a non-file URL', testStdioNonFileUrl, 3, execa);33test('inputFile cannot be a non-file URL - sync', testStdioNonFileUrl, 'inputFile', execaSync);34test('stdin cannot be a non-file URL - sync', testStdioNonFileUrl, 0, execaSync);35test('stdout cannot be a non-file URL - sync', testStdioNonFileUrl, 1, execaSync);36test('stderr cannot be a non-file URL - sync', testStdioNonFileUrl, 2, execaSync);37test('stdio[*] cannot be a non-file URL - sync', testStdioNonFileUrl, 3, execaSync);3839const testInvalidInputFile = (t, execaMethod) => {40 t.throws(() => {41 execaMethod('empty.js', getStdio('inputFile', false));42 }, {message: /a file path string or a file URL/});43};4445test('inputFile must be a file URL or string', testInvalidInputFile, execa);46test('inputFile must be a file URL or string - sync', testInvalidInputFile, execaSync);4748const testFilePathObject = (t, fdNumber, execaMethod) => {49 t.throws(() => {50 execaMethod('empty.js', getStdio(fdNumber, foobarString));51 }, {message: /must be used/});52};5354test('stdin must be an object when it is a file path string', testFilePathObject, 0, execa);55test('stdout must be an object when it is a file path string', testFilePathObject, 1, execa);56test('stderr must be an object when it is a file path string', testFilePathObject, 2, execa);57test('stdio[*] must be an object when it is a file path string', testFilePathObject, 3, execa);58test('stdin be an object when it is a file path string - sync', testFilePathObject, 0, execaSync);59test('stdout be an object when it is a file path string - sync', testFilePathObject, 1, execaSync);60test('stderr be an object when it is a file path string - sync', testFilePathObject, 2, execaSync);61test('stdio[*] must be an object when it is a file path string - sync', testFilePathObject, 3, execaSync);6263const testFileError = async (t, fixtureName, mapFile, fdNumber) => {64 await t.throwsAsync(65 execa(fixtureName, [`${fdNumber}`], getStdio(fdNumber, mapFile('./unknown/file'))),66 {code: 'ENOENT'},67 );68};6970test.serial('inputFile file URL errors should be handled', testFileError, 'stdin-fd.js', pathToFileURL, 'inputFile');71test.serial('stdin file URL errors should be handled', testFileError, 'stdin-fd.js', pathToFileURL, 0);72test.serial('stdout file URL errors should be handled', testFileError, 'noop-fd.js', pathToFileURL, 1);73test.serial('stderr file URL errors should be handled', testFileError, 'noop-fd.js', pathToFileURL, 2);74test.serial('stdio[*] file URL errors should be handled', testFileError, 'noop-fd.js', pathToFileURL, 3);75test.serial('inputFile file path errors should be handled', testFileError, 'stdin-fd.js', identity, 'inputFile');76test.serial('stdin file path errors should be handled', testFileError, 'stdin-fd.js', getAbsolutePath, 0);77test.serial('stdout file path errors should be handled', testFileError, 'noop-fd.js', getAbsolutePath, 1);78test.serial('stderr file path errors should be handled', testFileError, 'noop-fd.js', getAbsolutePath, 2);79test.serial('stdio[*] file path errors should be handled', testFileError, 'noop-fd.js', getAbsolutePath, 3);8081const testFileErrorSync = (t, mapFile, fdNumber) => {82 t.throws(() => {83 execaSync('empty.js', getStdio(fdNumber, mapFile('./unknown/file')));84 }, {code: 'ENOENT'});85};8687test('inputFile file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, 'inputFile');88test('stdin file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, 0);89test('stdout file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, 1);90test('stderr file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, 2);91test('stdio[*] file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, 3);92test('inputFile file path errors should be handled - sync', testFileErrorSync, identity, 'inputFile');93test('stdin file path errors should be handled - sync', testFileErrorSync, getAbsolutePath, 0);94test('stdout file path errors should be handled - sync', testFileErrorSync, getAbsolutePath, 1);95test('stderr file path errors should be handled - sync', testFileErrorSync, getAbsolutePath, 2);96test('stdio[*] file path errors should be handled - sync', testFileErrorSync, getAbsolutePath, 3);9798const testInputFileObject = async (t, fdNumber, mapFile, execaMethod) => {99 const filePath = tempfile();100 await writeFile(filePath, foobarString);101 t.throws(() => {102 execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [103 new Uint8Array(),104 mapFile(filePath),105 serializeGenerator(true),106 ]));107 }, {message: /cannot use both files and transforms in objectMode/});108 await rm(filePath);109};110111test('stdin cannot use objectMode together with input file paths', testInputFileObject, 0, getAbsolutePath, execa);112test('stdin cannot use objectMode together with input file URLs', testInputFileObject, 0, pathToFileURL, execa);113test('stdio[*] cannot use objectMode together with input file paths', testInputFileObject, 3, getAbsolutePath, execa);114test('stdio[*] cannot use objectMode together with input file URLs', testInputFileObject, 3, pathToFileURL, execa);115test('stdin cannot use objectMode together with input file paths, sync', testInputFileObject, 0, getAbsolutePath, execaSync);116test('stdin cannot use objectMode together with input file URLs, sync', testInputFileObject, 0, pathToFileURL, execaSync);117118const testOutputFileObject = async (t, fdNumber, mapFile, execaMethod) => {119 const filePath = tempfile();120 t.throws(() => {121 execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, [122 outputObjectGenerator(),123 mapFile(filePath),124 ]));125 }, {message: /cannot use both files and transforms in objectMode/});126 t.false(await pathExists(filePath));127};128129test('stdout cannot use objectMode together with output file paths', testOutputFileObject, 1, getAbsolutePath, execa);130test('stdout cannot use objectMode together with output file URLs', testOutputFileObject, 1, pathToFileURL, execa);131test('stderr cannot use objectMode together with output file paths', testOutputFileObject, 2, getAbsolutePath, execa);132test('stderr cannot use objectMode together with output file URLs', testOutputFileObject, 2, pathToFileURL, execa);133test('stdio[*] cannot use objectMode together with output file paths', testOutputFileObject, 3, getAbsolutePath, execa);134test('stdio[*] cannot use objectMode together with output file URLs', testOutputFileObject, 3, pathToFileURL, execa);135test('stdout cannot use objectMode together with output file paths, sync', testOutputFileObject, 1, getAbsolutePath, execaSync);136test('stdout cannot use objectMode together with output file URLs, sync', testOutputFileObject, 1, pathToFileURL, execaSync);137test('stderr cannot use objectMode together with output file paths, sync', testOutputFileObject, 2, getAbsolutePath, execaSync);138test('stderr cannot use objectMode together with output file URLs, sync', testOutputFileObject, 2, pathToFileURL, execaSync);139test('stdio[*] cannot use objectMode together with output file paths, sync', testOutputFileObject, 3, getAbsolutePath, execaSync);140test('stdio[*] cannot use objectMode together with output file URLs, sync', testOutputFileObject, 3, pathToFileURL, execaSync);141142test('Generator error stops writing to output file', async t => {143 const filePath = tempfile();144 const cause = new Error(foobarString);145 const error = await t.throwsAsync(execa('noop.js', {146 stdout: [throwingGenerator(cause)(), getAbsolutePath(filePath)],147 }));148 t.is(error.cause, cause);149 t.is(await readFile(filePath, 'utf8'), '');150});151152test('Generator error does not create output file, sync', async t => {153 const filePath = tempfile();154 const cause = new Error(foobarString);155 const error = t.throws(() => {156 execaSync('noop.js', {157 stdout: [throwingGenerator(cause)(), getAbsolutePath(filePath)],158 });159 });160 t.is(error.cause, cause);161 t.false(await pathExists(filePath));162});163164test('Output file error still returns transformed output, sync', async t => {165 const filePath = tempfile();166 const {stdout} = t.throws(() => {167 execaSync('noop-fd.js', ['1', foobarString], {168 stdout: [uppercaseGenerator(), getAbsolutePath('./unknown/file')],169 });170 }, {code: 'ENOENT'});171 t.false(await pathExists(filePath));172 t.is(stdout, foobarUppercase);173});