Let ls work on absolute paths

This commit is contained in:
Alex Gleason committed 2025-09-14 14:41:38 -05:00
1 parent feaa767632
commit ffb9c3d95e
2 files changed
+16 -9

No files matched your search

+12 -3
View File
@@ -131,10 +131,19 @@ describe('LsCommand', () => {
expect(result.stderr).toContain('No such file or directory');
});
it('should reject absolute paths', async () => {
it('should handle absolute paths', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
vi.mocked(mockFS.readdir).mockResolvedValue([
{ name: 'file1.txt', isDirectory: () => false, isFile: () => true },
]);
const result = await lsCommand.execute(['/absolute/path'], testCwd);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('absolute paths are not supported');
expect(result.exitCode).toBe(0);
expect(mockFS.stat).toHaveBeenCalledWith('/absolute/path');
expect(result.stdout).toContain('file1.txt');
});
});
+4 -6
View File
@@ -28,12 +28,10 @@ export class LsCommand implements ShellCommand {
for (const path of targetPaths) {
try {
// Handle absolute paths
if (path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)) {
return createErrorResult(`${this.name}: absolute paths are not supported: ${path}`);
}
const absolutePath = join(cwd, path);
// Determine the absolute path
const absolutePath = path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)
? path // Already absolute
: join(cwd, path); // Make relative path absolute
const stats = await this.fs.stat(absolutePath);
if (stats.isFile()) {