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'); 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); const result = await lsCommand.execute(['/absolute/path'], testCwd);
expect(result.exitCode).toBe(1); expect(result.exitCode).toBe(0);
expect(result.stderr).toContain('absolute paths are not supported'); 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) { for (const path of targetPaths) {
try { try {
// Handle absolute paths // Determine the absolute path
if (path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)) { const absolutePath = path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)
return createErrorResult(`${this.name}: absolute paths are not supported: ${path}`); ? path // Already absolute
} : join(cwd, path); // Make relative path absolute
const absolutePath = join(cwd, path);
const stats = await this.fs.stat(absolutePath); const stats = await this.fs.stat(absolutePath);
if (stats.isFile()) { if (stats.isFile()) {