|
1 | 1 | import json |
2 | 2 | from pathlib import Path |
| 3 | +from uuid import uuid4 |
3 | 4 |
|
| 5 | +from codemodder.semgrep import SemgrepResultSet |
4 | 6 | from core_codemods.sonar.results import SonarResultSet |
5 | 7 |
|
6 | 8 |
|
@@ -298,3 +300,72 @@ def test_sonar_robustness(self, tmpdir): |
298 | 300 | result = SonarResultSet.from_json(sonar_json) |
299 | 301 | # did not crash and returned an empty ResultSet |
300 | 302 | assert not result |
| 303 | + |
| 304 | + def test_sonar_result_by_finding_id(self, tmpdir): |
| 305 | + issues = { |
| 306 | + "issues": [ |
| 307 | + { |
| 308 | + "rule": "python:S5659", |
| 309 | + "status": "OPEN", |
| 310 | + "component": "code.py", |
| 311 | + "textRange": { |
| 312 | + "startLine": 2, |
| 313 | + "endLine": 2, |
| 314 | + "startOffset": 2, |
| 315 | + "endOffset": 2, |
| 316 | + }, |
| 317 | + "key": "1234", |
| 318 | + } |
| 319 | + ] |
| 320 | + } |
| 321 | + sonar_json = Path(tmpdir) / "sonar1.json" |
| 322 | + sonar_json.write_text(json.dumps(issues)) |
| 323 | + |
| 324 | + result_set = SonarResultSet.from_json(sonar_json) |
| 325 | + result = result_set.result_by_finding_id("1234") |
| 326 | + assert result is not None |
| 327 | + assert result.finding.rule.id == "python:S5659" |
| 328 | + |
| 329 | + def test_semgrep_sarif_result_by_finding_id(self, tmpdir): |
| 330 | + uuid = str(uuid4()) |
| 331 | + issues = { |
| 332 | + "runs": [ |
| 333 | + { |
| 334 | + "tool": { |
| 335 | + "driver": { |
| 336 | + "name": "Semgrep", |
| 337 | + "version": "0.100.0", |
| 338 | + } |
| 339 | + }, |
| 340 | + "results": [ |
| 341 | + { |
| 342 | + "message": { |
| 343 | + "text": "Found a potential issue", |
| 344 | + }, |
| 345 | + "guid": uuid, |
| 346 | + "ruleId": "python:fake.rule.name", |
| 347 | + "locations": [ |
| 348 | + { |
| 349 | + "physicalLocation": { |
| 350 | + "artifactLocation": { |
| 351 | + "uri": str(Path(tmpdir) / "code.py"), |
| 352 | + }, |
| 353 | + "region": { |
| 354 | + "startLine": 2, |
| 355 | + "startColumn": 2, |
| 356 | + }, |
| 357 | + } |
| 358 | + } |
| 359 | + ], |
| 360 | + } |
| 361 | + ], |
| 362 | + } |
| 363 | + ] |
| 364 | + } |
| 365 | + sarif_json = Path(tmpdir) / "semgrep.sarif" |
| 366 | + sarif_json.write_text(json.dumps(issues)) |
| 367 | + |
| 368 | + result_set = SemgrepResultSet.from_sarif(sarif_json) |
| 369 | + result = result_set.result_by_finding_id(uuid) |
| 370 | + assert result is not None |
| 371 | + assert result.finding.rule.id == "python:fake.rule.name" |
0 commit comments