implemented the cd command - not tested properly

This commit is contained in:
2026-07-28 05:44:17 +02:00
parent 3b50214041
commit f87aa4eb85
6 changed files with 233 additions and 3 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
import type { Result, ShellError } from "./result";
import type { ShellError } from "./errors";
import type { Result } from "./result";
import type { ShellState } from "./shell_state";
type CommandFn = (state: ShellState, flags: string[], args: string[]) => CommandOutput<ShellError>;
+2
View File
@@ -1,9 +1,11 @@
import type { CommandDescriptor } from "./command";
import { descriptor as cd } from "./commands/cd";
import { descriptor as echo } from "./commands/echo";
const reg = new Map<string, CommandDescriptor>();
//reg.set(descriptor.name, descriptor <- imported const from command implementation file);
reg.set(echo.name, echo);
reg.set(cd.name, cd);
Object.freeze(reg);
export const COMMAND_REGISTRY: ReadonlyMap<string, CommandDescriptor> = reg;
+112
View File
@@ -0,0 +1,112 @@
import type { CommandDescriptor, CommandOutput } from "@core/command";
import {
type InvalidArgumentsError,
type PathResolutionError,
type PermissionDeniedError,
type ShellError,
} from "@core/errors";
import type { FileSystem } from "@core/filesystem/filesystem";
import { resolveInode, resolvePath } from "@core/path_resolver";
import { createErrorResult, createSuccessResult } from "@core/result";
import { getCurrentSession } from "@core/session";
import { ShellState, withCwd } from "@core/shell_state";
import { createImmutableBuffer } from "@core/util/immutable_buffer";
export const COMMAND_NAME = "cd";
function main(state: ShellState, __flags: string[], args: string[]): CommandOutput<ShellError> {
if (args.length > 1) {
return {
state,
result: createErrorResult(
"InvalidArguments",
"The maximum ammount of arguments for this command is 1",
{ expected: "cd <path>", actual: `cd ${args.toString().replace(/[,]/g, " ")}` },
createImmutableBuffer(new Uint8Array()),
createImmutableBuffer(new Uint8Array()),
COMMAND_NAME,
),
} satisfies CommandOutput<InvalidArgumentsError>;
}
let inode: number;
const session = getCurrentSession(state);
switch (args[0]) {
case undefined:
inode = session.user.home;
break;
case "-":
inode = session.pwd;
break;
default:
const result = safeResolvePath(state.filesystem, session.cwd, args[0]);
if (result.type === "error") {
return {
state,
result: createErrorResult(
"PathResolutionError",
"No such file or directory",
{ path: args[0]! },
createImmutableBuffer(new Uint8Array()),
createImmutableBuffer(new Uint8Array()),
COMMAND_NAME,
),
} satisfies CommandOutput<PathResolutionError>;
}
inode = result.inode;
break;
}
if (state.filesystem.isFile(inode)) {
return {
state,
result: createErrorResult(
"PathResolutionError",
"No such file or directory",
{ path: resolveInode(state.filesystem, inode) },
createImmutableBuffer(new Uint8Array()),
createImmutableBuffer(new Uint8Array()),
COMMAND_NAME,
),
} satisfies CommandOutput<PathResolutionError>;
}
if (!state.filesystem.canExecute(inode, session.user)) {
return {
state,
result: createErrorResult(
"PermissionDenied",
"Permission denied",
{ resource: args[0]! },
createImmutableBuffer(new Uint8Array()),
createImmutableBuffer(new Uint8Array()),
COMMAND_NAME,
),
} satisfies CommandOutput<PermissionDeniedError>;
}
const returnState = withCwd(state, inode);
return {
state: returnState,
result: createSuccessResult(COMMAND_NAME),
};
function safeResolvePath(
fs: FileSystem,
cwd: number,
path: string,
): { type: "success"; inode: number } | { type: "error" } {
try {
return { type: "success", inode: resolvePath(fs, cwd, path) };
} catch {
return { type: "error" };
}
}
}
export const descriptor: CommandDescriptor = {
fn: main,
name: COMMAND_NAME,
};
+3 -1
View File
@@ -1,10 +1,12 @@
import type { CommandDescriptor, CommandOutput } from "@core/command";
import type { ResultSuccess, ShellError } from "@core/result";
import type { ShellError } from "@core/errors";
import type { ResultSuccess } from "@core/result";
import type { ShellState } from "@core/shell_state";
import { createImmutableBuffer } from "@core/util/immutable_buffer";
export const COMMAND_NAME = "echo";
//Need to implement escape sequences
function main(state: ShellState, flags: string[], args: string[]): CommandOutput<ShellError> {
const joined = args.join(" ");
const encoded = new TextEncoder().encode(joined);
+112
View File
@@ -0,0 +1,112 @@
import { descriptor as cd } from "@core/commands/cd";
import type { ResultSuccess } from "@core/result";
import { getCurrentSession } from "@core/session";
import { createInitialShellState, withCwd, 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("changes cwd to home", () => {
withCwd(state, 2);
const output = cd.fn(state, [], []);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
expect((result as ResultSuccess).exitCode).toBe(0);
expect(session.cwd).toBe(0);
expect(session.pwd).toBe(2);
});
test("changes cwd to pwd", () => {
withCwd(state, 2);
const output = cd.fn(state, [], ["-"]);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
expect(session.exitCode).toBe(0);
expect(session.cwd).toBe(0);
expect(session.pwd).toBe(2);
});
test("changes cwd to 4", () => {
const output = cd.fn(state, [], ["/sub/bye"]);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
expect(session.exitCode).toBe(0);
expect(session.cwd).toBe(4);
expect(session.pwd).toBe(0);
});
test("returns an error result (tried to enter a non existent location)", () => {
const output = cd.fn(state, [], ["/not/real"]);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
console.log(result);
expect(session.exitCode).toBe(0);
expect(session.cwd).toBe(4);
expect(session.pwd).toBe(0);
});
test("returns an error result (tried to enter a file)", () => {
const newState = cd.fn(state, [], ["/hello.txt"]).state;
const session = getCurrentSession(newState);
expect(session.exitCode).toBe(0);
expect(session.cwd).toBe(4);
expect(session.pwd).toBe(0);
});
+2 -1
View File
@@ -1,4 +1,5 @@
import { descriptor as echo } from "@core/commands/echo";
import type { ResultSuccess } from "@core/result";
import { createInitialShellState, type ShellState } from "@core/shell_state";
import type { UserInfo } from "@core/user_info";
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
@@ -58,6 +59,6 @@ test("returns a Result of type success", () => {
const string = new TextDecoder().decode(output.result.stdout.toArray());
expect(output.result.type).toBe("success");
expect(output.result.exitCode).toBe(0);
expect((output.result as ResultSuccess).exitCode).toBe(0);
expect(string).toBe("hello world");
});