Files
claude-plugin/plugins/reviews/skills/audit-terraform/tests/test_plan_runner.py
T
mroberts f5934181ec Move the code and terraform audits into the reviews plugin
Copy the standalone code-review and terraform-review skills into
plugins/reviews as audit-code and audit-terraform. The rename separates the
automated, linter-driven audits from the guided review-pr walkthrough that
already lived here.

Resolve bundled script paths through ${SKILL_DIR}, exported in a new step 0.
CLAUDE_PLUGIN_ROOT is not set in the Bash tool environment, so the obvious
substitution would have expanded to nothing and broken every collection
script invocation.

Replace the PLAN and DESIGN docs with READMEs written from the current
SKILL.md and scripts. The old docs had drifted badly: they named semgrep
where the code calls opengrep, scoped five review agents where there are
now eight, and predated Lua, PowerShell, and GitHub Actions support.

Add CONSISTENCY_NORMS to the audit-terraform agent inputs. The collection
script writes consistency_norms.json and the agent prompt declares it, but
SKILL.md never listed it, leaving the variable unsubstituted.

Drop the --ingest-verdicts instruction from both skills. review_stats.py
parses no arguments, so the ref-mode verdict template it told users to feed
back could never be read.

Point audit-terraform's smoke test at README.md and resolve its fixture
paths relative to the test file rather than an absolute home directory.

Tests: 197 passing (audit-code), 106 passing (audit-terraform).
2026-07-21 11:11:05 -05:00

68 lines
2.2 KiB
Python

from unittest.mock import patch, MagicMock
from scripts.plan_runner import detect_tool, run_init, run_plan, extract_summary
def test_detect_tool_terragrunt(tmp_path):
(tmp_path / "terragrunt.hcl").write_text("# tg\n")
assert detect_tool(tmp_path) == "terragrunt"
def test_detect_tool_tofu(tmp_path):
(tmp_path / "main.tf").write_text("# tf\n")
assert detect_tool(tmp_path) == "tofu"
def test_extract_summary_finds_plan_line():
output = """\
Terraform will perform the following actions:
# aws_s3_bucket.x will be created
+ resource "aws_s3_bucket" "x" {
bucket = "y"
}
Plan: 1 to add, 0 to change, 0 to destroy.
"""
assert extract_summary(output) == "1 to add, 0 to change, 0 to destroy"
def test_extract_summary_no_changes():
output = "No changes. Your infrastructure matches the configuration.\n"
assert extract_summary(output) == "no changes"
def test_run_init_ok(tmp_path):
(tmp_path / "main.tf").write_text("# tf\n")
fake = MagicMock(returncode=0, stdout="Initializing...\nok\n", stderr="")
with patch("subprocess.run", return_value=fake) as p:
result = run_init(tmp_path, "tofu")
assert result.ok is True
assert "ok" in result.stdout_tail
p.assert_called_once()
args = p.call_args[0][0]
assert args[0] == "tofu"
assert "init" in args
def test_run_init_failure_records_stderr(tmp_path):
(tmp_path / "main.tf").write_text("# tf\n")
fake = MagicMock(returncode=1, stdout="", stderr="provider not found\n")
with patch("subprocess.run", return_value=fake):
result = run_init(tmp_path, "tofu")
assert result.ok is False
assert "provider not found" in result.stderr_tail
def test_run_plan_streams_to_file(tmp_path):
out_path = tmp_path / "plan.txt"
fake = MagicMock(returncode=0,
stdout="Plan: 2 to add, 0 to change, 0 to destroy.\n",
stderr="")
with patch("subprocess.run", return_value=fake):
result = run_plan(tmp_path, "tofu", out_path)
assert result.ok is True
assert result.exit_code == 0
assert result.summary == "2 to add, 0 to change, 0 to destroy"
assert out_path.read_text().endswith("0 to destroy.\n")