summaryrefslogtreecommitdiff
path: root/cli/tests
diff options
context:
space:
mode:
authorFeng Yu <F3n67u@outlook.com>2021-10-11 21:21:18 +0800
committerGitHub <noreply@github.com>2021-10-11 15:21:18 +0200
commit668b400ff2fa5634f575e54f40ab1f0b78fcdf16 (patch)
treea5eacaf3e9f4822ea9ace0bc117ebcfd09fc8922 /cli/tests
parent423b02d8891c73f1b2864271796b067c81007f50 (diff)
feat(runtime): improve error messages of runtime fs (#11984)
This commit annotates errors returned from FS Deno APIs to include paths that were passed to the API calls. Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli/tests')
-rw-r--r--cli/tests/unit/chmod_test.ts24
-rw-r--r--cli/tests/unit/chown_test.ts20
-rw-r--r--cli/tests/unit/copy_file_test.ts20
-rw-r--r--cli/tests/unit/dir_test.ts10
-rw-r--r--cli/tests/unit/files_test.ts34
-rw-r--r--cli/tests/unit/link_test.ts83
-rw-r--r--cli/tests/unit/mkdir_test.ts20
-rw-r--r--cli/tests/unit/read_dir_test.ts30
-rw-r--r--cli/tests/unit/read_link_test.ts20
-rw-r--r--cli/tests/unit/remove_test.ts32
-rw-r--r--cli/tests/unit/rename_test.ts14
-rw-r--r--cli/tests/unit/stat_test.ts34
-rw-r--r--cli/tests/unit/symlink_test.ts31
-rw-r--r--cli/tests/unit/truncate_test.ts28
-rw-r--r--cli/tests/unit/utime_test.ts20
15 files changed, 348 insertions, 72 deletions
diff --git a/cli/tests/unit/chmod_test.ts b/cli/tests/unit/chmod_test.ts
index 8d73a3ba4..2ab4c5b0c 100644
--- a/cli/tests/unit/chmod_test.ts
+++ b/cli/tests/unit/chmod_test.ts
@@ -82,10 +82,14 @@ unitTest(
);
unitTest({ permissions: { write: true } }, function chmodSyncFailure() {
- assertThrows(() => {
- const filename = "/badfile.txt";
- Deno.chmodSync(filename, 0o777);
- }, Deno.errors.NotFound);
+ const filename = "/badfile.txt";
+ assertThrows(
+ () => {
+ Deno.chmodSync(filename, 0o777);
+ },
+ Deno.errors.NotFound,
+ `chmod '${filename}'`,
+ );
});
unitTest({ permissions: { write: false } }, function chmodSyncPerm() {
@@ -170,10 +174,14 @@ unitTest(
);
unitTest({ permissions: { write: true } }, async function chmodFailure() {
- await assertRejects(async () => {
- const filename = "/badfile.txt";
- await Deno.chmod(filename, 0o777);
- }, Deno.errors.NotFound);
+ const filename = "/badfile.txt";
+ await assertRejects(
+ async () => {
+ await Deno.chmod(filename, 0o777);
+ },
+ Deno.errors.NotFound,
+ `chmod '${filename}'`,
+ );
});
unitTest({ permissions: { write: false } }, async function chmodPerm() {
diff --git a/cli/tests/unit/chown_test.ts b/cli/tests/unit/chown_test.ts
index 60cb45959..82335a7af 100644
--- a/cli/tests/unit/chown_test.ts
+++ b/cli/tests/unit/chown_test.ts
@@ -48,9 +48,13 @@ unitTest(
const { uid, gid } = await getUidAndGid();
const filePath = Deno.makeTempDirSync() + "/chown_test_file.txt";
- assertThrows(() => {
- Deno.chownSync(filePath, uid, gid);
- }, Deno.errors.NotFound);
+ assertThrows(
+ () => {
+ Deno.chownSync(filePath, uid, gid);
+ },
+ Deno.errors.NotFound,
+ `chown '${filePath}'`,
+ );
},
);
@@ -63,9 +67,13 @@ unitTest(
const { uid, gid } = await getUidAndGid();
const filePath = (await Deno.makeTempDir()) + "/chown_test_file.txt";
- await assertRejects(async () => {
- await Deno.chown(filePath, uid, gid);
- }, Deno.errors.NotFound);
+ await assertRejects(
+ async () => {
+ await Deno.chown(filePath, uid, gid);
+ },
+ Deno.errors.NotFound,
+ `chown '${filePath}'`,
+ );
},
);
diff --git a/cli/tests/unit/copy_file_test.ts b/cli/tests/unit/copy_file_test.ts
index cc2699bd3..f232efba8 100644
--- a/cli/tests/unit/copy_file_test.ts
+++ b/cli/tests/unit/copy_file_test.ts
@@ -72,9 +72,13 @@ unitTest(
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
// We skip initial writing here, from.txt does not exist
- assertThrows(() => {
- Deno.copyFileSync(fromFilename, toFilename);
- }, Deno.errors.NotFound);
+ assertThrows(
+ () => {
+ Deno.copyFileSync(fromFilename, toFilename);
+ },
+ Deno.errors.NotFound,
+ `copy '${fromFilename}' -> '${toFilename}'`,
+ );
Deno.removeSync(tempDir, { recursive: true });
},
@@ -162,9 +166,13 @@ unitTest(
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
// We skip initial writing here, from.txt does not exist
- await assertRejects(async () => {
- await Deno.copyFile(fromFilename, toFilename);
- }, Deno.errors.NotFound);
+ await assertRejects(
+ async () => {
+ await Deno.copyFile(fromFilename, toFilename);
+ },
+ Deno.errors.NotFound,
+ `copy '${fromFilename}' -> '${toFilename}'`,
+ );
Deno.removeSync(tempDir, { recursive: true });
},
diff --git a/cli/tests/unit/dir_test.ts b/cli/tests/unit/dir_test.ts
index fdcab5ddf..bcddb744a 100644
--- a/cli/tests/unit/dir_test.ts
+++ b/cli/tests/unit/dir_test.ts
@@ -52,8 +52,12 @@ unitTest(
{ permissions: { read: true, write: true } },
function dirChdirError() {
const path = Deno.makeTempDirSync() + "test";
- assertThrows(() => {
- Deno.chdir(path);
- }, Deno.errors.NotFound);
+ assertThrows(
+ () => {
+ Deno.chdir(path);
+ },
+ Deno.errors.NotFound,
+ `chdir '${path}'`,
+ );
},
);
diff --git a/cli/tests/unit/files_test.ts b/cli/tests/unit/files_test.ts
index 410c89de9..9f5812d32 100644
--- a/cli/tests/unit/files_test.ts
+++ b/cli/tests/unit/files_test.ts
@@ -2,7 +2,13 @@
// deno-lint-ignore-file no-deprecated-deno-api
-import { assert, assertEquals, assertRejects, unitTest } from "./test_util.ts";
+import {
+ assert,
+ assertEquals,
+ assertRejects,
+ assertThrows,
+ unitTest,
+} from "./test_util.ts";
import { copy } from "../../../test_util/std/io/util.ts";
unitTest(function filesStdioFileDescriptors() {
@@ -362,6 +368,32 @@ unitTest(
);
unitTest(
+ { permissions: { write: true, read: true } },
+ async function openNotFound() {
+ await assertRejects(
+ async () => {
+ await Deno.open("bad_file_name");
+ },
+ Deno.errors.NotFound,
+ `open 'bad_file_name'`,
+ );
+ },
+);
+
+unitTest(
+ { permissions: { write: true, read: true } },
+ function openSyncNotFound() {
+ assertThrows(
+ () => {
+ Deno.openSync("bad_file_name");
+ },
+ Deno.errors.NotFound,
+ `open 'bad_file_name'`,
+ );
+ },
+);
+
+unitTest(
{ permissions: { read: true, write: true } },
async function createFile() {
const tempDir = await Deno.makeTempDir();
diff --git a/cli/tests/unit/link_test.ts b/cli/tests/unit/link_test.ts
index 1688b53f1..fdd7d2fe8 100644
--- a/cli/tests/unit/link_test.ts
+++ b/cli/tests/unit/link_test.ts
@@ -1,5 +1,11 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
-import { assert, assertEquals, assertThrows, unitTest } from "./test_util.ts";
+import {
+ assert,
+ assertEquals,
+ assertRejects,
+ assertThrows,
+ unitTest,
+} from "./test_util.ts";
unitTest(
{ permissions: { read: true, write: true } },
@@ -50,9 +56,13 @@ unitTest(
// newname is already created.
Deno.writeFileSync(newName, new TextEncoder().encode("newName"));
- assertThrows(() => {
- Deno.linkSync(oldName, newName);
- }, Deno.errors.AlreadyExists);
+ assertThrows(
+ () => {
+ Deno.linkSync(oldName, newName);
+ },
+ Deno.errors.AlreadyExists,
+ `link '${oldName}' -> '${newName}'`,
+ );
},
);
@@ -63,9 +73,13 @@ unitTest(
const oldName = testDir + "/oldname";
const newName = testDir + "/newname";
- assertThrows(() => {
- Deno.linkSync(oldName, newName);
- }, Deno.errors.NotFound);
+ assertThrows(
+ () => {
+ Deno.linkSync(oldName, newName);
+ },
+ Deno.errors.NotFound,
+ `link '${oldName}' -> '${newName}'`,
+ );
},
);
@@ -125,3 +139,58 @@ unitTest(
);
},
);
+
+unitTest(
+ { permissions: { read: true, write: true } },
+ async function linkExists() {
+ const testDir = Deno.makeTempDirSync();
+ const oldName = testDir + "/oldname";
+ const newName = testDir + "/newname";
+ Deno.writeFileSync(oldName, new TextEncoder().encode("oldName"));
+ // newname is already created.
+ Deno.writeFileSync(newName, new TextEncoder().encode("newName"));
+
+ await assertRejects(
+ async () => {
+ await Deno.link(oldName, newName);
+ },
+ Deno.errors.AlreadyExists,
+ `link '${oldName}' -> '${newName}'`,
+ );
+ },
+);
+
+unitTest(
+ { permissions: { read: true, write: true } },
+ async function linkNotFound() {
+ const testDir = Deno.makeTempDirSync();
+ const oldName = testDir + "/oldname";
+ const newName = testDir + "/newname";
+
+ await assertRejects(
+ async () => {
+ await Deno.link(oldName, newName);
+ },
+ Deno.errors.NotFound,
+ `link '${oldName}' -> '${newName}'`,
+ );
+ },
+);
+
+unitTest(
+ { permissions: { read: false, write: true } },
+ async function linkReadPerm() {
+ await assertRejects(async () => {
+ await Deno.link("oldbaddir", "newbaddir");
+ }, Deno.errors.PermissionDenied);
+ },
+);
+
+unitTest(
+ { permissions: { read: true, write: false } },
+ async function linkWritePerm() {
+ await assertRejects(async () => {
+ await Deno.link("oldbaddir", "newbaddir");
+ }, Deno.errors.PermissionDenied);
+ },
+);
diff --git a/cli/tests/unit/mkdir_test.ts b/cli/tests/unit/mkdir_test.ts
index 2e84ed06f..b06a1cd4c 100644
--- a/cli/tests/unit/mkdir_test.ts
+++ b/cli/tests/unit/mkdir_test.ts
@@ -59,15 +59,23 @@ unitTest(
);
unitTest({ permissions: { write: true } }, function mkdirErrSyncIfExists() {
- assertThrows(() => {
- Deno.mkdirSync(".");
- }, Deno.errors.AlreadyExists);
+ assertThrows(
+ () => {
+ Deno.mkdirSync(".");
+ },
+ Deno.errors.AlreadyExists,
+ `mkdir '.'`,
+ );
});
unitTest({ permissions: { write: true } }, async function mkdirErrIfExists() {
- await assertRejects(async () => {
- await Deno.mkdir(".");
- }, Deno.errors.AlreadyExists);
+ await assertRejects(
+ async () => {
+ await Deno.mkdir(".");
+ },
+ Deno.errors.AlreadyExists,
+ `mkdir '.'`,
+ );
});
unitTest(
diff --git a/cli/tests/unit/read_dir_test.ts b/cli/tests/unit/read_dir_test.ts
index ca900153a..686c38af3 100644
--- a/cli/tests/unit/read_dir_test.ts
+++ b/cli/tests/unit/read_dir_test.ts
@@ -40,15 +40,23 @@ unitTest({ permissions: { read: false } }, function readDirSyncPerm() {
});
unitTest({ permissions: { read: true } }, function readDirSyncNotDir() {
- assertThrows(() => {
- Deno.readDirSync("cli/tests/testdata/fixture.json");
- }, Error);
+ assertThrows(
+ () => {
+ Deno.readDirSync("cli/tests/testdata/fixture.json");
+ },
+ Error,
+ `readdir 'cli/tests/testdata/fixture.json'`,
+ );
});
unitTest({ permissions: { read: true } }, function readDirSyncNotFound() {
- assertThrows(() => {
- Deno.readDirSync("bad_dir_name");
- }, Deno.errors.NotFound);
+ assertThrows(
+ () => {
+ Deno.readDirSync("bad_dir_name");
+ },
+ Deno.errors.NotFound,
+ `readdir 'bad_dir_name'`,
+ );
});
unitTest({ permissions: { read: true } }, async function readDirSuccess() {
@@ -94,3 +102,13 @@ unitTest(
}
},
);
+
+unitTest({ permissions: { read: true } }, async function readDirNotFound() {
+ await assertRejects(
+ async () => {
+ await Deno.readDir("bad_dir_name")[Symbol.asyncIterator]().next();
+ },
+ Deno.errors.NotFound,
+ `readdir 'bad_dir_name'`,
+ );
+});
diff --git a/cli/tests/unit/read_link_test.ts b/cli/tests/unit/read_link_test.ts
index f468e6f52..d59dae2b8 100644
--- a/cli/tests/unit/read_link_test.ts
+++ b/cli/tests/unit/read_link_test.ts
@@ -44,9 +44,13 @@ unitTest({ permissions: { read: false } }, function readLinkSyncPerm() {
});
unitTest({ permissions: { read: true } }, function readLinkSyncNotFound() {
- assertThrows(() => {
- Deno.readLinkSync("bad_filename");
- }, Deno.errors.NotFound);
+ assertThrows(
+ () => {
+ Deno.readLinkSync("bad_filename");
+ },
+ Deno.errors.NotFound,
+ `readlink 'bad_filename'`,
+ );
});
unitTest(
@@ -84,3 +88,13 @@ unitTest({ permissions: { read: false } }, async function readLinkPerm() {
await Deno.readLink("/symlink");
}, Deno.errors.PermissionDenied);
});
+
+unitTest({ permissions: { read: true } }, async function readLinkNotFound() {
+ await assertRejects(
+ async () => {
+ await Deno.readLink("bad_filename");
+ },
+ Deno.errors.NotFound,
+ `readlink 'bad_filename'`,
+ );
+});
diff --git a/cli/tests/unit/remove_test.ts b/cli/tests/unit/remove_test.ts
index 192ac676e..5ea265ea8 100644
--- a/cli/tests/unit/remove_test.ts
+++ b/cli/tests/unit/remove_test.ts
@@ -80,15 +80,23 @@ unitTest(
const subPathInfo = Deno.statSync(subPath);
assert(subPathInfo.isDirectory); // check exist first
- await assertRejects(async () => {
- await Deno[method](path);
- }, Error);
+ await assertRejects(
+ async () => {
+ await Deno[method](path);
+ },
+ Error,
+ `remove '${path}'`,
+ );
// TODO(ry) Is Other really the error we should get here? What would Go do?
// NON-EXISTENT DIRECTORY/FILE
- await assertRejects(async () => {
- await Deno[method]("/baddir");
- }, Deno.errors.NotFound);
+ await assertRejects(
+ async () => {
+ await Deno[method]("/baddir");
+ },
+ Deno.errors.NotFound,
+ `remove '/baddir'`,
+ );
}
},
);
@@ -210,10 +218,14 @@ unitTest(
unitTest({ permissions: { write: true } }, async function removeAllFail() {
for (const method of REMOVE_METHODS) {
// NON-EXISTENT DIRECTORY/FILE
- await assertRejects(async () => {
- // Non-existent
- await Deno[method]("/baddir", { recursive: true });
- }, Deno.errors.NotFound);
+ await assertRejects(
+ async () => {
+ // Non-existent
+ await Deno[method]("/baddir", { recursive: true });
+ },
+ Deno.errors.NotFound,
+ `remove '/baddir'`,
+ );
}
});
diff --git a/cli/tests/unit/rename_test.ts b/cli/tests/unit/rename_test.ts
index 387c0a51d..a2291dd1c 100644
--- a/cli/tests/unit/rename_test.ts
+++ b/cli/tests/unit/rename_test.ts
@@ -164,6 +164,13 @@ unitTest(
Error,
"Not a directory",
);
+ assertThrows(
+ () => {
+ Deno.renameSync(olddir, file);
+ },
+ undefined,
+ `rename '${olddir}' -> '${file}'`,
+ );
const fileLink = testDir + "/fileLink";
const dirLink = testDir + "/dirLink";
@@ -242,6 +249,13 @@ unitTest(
Deno.errors.PermissionDenied,
"Access is denied",
);
+ assertThrows(
+ () => {
+ Deno.renameSync(olddir, emptydir);
+ },
+ undefined,
+ `rename '${olddir}' -> '${emptydir}'`,
+ );
// should succeed on Windows
Deno.renameSync(olddir, file);
diff --git a/cli/tests/unit/stat_test.ts b/cli/tests/unit/stat_test.ts
index 362899128..eefbab2c5 100644
--- a/cli/tests/unit/stat_test.ts
+++ b/cli/tests/unit/stat_test.ts
@@ -108,9 +108,13 @@ unitTest({ permissions: { read: false } }, function statSyncPerm() {
});
unitTest({ permissions: { read: true } }, function statSyncNotFound() {
- assertThrows(() => {
- Deno.statSync("bad_file_name");
- }, Deno.errors.NotFound);
+ assertThrows(
+ () => {
+ Deno.statSync("bad_file_name");
+ },
+ Deno.errors.NotFound,
+ `stat 'bad_file_name'`,
+ );
});
unitTest({ permissions: { read: true } }, function lstatSyncSuccess() {
@@ -148,9 +152,13 @@ unitTest({ permissions: { read: false } }, function lstatSyncPerm() {
});
unitTest({ permissions: { read: true } }, function lstatSyncNotFound() {
- assertThrows(() => {
- Deno.lstatSync("bad_file_name");
- }, Deno.errors.NotFound);
+ assertThrows(
+ () => {
+ Deno.lstatSync("bad_file_name");
+ },
+ Deno.errors.NotFound,
+ `stat 'bad_file_name'`,
+ );
});
unitTest(
@@ -228,8 +236,10 @@ unitTest({ permissions: { read: false } }, async function statPerm() {
unitTest({ permissions: { read: true } }, async function statNotFound() {
await assertRejects(
async () => {
- await Deno.stat("bad_file_name"), Deno.errors.NotFound;
+ await Deno.stat("bad_file_name");
},
+ Deno.errors.NotFound,
+ `stat 'bad_file_name'`,
);
});
@@ -268,9 +278,13 @@ unitTest({ permissions: { read: false } }, async function lstatPerm() {
});
unitTest({ permissions: { read: true } }, async function lstatNotFound() {
- await assertRejects(async () => {
- await Deno.lstat("bad_file_name");
- }, Deno.errors.NotFound);
+ await assertRejects(
+ async () => {
+ await Deno.lstat("bad_file_name");
+ },
+ Deno.errors.NotFound,
+ `stat 'bad_file_name'`,
+ );
});
unitTest(
diff --git a/cli/tests/unit/symlink_test.ts b/cli/tests/unit/symlink_test.ts
index b5700b4f3..f0db2d615 100644
--- a/cli/tests/unit/symlink_test.ts
+++ b/cli/tests/unit/symlink_test.ts
@@ -1,6 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import {
assert,
+ assertRejects,
assertThrows,
pathToAbsoluteFileUrl,
unitTest,
@@ -47,6 +48,21 @@ unitTest(function symlinkSyncPerm() {
unitTest(
{ permissions: { read: true, write: true } },
+ function symlinkSyncAlreadyExist() {
+ const existingFile = Deno.makeTempFileSync();
+ const existingFile2 = Deno.makeTempFileSync();
+ assertThrows(
+ () => {
+ Deno.symlinkSync(existingFile, existingFile2);
+ },
+ Deno.errors.AlreadyExists,
+ `symlink '${existingFile}' -> '${existingFile2}'`,
+ );
+ },
+);
+
+unitTest(
+ { permissions: { read: true, write: true } },
async function symlinkSuccess() {
const testDir = Deno.makeTempDirSync();
const oldname = testDir + "/oldname";
@@ -77,3 +93,18 @@ unitTest(
assert(newNameInfoStat.isDirectory, "NOT DIRECTORY");
},
);
+
+unitTest(
+ { permissions: { read: true, write: true } },
+ async function symlinkAlreadyExist() {
+ const existingFile = Deno.makeTempFileSync();
+ const existingFile2 = Deno.makeTempFileSync();
+ await assertRejects(
+ async () => {
+ await Deno.symlink(existingFile, existingFile2);
+ },
+ Deno.errors.AlreadyExists,
+ `symlink '${existingFile}' -> '${existingFile2}'`,
+ );
+ },
+);
diff --git a/cli/tests/unit/truncate_test.ts b/cli/tests/unit/truncate_test.ts
index 19a44dba9..33a1cd4b1 100644
--- a/cli/tests/unit/truncate_test.ts
+++ b/cli/tests/unit/truncate_test.ts
@@ -91,3 +91,31 @@ unitTest({ permissions: { write: false } }, async function truncatePerm() {
await Deno.truncate("/test_truncatePermission.txt");
}, Deno.errors.PermissionDenied);
});
+
+unitTest(
+ { permissions: { read: true, write: true } },
+ function truncateSyncNotFound() {
+ const filename = "/badfile.txt";
+ assertThrows(
+ () => {
+ Deno.truncateSync(filename);
+ },
+ Deno.errors.NotFound,
+ `truncate '${filename}'`,
+ );
+ },
+);
+
+unitTest(
+ { permissions: { read: true, write: true } },
+ async function truncateSyncNotFound() {
+ const filename = "/badfile.txt";
+ await assertRejects(
+ async () => {
+ await Deno.truncate(filename);
+ },
+ Deno.errors.NotFound,
+ `truncate '${filename}'`,
+ );
+ },
+);
diff --git a/cli/tests/unit/utime_test.ts b/cli/tests/unit/utime_test.ts
index 3e73722aa..a46bd7a0c 100644
--- a/cli/tests/unit/utime_test.ts
+++ b/cli/tests/unit/utime_test.ts
@@ -160,9 +160,13 @@ unitTest(
const atime = 1000;
const mtime = 50000;
- assertThrows(() => {
- Deno.utimeSync("/baddir", atime, mtime);
- }, Deno.errors.NotFound);
+ assertThrows(
+ () => {
+ Deno.utimeSync("/baddir", atime, mtime);
+ },
+ Deno.errors.NotFound,
+ "utime '/baddir'",
+ );
},
);
@@ -271,9 +275,13 @@ unitTest(
const atime = 1000;
const mtime = 50000;
- await assertRejects(async () => {
- await Deno.utime("/baddir", atime, mtime);
- }, Deno.errors.NotFound);
+ await assertRejects(
+ async () => {
+ await Deno.utime("/baddir", atime, mtime);
+ },
+ Deno.errors.NotFound,
+ "utime '/baddir'",
+ );
},
);