|
3 | 3 | Tests for FilePatternCondition, MaxFileSizeCondition, and MaxPrLocCondition classes. |
4 | 4 | """ |
5 | 5 |
|
| 6 | +from typing import Any |
6 | 7 | from unittest.mock import patch |
7 | 8 |
|
8 | 9 | import pytest |
@@ -96,6 +97,104 @@ def test_glob_to_regex_conversion(self) -> None: |
96 | 97 | assert FilePatternCondition._glob_to_regex("src/*.js") == "^src/.*\\.js$" |
97 | 98 | assert FilePatternCondition._glob_to_regex("file?.txt") == "^file.\\.txt$" |
98 | 99 |
|
| 100 | + def test_get_changed_files_from_pr_enriched_data(self) -> None: |
| 101 | + """Test extracting files from enriched PR changed_files (list of dicts).""" |
| 102 | + condition = FilePatternCondition() |
| 103 | + event = { |
| 104 | + "changed_files": [ |
| 105 | + {"filename": "src/main.py", "status": "modified", "additions": 10, "deletions": 2}, |
| 106 | + {"filename": "tests/test_main.py", "status": "added", "additions": 30, "deletions": 0}, |
| 107 | + ] |
| 108 | + } |
| 109 | + result = condition._get_changed_files(event) |
| 110 | + assert result == ["src/main.py", "tests/test_main.py"] |
| 111 | + |
| 112 | + def test_get_changed_files_from_pr_plain_strings(self) -> None: |
| 113 | + """Test extracting files when changed_files contains plain strings.""" |
| 114 | + condition = FilePatternCondition() |
| 115 | + event = {"changed_files": ["src/main.py", "README.md"]} |
| 116 | + result = condition._get_changed_files(event) |
| 117 | + assert result == ["src/main.py", "README.md"] |
| 118 | + |
| 119 | + def test_get_changed_files_from_push_commits(self) -> None: |
| 120 | + """Test extracting files from push event commit arrays.""" |
| 121 | + condition = FilePatternCondition() |
| 122 | + event = { |
| 123 | + "commits": [ |
| 124 | + {"added": ["new_file.py"], "modified": ["src/main.py"], "removed": []}, |
| 125 | + {"added": [], "modified": ["src/main.py"], "removed": ["old.py"]}, |
| 126 | + ] |
| 127 | + } |
| 128 | + result = condition._get_changed_files(event) |
| 129 | + assert result == ["new_file.py", "old.py", "src/main.py"] |
| 130 | + |
| 131 | + def test_get_changed_files_empty_event(self) -> None: |
| 132 | + """Test that an empty event returns no files.""" |
| 133 | + condition = FilePatternCondition() |
| 134 | + assert condition._get_changed_files({}) == [] |
| 135 | + |
| 136 | + def test_get_changed_files_with_malformed_payload(self) -> None: |
| 137 | + """Test that malformed payload entries are filtered out without raising.""" |
| 138 | + condition = FilePatternCondition() |
| 139 | + |
| 140 | + # changed_files with mixed valid/invalid entries |
| 141 | + event_cf: dict[str, Any] = { |
| 142 | + "changed_files": [ |
| 143 | + {"filename": "valid.py", "status": "modified"}, |
| 144 | + {"status": "added"}, # missing "filename" |
| 145 | + None, # type: ignore[list-item] |
| 146 | + 42, # type: ignore[list-item] |
| 147 | + "", # empty string |
| 148 | + {"filename": ""}, # empty filename |
| 149 | + "also_valid.txt", |
| 150 | + ] |
| 151 | + } |
| 152 | + result = condition._get_changed_files(event_cf) |
| 153 | + assert result == ["valid.py", "also_valid.txt"] |
| 154 | + |
| 155 | + # commits with non-dict entries and non-list/non-string values |
| 156 | + event_commits: dict[str, Any] = { |
| 157 | + "commits": [ |
| 158 | + {"added": ["good.py"], "modified": "not_a_list", "removed": [42, None, "removed.py"]}, |
| 159 | + "not_a_dict", # type: ignore[list-item] |
| 160 | + {"added": [None, "", "another.py"], "modified": [], "removed": []}, |
| 161 | + ] |
| 162 | + } |
| 163 | + result = condition._get_changed_files(event_commits) |
| 164 | + assert result == ["another.py", "good.py", "removed.py"] |
| 165 | + |
| 166 | + @pytest.mark.asyncio |
| 167 | + async def test_evaluate_with_real_pr_event(self) -> None: |
| 168 | + """Test full evaluate flow with enriched PR data (no mocking).""" |
| 169 | + condition = FilePatternCondition() |
| 170 | + context = { |
| 171 | + "parameters": {"pattern": "*.py", "condition_type": "files_match_pattern"}, |
| 172 | + "event": { |
| 173 | + "changed_files": [ |
| 174 | + {"filename": "src/app.py", "status": "modified", "additions": 5, "deletions": 1}, |
| 175 | + {"filename": "docs/readme.md", "status": "modified", "additions": 2, "deletions": 0}, |
| 176 | + ] |
| 177 | + }, |
| 178 | + } |
| 179 | + violations = await condition.evaluate(context) |
| 180 | + assert len(violations) == 0 |
| 181 | + |
| 182 | + @pytest.mark.asyncio |
| 183 | + async def test_evaluate_with_real_push_event(self) -> None: |
| 184 | + """Test full evaluate flow with push commit data (no mocking).""" |
| 185 | + condition = FilePatternCondition() |
| 186 | + context = { |
| 187 | + "parameters": {"pattern": "*.yaml", "condition_type": "files_not_match_pattern"}, |
| 188 | + "event": { |
| 189 | + "commits": [ |
| 190 | + {"added": ["config/app.yaml"], "modified": [], "removed": []}, |
| 191 | + ] |
| 192 | + }, |
| 193 | + } |
| 194 | + violations = await condition.evaluate(context) |
| 195 | + assert len(violations) == 1 |
| 196 | + assert "forbidden pattern" in violations[0].message |
| 197 | + |
99 | 198 |
|
100 | 199 | class TestMaxFileSizeCondition: |
101 | 200 | """Tests for MaxFileSizeCondition class.""" |
|
0 commit comments