Skip to content

Commit

Permalink
add assertions for sonar integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Apr 2, 2024
1 parent 9ce4625 commit 94689a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/codemodder/codemods/sonar.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class SonarCodemod(SASTCodemod):
def origin(self):
return "sonar"

@property
def rule_id(self):
return self._metadata.tool.rule_id

@classmethod
def from_core_codemod(
cls,
Expand Down
15 changes: 15 additions & 0 deletions src/codemodder/codemods/test/integration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jsonschema

from codemodder import __version__, registry
from codemodder.sonar_results import SonarResultSet

from .validations import execute_code

Expand Down Expand Up @@ -261,6 +262,7 @@ def setup_class(cls):
# in parallel at this time since they would all override the same
# tests/samples/requirements.txt file, unless we change that to
# a temporary file.
cls.check_sonar_issues()

@classmethod
def teardown_class(cls):
Expand All @@ -270,6 +272,19 @@ def teardown_class(cls):
with open(cls.code_path, mode="w", encoding="utf-8") as f:
f.write(cls.original_code)

@classmethod
def check_sonar_issues(cls):
sonar_results = SonarResultSet.from_json(cls.sonar_issues_json)

assert (
cls.codemod.rule_id in sonar_results
), f"Make sure to add a sonar issue for {cls.codemod.rule_id} in {cls.sonar_issues_json}"
results_for_codemod = sonar_results[cls.codemod.rule_id]
file_path = pathlib.Path(cls.code_filename)
assert (
file_path in results_for_codemod
), f"Make sure to add a sonar issue for file `{cls.code_filename}` under rule `{cls.codemod.rule_id}` in {cls.sonar_issues_json}"

def _assert_sonar_fields(self, result):
assert self.codemod_instance._metadata.tool is not None
assert (
Expand Down

0 comments on commit 94689a7

Please sign in to comment.