AI coding agents can edit files, run shell commands, and touch infrastructure. One bad rm -rf, force-push, or docker compose down -v can wipe hours of work or production data.
| 1 | # SPDX-License-Identifier: MIT |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | import json |
| 5 | from pathlib import Path |
| 6 | from unittest.mock import MagicMock, patch |
| 7 | |
| 8 | from typer.testing import CliRunner |
| 9 | |
| 10 | from agent_guard import __version__ |
| 11 | from agent_guard.cli import app |
| 12 | |
| 13 | runner = CliRunner() |
| 14 | |
| 15 | |
| 16 | def test_help() -> None: |
| 17 | result = runner.invoke(app, ["--help"]) |
| 18 | assert result.exit_code == 0 |
| 19 | assert "check" in result.output |
| 20 | |
| 21 | |
| 22 | def test_version() -> None: |
| 23 | result = runner.invoke(app, ["version"]) |
| 24 | assert result.exit_code == 0 |
| 25 | assert __version__ in result.output |
| 26 | |
| 27 | |
| 28 | def test_check_allow() -> None: |
| 29 | result = runner.invoke(app, ["check", "git status"]) |
| 30 | assert result.exit_code == 0 |
| 31 | assert "ALLOW" in result.output |
| 32 | |
| 33 | |
| 34 | def test_check_warn() -> None: |
| 35 | result = runner.invoke(app, ["check", "git reset --hard HEAD"]) |
| 36 | assert result.exit_code == 10 |
| 37 | assert "WARN" in result.output |
| 38 | |
| 39 | |
| 40 | def test_check_block() -> None: |
| 41 | result = runner.invoke(app, ["check", "rm -rf /"]) |
| 42 | assert result.exit_code == 20 |
| 43 | assert "BLOCK" in result.output |
| 44 | |
| 45 | |
| 46 | def test_check_json() -> None: |
| 47 | result = runner.invoke(app, ["check", "--json", "git status"]) |
| 48 | assert result.exit_code == 0 |
| 49 | data = json.loads(result.output) |
| 50 | assert data["decision"] == "ALLOW" |
| 51 | |
| 52 | |
| 53 | def test_check_never_executes() -> None: |
| 54 | with patch("subprocess.run") as mock_run: |
| 55 | runner.invoke(app, ["check", "rm -rf /"]) |
| 56 | mock_run.assert_not_called() |
| 57 | |
| 58 | |
| 59 | def test_rules() -> None: |
| 60 | result = runner.invoke(app, ["rules"]) |
| 61 | assert result.exit_code == 0 |
| 62 | assert "filesystem" in result.output or "git" in result.output |
| 63 | |
| 64 | |
| 65 | def test_rules_json() -> None: |
| 66 | result = runner.invoke(app, ["rules", "--json"]) |
| 67 | assert result.exit_code == 0 |
| 68 | data = json.loads(result.output) |
| 69 | assert isinstance(data, list) |
| 70 | |
| 71 | |
| 72 | def test_init(tmp_path: Path, monkeypatch: object) -> None: |
| 73 | monkeypatch.chdir(tmp_path) # type: ignore[attr-defined] |
| 74 | (tmp_path / ".git").mkdir() |
| 75 | result = runner.invoke(app, ["init"]) |
| 76 | assert result.exit_code == 0 |
| 77 | assert (tmp_path / ".agent-guard.yml").is_file() |
| 78 | |
| 79 | |
| 80 | def test_scan(tmp_path: Path, monkeypatch: object) -> None: |
| 81 | monkeypatch.chdir(tmp_path) # type: ignore[attr-defined] |
| 82 | (tmp_path / "package.json").write_text("{}", encoding="utf-8") |
| 83 | result = runner.invoke(app, ["scan"]) |
| 84 | assert result.exit_code == 0 |
| 85 | assert "Agent Guard Scan" in result.output |
| 86 | |
| 87 | |
| 88 | def test_run_block_no_subprocess() -> None: |
| 89 | with patch("agent_guard.commands.subprocess.run") as mock_run: |
| 90 | result = runner.invoke(app, ["run", "--", "rm", "-rf", "/"]) |
| 91 | assert result.exit_code == 20 |
| 92 | mock_run.assert_not_called() |
| 93 | |
| 94 | |
| 95 | def test_run_allow() -> None: |
| 96 | with patch("agent_guard.commands.subprocess.run") as mock_run: |
| 97 | mock_run.return_value = MagicMock(returncode=0) |
| 98 | result = runner.invoke(app, ["run", "--", "python", "-c", "print(1)"]) |
| 99 | assert result.exit_code == 0 |
| 100 | mock_run.assert_called_once() |
| 101 | |
| 102 | |
| 103 | def test_config_show(tmp_path: Path, monkeypatch: object) -> None: |
| 104 | monkeypatch.chdir(tmp_path) # type: ignore[attr-defined] |
| 105 | result = runner.invoke(app, ["config", "--json"]) |
| 106 | assert result.exit_code == 0 |
| 107 | data = json.loads(result.output) |
| 108 | assert "rules" in data |
| 109 | |
| 110 | |
| 111 | def test_file_operation() -> None: |
| 112 | result = runner.invoke(app, ["check", "--file-operation", "delete", "./src"]) |
| 113 | assert result.exit_code == 20 |