2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-12 10:12:27 -04:00
|
|
|
import { assert, assertEquals, fail } from "../../testing/asserts.ts";
|
2020-03-03 08:56:10 -05:00
|
|
|
import Dir from "./_fs_dir.ts";
|
2020-07-08 05:26:39 -04:00
|
|
|
import type Dirent from "./_fs_dirent.ts";
|
2020-03-03 08:56:10 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-03 08:56:10 -05:00
|
|
|
name: "Closing current directory with callback is successful",
|
2020-03-20 09:38:34 -04:00
|
|
|
fn() {
|
2020-03-03 08:56:10 -05:00
|
|
|
let calledBack = false;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
new Dir(".").close((valOrErr: any) => {
|
|
|
|
assert(!valOrErr);
|
|
|
|
calledBack = true;
|
|
|
|
});
|
|
|
|
assert(calledBack);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-03 08:56:10 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-03 08:56:10 -05:00
|
|
|
name: "Closing current directory without callback returns void Promise",
|
|
|
|
async fn() {
|
|
|
|
await new Dir(".").close();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-03 08:56:10 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-03 08:56:10 -05:00
|
|
|
name: "Closing current directory synchronously works",
|
2020-03-20 09:38:34 -04:00
|
|
|
fn() {
|
2020-03-03 08:56:10 -05:00
|
|
|
new Dir(".").closeSync();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-03 08:56:10 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-03 08:56:10 -05:00
|
|
|
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");
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-03 08:56:10 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-03 08:56:10 -05:00
|
|
|
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(
|
2020-07-14 15:24:17 -04:00
|
|
|
testDir,
|
2020-03-03 08:56:10 -05:00
|
|
|
// 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);
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-03 08:56:10 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-03 08:56:10 -05:00
|
|
|
name: "Async read returns one file at a time",
|
|
|
|
async fn() {
|
|
|
|
const testDir: string = Deno.makeTempDirSync();
|
2020-03-18 19:25:55 -04:00
|
|
|
const f1 = Deno.createSync(testDir + "/foo.txt");
|
|
|
|
f1.close();
|
|
|
|
const f2 = Deno.createSync(testDir + "/bar.txt");
|
|
|
|
f2.close();
|
2020-03-03 08:56:10 -05:00
|
|
|
|
|
|
|
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(
|
2020-07-14 15:24:17 -04:00
|
|
|
secondResult.name === "bar.txt" ||
|
|
|
|
secondResult.name === "foo.txt",
|
2020-03-03 08:56:10 -05:00
|
|
|
);
|
|
|
|
secondCallback = true;
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-03 08:56:10 -05:00
|
|
|
);
|
|
|
|
const thirdRead: Dirent | null = await dir.read();
|
2020-04-16 15:45:30 -04:00
|
|
|
const fourthRead: Dirent | null = await dir.read();
|
2020-03-03 08:56:10 -05:00
|
|
|
|
|
|
|
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);
|
2020-04-16 15:45:30 -04:00
|
|
|
assert(fourthRead === null);
|
2020-03-03 08:56:10 -05:00
|
|
|
} finally {
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-03 08:56:10 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-03 08:56:10 -05:00
|
|
|
name: "Sync read returns one file at a time",
|
|
|
|
fn() {
|
|
|
|
const testDir: string = Deno.makeTempDirSync();
|
2020-03-18 19:25:55 -04:00
|
|
|
const f1 = Deno.createSync(testDir + "/foo.txt");
|
|
|
|
f1.close();
|
|
|
|
const f2 = Deno.createSync(testDir + "/bar.txt");
|
|
|
|
f2.close();
|
2020-03-03 08:56:10 -05:00
|
|
|
|
|
|
|
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();
|
2020-04-16 15:45:30 -04:00
|
|
|
const fourthRead: Dirent | null = dir.readSync();
|
2020-03-03 08:56:10 -05:00
|
|
|
|
|
|
|
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);
|
2020-04-16 15:45:30 -04:00
|
|
|
assert(fourthRead === null);
|
2020-03-03 08:56:10 -05:00
|
|
|
} finally {
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-03 08:56:10 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-03 08:56:10 -05:00
|
|
|
name: "Async iteration over existing directory",
|
|
|
|
async fn() {
|
|
|
|
const testDir: string = Deno.makeTempDirSync();
|
2020-03-18 19:25:55 -04:00
|
|
|
const f1 = Deno.createSync(testDir + "/foo.txt");
|
|
|
|
f1.close();
|
|
|
|
const f2 = Deno.createSync(testDir + "/bar.txt");
|
|
|
|
f2.close();
|
2020-03-03 08:56:10 -05:00
|
|
|
|
|
|
|
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 });
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-03 08:56:10 -05:00
|
|
|
});
|