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.
153 lines
5.4 KiB
TypeScript
153 lines
5.4 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { GrepCommand } from './grep';
|
|
import type { JSRuntimeFS } from '../JSRuntime';
|
|
|
|
// Mock filesystem
|
|
const mockFS = {
|
|
stat: async (path: string) => {
|
|
if (path === '/project/test.txt') {
|
|
return { isFile: () => true, isDirectory: () => false };
|
|
}
|
|
if (path === '/project/dir') {
|
|
return { isFile: () => false, isDirectory: () => true };
|
|
}
|
|
throw new Error('ENOENT: not found');
|
|
},
|
|
readFile: async (path: string, _encoding: string) => {
|
|
if (path === '/project/test.txt') {
|
|
return 'Hello World\nThis is a test\nHELLO again\nAnother line\ntest file';
|
|
}
|
|
throw new Error('ENOENT: not found');
|
|
},
|
|
readdir: async (path: string, _options: { withFileTypes: boolean }) => {
|
|
if (path === '/project/dir') {
|
|
return [
|
|
{ name: 'file1.txt', isFile: () => true, isDirectory: () => false },
|
|
{ name: 'file2.txt', isFile: () => true, isDirectory: () => false },
|
|
];
|
|
}
|
|
return [];
|
|
},
|
|
} as unknown as JSRuntimeFS;
|
|
|
|
describe('GrepCommand', () => {
|
|
let command: GrepCommand;
|
|
|
|
beforeEach(() => {
|
|
command = new GrepCommand(mockFS);
|
|
});
|
|
|
|
it('should have correct name and description', () => {
|
|
expect(command.name).toBe('grep');
|
|
expect(command.description).toBe('Search for patterns in files');
|
|
expect(command.usage).toBe('grep [-EFGivwxclLnHhoqrRs] [-A NUM] [-B NUM] [-C NUM] [-e PATTERN] [-f FILE] [--include GLOB] [--exclude GLOB] [--] PATTERN [file...]');
|
|
});
|
|
|
|
it('should search for pattern in file', async () => {
|
|
const result = await command.execute(['test', 'test.txt'], '/project');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('This is a test');
|
|
expect(result.stdout).toContain('test file');
|
|
});
|
|
|
|
it('should search case-insensitive with -i', async () => {
|
|
const result = await command.execute(['-i', 'hello', 'test.txt'], '/project');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('Hello World');
|
|
expect(result.stdout).toContain('HELLO again');
|
|
});
|
|
|
|
it('should show line numbers with -n', async () => {
|
|
const result = await command.execute(['-n', 'test', 'test.txt'], '/project');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('2:This is a test');
|
|
expect(result.stdout).toContain('5:test file');
|
|
});
|
|
|
|
it('should return exit code 1 when no matches found', async () => {
|
|
const result = await command.execute(['nonexistent', 'test.txt'], '/project');
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stdout).toBe('');
|
|
});
|
|
|
|
it('should handle file not found', async () => {
|
|
const result = await command.execute(['pattern', 'nonexistent.txt'], '/project');
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('No such file or directory');
|
|
});
|
|
|
|
it('should handle directory without -r flag', async () => {
|
|
const result = await command.execute(['pattern', 'dir'], '/project');
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('Is a directory');
|
|
});
|
|
|
|
it('should require pattern argument', async () => {
|
|
const result = await command.execute([], '/project');
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('missing pattern');
|
|
});
|
|
|
|
it('should support absolute paths', async () => {
|
|
const result = await command.execute(['test', '/project/test.txt'], '/project');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('This is a test');
|
|
});
|
|
|
|
it('should read from stdin via -', async () => {
|
|
const result = await command.execute(['foo', '-'], '/project', 'foo\nbar\nfoobar\n');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('foo');
|
|
expect(result.stdout).toContain('foobar');
|
|
});
|
|
|
|
it('should support -v to invert match', async () => {
|
|
const result = await command.execute(['-v', 'test', 'test.txt'], '/project');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('Hello World');
|
|
expect(result.stdout).not.toContain('This is a test');
|
|
});
|
|
|
|
it('should support -c to count matches', async () => {
|
|
const result = await command.execute(['-c', 'test', 'test.txt'], '/project');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout.trim()).toBe('2');
|
|
});
|
|
|
|
it('should support -l to list files with matches', async () => {
|
|
const result = await command.execute(['-l', 'test', 'test.txt'], '/project');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout.trim()).toBe('test.txt');
|
|
});
|
|
|
|
it('should support -o to only show matches', async () => {
|
|
const result = await command.execute(['-o', 'test', 'test.txt'], '/project');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('test');
|
|
});
|
|
|
|
it('should support -q for quiet mode', async () => {
|
|
const result = await command.execute(['-q', 'test', 'test.txt'], '/project');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('');
|
|
});
|
|
|
|
it('should support -F for fixed strings', async () => {
|
|
const result = await command.execute(['-F', 'World', 'test.txt'], '/project');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('Hello World');
|
|
});
|
|
|
|
it('should support -w for word match', async () => {
|
|
const result = await command.execute(['-w', 'test', 'test.txt'], '/project');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('This is a test');
|
|
});
|
|
}); |