refactor(fs): align additional error messages (#5802)

This commit is contained in:
Ian Bull 2024-08-26 00:31:34 -04:00 committed by GitHub
parent d51b349381
commit a8a1d42aa7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 36 additions and 30 deletions

View File

@ -60,7 +60,7 @@ async function ensureValidCopy(
if (options.isFolder && !destStat.isDirectory) { if (options.isFolder && !destStat.isDirectory) {
throw new Error( throw new Error(
`Cannot overwrite non-directory '${dest}' with directory '${src}'.`, `Cannot overwrite non-directory '${dest}' with directory '${src}'`,
); );
} }
if (!options.overwrite) { if (!options.overwrite) {
@ -87,11 +87,11 @@ function ensureValidCopySync(
if (options.isFolder && !destStat.isDirectory) { if (options.isFolder && !destStat.isDirectory) {
throw new Error( throw new Error(
`Cannot overwrite non-directory '${dest}' with directory '${src}'.`, `Cannot overwrite non-directory '${dest}' with directory '${src}'`,
); );
} }
if (!options.overwrite) { if (!options.overwrite) {
throw new Deno.errors.AlreadyExists(`'${dest}' already exists.`); throw new Deno.errors.AlreadyExists(`'${dest}' already exists`);
} }
return destStat; return destStat;
@ -313,14 +313,14 @@ export async function copy(
dest = resolve(toPathString(dest)); dest = resolve(toPathString(dest));
if (src === dest) { if (src === dest) {
throw new Error("Source and destination cannot be the same."); throw new Error("Source and destination cannot be the same");
} }
const srcStat = await Deno.lstat(src); const srcStat = await Deno.lstat(src);
if (srcStat.isDirectory && isSubdir(src, dest)) { if (srcStat.isDirectory && isSubdir(src, dest)) {
throw new Error( throw new Error(
`Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`, `Cannot copy '${src}' to a subdirectory of itself: '${dest}'`,
); );
} }
@ -389,14 +389,14 @@ export function copySync(
dest = resolve(toPathString(dest)); dest = resolve(toPathString(dest));
if (src === dest) { if (src === dest) {
throw new Error("Source and destination cannot be the same."); throw new Error("Source and destination cannot be the same");
} }
const srcStat = Deno.lstatSync(src); const srcStat = Deno.lstatSync(src);
if (srcStat.isDirectory && isSubdir(src, dest)) { if (srcStat.isDirectory && isSubdir(src, dest)) {
throw new Error( throw new Error(
`Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`, `Cannot copy '${src}' to a subdirectory of itself: '${dest}'`,
); );
} }

View File

@ -64,7 +64,7 @@ testCopy(
await copy(srcFile, destFile); await copy(srcFile, destFile);
}, },
Error, Error,
"Source and destination cannot be the same.", "Source and destination cannot be the same",
); );
}, },
); );
@ -102,7 +102,7 @@ testCopy(
await copy(srcFile, destFile); await copy(srcFile, destFile);
}, },
Error, Error,
`'${destFile}' already exists.`, `'${destFile}' already exists`,
); );
// Modify destination file. // Modify destination file.
@ -186,7 +186,7 @@ testCopy(
await copy(srcDir, destDir); await copy(srcDir, destDir);
}, },
Error, Error,
`Cannot copy '${srcDir}' to a subdirectory of itself, '${destDir}'.`, `Cannot copy '${srcDir}' to a subdirectory of itself: '${destDir}'`,
); );
}, },
); );
@ -205,7 +205,7 @@ testCopy(
await copy(srcDir, destDir); await copy(srcDir, destDir);
}, },
Error, Error,
`Cannot overwrite non-directory '${destDir}' with directory '${srcDir}'.`, `Cannot overwrite non-directory '${destDir}' with directory '${srcDir}'`,
); );
}, },
); );
@ -241,7 +241,7 @@ testCopy(
await copy(srcDir, destDir); await copy(srcDir, destDir);
}, },
Error, Error,
`'${destDir}' already exists.`, `'${destDir}' already exists`,
); );
// Modify the file in the destination directory. // Modify the file in the destination directory.
@ -372,7 +372,7 @@ testCopySync(
copySync(srcFile, srcFile); copySync(srcFile, srcFile);
}, },
Error, Error,
"Source and destination cannot be the same.", "Source and destination cannot be the same",
); );
}, },
); );
@ -401,7 +401,7 @@ testCopySync("copySync() copies file to new destination", (tempDir: string) => {
copySync(srcFile, destFile); copySync(srcFile, destFile);
}, },
Error, Error,
`'${destFile}' already exists.`, `'${destFile}' already exists`,
); );
// Modify destination file. // Modify destination file.
@ -432,7 +432,7 @@ testCopySync(
copySync(srcDir, destDir); copySync(srcDir, destDir);
}, },
Error, Error,
`Cannot copy '${srcDir}' to a subdirectory of itself, '${destDir}'.`, `Cannot copy '${srcDir}' to a subdirectory of itself: '${destDir}'`,
); );
}, },
); );
@ -451,7 +451,7 @@ testCopySync(
copySync(srcDir, destDir); copySync(srcDir, destDir);
}, },
Error, Error,
`Cannot overwrite non-directory '${destDir}' with directory '${srcDir}'.`, `Cannot overwrite non-directory '${destDir}' with directory '${srcDir}'`,
); );
}, },
); );
@ -485,7 +485,7 @@ testCopySync("copySync() copies a directory", (tempDir: string) => {
copySync(srcDir, destDir); copySync(srcDir, destDir);
}, },
Error, Error,
`'${destDir}' already exists.`, `'${destDir}' already exists`,
); );
// Modify the file in the destination directory. // Modify the file in the destination directory.

View File

@ -100,7 +100,9 @@ export function ensureDirSync(dir: string | URL) {
function throwIfNotDirectory(fileInfo: Deno.FileInfo) { function throwIfNotDirectory(fileInfo: Deno.FileInfo) {
if (!fileInfo.isDirectory) { if (!fileInfo.isDirectory) {
throw new Error( throw new Error(
`Ensure path exists, expected 'dir', got '${getFileInfoType(fileInfo)}'`, `Failed to ensure directory exists: expected 'dir', got '${
getFileInfoType(fileInfo)
}'`,
); );
} }
} }

View File

@ -105,7 +105,7 @@ Deno.test("ensureDir() rejects if input is a file", async function () {
await ensureDir(testFile); await ensureDir(testFile);
}, },
Error, Error,
`Ensure path exists, expected 'dir', got 'file'`, `Failed to ensure directory exists: expected 'dir', got 'file'`,
); );
} finally { } finally {
await Deno.remove(baseDir, { recursive: true }); await Deno.remove(baseDir, { recursive: true });
@ -124,7 +124,7 @@ Deno.test("ensureDirSync() throws if input is a file", function () {
ensureDirSync(testFile); ensureDirSync(testFile);
}, },
Error, Error,
`Ensure path exists, expected 'dir', got 'file'`, `Failed to ensure directory exists: expected 'dir', got 'file'`,
); );
} finally { } finally {
Deno.removeSync(baseDir, { recursive: true }); Deno.removeSync(baseDir, { recursive: true });
@ -139,7 +139,7 @@ Deno.test("ensureDir() rejects links to files", async function () {
await ensureDir(lf); await ensureDir(lf);
}, },
Error, Error,
`Ensure path exists, expected 'dir', got 'file'`, `Failed to ensure directory exists: expected 'dir', got 'file'`,
); );
}); });
@ -151,7 +151,7 @@ Deno.test("ensureDirSync() rejects links to files", function () {
ensureDirSync(lf); ensureDirSync(lf);
}, },
Error, Error,
`Ensure path exists, expected 'dir', got 'file'`, `Failed to ensure directory exists: expected 'dir', got 'file'`,
); );
}); });

View File

@ -32,7 +32,9 @@ export async function ensureFile(filePath: string | URL): Promise<void> {
const stat = await Deno.lstat(filePath); const stat = await Deno.lstat(filePath);
if (!stat.isFile) { if (!stat.isFile) {
throw new Error( throw new Error(
`Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`, `Failed to ensure file exists: expected 'file', got '${
getFileInfoType(stat)
}'`,
); );
} }
} catch (err) { } catch (err) {
@ -77,7 +79,9 @@ export function ensureFileSync(filePath: string | URL): void {
const stat = Deno.lstatSync(filePath); const stat = Deno.lstatSync(filePath);
if (!stat.isFile) { if (!stat.isFile) {
throw new Error( throw new Error(
`Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`, `Failed to ensure file exists: expected 'file', got '${
getFileInfoType(stat)
}'`,
); );
} }
} catch (err) { } catch (err) {

View File

@ -79,7 +79,7 @@ Deno.test("ensureFile() rejects if input is dir", async function () {
await ensureFile(testDir); await ensureFile(testDir);
}, },
Error, Error,
`Ensure path exists, expected 'file', got 'dir'`, `Failed to ensure file exists: expected 'file', got 'dir'`,
); );
} finally { } finally {
await Deno.remove(testDir, { recursive: true }); await Deno.remove(testDir, { recursive: true });
@ -97,7 +97,7 @@ Deno.test("ensureFileSync() throws if input is dir", function () {
ensureFileSync(testDir); ensureFileSync(testDir);
}, },
Error, Error,
`Ensure path exists, expected 'file', got 'dir'`, `Failed to ensure file exists: expected 'file', got 'dir'`,
); );
} finally { } finally {
Deno.removeSync(testDir, { recursive: true }); Deno.removeSync(testDir, { recursive: true });

View File

@ -123,7 +123,7 @@ export async function exists(
) { ) {
if (options.isDirectory && options.isFile) { if (options.isDirectory && options.isFile) {
throw new TypeError( throw new TypeError(
"ExistsOptions.options.isDirectory and ExistsOptions.options.isFile must not be true together.", "ExistsOptions.options.isDirectory and ExistsOptions.options.isFile must not be true together",
); );
} }
if ( if (
@ -252,7 +252,7 @@ export function existsSync(
) { ) {
if (options.isDirectory && options.isFile) { if (options.isDirectory && options.isFile) {
throw new TypeError( throw new TypeError(
"ExistsOptions.options.isDirectory and ExistsOptions.options.isFile must not be true together.", "ExistsOptions.options.isDirectory and ExistsOptions.options.isFile must not be true together",
); );
} }
if ( if (

View File

@ -335,7 +335,7 @@ Deno.test("exists() returns false when both isDirectory and isFile sets true", a
assert(error instanceof TypeError); assert(error instanceof TypeError);
assertStringIncludes( assertStringIncludes(
error.message, error.message,
"ExistsOptions.options.isDirectory and ExistsOptions.options.isFile must not be true together.", "ExistsOptions.options.isDirectory and ExistsOptions.options.isFile must not be true together",
); );
} finally { } finally {
await Deno.remove(tempDirPath, { recursive: true }); await Deno.remove(tempDirPath, { recursive: true });
@ -356,7 +356,7 @@ Deno.test("existsSync() returns false when both isDirectory and isFile sets true
assert(error instanceof TypeError); assert(error instanceof TypeError);
assertStringIncludes( assertStringIncludes(
error.message, error.message,
"ExistsOptions.options.isDirectory and ExistsOptions.options.isFile must not be true together.", "ExistsOptions.options.isDirectory and ExistsOptions.options.isFile must not be true together",
); );
} finally { } finally {
await Deno.remove(tempDirPath, { recursive: true }); await Deno.remove(tempDirPath, { recursive: true });