diff --git a/src/codemodder/codemods/sonar.py b/src/codemodder/codemods/sonar.py index dbb7acae..a5d54153 100644 --- a/src/codemodder/codemods/sonar.py +++ b/src/codemodder/codemods/sonar.py @@ -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, diff --git a/src/codemodder/codemods/test/integration_utils.py b/src/codemodder/codemods/test/integration_utils.py index b6ae8199..4de44a9c 100644 --- a/src/codemodder/codemods/test/integration_utils.py +++ b/src/codemodder/codemods/test/integration_utils.py @@ -11,6 +11,7 @@ import jsonschema from codemodder import __version__, registry +from codemodder.sonar_results import SonarResultSet from .validations import execute_code @@ -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): @@ -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 (