Skip to content

Commit 8a8c3a3

Browse files
committed
Fix bug when handling empty Sonar issues JSON
1 parent 8d0af71 commit 8a8c3a3

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/core_codemods/sonar/results.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def from_json(cls, json_file: str | Path) -> Self:
9999
data = json.load(file)
100100

101101
result_set = cls()
102-
for result in data.get("issues") or [] + data.get("hotspots") or []:
102+
for result in data.get("issues", []) + data.get("hotspots", []):
103103
if result["status"].lower() in ("open", "to_review"):
104104
result_set.add_result(SonarResult.from_result(result))
105105

tests/test_sonar_results.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from core_codemods.sonar.results import SonarResult
1+
from pathlib import Path
2+
3+
from core_codemods.sonar.results import SonarResult, SonarResultSet
4+
5+
SAMPLE_DIR = Path(__file__).parent / "samples"
26

37

48
def test_result_without_textrange():
@@ -29,3 +33,33 @@ def test_result_without_textrange():
2933
assert sonar_result.finding_id == "AZJnP4pZPJb5bI8DP25Y"
3034
assert sonar_result.locations == ()
3135
assert sonar_result.codeflows == ()
36+
37+
38+
def test_parse_issues_json():
39+
results = SonarResultSet.from_json(SAMPLE_DIR / "sonar_issues.json")
40+
assert len(results) == 34
41+
42+
43+
def test_parse_hotspots_json():
44+
results = SonarResultSet.from_json(SAMPLE_DIR / "sonar_hotspots.json")
45+
assert len(results) == 2
46+
47+
48+
def test_empty_issues(tmpdir, caplog):
49+
empty_json = tmpdir / "empty.json"
50+
empty_json.write_text('{"issues": []}', encoding="utf-8")
51+
52+
results = SonarResultSet.from_json(empty_json)
53+
54+
assert len(results) == 0
55+
assert "Could not parse sonar json" not in caplog.text
56+
57+
58+
def test_empty_hotspots(tmpdir, caplog):
59+
empty_json = tmpdir / "empty.json"
60+
empty_json.write_text('{"hotspots": []}', encoding="utf-8")
61+
62+
results = SonarResultSet.from_json(empty_json)
63+
64+
assert len(results) == 0
65+
assert "Could not parse sonar json" not in caplog.text

0 commit comments

Comments
 (0)