From ace3e0b3acfa12695f93de428c5a77313eb63b80 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 20 Sep 2025 10:53:56 -0500 Subject: [PATCH] Fix cd command --- src/lib/commands/README.md | 5 +++-- src/lib/commands/cd.test.ts | 18 ++++++++++++++++-- src/lib/commands/cd.ts | 16 ++++++++-------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/lib/commands/README.md b/src/lib/commands/README.md index 153751fd..5bf08cfa 100644 --- a/src/lib/commands/README.md +++ b/src/lib/commands/README.md @@ -78,15 +78,16 @@ Change the current working directory. **Examples:** ```bash -cd # Go to project root +cd # Stay in current directory cd src/ # Change to src directory cd .. # Go to parent directory cd . # Stay in current directory +cd /projects/my-project # Change to absolute path ``` **Features:** - Changes working directory for subsequent commands -- Supports relative paths (., .., subdirectories) +- Supports both relative and absolute paths - Validates target exists and is a directory - Updates shell working directory state diff --git a/src/lib/commands/cd.test.ts b/src/lib/commands/cd.test.ts index ffa89eda..6ee6537f 100644 --- a/src/lib/commands/cd.test.ts +++ b/src/lib/commands/cd.test.ts @@ -98,11 +98,25 @@ describe('CdCommand', () => { expect(result.stderr).toContain('Not a directory'); }); - it('should reject absolute paths', async () => { + it('should handle absolute paths to non-existent directories', async () => { + vi.mocked(mockFS.stat).mockRejectedValue(new Error('ENOENT: no such file or directory')); + const result = await cdCommand.execute(['/absolute/path'], testCwd); expect(result.exitCode).toBe(1); - expect(result.stderr).toContain('absolute paths are not supported'); + expect(result.stderr).toContain('No such file or directory'); + }); + + it('should handle absolute paths to existing directories', async () => { + vi.mocked(mockFS.stat).mockResolvedValue({ + isDirectory: () => true, + isFile: () => false, + }); + + const result = await cdCommand.execute(['/projects/my-project'], testCwd); + + expect(result.exitCode).toBe(0); + expect(result.newCwd).toBe('/projects/my-project'); }); it('should reject too many arguments', async () => { diff --git a/src/lib/commands/cd.ts b/src/lib/commands/cd.ts index 19dbbd44..c2daf1d5 100644 --- a/src/lib/commands/cd.ts +++ b/src/lib/commands/cd.ts @@ -2,6 +2,7 @@ import { join, resolve, dirname } from "@std/path"; import type { JSRuntimeFS } from "../JSRuntime"; import type { ShellCommand, ShellCommandResult } from "./ShellCommand"; import { createSuccessResult, createErrorResult } from "./ShellCommand"; +import { isAbsolutePath } from "../security"; /** * Implementation of the 'cd' command @@ -31,20 +32,19 @@ export class CdCommand implements ShellCommand { return createErrorResult(`${this.name}: too many arguments`); } - // Handle absolute paths - if (targetPath.startsWith('/') || targetPath.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(targetPath)) { - return createErrorResult(`${this.name}: absolute paths are not supported: ${targetPath}`); - } - - // Resolve the new path + // Handle both absolute and relative paths let newCwd: string; - if (targetPath === '.') { + + if (isAbsolutePath(targetPath)) { + // Absolute path - use it directly + newCwd = targetPath; + } else if (targetPath === '.') { // Current directory newCwd = cwd; } else if (targetPath === '..') { // Parent directory const parentPath = dirname(cwd); - // Don't allow going above the project root + // Don't allow going above the filesystem root if (parentPath === cwd) { return createErrorResult(`${this.name}: cannot access parent directory: Permission denied`); }