mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
The old ShellTool used a flat string scanner that couldn't distinguish
quote context from operator context. In practice this produced a long
tail of surprising behaviours that confused AI agents and occasionally
corrupted the virtual filesystem:
* `echo hi 2>&1` wrote 'hi 2' to a file named literally `&1`
* `echo hi >&2` wrote to a file named `&2`
* `cat < in.txt` silently produced no output (input redirection
was unimplemented but not rejected)
* `for f in *.tsx; do ... done` was split into three separate
"command not found" errors
* `echo $HOME`, `echo $(pwd)`, `echo \`pwd\`` all printed
the unexpanded source text
* `# comment`, backslash escapes, brace expansion, tilde, and
arithmetic all fell through as literal characters
Introduce a proper mini-shell in src/lib/shell/:
* tokenizer.ts — POSIX-flavoured lexer that understands single and
double quotes, backslash escapes (including line continuations),
comments, operators, fd-prefixed redirections (IO_NUMBER), and
dollar-led expressions ($VAR, ${...}, $(...), $((...)), backticks).
* parser.ts — recursive-descent parser producing an AST covering
pipelines, and-or lists, sequences, redirections (including `<<`
heredocs and fd duplication like `2>&1`), and the full suite of
compound commands: if/elif/else/fi, for, while, until, case,
subshells, and brace groups.
* expand.ts — word expansion following POSIX ordering: brace
expansion, tilde, parameter, command substitution, arithmetic,
word splitting, pathname expansion, quote removal. Glob patterns
match against the virtual filesystem; unmatched patterns pass
through unchanged.
* executor.ts — AST interpreter that dispatches simple commands to
the existing ShellCommand registry, applies redirections safely
(validateWritePath runs BEFORE any file write so bogus paths can
no longer materialize), evaluates control flow, and tracks exit
status in $? with a safe iteration cap on while/until loops.
* Built-ins: true, false, :, export, unset, exit, test, [.
ShellTool.ts is now a thin integration layer that wires the registry
into a ShellExecutor and formats the final output, keeping its public
API (execute, getCurrentWorkingDirectory, setCurrentWorkingDirectory,
getAvailableCommands) unchanged so existing callers (ChatPane,
Terminal) don't need to change.
Tests:
* All 35 existing ShellTool tests still pass (adjusted two
assertions for the improved error messages).
* Added 29 tokenizer, 19 parser, 24 expander, and 49 end-to-end
tests covering the bash features listed above and confirming that
the data-corrupting redirection bugs are gone.
* Full suite: 1316 passed / 6 skipped, typecheck clean, lint clean,
build clean.
178 lines
6.0 KiB
TypeScript
178 lines
6.0 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { tokenize } from './tokenizer';
|
|
import { parse } from './parser';
|
|
import { expandWord, expandWordToString, type ShellEnv } from './expand';
|
|
import type { JSRuntimeFS } from '../JSRuntime';
|
|
import type { SimpleCommand, Word } from './ast';
|
|
|
|
function wordFrom(src: string, argIndex = 1): Word {
|
|
const ast = parse(tokenize(src), src);
|
|
const cmd = ast.commands[0] as SimpleCommand;
|
|
return cmd.words[argIndex];
|
|
}
|
|
|
|
function makeEnv(overrides: Partial<ShellEnv> & { vars?: Record<string, string>; files?: Record<string, string[]> } = {}): ShellEnv {
|
|
const vars = overrides.vars ?? {};
|
|
const files = overrides.files ?? {};
|
|
const fs = {
|
|
readFile: async () => '',
|
|
writeFile: async () => {},
|
|
readdir: async (p: string) => files[p] ?? [],
|
|
mkdir: async () => {},
|
|
stat: async () => ({ isDirectory: () => false, isFile: () => true }),
|
|
lstat: async () => ({ isDirectory: () => false, isFile: () => true }),
|
|
unlink: async () => {},
|
|
rmdir: async () => {},
|
|
rename: async () => {},
|
|
readlink: async () => '',
|
|
symlink: async () => {},
|
|
} as unknown as JSRuntimeFS;
|
|
return {
|
|
getVar: (n) => vars[n] ?? '',
|
|
getCwd: () => '/work',
|
|
getHome: () => '/home/user',
|
|
runSubshell: async () => '',
|
|
getFS: () => fs,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe('expandWord', () => {
|
|
it('expands a plain literal', async () => {
|
|
const w = wordFrom('echo hello');
|
|
expect(await expandWord(w, makeEnv())).toEqual(['hello']);
|
|
});
|
|
|
|
it('expands a $VAR', async () => {
|
|
const w = wordFrom('echo $HOME');
|
|
const env = makeEnv({ vars: { HOME: '/h' } });
|
|
expect(await expandWord(w, env)).toEqual(['/h']);
|
|
});
|
|
|
|
it('expands a ${VAR}', async () => {
|
|
const w = wordFrom('echo ${USER}x');
|
|
const env = makeEnv({ vars: { USER: 'alex' } });
|
|
expect(await expandWord(w, env)).toEqual(['alexx']);
|
|
});
|
|
|
|
it('does not expand variables in single quotes', async () => {
|
|
const w = wordFrom("echo '$HOME'");
|
|
const env = makeEnv({ vars: { HOME: '/h' } });
|
|
expect(await expandWord(w, env)).toEqual(['$HOME']);
|
|
});
|
|
|
|
it('does expand variables in double quotes', async () => {
|
|
const w = wordFrom('echo "home: $HOME"');
|
|
const env = makeEnv({ vars: { HOME: '/h' } });
|
|
expect(await expandWord(w, env)).toEqual(['home: /h']);
|
|
});
|
|
|
|
it('splits unquoted variable values on whitespace', async () => {
|
|
const w = wordFrom('echo $LIST');
|
|
const env = makeEnv({ vars: { LIST: 'a b c' } });
|
|
expect(await expandWord(w, env)).toEqual(['a', 'b', 'c']);
|
|
});
|
|
|
|
it('does NOT split quoted variable values', async () => {
|
|
const w = wordFrom('echo "$LIST"');
|
|
const env = makeEnv({ vars: { LIST: 'a b c' } });
|
|
expect(await expandWord(w, env)).toEqual(['a b c']);
|
|
});
|
|
|
|
it('expands tilde to HOME', async () => {
|
|
const w = wordFrom('echo ~/foo');
|
|
expect(await expandWord(w, makeEnv())).toEqual(['/home/user/foo']);
|
|
});
|
|
|
|
it('does not expand tilde in quotes', async () => {
|
|
const w = wordFrom('echo "~/foo"');
|
|
expect(await expandWord(w, makeEnv())).toEqual(['~/foo']);
|
|
});
|
|
|
|
it('expands command substitution', async () => {
|
|
const w = wordFrom('echo $(pwd)');
|
|
const env = makeEnv({ runSubshell: async () => '/tmp\n' });
|
|
expect(await expandWord(w, env)).toEqual(['/tmp']);
|
|
});
|
|
|
|
it('expands backticks like $(...)', async () => {
|
|
const w = wordFrom('echo `pwd`');
|
|
const env = makeEnv({ runSubshell: async () => '/tmp\n' });
|
|
expect(await expandWord(w, env)).toEqual(['/tmp']);
|
|
});
|
|
|
|
it('expands $? via getVar', async () => {
|
|
const w = wordFrom('echo $?');
|
|
const env = makeEnv({ vars: { '?': '42' } });
|
|
expect(await expandWord(w, env)).toEqual(['42']);
|
|
});
|
|
|
|
it('expands arithmetic', async () => {
|
|
const w = wordFrom('echo $((1 + 2 * 3))');
|
|
expect(await expandWord(w, makeEnv())).toEqual(['7']);
|
|
});
|
|
|
|
it('expands arithmetic with variables', async () => {
|
|
const w = wordFrom('echo $((N + 1))');
|
|
const env = makeEnv({ vars: { N: '10' } });
|
|
expect(await expandWord(w, env)).toEqual(['11']);
|
|
});
|
|
|
|
it('expands brace list', async () => {
|
|
const w = wordFrom('echo {a,b,c}.txt');
|
|
expect(await expandWord(w, makeEnv())).toEqual(['a.txt', 'b.txt', 'c.txt']);
|
|
});
|
|
|
|
it('expands numeric brace range', async () => {
|
|
const w = wordFrom('echo {1..3}');
|
|
expect(await expandWord(w, makeEnv())).toEqual(['1', '2', '3']);
|
|
});
|
|
|
|
it('expands globs against the virtual filesystem', async () => {
|
|
const w = wordFrom('echo *.tsx');
|
|
const env = makeEnv({ files: { '/work': ['a.tsx', 'b.tsx', 'readme.md'] } });
|
|
const result = await expandWord(w, env);
|
|
expect(result.sort()).toEqual(['a.tsx', 'b.tsx']);
|
|
});
|
|
|
|
it('leaves glob pattern literal when nothing matches', async () => {
|
|
const w = wordFrom('echo *.xyz');
|
|
const env = makeEnv({ files: { '/work': ['a.tsx'] } });
|
|
expect(await expandWord(w, env)).toEqual(['*.xyz']);
|
|
});
|
|
|
|
it('does not glob quoted patterns', async () => {
|
|
const w = wordFrom('echo "*.tsx"');
|
|
const env = makeEnv({ files: { '/work': ['a.tsx', 'b.tsx'] } });
|
|
expect(await expandWord(w, env)).toEqual(['*.tsx']);
|
|
});
|
|
|
|
it('expands empty unquoted variable to zero args', async () => {
|
|
const w = wordFrom('echo $MISSING');
|
|
expect(await expandWord(w, makeEnv())).toEqual([]);
|
|
});
|
|
|
|
it('preserves empty double-quoted string as an empty arg', async () => {
|
|
const w = wordFrom('echo ""');
|
|
expect(await expandWord(w, makeEnv())).toEqual(['']);
|
|
});
|
|
|
|
it('concatenates quoted+unquoted parts', async () => {
|
|
const w = wordFrom('echo foo"bar"baz');
|
|
expect(await expandWord(w, makeEnv())).toEqual(['foobarbaz']);
|
|
});
|
|
|
|
it('handles "it\'s" style mixed quoting', async () => {
|
|
const w = wordFrom(`echo "it's" fine`);
|
|
expect(await expandWord(w, makeEnv())).toEqual(["it's"]);
|
|
});
|
|
});
|
|
|
|
describe('expandWordToString', () => {
|
|
it('joins all pieces without splitting', async () => {
|
|
const w = wordFrom('echo $LIST');
|
|
const env = makeEnv({ vars: { LIST: 'a b c' } });
|
|
expect(await expandWordToString(w, env)).toBe('a b c');
|
|
});
|
|
});
|