Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

CI: Add pytest rules

Kilian Lieretbcb6d709f4ff
diff --git a/pyproject.toml b/pyproject.toml
index 5e14137c..7e91130a 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -159,6 +159,8 @@ select = [
"RUF019", # unneded key in dict check
# other
"COM812", # use trailing commas
+ # pytest
+ "PT",
]
ignore = [
# flake8-return
@@ -170,6 +172,9 @@ ignore = [
"E402", # import not on top of file
"E722", # bare except
"E741", # ambiguous symbol
+ # pytest
+ "PT011",
+ "PT018",
]
# Allow fix for all enabled rules (when `--fix`) is provided.
diff --git a/tests/conftest.py b/tests/conftest.py
index 7045cd97..cd2cc5db 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -5,7 +5,7 @@ import os
import sys
from pathlib import Path
-from pytest import fixture
+import pytest
import sweagent.environment.utils as env_utils
@@ -19,28 +19,28 @@ os.environ["SWE_AGENT_EXPERIMENTAL_COMMUNICATE"] = "1"
env_utils.START_UP_DELAY = 1
-@fixture
def test_data_path() -> Path:
p = _this_dir / "test_data"
assert p.is_dir()
return p
-@fixture
def test_trajectories_path(test_data_path) -> Path:
p = test_data_path / "trajectories"
assert p.is_dir()
return p
-@fixture
def test_data_sources_path(test_data_path) -> Path:
p = test_data_path / "data_sources"
assert p.is_dir()
return p
-@fixture
def test_trajectory_path(test_trajectories_path) -> Path:
traj = (
test_trajectories_path
@@ -51,6 +51,6 @@ def test_trajectory_path(test_trajectories_path) -> Path:
return traj
-@fixture
def test_trajectory(test_trajectory_path):
return json.loads(test_trajectory_path.read_text())
diff --git a/tests/test_env.py b/tests/test_env.py
index f4920267..c354969e 100644
--- a/tests/test_env.py
+++ b/tests/test_env.py
@@ -49,20 +49,20 @@ def swe_env_context(env_args):
env.close()
def test_init_swe_env(test_env_args):
with swe_env_context(test_env_args) as env:
env.reset()
def test_init_swe_env_non_persistent(test_env_args):
test_env_args = dataclasses.replace(test_env_args, container_name=None)
with swe_env_context(test_env_args) as env:
env.reset()
def test_init_swe_env_cached_task_image(test_env_args):
test_env_args = dataclasses.replace(test_env_args, cache_task_images=True)
start = time.perf_counter()
@@ -89,7 +89,7 @@ def test_init_swe_env_cached_task_image(test_env_args):
client.images.remove(image.id)
def test_execute_setup_script(tmp_path, test_env_args):
test_script = "echo 'hello world'"
script_path = Path(tmp_path / "test_script.sh")
@@ -99,7 +99,7 @@ def test_execute_setup_script(tmp_path, test_env_args):
env.reset()
def test_execute_environment(tmp_path, test_env_args):
test_env = {
"python": "3.6",
@@ -114,7 +114,7 @@ def test_execute_environment(tmp_path, test_env_args):
env.reset()
def test_open_pr(test_env_args):
test_env_args = dataclasses.replace(
test_env_args,
@@ -126,14 +126,14 @@ def test_open_pr(test_env_args):
env.open_pr(_dry_run=True, trajectory=[])
def test_interrupt_close(test_env_args):
with swe_env_context(test_env_args) as env:
env.reset()
env.interrupt()
def test_communicate_old(test_env_args):
del os.environ["SWE_AGENT_EXPERIMENTAL_COMMUNICATE"]
try:
@@ -145,7 +145,7 @@ def test_communicate_old(test_env_args):
os.environ["SWE_AGENT_EXPERIMENTAL_COMMUNICATE"] = "1"
def test_env_with_hook(test_env_args):
with swe_env_context(test_env_args) as env:
env.add_hook(EnvHook())
diff --git a/tests/test_models.py b/tests/test_models.py
index da58a91d..faf132a8 100644
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -7,7 +7,7 @@ import pytest
from sweagent.agent.models import ModelArguments, OpenAIModel, TogetherModel
def openai_mock_client():
model = Mock()
response = Mock()
@@ -21,7 +21,7 @@ def openai_mock_client():
return model
def mock_together_response():
return {
"choices": [{"text": "<human>Hello</human>"}],
diff --git a/tests/test_replay.py b/tests/test_replay.py
index 19dc36f8..534ea2dd 100644
--- a/tests/test_replay.py
+++ b/tests/test_replay.py
@@ -8,7 +8,7 @@ import pytest
from run_replay import get_args, main
def swe_agent_test_repo_clone(tmp_path):
local_repo_path = tmp_path / "swe-agent-test-repo"
clone_cmd = ["git", "clone", "https://github.com/klieret/swe-agent-test-repo", local_repo_path]
@@ -16,7 +16,7 @@ def swe_agent_test_repo_clone(tmp_path):
return local_repo_path
def swe_agent_test_repo_traj(test_trajectories_path) -> Path:
p = (
test_trajectories_path
@@ -27,14 +27,14 @@ def swe_agent_test_repo_traj(test_trajectories_path) -> Path:
return p
def swe_agent_test_repo_local_problem_stmt(swe_agent_test_repo_clone) -> Path:
problem_stmt = swe_agent_test_repo_clone / "problem_statements" / "1.md"
assert problem_stmt.is_file()
return problem_stmt
@pytest.mark.parametrize("problem_statement_source", ["github", "local"])
def test_model_replay_github_repo(
swe_agent_test_repo_traj,
@@ -60,7 +60,7 @@ def test_model_replay_github_repo(
main(**vars(args), forward_args=remaining_args)
def test_model_replay_from_json(test_trajectories_path, test_data_sources_path):
traj_path = (
test_trajectories_path
@@ -92,7 +92,7 @@ def test_run_cli_help():
subprocess.run(args, check=True)
@pytest.mark.parametrize("problem_statement_source", ["github", "local"])
def test_model_replay_local_repo(swe_agent_test_repo_clone, swe_agent_test_repo_traj, problem_statement_source):
local_repo_path = swe_agent_test_repo_clone
diff --git a/tests/test_run.py b/tests/test_run.py
index 97ecddb3..cf4e84a4 100644
--- a/tests/test_run.py
+++ b/tests/test_run.py
@@ -14,7 +14,7 @@ from sweagent.agent.models import ModelArguments
from sweagent.environment.swe_env import EnvironmentArguments, SWEEnv
def test_run_cli_help():
args = [
"python",
@@ -24,7 +24,7 @@ def test_run_cli_help():
subprocess.run(args, check=True)
def open_pr_hook_init_for_sop():
hook = OpenPRHook()
hook._token = os.environ.get("GITHUB_TOKEN", "")
@@ -34,7 +34,7 @@ def open_pr_hook_init_for_sop():
return hook
def info_dict():
return {
"submission": "asdf",
@@ -97,7 +97,7 @@ class RaisesExceptionHook(MainHook):
raise ValueError(msg)
def test_script_args():
return ScriptArguments(
suffix="",
@@ -124,7 +124,7 @@ def test_script_args():
)
def test_exception_raised(test_script_args):
assert test_script_args.raise_exceptions
main = Main(test_script_args)
@@ -133,7 +133,7 @@ def test_exception_raised(test_script_args):
main.main()
class CreateFakeLogFile(MainHook):
"""Testing the skip functionality"""
@@ -149,21 +149,21 @@ class CreateFakeLogFile(MainHook):
(self._traj_dir / f"{instance_id}.traj").write_text(json.dumps(dct))
def test_existing_corrupted_args(test_script_args):
main = Main(test_script_args)
main.add_hook(CreateFakeLogFile())
main.main()
def test_main_hook(test_script_args):
main = Main(test_script_args)
main.add_hook(MainHook())
main.main()
def test_agent_with_hook(test_script_args):
main = Main(test_script_args)
main.agent.add_hook(AgentHook())