import textwrap from scripts.hcl_diff import find_resource_blocks, touched_resources def test_finds_resource_block_spans(tmp_path): body = textwrap.dedent("""\ variable "x" { default = 1 } resource "aws_s3_bucket" "a" { bucket = "x" } resource "aws_iam_role" "b" { name = "y" assume_role_policy = "..." } """) f = tmp_path / "main.tf" f.write_text(body) blocks = find_resource_blocks(f) by_name = {b.name: b for b in blocks} assert by_name["a"].type == "aws_s3_bucket" assert by_name["a"].start <= 3 <= by_name["a"].end assert by_name["b"].type == "aws_iam_role" assert by_name["b"].start <= 7 <= by_name["b"].end def test_touched_resources_intersects(tmp_path): body = textwrap.dedent("""\ resource "aws_s3_bucket" "a" { bucket = "x" } resource "aws_iam_role" "b" { name = "y" } """) f = tmp_path / "main.tf" f.write_text(body) hits = touched_resources(f, added_ranges=[(6, 6)]) assert len(hits) == 1 assert hits[0].type == "aws_iam_role" assert hits[0].name == "b" def test_touched_resources_ignores_data_blocks(tmp_path): body = textwrap.dedent("""\ data "aws_caller_identity" "current" {} """) f = tmp_path / "main.tf" f.write_text(body) hits = touched_resources(f, added_ranges=[(1, 1)]) assert hits == [] def test_touched_resources_empty_when_no_overlap(tmp_path): body = textwrap.dedent("""\ resource "aws_s3_bucket" "a" { bucket = "x" } """) f = tmp_path / "main.tf" f.write_text(body) assert touched_resources(f, added_ranges=[(10, 12)]) == []