resolvePath function implemented and tested, all green.

This commit is contained in:
2026-07-26 16:43:39 +02:00
parent 994b93ae47
commit cd08a4bf32
8 changed files with 302 additions and 7 deletions
+63
View File
@@ -0,0 +1,63 @@
import { descriptor as echo } from "@core/commands/echo";
import { createInitialShellState, type ShellState } from "@core/shell_state";
import type { UserInfo } from "@core/user_info";
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
import { expect, test } from "vitest";
const testTree: FixtureNode = {
inode: 0,
name: "root",
type: "Dir",
children: [
{
inode: 1,
name: "hello.txt",
type: "File",
content: "Goodbye World!",
children: [],
},
{
inode: 2,
name: "sub",
type: "Dir",
children: [
{
inode: 3,
name: "world.txt",
type: "File",
content: "Goodbye!",
children: [],
},
{
inode: 4,
name: "bye",
type: "Dir",
children: [],
},
],
},
],
};
const fs = buildTestFS(testTree);
const users: Map<number, UserInfo> = new Map();
const user: UserInfo = {
name: "root",
uid: 0,
gid: 0,
group: [0],
home: 0,
};
users.set(0, user);
const state: ShellState = createInitialShellState(fs, users, user);
test("returns a Result of type success", () => {
const output = echo.fn(state, [], ["hello", "world"]);
const string = new TextDecoder().decode(output.result.stdout.toArray());
expect(output.result.type).toBe("success");
expect(output.result.exitCode).toBe(0);
expect(string).toBe("hello world");
});
+58 -7
View File
@@ -1,5 +1,6 @@
import { FileSystem } from "@core/filesystem/filesystem";
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/treenode";
import type { UserInfo } from "@core/user_info";
import { createImmutableBuffer } from "@core/util/immutable_buffer";
import {
expectAlreadyExistsError,
@@ -126,44 +127,93 @@ describe("permission method group", () => {
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
it("returns true for user read permissions", () => {
const user: UserInfo = {
name: "root",
uid: 1000,
gid: 1000,
group: [1000],
home: 0,
};
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o400, 1000, 1000, content);
const perms = newFS.canRead(4, { uid: 1000, gid: 1000 });
const perms = newFS.canRead(4, user);
expect(perms).toBe(true);
});
it("returns false for user read permissions", () => {
const user: UserInfo = {
name: "root",
uid: 1000,
gid: 1000,
group: [1000],
home: 0,
};
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o200, 1000, 1000, content);
const perms = newFS.canRead(5, { uid: 1000, gid: 1000 });
const perms = newFS.canRead(5, user);
expect(perms).toBe(false);
});
it("returns true for group read permissions", () => {
const user: UserInfo = {
name: "root",
uid: 999,
gid: 2000,
group: [1000],
home: 0,
};
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o040, 1000, 2000, content);
const perms = newFS.canRead(5, { uid: 999, gid: 2000 });
const perms = newFS.canRead(5, user);
expect(perms).toBe(true);
});
it("returns false for group read permissions", () => {
const user: UserInfo = {
name: "root",
uid: 999,
gid: 2000,
group: [1000],
home: 0,
};
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o020, 1000, 2000, content);
const perms = newFS.canRead(5, { uid: 999, gid: 2000 });
const perms = newFS.canRead(5, user);
expect(perms).toBe(false);
});
it("returns true for other read permissions", () => {
const user: UserInfo = {
name: "root",
uid: 999,
gid: 3000,
group: [1000],
home: 0,
};
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o004, 1000, 2000, content);
const perms = newFS.canRead(5, { uid: 999, gid: 3000 });
const perms = newFS.canRead(5, user);
expect(perms).toBe(true);
});
it("returns false for other read permissions", () => {
const user: UserInfo = {
name: "root",
uid: 999,
gid: 3000,
group: [1000],
home: 0,
};
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o002, 1000, 2000, content);
const perms = newFS.canRead(5, { uid: 999, gid: 3000 });
const perms = newFS.canRead(5, user);
expect(perms).toBe(false);
});
it("returns true for user execute permissions", () => {
const user: UserInfo = {
name: "root",
uid: 1000,
gid: 2000,
group: [1000],
home: 0,
};
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o100, 1000, 2000, content);
const perms = newFS.canExecute(5, { uid: 1000, gid: 2000 });
const perms = newFS.canExecute(5, user);
expect(perms).toBe(true);
});
@@ -334,6 +384,7 @@ describe("moveNode", () => {
expect(nodeDest.children[nodeSour.name]).not.toBeUndefined();
expect((newFS.getNode(0) as DirectoryNode).children[nodeSour.name]).toBeUndefined();
});
it("successfully renames a node", () => {
const newFS = fs.moveNode(3, 2, "foo.foo");
const nodeSour = newFS.getNode(3) as FileNode;
+63
View File
@@ -0,0 +1,63 @@
import { resolvePath } from "@core/path_resolver";
import { expectNodeNotFoundError, expectNotADirectoryError } from "@tests/helpers/error_matchers";
import { buildTestFS } from "@tests/helpers/filesystems_fixture";
import { depth7Tree } from "@tests/helpers/tree_fixture";
import { describe, expect, test } from "vitest";
const fs = buildTestFS(depth7Tree);
describe("resolvePath", () => {
test("returns an inode from an absolute path", () => {
const path = "/d1/d2/d3/d4/";
const result = resolvePath(fs, 1, path);
expect(result).toBeDefined();
expect(fs.getNode(result as number)!.name).toBe("d4");
});
test("returns an inode from a relative path", () => {
const path = "./d2/d3/d4/";
const result = resolvePath(fs, 1, path);
expect(result).toBeDefined();
expect(fs.getNode(result as number)!.name).toBe("d4");
});
test("returns an inode from a relative parent path", () => {
const path = "../d1/d2/d3/d4/";
const result = resolvePath(fs, 1, path);
expect(result).toBeDefined();
expect(fs.getNode(result as number)!.name).toBe("d4");
});
test("returns inode 0", () => {
const path = "/";
const result = resolvePath(fs, 1, path);
expect(result).toBeDefined();
expect(result).toBe(0);
expect(fs.getNode(result as number)!.name).toBe("root");
});
test("returns inode 1 (cwd)", () => {
const path = "";
const result = resolvePath(fs, 1, path);
expect(result).toBeDefined();
expect(result).toBe(1);
expect(fs.getNode(result as number)!.name).toBe("d1");
});
test("throws because of a nonexistent path", () => {
expectNodeNotFoundError(() => resolvePath(fs, 1, "/d3"));
});
test("throws because of file path component", () => {
expectNotADirectoryError(() => resolvePath(fs, 6, "./deep_file.txt/d8"));
});
test("throws because of a nonexistent cwd inode (technicaly should never happen)", () => {
expectNodeNotFoundError(() => resolvePath(fs, 69, "/d3"));
});
});