From 915123f282031d4d26481034687bbec63307c2ec Mon Sep 17 00:00:00 2001 From: andrecs <12188364+andrecsilva@users.noreply.github.com> Date: Fri, 26 Jan 2024 14:07:25 -0300 Subject: [PATCH] Some refactoring --- integration_tests/base_test.py | 17 +++++++---------- .../test_sonar_numpy_nan_equality.py | 2 +- src/codemodder/codemodder.py | 8 -------- src/codemodder/codemods/base_codemod.py | 4 ++-- src/codemodder/context.py | 5 +---- src/codemodder/registry.py | 15 ++++++--------- src/core_codemods/__init__.py | 2 +- .../{ => sonar}/sonar_numpy_nan_equality.py | 0 tests/codemods/test_sonar_numpy_nan_equality.py | 2 +- tests/test_codemod_docs.py | 1 - 10 files changed, 19 insertions(+), 37 deletions(-) rename src/core_codemods/{ => sonar}/sonar_numpy_nan_equality.py (100%) diff --git a/integration_tests/base_test.py b/integration_tests/base_test.py index afca61897..b3067afc3 100644 --- a/integration_tests/base_test.py +++ b/integration_tests/base_test.py @@ -83,16 +83,13 @@ def _assert_run_fields(self, run, output_path): assert run["tool"] == "codemodder-python" assert run["version"] == __version__ assert run["elapsed"] != "" - if self.sonar_issues_json: - assert ( - run["commandLine"] - == f"codemodder {SAMPLES_DIR} --output {output_path} --codemod-include={self.codemod_instance.name} --path-include={code_path} --sonar-issues-json={self.sonar_issues_json}" - ) - else: - assert ( - run["commandLine"] - == f"codemodder {SAMPLES_DIR} --output {output_path} --codemod-include={self.codemod_instance.name} --path-include={code_path}" - ) + assert run[ + "commandLine" + ] == f"codemodder {SAMPLES_DIR} --output {output_path} --codemod-include={self.codemod_instance.name} --path-include={code_path}" + ( + f" --sonar-issues-json={self.sonar_issues_json}" + if self.sonar_issues_json + else "" + ) assert run["directory"] == os.path.abspath(SAMPLES_DIR) assert run["sarifs"] == [] diff --git a/integration_tests/test_sonar_numpy_nan_equality.py b/integration_tests/test_sonar_numpy_nan_equality.py index c3365ddc5..344d798c0 100644 --- a/integration_tests/test_sonar_numpy_nan_equality.py +++ b/integration_tests/test_sonar_numpy_nan_equality.py @@ -1,4 +1,4 @@ -from core_codemods.sonar_numpy_nan_equality import ( +from core_codemods.sonar.sonar_numpy_nan_equality import ( SonarNumpyNanEquality, SonarNumpyNanEqualityTransformer, ) diff --git a/src/codemodder/codemodder.py b/src/codemodder/codemodder.py index ce92e0c5a..ff886dee8 100644 --- a/src/codemodder/codemodder.py +++ b/src/codemodder/codemodder.py @@ -20,7 +20,6 @@ from codemodder.report.codetf_reporter import report_default from codemodder.result import ResultSet from codemodder.semgrep import run as run_semgrep -from codemodder.sonar_results import SonarResultSet def update_code(file_path, new_code): @@ -137,13 +136,6 @@ def record_dependency_update(dependency_results: dict[Dependency, PackageStore | logger.debug("The following dependencies could not be added: %s", str_list) -def process_sonar_findings(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 run(original_args) -> int: start = datetime.datetime.now() diff --git a/src/codemodder/codemods/base_codemod.py b/src/codemodder/codemods/base_codemod.py index cdd36a50c..9ec3cfbc5 100644 --- a/src/codemodder/codemods/base_codemod.py +++ b/src/codemodder/codemods/base_codemod.py @@ -108,10 +108,10 @@ def docs_module(self) -> Traversable: @cached_property def description(self) -> str: - if self._metadata.description == None: + if self._metadata.description is None: doc_path = self.docs_module / f"{self.origin}_python_{self.name}.md" return doc_path.read_text() - return self._metadata.description # type: ignore + return self._metadata.description @property def review_guidance(self): diff --git a/src/codemodder/context.py b/src/codemodder/context.py index a61e80e50..4f077c41d 100644 --- a/src/codemodder/context.py +++ b/src/codemodder/context.py @@ -63,10 +63,7 @@ def __init__( self.path_include = path_include self.path_exclude = path_exclude self.max_workers = max_workers - if tool_result_files_map: - self.tool_result_files_map = tool_result_files_map - else: - self.tool_result_files_map = {} + self.tool_result_files_map = tool_result_files_map or {} def add_results(self, codemod_name: str, change_sets: List[ChangeSet]): self._results_by_codemod.setdefault(codemod_name, []).extend(change_sets) diff --git a/src/codemodder/registry.py b/src/codemodder/registry.py index 15ba125b7..6b15acbba 100644 --- a/src/codemodder/registry.py +++ b/src/codemodder/registry.py @@ -63,15 +63,12 @@ def match_codemods( ) -> list[BaseCodemod]: codemod_include = codemod_include or [] codemod_exclude = codemod_exclude or DEFAULT_EXCLUDED_CODEMODS - - if sast_only: - base_list = [ - codemod for codemod in self.codemods if codemod.origin != "pixee" - ] - else: - base_list = [ - codemod for codemod in self.codemods if codemod.origin == "pixee" - ] + base_list = [ + codemod + for codemod in self.codemods + if (sast_only and codemod.origin != "pixee") + or (not sast_only and codemod.origin == "pixee") + ] if codemod_exclude and not codemod_include: return [ diff --git a/src/core_codemods/__init__.py b/src/core_codemods/__init__.py index 174c37670..a71c611d3 100644 --- a/src/core_codemods/__init__.py +++ b/src/core_codemods/__init__.py @@ -50,7 +50,7 @@ from .remove_assertion_in_pytest_raises import RemoveAssertionInPytestRaises from .fix_assert_tuple import FixAssertTuple -from .sonar_numpy_nan_equality import SonarNumpyNanEquality +from .sonar.sonar_numpy_nan_equality import SonarNumpyNanEquality registry = CodemodCollection( origin="pixee", diff --git a/src/core_codemods/sonar_numpy_nan_equality.py b/src/core_codemods/sonar/sonar_numpy_nan_equality.py similarity index 100% rename from src/core_codemods/sonar_numpy_nan_equality.py rename to src/core_codemods/sonar/sonar_numpy_nan_equality.py diff --git a/tests/codemods/test_sonar_numpy_nan_equality.py b/tests/codemods/test_sonar_numpy_nan_equality.py index 52d25bf37..1378c20af 100644 --- a/tests/codemods/test_sonar_numpy_nan_equality.py +++ b/tests/codemods/test_sonar_numpy_nan_equality.py @@ -1,5 +1,5 @@ import json -from core_codemods.sonar_numpy_nan_equality import SonarNumpyNanEquality +from core_codemods.sonar.sonar_numpy_nan_equality import SonarNumpyNanEquality from tests.codemods.base_codemod_test import BaseSASTCodemodTest from textwrap import dedent diff --git a/tests/test_codemod_docs.py b/tests/test_codemod_docs.py index a2fa46f24..963116cef 100644 --- a/tests/test_codemod_docs.py +++ b/tests/test_codemod_docs.py @@ -12,7 +12,6 @@ def pytest_generate_tests(metafunc): def test_load_codemod_docs_info(codemod: BaseCodemod): - print(codemod.name) if codemod.name in ["order-imports"]: pytest.xfail(reason=f"{codemod.name} has no description")