"""Tests for adapters/terraform/adapter.py — REQ-25.""" import sys from pathlib import Path import pytest from adapters.terraform.adapter import adapt, _tf_value def _stack(resources): return { "contract_id": "test", "contract_name": "Test", "environment": "dev", "resources": resources, } class TestAdapt: def test_single_resource(self, repo_root): stack = _stack([ {"module": "s3", "version": "1.0.0", "inputs": {"bucket_name": "test-bucket", "enabled": True}}, ]) hcl = adapt(stack, repo_root) assert 'module "s3" {' in hcl assert 'source = ' in hcl assert 'bucket_name = "test-bucket"' in hcl assert "enabled = true" in hcl def test_multi_resource(self, repo_root): stack = _stack([ {"module": "s3", "version": "1.0.0", "inputs": {"bucket_name": "b1", "enabled": True}}, {"module": "vpc", "version": "1.0.0", "inputs": {"cidr": "10.0.0.0/16"}}, ]) hcl = adapt(stack, repo_root) assert 'module "s3" {' in hcl assert 'module "vpc" {' in hcl def test_input_passthrough_scalar(self, repo_root): stack = _stack([ {"module": "s3", "version": "1.0.0", "inputs": {"bucket_name": "my-bucket", "region": "us-east-1"}}, ]) hcl = adapt(stack, repo_root) assert 'bucket_name = "my-bucket"' in hcl assert "us-east-1" not in hcl.split("inputs")[0] if "inputs" in hcl else True # region is skipped (provider-level) def test_input_passthrough_list(self, repo_root): stack = _stack([ {"module": "vpc", "version": "1.0.0", "inputs": {"azs": ["us-east-1a", "us-east-1b"]}}, ]) hcl = adapt(stack, repo_root) assert 'azs = ["us-east-1a", "us-east-1b"]' in hcl def test_input_passthrough_number(self, repo_root): stack = _stack([ {"module": "vpc", "version": "1.0.0", "inputs": {"desired_count": 3}}, ]) hcl = adapt(stack, repo_root) assert "desired_count = 3" in hcl def test_hcl_validity_balanced_braces(self, repo_root): stack = _stack([ {"module": "s3", "version": "1.0.0", "inputs": {"bucket_name": "b", "enabled": True}}, ]) hcl = adapt(stack, repo_root) assert hcl.count("{") == hcl.count("}") def test_unknown_module_raises(self, repo_root): stack = _stack([ {"module": "nonexistent", "version": "1.0.0", "inputs": {}}, ]) with pytest.raises(ValueError, match="no terraform_dir"): adapt(stack, repo_root) class TestTfValue: def test_bool(self): assert _tf_value(True) == "true" assert _tf_value(False) == "false" def test_int(self): assert _tf_value(42) == "42" def test_string(self): assert _tf_value("hello") == '"hello"' def test_list(self): assert _tf_value(["a", "b"]) == '["a", "b"]'