Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 'cd -' #11

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/commands/builtin_command_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@ export class BuiltinCommandRunner implements ICommandRunner {
if (args.length < 1) {
// Do nothing.
return;
} else if (args.length > 1) {
throw new Error("cd: too many arguments")
}

let path = args[0]
if (path == "-") {
const oldPwd = context.environment.get("OLDPWD")
if (oldPwd === null) {
throw new Error("cd: OLDPWD not set")
}
path = oldPwd
}
const path = args[0] // Ignore other arguments?
// Need to handle path of "-". Maybe previous path is in an env var? "OLDPWD"

const { FS } = context.fileSystem
const oldPwd = FS.cwd()
FS.chdir(path)
context.environment.set("OLDPWD", oldPwd)
context.environment.set("PWD", FS.cwd())
}
}
44 changes: 44 additions & 0 deletions tests/commands/cd.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
import { shell_setup_simple } from "../shell_setup"

describe("cd command", () => {
it("should do nothing if no arguments", async () => {
const { shell, output } = await shell_setup_simple()
await shell._runCommands("cd")
expect(output.text).toEqual("")
})

it("should error if more than one argument", async () => {
const { shell, output } = await shell_setup_simple()
await shell._runCommands("cd a b")
expect(output.text).toMatch(/cd: too many arguments/)
})

it("should change directory", async () => {
const { shell, output } = await shell_setup_simple()

await shell._runCommands("pwd")
expect(output.text).toEqual("/drive\r\n")
output.clear()

await shell._runCommands("cd dirA")
await shell._runCommands("pwd")
expect(output.text).toEqual("/drive/dirA\r\n")
})

it("should update PWD", async () => {
const { shell } = await shell_setup_simple()
const { environment } = shell

expect(environment.get("PWD")).toEqual("/drive")
await shell._runCommands("cd dirA")
expect(environment.get("PWD")).toEqual("/drive/dirA")
})

it("should support cd -", async () => {
const { shell } = await shell_setup_simple()
const { environment } = shell

expect(environment.get("OLDPWD")).toBeNull()
await shell._runCommands("cd dirA")
expect(environment.get("PWD")).toEqual("/drive/dirA")
expect(environment.get("OLDPWD")).toEqual("/drive")
await shell._runCommands("cd -")
expect(environment.get("PWD")).toEqual("/drive")
expect(environment.get("OLDPWD")).toEqual("/drive/dirA")
})

it("should error if use cd - and OLDPWD not set", async () => {
const { shell, output } = await shell_setup_simple()
await shell._runCommands("cd -")
expect(output.text).toMatch(/cd: OLDPWD not set/)
})
})
Loading