Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use relative paths for codetf results #31

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions integration_tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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()}",
Expand Down
3 changes: 2 additions & 1 deletion src/codemodder/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the real change; everything else is just for making tests pass.

diff,
changes=file_context.codemod_changes,
).to_json()
Expand Down
2 changes: 2 additions & 0 deletions src/codemodder/codemods/base_codemod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions src/codemodder/global_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""


Expand Down