mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +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.
126 lines
4.5 KiB
TypeScript
126 lines
4.5 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { FindCommand } from './find';
|
|
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' || path === '/project/src') {
|
|
return { isFile: () => false, isDirectory: () => true };
|
|
}
|
|
throw new Error('ENOENT: not found');
|
|
},
|
|
readdir: async (path: string, _options: { withFileTypes: boolean }) => {
|
|
if (path === '/project') {
|
|
return [
|
|
{ name: 'test.txt', isFile: () => true, isDirectory: () => false },
|
|
{ name: 'package.json', isFile: () => true, isDirectory: () => false },
|
|
{ name: 'src', isFile: () => false, isDirectory: () => true },
|
|
];
|
|
}
|
|
if (path === '/project/src') {
|
|
return [
|
|
{ name: 'index.ts', isFile: () => true, isDirectory: () => false },
|
|
{ name: 'utils.ts', isFile: () => true, isDirectory: () => false },
|
|
];
|
|
}
|
|
return [];
|
|
},
|
|
} as unknown as JSRuntimeFS;
|
|
|
|
describe('FindCommand', () => {
|
|
let command: FindCommand;
|
|
|
|
beforeEach(() => {
|
|
command = new FindCommand(mockFS);
|
|
});
|
|
|
|
it('should have correct name and description', () => {
|
|
expect(command.name).toBe('find');
|
|
expect(command.description).toBe('Search for files and directories');
|
|
expect(command.usage).toBe('find [path...] [expression]');
|
|
});
|
|
|
|
it('should find all files and directories by default', async () => {
|
|
const result = await command.execute(['.'], '/project');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('.');
|
|
expect(result.stdout).toContain('./test.txt');
|
|
expect(result.stdout).toContain('./package.json');
|
|
expect(result.stdout).toContain('./src');
|
|
expect(result.stdout).toContain('./src/index.ts');
|
|
});
|
|
|
|
it('should filter by file type with -type f', async () => {
|
|
const result = await command.execute(['.', '-type', 'f'], '/project');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('./test.txt');
|
|
expect(result.stdout).toContain('./package.json');
|
|
expect(result.stdout.split('\n')).not.toContain('./src');
|
|
});
|
|
|
|
it('should filter by directory type with -type d', async () => {
|
|
const result = await command.execute(['.', '-type', 'd'], '/project');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('.');
|
|
expect(result.stdout).toContain('./src');
|
|
expect(result.stdout).not.toContain('./test.txt');
|
|
});
|
|
|
|
it('should filter by name pattern', async () => {
|
|
const result = await command.execute(['.', '-name', '*.ts'], '/project');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('./src/index.ts');
|
|
expect(result.stdout).toContain('./src/utils.ts');
|
|
expect(result.stdout).not.toContain('./test.txt');
|
|
});
|
|
|
|
it('should combine name and type filters', async () => {
|
|
const result = await command.execute(['.', '-name', '*.ts', '-type', 'f'], '/project');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('./src/index.ts');
|
|
expect(result.stdout).toContain('./src/utils.ts');
|
|
expect(result.stdout.split('\n')).not.toContain('./src');
|
|
});
|
|
|
|
it('should handle file not found', async () => {
|
|
const result = await command.execute(['nonexistent'], '/project');
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('ENOENT');
|
|
});
|
|
|
|
it('should support absolute paths', async () => {
|
|
const result = await command.execute(['/project', '-name', '*.txt'], '/project');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('/project/test.txt');
|
|
});
|
|
|
|
it('should support -iname for case-insensitive matching', async () => {
|
|
const result = await command.execute(['.', '-iname', '*.TS'], '/project');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('./src/index.ts');
|
|
});
|
|
|
|
it('should support -maxdepth', async () => {
|
|
const result = await command.execute(['.', '-maxdepth', '1', '-type', 'f'], '/project');
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('./test.txt');
|
|
expect(result.stdout).not.toContain('./src/index.ts');
|
|
});
|
|
|
|
it('should default to current directory when no path specified', async () => {
|
|
const result = await command.execute(['-name', '*.txt'], '/project');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('./test.txt');
|
|
});
|
|
}); |