| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import { describe, expect, test } from "bun:test"
- import { Effect } from "effect"
- import os from "os"
- import path from "path"
- import { ShellParse } from "@opencode-ai/core/shell/parse"
- describe("ShellParse", () => {
- test("splits bash commands and derives reusable prefixes", async () => {
- const result = await Effect.runPromise(
- ShellParse.scan("git status && npm run test -- --watch", "/bin/bash", "/workspace"),
- )
- expect(result).toEqual({
- commands: [
- { resource: "git status", save: "git status *" },
- { resource: "npm run test -- --watch", save: "npm run test *" },
- ],
- directories: [],
- })
- })
- test("portable scanning never adds permission resources", async () => {
- const commands = [
- "git status && npm run test -- --watch",
- "echo $(curl evil | sed s/x/y/)",
- "cat <<'EOF'\nstatic body\nEOF",
- "cat <<EOF\n$(printf dynamic)\nEOF",
- "cd /tmp/$USER && git status",
- "$COMMAND status",
- "if true; then printf yes; else printf no; fi",
- ]
- for (const command of commands) {
- const legacy = await Effect.runPromise(ShellParse.scan(command, "/bin/bash", "/workspace"))
- const portable = await Effect.runPromise(ShellParse.scan(command, "/bin/bash", "/workspace", { portable: true }))
- expect(
- portable.commands.every((item) => legacy.commands.some((candidate) => candidate.resource === item.resource)),
- ).toBe(true)
- expect(portable.directories.every((item) => legacy.directories.includes(item))).toBe(true)
- }
- })
- test("portable scanning authorizes opaque heredocs without inferring directories", async () => {
- const command = "cat <<'EOF'\nstatic body\nEOF"
- const portable = await Effect.runPromise(ShellParse.scan(command, "/bin/bash", "/workspace", { portable: true }))
- expect(portable).toEqual({ commands: [{ resource: command, save: command }], directories: [] })
- })
- test.each(['c"\\d" relative', "'cd' /tmp", "c''d /tmp", "c\\\nd /tmp"])(
- "portable scanning keeps source-shaped command heads under shell authorization: %s",
- async (command) => {
- const portable = await Effect.runPromise(ShellParse.scan(command, "/bin/bash", "/workspace", { portable: true }))
- expect(portable.commands.map((item) => item.resource)).toEqual([command])
- expect(portable.directories).toEqual([])
- },
- )
- test("splits PowerShell commands case-insensitively", async () => {
- const result = await Effect.runPromise(
- ShellParse.scan(
- "Get-ChildItem; Write-Output 'done'",
- "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
- "C:\\workspace",
- ),
- )
- expect(result.commands).toEqual([
- { resource: "Get-ChildItem", save: "Get-ChildItem *" },
- { resource: "Write-Output 'done'", save: "Write-Output *" },
- ])
- })
- test("does not permission directory changes separately", async () => {
- const result = await Effect.runPromise(ShellParse.scan("cd 'src dir' && git status", "/bin/bash", "/workspace"))
- expect(result).toEqual({
- commands: [{ resource: "git status", save: "git status *" }],
- directories: ["src dir"],
- })
- })
- test("extracts PowerShell directory parameters", async () => {
- const result = await Effect.runPromise(
- ShellParse.scan("Set-Location -LiteralPath '..\\outside'; Get-ChildItem", "pwsh", "C:\\workspace"),
- )
- expect(result.directories).toEqual(["..\\outside"])
- })
- test("expands deterministic directory variables", async () => {
- const bash = await Effect.runPromise(ShellParse.scan("cd ~/src", "/bin/bash", "/workspace"))
- expect(bash.directories).toEqual([path.join(os.homedir(), "src")])
- const powershell = await Effect.runPromise(
- ShellParse.scan('Set-Location "$PWD/src"; Set-Location $PSHOME', "/usr/local/bin/pwsh", "/workspace"),
- )
- expect(powershell.directories).toEqual(["/workspace/src", "/usr/local/bin"])
- })
- })
|