from unittest.mock import patch from scripts.git_diff import changed_files, changed_dirs, changed_line_ranges _DIFF = """\ diff --git a/live/prod/app/main.tf b/live/prod/app/main.tf index abc..def 100644 --- a/live/prod/app/main.tf +++ b/live/prod/app/main.tf @@ -12 +12,2 @@ - acl = "old" + acl = "new" + versioning = true diff --git a/modules/app-role/main.tf b/modules/app-role/main.tf index 111..222 100644 --- a/modules/app-role/main.tf +++ b/modules/app-role/main.tf @@ -5,0 +6,2 @@ + name = "x" + assume_role_policy = "..." diff --git a/README.md b/README.md index 333..444 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -old +new """ def _fake_run(args, **kwargs): class R: returncode = 0 stdout = _DIFF stderr = "" return R() def test_changed_files_filters_tf_hcl(): with patch("subprocess.run", side_effect=_fake_run): files = changed_files("/repo", "main", "HEAD") assert "live/prod/app/main.tf" in files assert "modules/app-role/main.tf" in files assert "README.md" not in files def test_changed_dirs_dedupes(): with patch("subprocess.run", side_effect=_fake_run): dirs = changed_dirs("/repo", "main", "HEAD") assert dirs == {"live/prod/app", "modules/app-role"} def test_changed_line_ranges_parses_hunks(): with patch("subprocess.run", side_effect=_fake_run): ranges = changed_line_ranges("/repo", "main", "HEAD", "live/prod/app/main.tf") assert ranges == [(12, 13)] def test_changed_line_ranges_unknown_file(): with patch("subprocess.run", side_effect=_fake_run): ranges = changed_line_ranges("/repo", "main", "HEAD", "no/such.tf") assert ranges == []