from pathlib import Path from scripts.reference_set import compute_consistency_norms, compute_reference_sets from scripts.manifest import ModuleGraphEntry def _touch(p: Path) -> None: p.parent.mkdir(parents=True, exist_ok=True) p.write_text("") def test_module_change_uses_sibling_modules(tmp_path): _touch(tmp_path / "modules/app-role/main.tf") _touch(tmp_path / "modules/iam-policy-doc/main.tf") _touch(tmp_path / "modules/eks-cluster/main.tf") graph = { "modules/app-role": ModuleGraphEntry( callsites=["live/prod/app"], sibling_modules_at_callsites=["modules/eks-cluster", "modules/iam-policy-doc"], ) } refs = compute_reference_sets( tmp_path, changed_dirs={"modules/app-role"}, module_graph=graph, ) assert refs["modules/app-role"] == [ "modules/eks-cluster", "modules/iam-policy-doc", ] def test_terragrunt_component_uses_region_and_cross_env(tmp_path): _touch(tmp_path / "live/prod/us-east-1/eks/terragrunt.hcl") _touch(tmp_path / "live/prod/us-east-1/audit/terragrunt.hcl") _touch(tmp_path / "live/prod/us-east-1/app/terragrunt.hcl") _touch(tmp_path / "live/staging/us-east-1/eks/terragrunt.hcl") _touch(tmp_path / "live/prod/us-west-2/eks/terragrunt.hcl") refs = compute_reference_sets( tmp_path, changed_dirs={"live/prod/us-east-1/eks"}, module_graph={}, ) ref = set(refs["live/prod/us-east-1/eks"]) assert "live/prod/us-east-1/audit" in ref assert "live/prod/us-east-1/app" in ref assert "live/staging/us-east-1/eks" in ref assert "live/prod/us-west-2/eks" in ref assert "live/prod/us-east-1/eks" not in ref def test_plain_terraform_uses_sibling_dirs(tmp_path): _touch(tmp_path / "envs/prod/main.tf") _touch(tmp_path / "envs/staging/main.tf") _touch(tmp_path / "envs/dev/main.tf") refs = compute_reference_sets( tmp_path, changed_dirs={"envs/prod"}, module_graph={}, ) assert set(refs["envs/prod"]) == {"envs/staging", "envs/dev"} def test_compute_consistency_norms_ignores_single_peer_drift(tmp_path): (tmp_path / "envs/prod").mkdir(parents=True, exist_ok=True) (tmp_path / "envs/staging").mkdir(parents=True, exist_ok=True) (tmp_path / "envs/dev").mkdir(parents=True, exist_ok=True) (tmp_path / "envs/qa").mkdir(parents=True, exist_ok=True) (tmp_path / "envs/staging/main.tf").write_text('kms_key_arn = "a"\n') (tmp_path / "envs/dev/main.tf").write_text('kms_key_arn = "b"\n') (tmp_path / "envs/qa/main.tf").write_text('bucket_key_enabled = true\n') refs = {"envs/prod": ["envs/staging", "envs/dev", "envs/qa"]} norms = compute_consistency_norms(tmp_path, refs) attr_norms = norms["envs/prod"]["attribute_norms"] assert {"attribute": "kms_key_arn", "peer_dirs": ["envs/dev", "envs/staging"]} in attr_norms assert not any(norm["attribute"] == "bucket_key_enabled" for norm in attr_norms)