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.
274 lines
8.9 KiB
TypeScript
274 lines
8.9 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { SedCommand } from './sed';
|
|
import type { JSRuntimeFS } from '../JSRuntime';
|
|
|
|
// Mock filesystem
|
|
const mockFS: JSRuntimeFS = {
|
|
readFile: ((path: string, encoding?: string) => {
|
|
const files: Record<string, string> = {
|
|
'/test/hello.txt': 'hello world\nfoo bar\nbaz qux\n',
|
|
'/test/numbers.txt': '1\n2\n3\n4\n5\n',
|
|
'/test/mixed.txt': 'apple\nbanana\ncherry\napple pie\n',
|
|
};
|
|
|
|
if (path in files) {
|
|
if (encoding === 'utf8' || (encoding && encoding !== undefined)) {
|
|
return Promise.resolve(files[path]);
|
|
} else {
|
|
// No encoding specified, return Uint8Array
|
|
return Promise.resolve(new TextEncoder().encode(files[path]));
|
|
}
|
|
}
|
|
return Promise.reject(new Error('ENOENT: no such file or directory'));
|
|
}) as JSRuntimeFS['readFile'],
|
|
|
|
writeFile: async (_path: string, _data: string | Uint8Array, _encoding?: string) => {
|
|
// Mock write operation
|
|
},
|
|
|
|
stat: async (_path: string) => ({
|
|
isFile: () => true,
|
|
isDirectory: () => false,
|
|
size: 100,
|
|
mtimeMs: Date.now(),
|
|
}),
|
|
|
|
lstat: async (_path: string) => ({
|
|
isFile: () => true,
|
|
isDirectory: () => false,
|
|
size: 100,
|
|
mtimeMs: Date.now(),
|
|
}),
|
|
|
|
readdir: (async (_path: string, _options?: { withFileTypes?: boolean }) => {
|
|
if (_options?.withFileTypes) {
|
|
return [];
|
|
}
|
|
return [];
|
|
}) as JSRuntimeFS['readdir'],
|
|
mkdir: async () => {},
|
|
rmdir: async () => {},
|
|
unlink: async () => {},
|
|
rename: async () => {},
|
|
readlink: async () => '',
|
|
symlink: async () => {},
|
|
};
|
|
|
|
describe('SedCommand', () => {
|
|
let sedCommand: SedCommand;
|
|
|
|
beforeEach(() => {
|
|
sedCommand = new SedCommand(mockFS);
|
|
});
|
|
|
|
describe('basic functionality', () => {
|
|
it('should have correct name and description', () => {
|
|
expect(sedCommand.name).toBe('sed');
|
|
expect(sedCommand.description).toBe('Stream editor for filtering and transforming text');
|
|
});
|
|
|
|
it('should return error when no script provided', async () => {
|
|
const result = await sedCommand.execute([], '/test');
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('missing script');
|
|
});
|
|
});
|
|
|
|
describe('substitute command', () => {
|
|
it('should substitute text with s command', async () => {
|
|
const input = 'hello world\nfoo bar\n';
|
|
const result = await sedCommand.execute(['s/hello/hi/'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('hi world\nfoo bar\n');
|
|
});
|
|
|
|
it('should substitute globally with g flag', async () => {
|
|
const input = 'hello hello world\n';
|
|
const result = await sedCommand.execute(['s/hello/hi/g'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('hi hi world\n');
|
|
});
|
|
|
|
it('should substitute case-insensitively with i flag', async () => {
|
|
const input = 'Hello HELLO world\n';
|
|
const result = await sedCommand.execute(['s/hello/hi/gi'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('hi hi world\n');
|
|
});
|
|
|
|
it('should substitute with special characters', async () => {
|
|
const input = 'test.txt\n';
|
|
const result = await sedCommand.execute(['s/\\.txt/.bak/'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('test.bak\n');
|
|
});
|
|
});
|
|
|
|
describe('delete command', () => {
|
|
it('should delete all lines with d command', async () => {
|
|
const input = 'line1\nline2\nline3\n';
|
|
const result = await sedCommand.execute(['d'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('\n');
|
|
});
|
|
|
|
it('should delete specific line with line number', async () => {
|
|
const input = 'line1\nline2\nline3\n';
|
|
const result = await sedCommand.execute(['2d'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('line1\nline3\n');
|
|
});
|
|
|
|
it('should delete lines matching pattern', async () => {
|
|
const input = 'keep this\ndelete this line\nkeep this too\n';
|
|
const result = await sedCommand.execute(['/delete/d'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('keep this\nkeep this too\n');
|
|
});
|
|
});
|
|
|
|
describe('print command', () => {
|
|
it('should print all lines normally without -n', async () => {
|
|
const input = 'line1\nline2\n';
|
|
const result = await sedCommand.execute(['p'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('line1\nline1\nline2\nline2\n'); // Each line printed twice (normal + p command)
|
|
});
|
|
|
|
it('should print only specified lines with -n', async () => {
|
|
const input = 'line1\nline2\nline3\n';
|
|
const result = await sedCommand.execute(['-n', '2p'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('line2\n');
|
|
});
|
|
});
|
|
|
|
describe('quit command', () => {
|
|
it('should quit after processing specified line', async () => {
|
|
const input = 'line1\nline2\nline3\nline4\n';
|
|
const result = await sedCommand.execute(['2q'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('line1\nline2\n');
|
|
});
|
|
});
|
|
|
|
describe('append, insert, and change commands', () => {
|
|
it('should append text after line', async () => {
|
|
const input = 'line1\nline2\n';
|
|
const result = await sedCommand.execute(['1a\\appended text'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('line1\nappended text\nline2\n');
|
|
});
|
|
|
|
it('should insert text before line', async () => {
|
|
const input = 'line1\nline2\n';
|
|
const result = await sedCommand.execute(['2i\\inserted text'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('line1\ninserted text\nline2\n');
|
|
});
|
|
|
|
it('should change line', async () => {
|
|
const input = 'line1\nline2\nline3\n';
|
|
const result = await sedCommand.execute(['2c\\changed text'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('line1\nchanged text\nline3\n');
|
|
});
|
|
});
|
|
|
|
describe('file operations', () => {
|
|
it('should process file content', async () => {
|
|
const result = await sedCommand.execute(['s/hello/hi/'], '/test', undefined);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('');
|
|
});
|
|
|
|
it('should handle file not found error', async () => {
|
|
const result = await sedCommand.execute(['s/hello/hi/', 'nonexistent.txt'], '/test');
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('No such file or directory');
|
|
});
|
|
|
|
it('should accept absolute paths', async () => {
|
|
const result = await sedCommand.execute(['s/hello/hi/', '/test/hello.txt'], '/test');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain('hi world');
|
|
});
|
|
});
|
|
|
|
describe('option parsing', () => {
|
|
it('should parse -n option', async () => {
|
|
const input = 'line1\nline2\n';
|
|
const result = await sedCommand.execute(['-n', 'p'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('line1\nline2\n');
|
|
});
|
|
|
|
it('should parse -e option', async () => {
|
|
const input = 'hello world\n';
|
|
const result = await sedCommand.execute(['-e', 's/hello/hi/'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('hi world\n');
|
|
});
|
|
|
|
it('should parse combined options', async () => {
|
|
const input = 'line1\nline2\n';
|
|
const result = await sedCommand.execute(['-ne', 'p'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('line1\nline2\n');
|
|
});
|
|
});
|
|
|
|
describe('multiple scripts', () => {
|
|
it('should process multiple scripts in order', async () => {
|
|
const input = 'hello world\n';
|
|
const result = await sedCommand.execute(['-e', 's/hello/hi/', '-e', 's/world/universe/'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('hi universe\n');
|
|
});
|
|
});
|
|
|
|
describe('edge cases', () => {
|
|
it('should handle empty input', async () => {
|
|
const result = await sedCommand.execute(['s/hello/hi/'], '/test', '');
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('');
|
|
});
|
|
|
|
it('should handle invalid regex patterns gracefully', async () => {
|
|
const input = 'hello world\n';
|
|
const result = await sedCommand.execute(['s/[/hi/'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toBe('hello world\n'); // Should return original
|
|
});
|
|
|
|
it('should handle invalid script format', async () => {
|
|
const input = 'hello world\n';
|
|
const result = await sedCommand.execute(['invalid_script'], '/test', input);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('Invalid sed script');
|
|
});
|
|
});
|
|
}); |