Files
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

117 lines
3.8 KiB
Python

"""Tests for interactive-shell command parsing.
Alpha mode removed the shell-command safety policy (allowlist / restricted /
mutating classification and the deny floor). Parsing now only decides *how* a
command runs — via ``argv`` or through a shell — and never blocks a command for
safety. The only non-execution outcome is empty input.
"""
from __future__ import annotations
from tools.interactive_shell.shell.parsing import (
argv_for_repl_builtin_detection,
parse_shell_command,
)
def test_parse_shell_command_detects_passthrough_prefix() -> None:
parsed = parse_shell_command("!echo hello", is_windows=False)
assert parsed.passthrough is True
assert parsed.use_shell is True
assert parsed.command == "echo hello"
assert parsed.argv is None
assert parsed.parse_error is None
def test_plain_command_uses_argv_without_shell() -> None:
parsed = parse_shell_command("rm -rf /tmp/x", is_windows=False)
assert parsed.passthrough is False
assert parsed.use_shell is False
assert parsed.argv == ["rm", "-rf", "/tmp/x"]
assert parsed.parse_error is None
def test_restricted_command_is_no_longer_blocked() -> None:
"""``sudo`` and friends used to be a hard deny; alpha mode just runs them."""
parsed = parse_shell_command("sudo systemctl restart nginx", is_windows=False)
assert parsed.use_shell is False
assert parsed.argv == ["sudo", "systemctl", "restart", "nginx"]
assert parsed.parse_error is None
def test_operators_run_through_shell_without_block() -> None:
parsed = parse_shell_command("ls | wc -l", is_windows=False)
assert parsed.use_shell is True
assert parsed.passthrough is False
assert parsed.argv is None
assert parsed.parse_error is None
def test_command_substitution_runs_through_shell() -> None:
parsed = parse_shell_command("echo $(date)", is_windows=False)
assert parsed.use_shell is True
assert parsed.parse_error is None
def test_quoted_heredoc_runs_through_shell() -> None:
command = """python3 - <<'PY'
print("hello-heredoc")
PY"""
parsed = parse_shell_command(command, is_windows=False)
assert parsed.use_shell is True
assert parsed.argv is None
assert parsed.command == command.strip()
assert parsed.parse_error is None
def test_unquoted_heredoc_runs_through_shell() -> None:
command = """cat <<EOF
line one
EOF"""
parsed = parse_shell_command(command, is_windows=False)
assert parsed.use_shell is True
assert parsed.argv is None
def test_unbalanced_quotes_fall_back_to_shell() -> None:
parsed = parse_shell_command('echo "unterminated', is_windows=False)
assert parsed.use_shell is True
assert parsed.parse_error is None
def test_empty_passthrough_is_parse_error() -> None:
parsed = parse_shell_command("!", is_windows=False)
assert parsed.parse_error is not None
assert parsed.passthrough is True
def test_empty_command_is_parse_error() -> None:
parsed = parse_shell_command(" ", is_windows=False)
assert parsed.parse_error == "empty command."
def test_argv_for_repl_builtin_detection_splits_passthrough_for_cd() -> None:
parsed = parse_shell_command("!cd /tmp", is_windows=False)
assert argv_for_repl_builtin_detection(parsed=parsed, is_windows=False) == ["cd", "/tmp"]
def test_argv_for_repl_builtin_detection_returns_plain_argv() -> None:
parsed = parse_shell_command("pwd", is_windows=False)
assert argv_for_repl_builtin_detection(parsed=parsed, is_windows=False) == ["pwd"]
def test_argv_for_repl_builtin_detection_skips_operator_command() -> None:
"""A leading ``cd`` in an operator command must not be hijacked as a builtin."""
parsed = parse_shell_command("cd /tmp && ls", is_windows=False)
assert argv_for_repl_builtin_detection(parsed=parsed, is_windows=False) is None