0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/std/node/_fs/_fs_dirent_test.ts
Bert Belder ee4e6a1ef9
Rename FileInfo time fields and represent them as Date objects (#4932)
This patch also increases the resolution of reported file times to
sub-millisecond precision.
2020-04-27 21:13:32 +02:00

112 lines
2.6 KiB
TypeScript

const { test } = Deno;
import { assert, assertEquals, assertThrows } from "../../testing/asserts.ts";
import Dirent from "./_fs_dirent.ts";
class DirEntryMock implements Deno.DirEntry {
isFile = false;
isDirectory = false;
isSymlink = false;
size = -1;
mtime = new Date(-1);
atime = new Date(-1);
birthtime = new Date(-1);
name = "";
dev = -1;
ino = -1;
mode = -1;
nlink = -1;
uid = -1;
gid = -1;
rdev = -1;
blksize = -1;
blocks: number | null = null;
}
test({
name: "Block devices are correctly identified",
fn() {
const fileInfo: DirEntryMock = new DirEntryMock();
fileInfo.blocks = 5;
assert(new Dirent(fileInfo).isBlockDevice());
assert(!new Dirent(fileInfo).isCharacterDevice());
},
});
test({
name: "Character devices are correctly identified",
fn() {
const fileInfo: DirEntryMock = new DirEntryMock();
fileInfo.blocks = null;
assert(new Dirent(fileInfo).isCharacterDevice());
assert(!new Dirent(fileInfo).isBlockDevice());
},
});
test({
name: "Directories are correctly identified",
fn() {
const fileInfo: DirEntryMock = new DirEntryMock();
fileInfo.isDirectory = true;
fileInfo.isFile = false;
fileInfo.isSymlink = false;
assert(new Dirent(fileInfo).isDirectory());
assert(!new Dirent(fileInfo).isFile());
assert(!new Dirent(fileInfo).isSymbolicLink());
},
});
test({
name: "Files are correctly identified",
fn() {
const fileInfo: DirEntryMock = new DirEntryMock();
fileInfo.isDirectory = false;
fileInfo.isFile = true;
fileInfo.isSymlink = false;
assert(!new Dirent(fileInfo).isDirectory());
assert(new Dirent(fileInfo).isFile());
assert(!new Dirent(fileInfo).isSymbolicLink());
},
});
test({
name: "Symlinks are correctly identified",
fn() {
const fileInfo: DirEntryMock = new DirEntryMock();
fileInfo.isDirectory = false;
fileInfo.isFile = false;
fileInfo.isSymlink = true;
assert(!new Dirent(fileInfo).isDirectory());
assert(!new Dirent(fileInfo).isFile());
assert(new Dirent(fileInfo).isSymbolicLink());
},
});
test({
name: "File name is correct",
fn() {
const fileInfo: DirEntryMock = new DirEntryMock();
fileInfo.name = "my_file";
assertEquals(new Dirent(fileInfo).name, "my_file");
},
});
test({
name: "Socket and FIFO pipes aren't yet available",
fn() {
const fileInfo: DirEntryMock = new DirEntryMock();
assertThrows(
() => {
new Dirent(fileInfo).isFIFO();
},
Error,
"does not yet support"
);
assertThrows(
() => {
new Dirent(fileInfo).isSocket();
},
Error,
"does not yet support"
);
},
});