-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sonar Integration #223
Sonar Integration #223
Changes from all commits
32a70e2
11008f1
5d52426
771f9cb
d642ec8
1d316cb
7e7d411
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from core_codemods.sonar.sonar_numpy_nan_equality import ( | ||
SonarNumpyNanEquality, | ||
SonarNumpyNanEqualityTransformer, | ||
) | ||
from integration_tests.base_test import ( | ||
BaseIntegrationTest, | ||
original_and_expected_from_code_path, | ||
) | ||
|
||
|
||
class TestNumpyNanEquality(BaseIntegrationTest): | ||
codemod = SonarNumpyNanEquality | ||
code_path = "tests/samples/numpy_nan_equality.py" | ||
original_code, expected_new_code = original_and_expected_from_code_path( | ||
code_path, | ||
[ | ||
(3, """if np.isnan(a):\n"""), | ||
], | ||
) | ||
sonar_issues_json = "tests/samples/sonar_issues.json" | ||
|
||
# fmt: off | ||
expected_diff =( | ||
"""--- \n""" | ||
"""+++ \n""" | ||
"""@@ -1,5 +1,5 @@\n""" | ||
""" import numpy as np\n""" | ||
""" \n""" | ||
""" a = np.nan\n""" | ||
"""-if a == np.nan:\n""" | ||
"""+if np.isnan(a):\n""" | ||
""" pass\n""" | ||
) | ||
# fmt: on | ||
|
||
expected_line_change = "4" | ||
change_description = SonarNumpyNanEqualityTransformer.change_description | ||
num_changed_files = 1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from pathlib import Path | ||
from codemodder.codemods.base_detector import BaseDetector | ||
from codemodder.context import CodemodExecutionContext | ||
from codemodder.result import ResultSet | ||
from codemodder.sonar_results import SonarResultSet | ||
from core_codemods.api.core_codemod import SASTCodemod | ||
|
||
|
||
class SonarCodemod(SASTCodemod): | ||
@property | ||
def origin(self): | ||
return "sonar" | ||
|
||
|
||
class SonarDetector(BaseDetector): | ||
_lazy_cache = None | ||
|
||
def _process_sonar_findings(self, sonar_json_files: list[str]) -> SonarResultSet: | ||
combined_result_set = SonarResultSet() | ||
for file in sonar_json_files or []: | ||
combined_result_set |= SonarResultSet.from_json(file) | ||
return combined_result_set | ||
|
||
def apply( | ||
self, | ||
codemod_id: str, | ||
context: CodemodExecutionContext, | ||
files_to_analyze: list[Path], | ||
) -> ResultSet: | ||
if not self._lazy_cache: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason not to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
self._lazy_cache = self._process_sonar_findings( | ||
context.tool_result_files_map.get("sonar", []) | ||
) | ||
return self._lazy_cache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you explain the goal of this assertion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal is to make sure no non-sast codemods are loaded by default while the
--sarif
or--sonar-...
flags are passed. Couldn't think of a better way other than looking for the codemod ids printed to stdout.RemoveAssertionInPytestRaises
just happens to the one I've used to check it.