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.
129 lines
4.6 KiB
TypeScript
129 lines
4.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { DiffCommand } from './diff';
|
|
import type { JSRuntimeFS } from '../JSRuntime';
|
|
|
|
describe('DiffCommand', () => {
|
|
const mockFS = {
|
|
stat: vi.fn(),
|
|
readFile: vi.fn(),
|
|
} as unknown as JSRuntimeFS;
|
|
|
|
const diffCommand = new DiffCommand(mockFS);
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('should return empty output for identical files', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
|
|
vi.mocked(mockFS.readFile).mockResolvedValue('hello\nworld\n');
|
|
|
|
const result = await diffCommand.execute(['file1.txt', 'file2.txt'], '/test');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('');
|
|
expect(result.stderr).toBe('');
|
|
});
|
|
|
|
it('should show differences in normal format', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
|
|
vi.mocked(mockFS.readFile)
|
|
.mockResolvedValueOnce('hello\nworld\n')
|
|
.mockResolvedValueOnce('hello\nplanet\n');
|
|
|
|
const result = await diffCommand.execute(['file1.txt', 'file2.txt'], '/test');
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stdout).toContain('2c2');
|
|
expect(result.stdout).toContain('< world');
|
|
expect(result.stdout).toContain('> planet');
|
|
});
|
|
|
|
it('should show differences in unified format', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
|
|
vi.mocked(mockFS.readFile)
|
|
.mockResolvedValueOnce('hello\nworld\n')
|
|
.mockResolvedValueOnce('hello\nplanet\n');
|
|
|
|
const result = await diffCommand.execute(['-u', 'file1.txt', 'file2.txt'], '/test');
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stdout).toContain('--- file1.txt');
|
|
expect(result.stdout).toContain('+++ file2.txt');
|
|
expect(result.stdout).toContain('-world');
|
|
expect(result.stdout).toContain('+planet');
|
|
});
|
|
|
|
it('should handle added lines', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
|
|
vi.mocked(mockFS.readFile)
|
|
.mockResolvedValueOnce('hello\n')
|
|
.mockResolvedValueOnce('hello\nworld\n');
|
|
|
|
const result = await diffCommand.execute(['file1.txt', 'file2.txt'], '/test');
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stdout).toContain('a');
|
|
expect(result.stdout).toContain('> world');
|
|
});
|
|
|
|
it('should handle deleted lines', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
|
|
vi.mocked(mockFS.readFile)
|
|
.mockResolvedValueOnce('hello\nworld\n')
|
|
.mockResolvedValueOnce('hello\n');
|
|
|
|
const result = await diffCommand.execute(['file1.txt', 'file2.txt'], '/test');
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stdout).toContain('d');
|
|
expect(result.stdout).toContain('< world');
|
|
});
|
|
|
|
it('should error when wrong number of arguments', async () => {
|
|
const result = await diffCommand.execute(['file1.txt'], '/test');
|
|
|
|
expect(result.exitCode).not.toBe(0);
|
|
expect(result.stderr).toContain('missing operand');
|
|
});
|
|
|
|
it('should error for non-existent file', async () => {
|
|
vi.mocked(mockFS.stat).mockRejectedValue(new Error('ENOENT: no such file'));
|
|
|
|
const result = await diffCommand.execute(['nonexistent1.txt', 'nonexistent2.txt'], '/test');
|
|
|
|
expect(result.exitCode).not.toBe(0);
|
|
expect(result.stderr).toContain('No such file or directory');
|
|
});
|
|
|
|
it('should error for directory', async () => {
|
|
vi.mocked(mockFS.stat)
|
|
.mockResolvedValueOnce({ isDirectory: () => true, isFile: () => false })
|
|
.mockResolvedValueOnce({ isDirectory: () => false, isFile: () => true });
|
|
|
|
const result = await diffCommand.execute(['directory', 'file.txt'], '/test');
|
|
|
|
expect(result.exitCode).not.toBe(0);
|
|
expect(result.stderr).toContain('Is a directory');
|
|
});
|
|
|
|
it('should support absolute paths', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
|
|
vi.mocked(mockFS.readFile).mockResolvedValue('same\n');
|
|
const result = await diffCommand.execute(['/a/file1', '/b/file2'], '/test');
|
|
expect(result.exitCode).toBe(0);
|
|
});
|
|
|
|
it('should handle empty files', async () => {
|
|
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
|
|
vi.mocked(mockFS.readFile)
|
|
.mockResolvedValueOnce('')
|
|
.mockResolvedValueOnce('');
|
|
|
|
const result = await diffCommand.execute(['empty1.txt', 'empty2.txt'], '/test');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('');
|
|
expect(result.stderr).toBe('');
|
|
});
|
|
}); |