From bd01979d7ff1cf11746eefc5fd1d3f0f225cf035 Mon Sep 17 00:00:00 2001 From: andrecs <12188364+andrecsilva@users.noreply.github.com> Date: Fri, 26 Jan 2024 08:56:05 -0300 Subject: [PATCH] Some refactoring --- integration_tests/base_test.py | 17 +++++++---------- src/codemodder/codemods/base_codemod.py | 4 ++-- src/codemodder/context.py | 5 +---- src/codemodder/registry.py | 15 ++++++--------- tests/test_codemod_docs.py | 1 - 5 files changed, 16 insertions(+), 26 deletions(-) diff --git a/integration_tests/base_test.py b/integration_tests/base_test.py index ddebae48a..9dc5b9343 100644 --- a/integration_tests/base_test.py +++ b/integration_tests/base_test.py @@ -82,16 +82,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={self.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={self.code_path}" - ) + assert run[ + "commandLine" + ] == f"codemodder {SAMPLES_DIR} --output {output_path} --codemod-include={self.codemod_instance.name} --path-include={self.code_path} --sonar-issues-json={self.sonar_issues_json}" + ( + 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/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/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")