Phase 1 Recursive DFS pre-order algorithm, tree print helper for visualizing tree structures, filesystem test fixture builder helper function.
deleteNode fully implemented, test suite implemented
This commit is contained in:
@@ -1,30 +1,62 @@
|
||||
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 { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const map: Map<number, TreeNode> = new Map();
|
||||
const time = Date.now();
|
||||
|
||||
const root: DirectoryNode = {
|
||||
type: TreeNodeType.Directory,
|
||||
const testTree: FixtureNode = {
|
||||
inode: 0,
|
||||
name: "root",
|
||||
parent: 0,
|
||||
mode: 0o555,
|
||||
owner: 0,
|
||||
group: 0,
|
||||
mtime: time,
|
||||
atime: time,
|
||||
ctime: time,
|
||||
children: {},
|
||||
type: "Dir",
|
||||
children: [
|
||||
{
|
||||
inode: 1,
|
||||
name: "hello.txt",
|
||||
type: "File",
|
||||
content: "Hello, world!",
|
||||
children: [],
|
||||
},
|
||||
{
|
||||
inode: 2,
|
||||
name: "sub",
|
||||
type: "Dir",
|
||||
children: [
|
||||
{
|
||||
inode: 3,
|
||||
name: "world.txt",
|
||||
type: "File",
|
||||
content: "Goodbye!",
|
||||
children: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
map.set(0, root);
|
||||
|
||||
const base: ReadonlyMap<number, TreeNode> = map;
|
||||
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
|
||||
const fs = buildTestFS(testTree);
|
||||
|
||||
describe("FileSystem.init", () => {
|
||||
const map: Map<number, TreeNode> = new Map();
|
||||
const time = Date.now();
|
||||
|
||||
const root: DirectoryNode = {
|
||||
type: TreeNodeType.Directory,
|
||||
inode: 0,
|
||||
name: "root",
|
||||
parent: 0,
|
||||
mode: 0o555,
|
||||
owner: 0,
|
||||
group: 0,
|
||||
mtime: time,
|
||||
atime: time,
|
||||
ctime: time,
|
||||
children: {},
|
||||
};
|
||||
map.set(0, root);
|
||||
|
||||
const base: ReadonlyMap<number, TreeNode> = map;
|
||||
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
|
||||
|
||||
it("initializes correctly", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
expect(fs).toBeInstanceOf(FileSystem);
|
||||
@@ -33,14 +65,13 @@ describe("FileSystem.init", () => {
|
||||
});
|
||||
|
||||
describe("createNode", () => {
|
||||
it("creates a file and updates parent", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
|
||||
it("creates a file and updates parent", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||
|
||||
expect(newFS).toBeInstanceOf(FileSystem);
|
||||
const newNode = newFS.getNode(1) as FileNode;
|
||||
const newNode = newFS.getNode(4) as FileNode;
|
||||
|
||||
expect(newNode.type).toBe(TreeNodeType.File);
|
||||
expect(newNode.name).toBe("readme");
|
||||
@@ -50,85 +81,115 @@ describe("createNode", () => {
|
||||
|
||||
const root = newFS.getNode(0) as DirectoryNode;
|
||||
|
||||
expect(root.children["readme"]).toBe(1);
|
||||
expect(root.children["readme"]).toBe(4);
|
||||
|
||||
expect(newFS.INodeIndex).toBe(1);
|
||||
expect(newFS.INodeIndex).toBe(4);
|
||||
expect(newFS.Overlay.size).toBe(2);
|
||||
|
||||
expect(fs.getNode(1)).toBeUndefined();
|
||||
expect(fs.getNode(4)).toBeUndefined();
|
||||
|
||||
const oldRoot = fs.getNode(0) as DirectoryNode;
|
||||
expect(oldRoot.children).toEqual({});
|
||||
expect(oldRoot.children).toEqual({ "hello.txt": 1, sub: 2 });
|
||||
});
|
||||
|
||||
it("fails to create a file as it alread exists", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||
|
||||
expect(() => newFS.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
|
||||
});
|
||||
|
||||
it("fails to create a file because the parent is not a directory", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||
|
||||
expect(() => newFS.createNode(1, "test", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
|
||||
});
|
||||
|
||||
it("fails to create a file because the specified parent doesnt exist", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
|
||||
expect(() => fs.createNode(999, "readme", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("permission method group", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
|
||||
it("returns true for user read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o400, 1000, 1000, content);
|
||||
const perms = newFS.canRead(1, { uid: 1000, gid: 1000 });
|
||||
const perms = newFS.canRead(4, { uid: 1000, gid: 1000 });
|
||||
|
||||
expect(perms).toBe(true);
|
||||
});
|
||||
it("returns false for user read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o200, 1000, 1000, content);
|
||||
const perms = newFS.canRead(1, { uid: 1000, gid: 1000 });
|
||||
const perms = newFS.canRead(4, { 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(1, { uid: 999, gid: 2000 });
|
||||
const perms = newFS.canRead(4, { 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(1, { uid: 999, gid: 2000 });
|
||||
const perms = newFS.canRead(4, { 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(1, { uid: 999, gid: 3000 });
|
||||
const perms = newFS.canRead(4, { 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(1, { uid: 999, gid: 3000 });
|
||||
const perms = newFS.canRead(4, { 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(1, { uid: 1000, gid: 2000 });
|
||||
const perms = newFS.canExecute(4, { uid: 1000, gid: 2000 });
|
||||
|
||||
expect(perms).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteNode", () => {
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||
const newNode = newFS.getNode(1) as FileNode;
|
||||
|
||||
it("fails to delete the root node", () => {
|
||||
expect(() => fs.deleteNode(0)).toThrow();
|
||||
});
|
||||
|
||||
it("fails to delete a non existant node", () => {
|
||||
expect(() => fs.deleteNode(900)).toThrow();
|
||||
});
|
||||
|
||||
it("successfully deletes a single child node", () => {
|
||||
const newnewFS = newFS.deleteNode(1);
|
||||
const overlay = newnewFS.Overlay;
|
||||
const parent = newnewFS.getNode(0) as DirectoryNode;
|
||||
|
||||
expect(overlay.get(1)).toBe(null);
|
||||
expect(parent.children).not.toHaveProperty(newNode.name);
|
||||
});
|
||||
|
||||
it("successfully deletes a directory recursively", () => {
|
||||
const newFS = fs.deleteNode(2);
|
||||
const parent = newFS.getNode(0) as DirectoryNode;
|
||||
|
||||
expect(newFS.getNode(2)).toBe(undefined);
|
||||
expect(newFS.getNode(3)).toBe(undefined);
|
||||
expect(newFS.Overlay.size).toBeGreaterThanOrEqual(3);
|
||||
expect(newFS.Overlay.get(2)).toBe(null);
|
||||
expect(newFS.Overlay.get(3)).toBe(null);
|
||||
expect(parent.children).not.toHaveProperty("sub");
|
||||
expect(fs.getNode(2)).not.toBe(undefined);
|
||||
expect(fs.Overlay.size).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { traverseTree } from "@core/util/tree_traversal";
|
||||
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
const testTree: FixtureNode = {
|
||||
inode: 0,
|
||||
name: "root",
|
||||
type: "Dir",
|
||||
children: [
|
||||
{
|
||||
inode: 1,
|
||||
name: "hello.txt",
|
||||
type: "File",
|
||||
content: "Hello, world!",
|
||||
children: [],
|
||||
},
|
||||
{
|
||||
inode: 2,
|
||||
name: "sub",
|
||||
type: "Dir",
|
||||
children: [
|
||||
{
|
||||
inode: 3,
|
||||
name: "world.txt",
|
||||
type: "File",
|
||||
content: "Goodbye!",
|
||||
children: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const fs = buildTestFS(testTree);
|
||||
|
||||
describe("traverseTree", () => {
|
||||
test("successfully traverses the tree in depth-first pre-order", () => {
|
||||
const result = traverseTree(fs, 0);
|
||||
expect(result, `Checking the result ${result} to be [0, 1, 2, 3]`).toEqual([0, 2, 3, 1]);
|
||||
});
|
||||
|
||||
test("returns empty array for a non existent root inode", () => {
|
||||
expect(traverseTree(fs, 999)).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,71 @@
|
||||
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";
|
||||
|
||||
export interface FixtureNode {
|
||||
inode: number;
|
||||
name: string;
|
||||
type: "File" | "Dir";
|
||||
children: FixtureNode[];
|
||||
content?: string;
|
||||
mode?: number;
|
||||
owner?: number;
|
||||
group?: number;
|
||||
mtime?: number;
|
||||
ctime?: number;
|
||||
atime?: number;
|
||||
}
|
||||
|
||||
export function buildTestFS(tree: FixtureNode): FileSystem {
|
||||
const map = new Map<number, TreeNode>();
|
||||
|
||||
function parse(node: FixtureNode, parent: number): void {
|
||||
if (node.type === "Dir") {
|
||||
for (const child of node.children) {
|
||||
parse(child, parent);
|
||||
}
|
||||
|
||||
const children: Record<string, number> = {};
|
||||
for (const child of node.children) {
|
||||
children[child.name] = child.inode;
|
||||
}
|
||||
|
||||
const directoryNode: DirectoryNode = {
|
||||
type: TreeNodeType.Directory,
|
||||
inode: node.inode,
|
||||
name: node.name,
|
||||
parent: parent,
|
||||
mode: node.mode ?? 0o777,
|
||||
owner: node.owner ?? 0,
|
||||
group: node.group ?? 0,
|
||||
mtime: node.mtime ?? Date.now(),
|
||||
ctime: node.ctime ?? Date.now(),
|
||||
atime: node.atime ?? Date.now(),
|
||||
children: children,
|
||||
};
|
||||
map.set(node.inode, Object.freeze(directoryNode));
|
||||
} else {
|
||||
const fileNode: FileNode = {
|
||||
type: TreeNodeType.File,
|
||||
inode: node.inode,
|
||||
name: node.name,
|
||||
parent: parent,
|
||||
mode: node.mode ?? 0o777,
|
||||
owner: node.owner ?? 0,
|
||||
group: node.group ?? 0,
|
||||
mtime: node.mtime ?? Date.now(),
|
||||
ctime: node.ctime ?? Date.now(),
|
||||
atime: node.atime ?? Date.now(),
|
||||
content: createImmutableBuffer(new TextEncoder().encode(node.content)),
|
||||
};
|
||||
map.set(node.inode, Object.freeze(fileNode));
|
||||
}
|
||||
}
|
||||
|
||||
parse(tree, 0);
|
||||
|
||||
const base: ReadonlyMap<number, TreeNode> = map;
|
||||
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
|
||||
|
||||
return FileSystem.init(base, overlay, base.size - 1);
|
||||
}
|
||||
Reference in New Issue
Block a user