import textwrap from scripts.source_lookup import find_block def test_finds_block_in_single_file(tmp_path): main_tf = tmp_path / "main.tf" main_tf.write_text(textwrap.dedent("""\ resource "aws_s3_bucket" "logs" { bucket = "logs" } resource "aws_iam_role" "svc" { name = "svc" } """)) loc = find_block(tmp_path, "aws_iam_role.svc") assert loc is not None assert loc.file.name == "main.tf" assert loc.start_line == 5 assert loc.end_line == 7 assert 'resource "aws_iam_role" "svc"' in loc.text assert loc.text.rstrip().endswith("}") def test_searches_multiple_tf_files(tmp_path): (tmp_path / "a.tf").write_text( 'resource "aws_s3_bucket" "a" { bucket = "a" }\n' ) (tmp_path / "b.tf").write_text( 'resource "aws_s3_bucket" "b" { bucket = "b" }\n' ) loc = find_block(tmp_path, "aws_s3_bucket.b") assert loc is not None assert loc.file.name == "b.tf" assert 'bucket = "b"' in loc.text def test_returns_none_when_missing(tmp_path): (tmp_path / "main.tf").write_text( 'resource "aws_s3_bucket" "logs" { bucket = "x" }\n' ) assert find_block(tmp_path, "aws_iam_role.svc") is None def test_ignores_data_blocks(tmp_path): (tmp_path / "main.tf").write_text(textwrap.dedent("""\ data "aws_caller_identity" "current" {} resource "aws_iam_role" "real" { name = "x" } """)) loc = find_block(tmp_path, "aws_caller_identity.current") assert loc is None loc2 = find_block(tmp_path, "aws_iam_role.real") assert loc2 is not None