added a helper to tests for one-liner error matching in unit tests. fixed bugs in tests after the tree fixture changed
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
import { FileSystem } from "@core/filesystem/filesystem";
|
||||
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/treenode";
|
||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||
import {
|
||||
expectAlreadyExistsError,
|
||||
expectDirectoryLoopError,
|
||||
expectIllegalMutationError,
|
||||
expectNodeNotFoundError,
|
||||
expectNotADirectoryError,
|
||||
} from "@tests/helpers/error_matchers";
|
||||
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
@@ -77,7 +84,9 @@ describe("createNode", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||
|
||||
expect(newFS).toBeInstanceOf(FileSystem);
|
||||
const newNode = newFS.getNode(4) as FileNode;
|
||||
const newNode = newFS.getNode(5) as FileNode;
|
||||
|
||||
console.log(newNode);
|
||||
|
||||
expect(newNode.type).toBe(TreeNodeType.File);
|
||||
expect(newNode.name).toBe("readme");
|
||||
@@ -87,12 +96,12 @@ describe("createNode", () => {
|
||||
|
||||
const root = newFS.getNode(0) as DirectoryNode;
|
||||
|
||||
expect(root.children["readme"]).toBe(4);
|
||||
expect(root.children["readme"]).toBe(5);
|
||||
|
||||
expect(newFS.INodeIndex).toBe(4);
|
||||
expect(newFS.INodeIndex).toBe(5);
|
||||
expect(newFS.Overlay.size).toBe(2);
|
||||
|
||||
expect(fs.getNode(4)).toBeUndefined();
|
||||
expect(fs.getNode(5)).toBeUndefined();
|
||||
|
||||
const oldRoot = fs.getNode(0) as DirectoryNode;
|
||||
expect(oldRoot.children).toEqual({ "hello.txt": 1, sub: 2 });
|
||||
@@ -101,17 +110,17 @@ describe("createNode", () => {
|
||||
it("fails to create a file as it alread exists", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||
|
||||
expect(() => newFS.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
|
||||
expectAlreadyExistsError(() => newFS.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content));
|
||||
});
|
||||
|
||||
it("fails to create a file because the parent is not a directory", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||
|
||||
expect(() => newFS.createNode(1, "test", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
|
||||
expectNotADirectoryError(() => newFS.createNode(1, "test", TreeNodeType.File, 0o644, 0, 0, content));
|
||||
});
|
||||
|
||||
it("fails to create a file because the specified parent doesnt exist", () => {
|
||||
expect(() => fs.createNode(999, "readme", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
|
||||
expectNodeNotFoundError(() => fs.createNode(999, "readme", TreeNodeType.File, 0o644, 0, 0, content));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -126,37 +135,37 @@ describe("permission method group", () => {
|
||||
});
|
||||
it("returns false for user read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o200, 1000, 1000, content);
|
||||
const perms = newFS.canRead(4, { uid: 1000, gid: 1000 });
|
||||
const perms = newFS.canRead(5, { uid: 1000, gid: 1000 });
|
||||
|
||||
expect(perms).toBe(false);
|
||||
});
|
||||
it("returns true for group read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o040, 1000, 2000, content);
|
||||
const perms = newFS.canRead(4, { uid: 999, gid: 2000 });
|
||||
const perms = newFS.canRead(5, { uid: 999, gid: 2000 });
|
||||
|
||||
expect(perms).toBe(true);
|
||||
});
|
||||
it("returns false for group read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o020, 1000, 2000, content);
|
||||
const perms = newFS.canRead(4, { uid: 999, gid: 2000 });
|
||||
const perms = newFS.canRead(5, { uid: 999, gid: 2000 });
|
||||
|
||||
expect(perms).toBe(false);
|
||||
});
|
||||
it("returns true for other read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o004, 1000, 2000, content);
|
||||
const perms = newFS.canRead(4, { uid: 999, gid: 3000 });
|
||||
const perms = newFS.canRead(5, { uid: 999, gid: 3000 });
|
||||
|
||||
expect(perms).toBe(true);
|
||||
});
|
||||
it("returns false for other read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o002, 1000, 2000, content);
|
||||
const perms = newFS.canRead(4, { uid: 999, gid: 3000 });
|
||||
const perms = newFS.canRead(5, { uid: 999, gid: 3000 });
|
||||
|
||||
expect(perms).toBe(false);
|
||||
});
|
||||
it("returns true for user execute permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o100, 1000, 2000, content);
|
||||
const perms = newFS.canExecute(4, { uid: 1000, gid: 2000 });
|
||||
const perms = newFS.canExecute(5, { uid: 1000, gid: 2000 });
|
||||
|
||||
expect(perms).toBe(true);
|
||||
});
|
||||
@@ -169,11 +178,11 @@ describe("deleteNode", () => {
|
||||
const newNode = newFS.getNode(1) as FileNode;
|
||||
|
||||
it("fails to delete the root node", () => {
|
||||
expect(() => fs.deleteNode(0)).toThrow();
|
||||
expectIllegalMutationError(() => fs.deleteNode(0));
|
||||
});
|
||||
|
||||
it("fails to delete a non existant node", () => {
|
||||
expect(() => fs.deleteNode(900)).toThrow();
|
||||
expectNodeNotFoundError(() => fs.deleteNode(999));
|
||||
});
|
||||
|
||||
it("successfully deletes a single child node", () => {
|
||||
@@ -294,28 +303,28 @@ describe("modifyNode", () => {
|
||||
group: 2,
|
||||
};
|
||||
|
||||
expect(() => fs.modifyNode(999, changes)).toThrow();
|
||||
expectNodeNotFoundError(() => fs.modifyNode(999, changes));
|
||||
});
|
||||
});
|
||||
|
||||
describe("moveNode", () => {
|
||||
it("throws because of invalid destination node", () => {
|
||||
expect(() => fs.moveNode(1, 999)).toThrow();
|
||||
expectNodeNotFoundError(() => fs.moveNode(1, 999));
|
||||
});
|
||||
it("throws because of invalid source node", () => {
|
||||
expect(() => fs.moveNode(999, 2)).toThrow();
|
||||
expectNodeNotFoundError(() => fs.moveNode(999, 2));
|
||||
});
|
||||
it("throws because of invalid destination type", () => {
|
||||
expect(() => fs.moveNode(3, 1)).toThrow();
|
||||
expectNotADirectoryError(() => fs.moveNode(3, 1));
|
||||
});
|
||||
it("throws because of an already exising child under destination with the same name", () => {
|
||||
expect(() => fs.moveNode(1, 2, "world.txt")).toThrow();
|
||||
expectAlreadyExistsError(() => fs.moveNode(1, 2, "world.txt"));
|
||||
});
|
||||
it("throws because it tried to move a node to a descendant of itself", () => {
|
||||
expect(() => fs.moveNode(2, 4)).toThrow();
|
||||
expectDirectoryLoopError(() => fs.moveNode(2, 4));
|
||||
});
|
||||
it("throws because it tried to move root", () => {
|
||||
expect(() => fs.moveNode(0, 1)).toThrow();
|
||||
expectIllegalMutationError(() => fs.moveNode(0, 1));
|
||||
});
|
||||
|
||||
it("successfully moves a node to another directory", () => {
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import type {
|
||||
AlreadyExistsError,
|
||||
DirectoryLoopError,
|
||||
IllegalMutationError,
|
||||
NodeNotFoundError,
|
||||
NotADirectoryError,
|
||||
} from "@core/filesystem/errors";
|
||||
import type { TreeNodeType } from "@core/filesystem/treenode";
|
||||
import { expect } from "vitest";
|
||||
|
||||
export function expectThrows(fn: () => unknown, expectedShape: Record<string, unknown>) {
|
||||
let thrown: unknown;
|
||||
try {
|
||||
fn();
|
||||
} catch (e) {
|
||||
thrown = e;
|
||||
}
|
||||
|
||||
expect(thrown).toBeDefined();
|
||||
expect(thrown).toMatchObject(expectedShape);
|
||||
}
|
||||
|
||||
export function expectNotADirectoryError(fn: () => unknown, overrides?: Partial<NotADirectoryError>) {
|
||||
expectThrows(fn, {
|
||||
type: "NotADirectory",
|
||||
inode: expect.any(Number) as number,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
nodeType: expect.any(String) as TreeNodeType,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export function expectAlreadyExistsError(fn: () => unknown, overrides?: Partial<AlreadyExistsError>) {
|
||||
expectThrows(fn, {
|
||||
type: "AlreadyExists",
|
||||
parentInode: expect.any(Number) as number,
|
||||
name: expect.any(String) as string,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export function expectNodeNotFoundError(fn: () => unknown, overrides?: Partial<NodeNotFoundError>) {
|
||||
expectThrows(fn, {
|
||||
type: "NodeNotFound",
|
||||
inode: expect.any(Number) as number,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export function expectIllegalMutationError(fn: () => unknown, overrides?: Partial<IllegalMutationError>) {
|
||||
expectThrows(fn, {
|
||||
type: "IllegalMutation",
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export function expectDirectoryLoopError(fn: () => void, overrides?: Partial<DirectoryLoopError>) {
|
||||
expectThrows(fn, {
|
||||
type: "DirectoryLoop",
|
||||
descendantInode: expect.any(Number) as number,
|
||||
ancestorInode: expect.any(Number) as number,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user