Skip to content

Commit

Permalink
Normalize Sonar paths to base directory path (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella authored Mar 8, 2024
1 parent 1b43f0b commit c7a729a
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/codemodder/sonar_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from pathlib import Path

import libcst as cst
from typing_extensions import Self
from typing_extensions import Self, override

from codemodder.context import CodemodExecutionContext
from codemodder.logging import logger
from codemodder.result import LineInfo, Location, Result, ResultSet

Expand Down Expand Up @@ -58,3 +59,10 @@ def from_json(cls, json_file: str | Path) -> Self:
except Exception:
logger.debug("Could not parse sonar json %s", json_file)
return cls()

@override
def results_for_rule_and_file(
self, context: CodemodExecutionContext, rule_id: str, file: Path
) -> list[Result]:
paths_for_rule = self.get(rule_id, {})
return paths_for_rule.get(file.relative_to(context.directory), [])
2 changes: 1 addition & 1 deletion tests/codemods/test_sonar_django_json_response_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def foo(request):
{
"rule": "pythonsecurity:S5131",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"component": "code.py",
"textRange": {
"startLine": 7,
"endLine": 7,
Expand Down
2 changes: 1 addition & 1 deletion tests/codemods/test_sonar_django_receiver_on_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def foo():
{
"rule": "python:S6552",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"component": "code.py",
"textRange": {
"startLine": 5,
"endLine": 5,
Expand Down
2 changes: 1 addition & 1 deletion tests/codemods/test_sonar_exception_without_raise.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_simple(self, tmpdir):
{
"rule": "python:S3984",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"component": "code.py",
"textRange": {
"startLine": 2,
"endLine": 2,
Expand Down
2 changes: 1 addition & 1 deletion tests/codemods/test_sonar_fix_assert_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_simple(self, tmpdir):
{
"rule": "python:S5905",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"component": "code.py",
"textRange": {
"startLine": 2,
"endLine": 2,
Expand Down
2 changes: 1 addition & 1 deletion tests/codemods/test_sonar_flask_json_response_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def foo(request):
{
"rule": "pythonsecurity:S5131",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"component": "code.py",
"textRange": {
"startLine": 10,
"endLine": 10,
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_sonar_jwt_decode_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_simple(self, tmpdir):
{
"rule": "python:S5659",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"component": "code.py",
"textRange": {
"startLine": 11,
"endLine": 11,
Expand All @@ -54,7 +54,7 @@ def test_simple(self, tmpdir):
{
"rule": "python:S5659",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"component": "code.py",
"textRange": {
"startLine": 12,
"endLine": 12,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_list(self, tmpdir):
{
"rule": "python:S5796",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"component": "code.py",
"textRange": {
"startLine": 2,
"endLine": 2,
Expand Down
2 changes: 1 addition & 1 deletion tests/codemods/test_sonar_numpy_nan_equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_simple(self, tmpdir):
{
"rule": "python:S6725",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"component": "code.py",
"textRange": {
"startLine": 3,
"endLine": 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def foo():
{
"rule": "python:S5915",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"component": "code.py",
"textRange": {
"startLine": 6,
"endLine": 6,
Expand Down
2 changes: 1 addition & 1 deletion tests/samples/sonar_issues.json

Large diffs are not rendered by default.

0 comments on commit c7a729a

Please sign in to comment.