From a8a1d42aa7eff784d2aa09dae56c85e4c3db30b4 Mon Sep 17 00:00:00 2001 From: Ian Bull Date: Mon, 26 Aug 2024 00:31:34 -0400 Subject: [PATCH] refactor(fs): align additional error messages (#5802) --- fs/copy.ts | 14 +++++++------- fs/copy_test.ts | 20 ++++++++++---------- fs/ensure_dir.ts | 4 +++- fs/ensure_dir_test.ts | 8 ++++---- fs/ensure_file.ts | 8 ++++++-- fs/ensure_file_test.ts | 4 ++-- fs/exists.ts | 4 ++-- fs/exists_test.ts | 4 ++-- 8 files changed, 36 insertions(+), 30 deletions(-) diff --git a/fs/copy.ts b/fs/copy.ts index c7585c96f..bce581f0a 100644 --- a/fs/copy.ts +++ b/fs/copy.ts @@ -60,7 +60,7 @@ async function ensureValidCopy( if (options.isFolder && !destStat.isDirectory) { throw new Error( - `Cannot overwrite non-directory '${dest}' with directory '${src}'.`, + `Cannot overwrite non-directory '${dest}' with directory '${src}'`, ); } if (!options.overwrite) { @@ -87,11 +87,11 @@ function ensureValidCopySync( if (options.isFolder && !destStat.isDirectory) { throw new Error( - `Cannot overwrite non-directory '${dest}' with directory '${src}'.`, + `Cannot overwrite non-directory '${dest}' with directory '${src}'`, ); } if (!options.overwrite) { - throw new Deno.errors.AlreadyExists(`'${dest}' already exists.`); + throw new Deno.errors.AlreadyExists(`'${dest}' already exists`); } return destStat; @@ -313,14 +313,14 @@ export async function copy( dest = resolve(toPathString(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); if (srcStat.isDirectory && isSubdir(src, dest)) { 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)); 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); if (srcStat.isDirectory && isSubdir(src, dest)) { throw new Error( - `Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`, + `Cannot copy '${src}' to a subdirectory of itself: '${dest}'`, ); } diff --git a/fs/copy_test.ts b/fs/copy_test.ts index c784d818a..6bc5f5170 100644 --- a/fs/copy_test.ts +++ b/fs/copy_test.ts @@ -64,7 +64,7 @@ testCopy( await copy(srcFile, destFile); }, Error, - "Source and destination cannot be the same.", + "Source and destination cannot be the same", ); }, ); @@ -102,7 +102,7 @@ testCopy( await copy(srcFile, destFile); }, Error, - `'${destFile}' already exists.`, + `'${destFile}' already exists`, ); // Modify destination file. @@ -186,7 +186,7 @@ testCopy( await copy(srcDir, destDir); }, 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); }, 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); }, Error, - `'${destDir}' already exists.`, + `'${destDir}' already exists`, ); // Modify the file in the destination directory. @@ -372,7 +372,7 @@ testCopySync( copySync(srcFile, srcFile); }, 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); }, Error, - `'${destFile}' already exists.`, + `'${destFile}' already exists`, ); // Modify destination file. @@ -432,7 +432,7 @@ testCopySync( copySync(srcDir, destDir); }, 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); }, 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); }, Error, - `'${destDir}' already exists.`, + `'${destDir}' already exists`, ); // Modify the file in the destination directory. diff --git a/fs/ensure_dir.ts b/fs/ensure_dir.ts index 30cae58ad..fc273f968 100644 --- a/fs/ensure_dir.ts +++ b/fs/ensure_dir.ts @@ -100,7 +100,9 @@ export function ensureDirSync(dir: string | URL) { function throwIfNotDirectory(fileInfo: Deno.FileInfo) { if (!fileInfo.isDirectory) { throw new Error( - `Ensure path exists, expected 'dir', got '${getFileInfoType(fileInfo)}'`, + `Failed to ensure directory exists: expected 'dir', got '${ + getFileInfoType(fileInfo) + }'`, ); } } diff --git a/fs/ensure_dir_test.ts b/fs/ensure_dir_test.ts index 12dc4bf9a..bc7fdb096 100644 --- a/fs/ensure_dir_test.ts +++ b/fs/ensure_dir_test.ts @@ -105,7 +105,7 @@ Deno.test("ensureDir() rejects if input is a file", async function () { await ensureDir(testFile); }, Error, - `Ensure path exists, expected 'dir', got 'file'`, + `Failed to ensure directory exists: expected 'dir', got 'file'`, ); } finally { await Deno.remove(baseDir, { recursive: true }); @@ -124,7 +124,7 @@ Deno.test("ensureDirSync() throws if input is a file", function () { ensureDirSync(testFile); }, Error, - `Ensure path exists, expected 'dir', got 'file'`, + `Failed to ensure directory exists: expected 'dir', got 'file'`, ); } finally { Deno.removeSync(baseDir, { recursive: true }); @@ -139,7 +139,7 @@ Deno.test("ensureDir() rejects links to files", async function () { await ensureDir(lf); }, 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); }, Error, - `Ensure path exists, expected 'dir', got 'file'`, + `Failed to ensure directory exists: expected 'dir', got 'file'`, ); }); diff --git a/fs/ensure_file.ts b/fs/ensure_file.ts index 615e92875..5f73dc64d 100644 --- a/fs/ensure_file.ts +++ b/fs/ensure_file.ts @@ -32,7 +32,9 @@ export async function ensureFile(filePath: string | URL): Promise { const stat = await Deno.lstat(filePath); if (!stat.isFile) { throw new Error( - `Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`, + `Failed to ensure file exists: expected 'file', got '${ + getFileInfoType(stat) + }'`, ); } } catch (err) { @@ -77,7 +79,9 @@ export function ensureFileSync(filePath: string | URL): void { const stat = Deno.lstatSync(filePath); if (!stat.isFile) { throw new Error( - `Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`, + `Failed to ensure file exists: expected 'file', got '${ + getFileInfoType(stat) + }'`, ); } } catch (err) { diff --git a/fs/ensure_file_test.ts b/fs/ensure_file_test.ts index 38c4390df..1ae5a1b9d 100644 --- a/fs/ensure_file_test.ts +++ b/fs/ensure_file_test.ts @@ -79,7 +79,7 @@ Deno.test("ensureFile() rejects if input is dir", async function () { await ensureFile(testDir); }, Error, - `Ensure path exists, expected 'file', got 'dir'`, + `Failed to ensure file exists: expected 'file', got 'dir'`, ); } finally { await Deno.remove(testDir, { recursive: true }); @@ -97,7 +97,7 @@ Deno.test("ensureFileSync() throws if input is dir", function () { ensureFileSync(testDir); }, Error, - `Ensure path exists, expected 'file', got 'dir'`, + `Failed to ensure file exists: expected 'file', got 'dir'`, ); } finally { Deno.removeSync(testDir, { recursive: true }); diff --git a/fs/exists.ts b/fs/exists.ts index 14d8ccd1f..ad9836213 100644 --- a/fs/exists.ts +++ b/fs/exists.ts @@ -123,7 +123,7 @@ export async function exists( ) { if (options.isDirectory && options.isFile) { 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 ( @@ -252,7 +252,7 @@ export function existsSync( ) { if (options.isDirectory && options.isFile) { 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 ( diff --git a/fs/exists_test.ts b/fs/exists_test.ts index 5f6ceebd5..6c5bc0436 100644 --- a/fs/exists_test.ts +++ b/fs/exists_test.ts @@ -335,7 +335,7 @@ Deno.test("exists() returns false when both isDirectory and isFile sets true", a assert(error instanceof TypeError); assertStringIncludes( 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 { 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); assertStringIncludes( 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 { await Deno.remove(tempDirPath, { recursive: true });