From 3c1fadb316bd6ae6a4e615c22e7750cf09984899 Mon Sep 17 00:00:00 2001 From: Daniel D'Avella Date: Mon, 11 Sep 2023 16:21:58 -0400 Subject: [PATCH] Use relative paths for codetf results --- integration_tests/base_test.py | 17 ++++++++++++----- src/codemodder/__main__.py | 3 ++- src/codemodder/codemods/base_codemod.py | 2 ++ src/codemodder/global_state.py | 1 + 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/integration_tests/base_test.py b/integration_tests/base_test.py index fef68c76..8a7b45b7 100644 --- a/integration_tests/base_test.py +++ b/integration_tests/base_test.py @@ -8,12 +8,16 @@ from codemodder import __VERSION__ +SAMPLES_DIR = "tests/samples" + + class CleanRepoMixin: @classmethod def teardown_class(cls): """Ensure any re-written file is undone after integration test class""" + # TODO: we should probably use temporary directories instead repo = git.Git(os.getcwd()) - repo.execute(["git", "checkout", os.getcwd() + "/tests/samples"]) + repo.execute(["git", "checkout", os.path.join(os.getcwd(), SAMPLES_DIR)]) pathlib.Path(cls.output_path).unlink(missing_ok=True) @@ -54,9 +58,9 @@ def _assert_run_fields(self, run, output_path): assert run["elapsed"] != "" assert ( run["commandLine"] - == f"codemodder tests/samples/ --output {output_path} --codemod-include={self.codemod.name()}" + == f"codemodder {SAMPLES_DIR} --output {output_path} --codemod-include={self.codemod.name()}" ) - assert run["directory"] == os.path.abspath("tests/samples/") + assert run["directory"] == os.path.abspath(SAMPLES_DIR) assert run["sarifs"] == [] def _assert_results_fields(self, results, output_path): @@ -88,7 +92,10 @@ def _assert_codetf_output(self): run = codetf["run"] self._assert_run_fields(run, self.output_path) results = codetf["results"] - self._assert_results_fields(results, self.code_path) + # CodeTf2 spec requires relative paths + self._assert_results_fields( + results, os.path.relpath(self.code_path, SAMPLES_DIR) + ) def check_code_before(self): with open(self.code_path, "r", encoding="utf-8") as f: @@ -113,7 +120,7 @@ def test_file_rewritten(self): "python", "-m", "codemodder", - "tests/samples/", + SAMPLES_DIR, "--output", self.output_path, f"--codemod-include={self.codemod.name()}", diff --git a/src/codemodder/__main__.py b/src/codemodder/__main__.py index 6d2233e8..dded2fbf 100644 --- a/src/codemodder/__main__.py +++ b/src/codemodder/__main__.py @@ -74,7 +74,8 @@ def run_codemods_for_file( codemod_kls.CHANGESET_ALL_FILES.append( ChangeSet( - str(file_context.file_path), + # TODO: we should not be using global state here + str(file_context.file_path.relative_to(global_state.DIRECTORY)), diff, changes=file_context.codemod_changes, ).to_json() diff --git a/src/codemodder/codemods/base_codemod.py b/src/codemodder/codemods/base_codemod.py index 41d942c6..802c7ee3 100644 --- a/src/codemodder/codemods/base_codemod.py +++ b/src/codemodder/codemods/base_codemod.py @@ -26,6 +26,8 @@ class BaseCodemod: def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) + # TODO: this should not belong to codemod classes + # Instead it should be owned by a global context that gets passed in cls.CHANGESET_ALL_FILES: list = [] if "codemodder.codemods.base_codemod.SemgrepCodemod" in str(cls): diff --git a/src/codemodder/global_state.py b/src/codemodder/global_state.py index 1a3df8d3..2fe883c2 100644 --- a/src/codemodder/global_state.py +++ b/src/codemodder/global_state.py @@ -3,6 +3,7 @@ # Source code directory codemodder will analyze. # Should be overridden when codemodder starts. +# TODO: we should not be managing global state at all DIRECTORY = ""