file-path-error.js
This file
Path
test/stdio/file-path-error.js
175 lines · 9.7 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';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 2–3why, one step back2import {readFile, writeFile, rm} from 'node:fs/promises';3import {pathToFileURL} from 'node:url';TweaksSindre Sorhusbefore adoption698a1b8
line 4why, one step back4import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 5–6why, one step back5import {pathExists} from 'path-exists';6import tempfile from 'tempfile';TweaksSindre Sorhusbefore adoption698a1b8
line 7why, one step back7import {assertThrows, assertRejects} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 8–23why, one step back8import {execa, execaSync} from '../../index.js';9import {setFixtureDirectory} from '../helpers/fixtures-directory.js';10import {identity, getStdio} from '../helpers/stdio.js';11import {foobarString, foobarUppercase} from '../helpers/input.js';12import {13 outputObjectGenerator,14 uppercaseGenerator,15 serializeGenerator,16 throwingGenerator,17} from '../helpers/generator.js';18import {getAbsolutePath} from '../helpers/file-path.js';1920setFixtureDirectory();2122const nonFileUrl = new URL('https://example.com');23TweaksSindre Sorhusbefore adoption698a1b8
lines 24–25why, one step back24const testStdioNonFileUrl = (fdNumber, execaMethod) => {25 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 26–29why, one step back26 execaMethod('empty.js', getStdio(fdNumber, nonFileUrl));27 }, {message: /pathToFileURL/});28};29TweaksSindre Sorhusbefore adoption698a1b8
lines 30–39why, one step back30test('inputFile cannot be a non-file URL', () => testStdioNonFileUrl('inputFile', execa));31test('stdin cannot be a non-file URL', () => testStdioNonFileUrl(0, execa));32test('stdout cannot be a non-file URL', () => testStdioNonFileUrl(1, execa));33test('stderr cannot be a non-file URL', () => testStdioNonFileUrl(2, execa));34test('stdio[*] cannot be a non-file URL', () => testStdioNonFileUrl(3, execa));35test('inputFile cannot be a non-file URL - sync', () => testStdioNonFileUrl('inputFile', execaSync));36test('stdin cannot be a non-file URL - sync', () => testStdioNonFileUrl(0, execaSync));37test('stdout cannot be a non-file URL - sync', () => testStdioNonFileUrl(1, execaSync));38test('stderr cannot be a non-file URL - sync', () => testStdioNonFileUrl(2, execaSync));39test('stdio[*] cannot be a non-file URL - sync', () => testStdioNonFileUrl(3, execaSync));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 40why, one step back40TweaksSindre Sorhusbefore adoption698a1b8
lines 41–42why, one step back41const testInvalidInputFile = execaMethod => {42 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 43–46why, one step back43 execaMethod('empty.js', getStdio('inputFile', false));44 }, {message: /a file path string or a file URL/});45};46TweaksSindre Sorhusbefore adoption698a1b8
lines 47–48why, one step back47test('inputFile must be a file URL or string', () => testInvalidInputFile(execa));48test('inputFile must be a file URL or string - sync', () => testInvalidInputFile(execaSync));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 49why, one step back49TweaksSindre Sorhusbefore adoption698a1b8
lines 50–51why, one step back50const testFilePathObject = (fdNumber, execaMethod) => {51 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 52–55why, one step back52 execaMethod('empty.js', getStdio(fdNumber, foobarString));53 }, {message: /must be used/});54};55TweaksSindre Sorhusbefore adoption698a1b8
lines 56–63why, one step back56test('stdin must be an object when it is a file path string', () => testFilePathObject(0, execa));57test('stdout must be an object when it is a file path string', () => testFilePathObject(1, execa));58test('stderr must be an object when it is a file path string', () => testFilePathObject(2, execa));59test('stdio[*] must be an object when it is a file path string', () => testFilePathObject(3, execa));60test('stdin be an object when it is a file path string - sync', () => testFilePathObject(0, execaSync));61test('stdout be an object when it is a file path string - sync', () => testFilePathObject(1, execaSync));62test('stderr be an object when it is a file path string - sync', () => testFilePathObject(2, execaSync));63test('stdio[*] must be an object when it is a file path string - sync', () => testFilePathObject(3, execaSync));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 64why, one step back64TweaksSindre Sorhusbefore adoption698a1b8
lines 65–66why, one step back65const testFileError = async (fixtureName, mapFile, fdNumber) => {66 await assertRejects(100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 67–71why, one step back67 execa(fixtureName, [`${fdNumber}`], getStdio(fdNumber, mapFile('./unknown/file'))),68 {code: 'ENOENT'},69 );70};71TweaksSindre Sorhusbefore adoption698a1b8
lines 72–81why, one step back72test('inputFile file URL errors should be handled', () => testFileError('stdin-fd.js', pathToFileURL, 'inputFile'));73test('stdin file URL errors should be handled', () => testFileError('stdin-fd.js', pathToFileURL, 0));74test('stdout file URL errors should be handled', () => testFileError('noop-fd.js', pathToFileURL, 1));75test('stderr file URL errors should be handled', () => testFileError('noop-fd.js', pathToFileURL, 2));76test('stdio[*] file URL errors should be handled', () => testFileError('noop-fd.js', pathToFileURL, 3));77test('inputFile file path errors should be handled', () => testFileError('stdin-fd.js', identity, 'inputFile'));78test('stdin file path errors should be handled', () => testFileError('stdin-fd.js', getAbsolutePath, 0));79test('stdout file path errors should be handled', () => testFileError('noop-fd.js', getAbsolutePath, 1));80test('stderr file path errors should be handled', () => testFileError('noop-fd.js', getAbsolutePath, 2));81test('stdio[*] file path errors should be handled', () => testFileError('noop-fd.js', getAbsolutePath, 3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 82why, one step back82TweaksSindre Sorhusbefore adoption698a1b8
lines 83–84why, one step back83const testFileErrorSync = (mapFile, fdNumber) => {84 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 85–88why, one step back85 execaSync('empty.js', getStdio(fdNumber, mapFile('./unknown/file')));86 }, {code: 'ENOENT'});87};88TweaksSindre Sorhusbefore adoption698a1b8
lines 89–98why, one step back89test('inputFile file URL errors should be handled - sync', () => testFileErrorSync(pathToFileURL, 'inputFile'));90test('stdin file URL errors should be handled - sync', () => testFileErrorSync(pathToFileURL, 0));91test('stdout file URL errors should be handled - sync', () => testFileErrorSync(pathToFileURL, 1));92test('stderr file URL errors should be handled - sync', () => testFileErrorSync(pathToFileURL, 2));93test('stdio[*] file URL errors should be handled - sync', () => testFileErrorSync(pathToFileURL, 3));94test('inputFile file path errors should be handled - sync', () => testFileErrorSync(identity, 'inputFile'));95test('stdin file path errors should be handled - sync', () => testFileErrorSync(getAbsolutePath, 0));96test('stdout file path errors should be handled - sync', () => testFileErrorSync(getAbsolutePath, 1));97test('stderr file path errors should be handled - sync', () => testFileErrorSync(getAbsolutePath, 2));98test('stdio[*] file path errors should be handled - sync', () => testFileErrorSync(getAbsolutePath, 3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 99why, one step back99TweaksSindre Sorhusbefore adoption698a1b8
line 100why, one step back100const testInputFileObject = async (fdNumber, mapFile, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 101–102why, one step back101 const filePath = tempfile();102 await writeFile(filePath, foobarString);TweaksSindre Sorhusbefore adoption698a1b8
line 103why, one step back103 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 104–112why, one step back104 execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [105 new Uint8Array(),106 mapFile(filePath),107 serializeGenerator(true),108 ]));109 }, {message: /cannot use both files and transforms in objectMode/});110 await rm(filePath);111};112TweaksSindre Sorhusbefore adoption698a1b8
lines 113–118why, one step back113test('stdin cannot use objectMode together with input file paths', () => testInputFileObject(0, getAbsolutePath, execa));114test('stdin cannot use objectMode together with input file URLs', () => testInputFileObject(0, pathToFileURL, execa));115test('stdio[*] cannot use objectMode together with input file paths', () => testInputFileObject(3, getAbsolutePath, execa));116test('stdio[*] cannot use objectMode together with input file URLs', () => testInputFileObject(3, pathToFileURL, execa));117test('stdin cannot use objectMode together with input file paths, sync', () => testInputFileObject(0, getAbsolutePath, execaSync));118test('stdin cannot use objectMode together with input file URLs, sync', () => testInputFileObject(0, pathToFileURL, execaSync));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 119why, one step back119TweaksSindre Sorhusbefore adoption698a1b8
line 120why, one step back120const testOutputFileObject = async (fdNumber, mapFile, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 121why, one step back121 const filePath = tempfile();TweaksSindre Sorhusbefore adoption698a1b8
line 122why, one step back122 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 123–127why, one step back123 execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, [124 outputObjectGenerator(),125 mapFile(filePath),126 ]));127 }, {message: /cannot use both files and transforms in objectMode/});TweaksSindre Sorhusbefore adoption698a1b8
line 128why, one step back128 assert.ok(!await pathExists(filePath));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 129–130why, one step back129};130TweaksSindre Sorhusbefore adoption698a1b8
lines 131–142why, one step back131test('stdout cannot use objectMode together with output file paths', () => testOutputFileObject(1, getAbsolutePath, execa));132test('stdout cannot use objectMode together with output file URLs', () => testOutputFileObject(1, pathToFileURL, execa));133test('stderr cannot use objectMode together with output file paths', () => testOutputFileObject(2, getAbsolutePath, execa));134test('stderr cannot use objectMode together with output file URLs', () => testOutputFileObject(2, pathToFileURL, execa));135test('stdio[*] cannot use objectMode together with output file paths', () => testOutputFileObject(3, getAbsolutePath, execa));136test('stdio[*] cannot use objectMode together with output file URLs', () => testOutputFileObject(3, pathToFileURL, execa));137test('stdout cannot use objectMode together with output file paths, sync', () => testOutputFileObject(1, getAbsolutePath, execaSync));138test('stdout cannot use objectMode together with output file URLs, sync', () => testOutputFileObject(1, pathToFileURL, execaSync));139test('stderr cannot use objectMode together with output file paths, sync', () => testOutputFileObject(2, getAbsolutePath, execaSync));140test('stderr cannot use objectMode together with output file URLs, sync', () => testOutputFileObject(2, pathToFileURL, execaSync));141test('stdio[*] cannot use objectMode together with output file paths, sync', () => testOutputFileObject(3, getAbsolutePath, execaSync));142test('stdio[*] cannot use objectMode together with output file URLs, sync', () => testOutputFileObject(3, pathToFileURL, execaSync));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 143why, one step back143TweaksSindre Sorhusbefore adoption698a1b8
line 144why, one step back144test('Generator error stops writing to output file', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 145–146why, one step back145 const filePath = tempfile();146 const cause = new Error(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
line 147why, one step back147 const error = await assertRejects(execa('noop.js', {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 148–149why, one step back148 stdout: [throwingGenerator(cause)(), getAbsolutePath(filePath)],149 }));TweaksSindre Sorhusbefore adoption698a1b8
lines 150–151why, one step back150 assert.equal(error.cause, cause);151 assert.equal(await readFile(filePath, 'utf8'), '');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 152–153why, one step back152});153TweaksSindre Sorhusbefore adoption698a1b8
line 154why, one step back154test('Generator error does not create output file, sync', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 155–156why, one step back155 const filePath = tempfile();156 const cause = new Error(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
line 157why, one step back157 const error = assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 158–161why, one step back158 execaSync('noop.js', {159 stdout: [throwingGenerator(cause)(), getAbsolutePath(filePath)],160 });161 });TweaksSindre Sorhusbefore adoption698a1b8
lines 162–163why, one step back162 assert.equal(error.cause, cause);163 assert.ok(!await pathExists(filePath));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 164–165why, one step back164});165TweaksSindre Sorhusbefore adoption698a1b8
line 166why, one step back166test('Output file error still returns transformed output, sync', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 167why, one step back167 const filePath = tempfile();TweaksSindre Sorhusbefore adoption698a1b8
line 168why, one step back168 const {stdout} = assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 169–172why, one step back169 execaSync('noop-fd.js', ['1', foobarString], {170 stdout: [uppercaseGenerator(), getAbsolutePath('./unknown/file')],171 });172 }, {code: 'ENOENT'});TweaksSindre Sorhusbefore adoption698a1b8
lines 173–174why, one step back173 assert.ok(!await pathExists(filePath));174 assert.equal(stdout, foobarUppercase);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 175why, one step back175});