from pathlib import Path import pytest from scripts.plan_unit import classify_dir, DirKind def _write(p: Path, body: str) -> None: p.parent.mkdir(parents=True, exist_ok=True) p.write_text(body) def test_terragrunt_hcl_marks_plan_unit(tmp_path): _write(tmp_path / "live/prod/app/terragrunt.hcl", 'include "root" { path = "x" }\n') assert classify_dir(tmp_path, "live/prod/app") == DirKind.PLAN_UNIT def test_tf_with_backend_block_marks_plan_unit(tmp_path): body = ''' terraform { required_version = ">= 1.5" backend "s3" { bucket = "x" key = "y" region = "us-east-1" } } ''' _write(tmp_path / "live/prod/app/main.tf", body) assert classify_dir(tmp_path, "live/prod/app") == DirKind.PLAN_UNIT def test_tf_without_backend_marks_module(tmp_path): body = ''' resource "aws_iam_role" "this" { name = var.name } ''' _write(tmp_path / "modules/app-role/main.tf", body) assert classify_dir(tmp_path, "modules/app-role") == DirKind.MODULE def test_no_hcl_files_marks_unknown(tmp_path): _write(tmp_path / "docs/readme.md", "hi") assert classify_dir(tmp_path, "docs") == DirKind.UNKNOWN def test_missing_dir_raises(tmp_path): with pytest.raises(FileNotFoundError): classify_dir(tmp_path, "nope")