summaryrefslogtreecommitdiff
path: root/std/node/_fs/_fs_dir_test.ts
diff options
context:
space:
mode:
authorChris Knight <cknight1234@gmail.com>2020-03-12 14:12:27 +0000
committerGitHub <noreply@github.com>2020-03-12 10:12:27 -0400
commitcabe63eb05f334bc9921dc8633b254b05519b434 (patch)
tree4e6ad72e742a2acca605c0cf253e1f97586ce9da /std/node/_fs/_fs_dir_test.ts
parent3ed6ccc905394ed9c5d9cbcb8fa2426151780788 (diff)
fix: Node polyfill fsAppend rework (#4322)
* My original implementation of `fs.appendFile` used an async API, which, though it would work fine as a polyfill, wasn't an exact match with the Node API. This PR reworks that API to mimic the Node API fully as a synchronous void function with an async internal implementation. * Refactor move of other internal fs `dirent` and `dir` classes to the _fs internal directory.
Diffstat (limited to 'std/node/_fs/_fs_dir_test.ts')
-rw-r--r--std/node/_fs/_fs_dir_test.ts156
1 files changed, 156 insertions, 0 deletions
diff --git a/std/node/_fs/_fs_dir_test.ts b/std/node/_fs/_fs_dir_test.ts
new file mode 100644
index 000000000..aefa85854
--- /dev/null
+++ b/std/node/_fs/_fs_dir_test.ts
@@ -0,0 +1,156 @@
+const { test } = Deno;
+import { assert, assertEquals, fail } from "../../testing/asserts.ts";
+import Dir from "./_fs_dir.ts";
+import Dirent from "./_fs_dirent.ts";
+
+test({
+ name: "Closing current directory with callback is successful",
+ async fn() {
+ let calledBack = false;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ new Dir(".").close((valOrErr: any) => {
+ assert(!valOrErr);
+ calledBack = true;
+ });
+ assert(calledBack);
+ }
+});
+
+test({
+ name: "Closing current directory without callback returns void Promise",
+ async fn() {
+ await new Dir(".").close();
+ }
+});
+
+test({
+ name: "Closing current directory synchronously works",
+ async fn() {
+ new Dir(".").closeSync();
+ }
+});
+
+test({
+ name: "Path is correctly returned",
+ fn() {
+ assertEquals(new Dir("std/node").path, "std/node");
+
+ const enc: Uint8Array = new TextEncoder().encode("std/node");
+ assertEquals(new Dir(enc).path, "std/node");
+ }
+});
+
+test({
+ name: "read returns null for empty directory",
+ async fn() {
+ const testDir: string = Deno.makeTempDirSync();
+ try {
+ const file: Dirent | null = await new Dir(testDir).read();
+ assert(file === null);
+
+ let calledBack = false;
+ const fileFromCallback: Dirent | null = await new Dir(
+ testDir
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ ).read((err: any, res: Dirent) => {
+ assert(res === null);
+ assert(err === null);
+ calledBack = true;
+ });
+ assert(fileFromCallback === null);
+ assert(calledBack);
+
+ assertEquals(new Dir(testDir).readSync(), null);
+ } finally {
+ Deno.removeSync(testDir);
+ }
+ }
+});
+
+test({
+ name: "Async read returns one file at a time",
+ async fn() {
+ const testDir: string = Deno.makeTempDirSync();
+ Deno.createSync(testDir + "/foo.txt");
+ Deno.createSync(testDir + "/bar.txt");
+
+ try {
+ let secondCallback = false;
+ const dir: Dir = new Dir(testDir);
+ const firstRead: Dirent | null = await dir.read();
+ const secondRead: Dirent | null = await dir.read(
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ (err: any, secondResult: Dirent) => {
+ assert(
+ secondResult.name === "bar.txt" || secondResult.name === "foo.txt"
+ );
+ secondCallback = true;
+ }
+ );
+ const thirdRead: Dirent | null = await dir.read();
+
+ if (firstRead?.name === "foo.txt") {
+ assertEquals(secondRead?.name, "bar.txt");
+ } else if (firstRead?.name === "bar.txt") {
+ assertEquals(secondRead?.name, "foo.txt");
+ } else {
+ fail("File not found during read");
+ }
+ assert(secondCallback);
+ assert(thirdRead === null);
+ } finally {
+ Deno.removeSync(testDir, { recursive: true });
+ }
+ }
+});
+
+test({
+ name: "Sync read returns one file at a time",
+ fn() {
+ const testDir: string = Deno.makeTempDirSync();
+ Deno.createSync(testDir + "/foo.txt");
+ Deno.createSync(testDir + "/bar.txt");
+
+ try {
+ const dir: Dir = new Dir(testDir);
+ const firstRead: Dirent | null = dir.readSync();
+ const secondRead: Dirent | null = dir.readSync();
+ const thirdRead: Dirent | null = dir.readSync();
+
+ if (firstRead?.name === "foo.txt") {
+ assertEquals(secondRead?.name, "bar.txt");
+ } else if (firstRead?.name === "bar.txt") {
+ assertEquals(secondRead?.name, "foo.txt");
+ } else {
+ fail("File not found during read");
+ }
+ assert(thirdRead === null);
+ } finally {
+ Deno.removeSync(testDir, { recursive: true });
+ }
+ }
+});
+
+test({
+ name: "Async iteration over existing directory",
+ async fn() {
+ const testDir: string = Deno.makeTempDirSync();
+ Deno.createSync(testDir + "/foo.txt");
+ Deno.createSync(testDir + "/bar.txt");
+
+ try {
+ const dir: Dir = new Dir(testDir);
+ const results: Array<string | null> = [];
+
+ for await (const file of dir[Symbol.asyncIterator]()) {
+ results.push(file.name);
+ }
+
+ assert(results.length === 2);
+ assert(results.includes("foo.txt"));
+ assert(results.includes("bar.txt"));
+ } finally {
+ Deno.removeSync(testDir, { recursive: true });
+ }
+ }
+});