mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Each built-in shell command has been audited and rewritten to support the options AI agents (and humans) typically reach for, close critical behavior gaps, and align with POSIX/GNU conventions: - Shared argument parser (utils.ts) handles combined short flags, long options with '=VALUE' or space-separated values, the '--' sentinel, numeric shortcuts like 'head -5', and '-' for stdin. - cat, ls, cp, mv, rm, mkdir, touch: added missing common flags; removed the legacy 'absolute paths are not supported' and 'reading from stdin is not supported' barriers where they made agent use awkward. - mv now overwrites destinations by default (POSIX) and supports -n. - touch updates mtime on existing files (by rewriting content) and accepts -c/-a/-m/-h/-r/-d/-t options. - find: new expression engine with -iname, -path, -regex, -type, -empty, -size, -maxdepth, -mindepth, -not, -or, -print/-print0, -delete, -quit. - grep: -v, -c, -l, -L, -w, -x, -o, -q, -E, -F, -G, -H, -h, -A/-B/-C context, --include/--exclude, multiple -e, -f scriptfile. - head/tail: -c bytes, -NUM shorthand, negative/positive line counts, proper multi-file headers. - sort: -k keys, -t separator, -o output, -f/-b/-d/-s, -h/-V/-M modes. - uniq: -i, -f, -s, -w, -D, and the [input [output]] form. - wc: -m characters and -L longest line; -c now counts bytes (UTF-8). - cut: open-ended ranges (3-, -5), -s, --complement, --output-delimiter. - tr: -c/-s/-t, [:class:] character classes, escape sequences. - sed: real multi-command parser supporting ';', addresses (N, $, /re/, N,M), '!' negation, proper -n quiet mode, y///, and -i[SUFFIX]. - echo: -n, -e, -E with standard backslash escapes. - pwd/cd: -L/-P, plus cd -, cd ~, cd $HOME with OLDPWD tracking. - diff: proper LCS algorithm producing correct normal and unified output; adds -q, -s, -N, -i, -w, -b, -B. - env: -i, -u, NAME=VAL assignments; reads the live shell environment. - date: -u, -d, -R, -I, and a full GNU-subset format specifier set. Tests for each command were updated to reflect the corrected behavior (e.g. absolute paths and stdin are now accepted, POSIX error messages). All 1334 tests pass; typecheck and production build succeed.
147 lines
4.3 KiB
TypeScript
147 lines
4.3 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
import { CdCommand } from './cd';
|
|
import type { JSRuntimeFS } from '../JSRuntime';
|
|
|
|
// Mock filesystem
|
|
const createMockFS = (): JSRuntimeFS => ({
|
|
readFile: vi.fn(),
|
|
writeFile: vi.fn(),
|
|
readdir: vi.fn(),
|
|
mkdir: vi.fn(),
|
|
stat: vi.fn(),
|
|
lstat: vi.fn(),
|
|
unlink: vi.fn(),
|
|
rmdir: vi.fn(),
|
|
rename: vi.fn(),
|
|
readlink: vi.fn(),
|
|
symlink: vi.fn(),
|
|
});
|
|
|
|
describe('CdCommand', () => {
|
|
let mockFS: JSRuntimeFS;
|
|
let cdCommand: CdCommand;
|
|
const testCwd = '/test/current/dir';
|
|
|
|
beforeEach(() => {
|
|
mockFS = createMockFS();
|
|
cdCommand = new CdCommand(mockFS);
|
|
});
|
|
|
|
it('should have correct command properties', () => {
|
|
expect(cdCommand.name).toBe('cd');
|
|
expect(cdCommand.description).toBe('Change directory');
|
|
expect(cdCommand.usage).toBe('cd [-LP] [directory | -]');
|
|
});
|
|
|
|
it('should change to specified directory', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({
|
|
isDirectory: () => true,
|
|
isFile: () => false,
|
|
});
|
|
|
|
const result = await cdCommand.execute(['subdir'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.newCwd).toBe('/test/current/dir/subdir');
|
|
expect(result.stderr).toBe('');
|
|
});
|
|
|
|
it('should handle current directory (.)', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({
|
|
isDirectory: () => true,
|
|
isFile: () => false,
|
|
});
|
|
|
|
const result = await cdCommand.execute(['.'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.newCwd).toBe(testCwd);
|
|
});
|
|
|
|
it('should handle parent directory (..)', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({
|
|
isDirectory: () => true,
|
|
isFile: () => false,
|
|
});
|
|
|
|
const result = await cdCommand.execute(['..'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.newCwd).toBe('/test/current');
|
|
});
|
|
|
|
it('should go to home directory when no arguments', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({
|
|
isDirectory: () => true,
|
|
isFile: () => false,
|
|
});
|
|
cdCommand.setHomeDir('/home/user');
|
|
|
|
const result = await cdCommand.execute([], testCwd);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.newCwd).toBe('/home/user');
|
|
});
|
|
|
|
it('should support cd - to go to OLDPWD', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({
|
|
isDirectory: () => true,
|
|
isFile: () => false,
|
|
});
|
|
// First cd to establish OLDPWD
|
|
await cdCommand.execute(['/other'], testCwd);
|
|
// Now cd - should go back to testCwd
|
|
const result = await cdCommand.execute(['-'], '/other');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.newCwd).toBe(testCwd);
|
|
});
|
|
|
|
it('should return error for non-existent directory', async () => {
|
|
vi.mocked(mockFS.stat).mockRejectedValue(new Error('ENOENT: no such file or directory'));
|
|
|
|
const result = await cdCommand.execute(['nonexistent'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('No such file or directory');
|
|
});
|
|
|
|
it('should return error when target is not a directory', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({
|
|
isDirectory: () => false,
|
|
isFile: () => true,
|
|
});
|
|
|
|
const result = await cdCommand.execute(['file.txt'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('Not a directory');
|
|
});
|
|
|
|
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('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 () => {
|
|
const result = await cdCommand.execute(['dir1', 'dir2'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('too many arguments');
|
|
});
|
|
}); |