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]
Speed up testing with persistent containers & remove them end of session (#238)
Kilian Lieret3e5b4eeb9e26
diff --git a/tests/test_env.py b/tests/test_env.pyindex 2d8f6a0a..b1d7e7eb 100644--- a/tests/test_env.py+++ b/tests/test_env.py@@ -1,47 +1,58 @@import dataclassesimport osfrom pathlib import Path+import subprocessimport pytestimport yamlfrom sweagent.environment.swe_env import EnvironmentArguments, SWEEnv+from contextlib import contextmanager+import docker-# TEST_INSTANCE = {"repo": "test/test",-# "instance_id": "test__test-4764",-# "base_commit": "",-# "patch": "",-# "problem_statement": "",-# "hints_text": "",-# "created_at": "2023-04-16T14:24:42Z",-# "version": "1.4",-# "FAIL_TO_PASS": [-# ],-# "PASS_TO_PASS": [-# ],-# "environment_setup_commit": "test"-# }---def test_env_args():[email protected](scope="module")+def test_env_args(tmpdir_factory, ):+ """This will use a persistent container"""+ local_repo_path = tmpdir_factory.getbasetemp() / "swe-agent-test-repo"+ clone_cmd = ["git", "clone", "https://github.com/klieret/swe-agent-test-repo", local_repo_path]+ subprocess.run(clone_cmd, check=True)+ data_path = local_repo_path / "problem_statements" / "1.md"test_env_args = EnvironmentArguments(- data_path="https://github.com/klieret/swe-agent-test-repo/issues/1",+ data_path=str(data_path),+ repo_path=str(local_repo_path),image_name="sweagent/swe-agent:latest",+ container_name="test-container-134245890345098",)- return test_env_args+ yield test_env_args+ # Cleanup (after session ends)+ client = docker.from_env()+ container = client.containers.get(test_env_args.container_name)+ container.remove(force=True)+++@contextmanager+def swe_env_context(env_args):+ """Context manager to make sure we close the shell on the container+ so that we can reuse it.+ """++ env = SWEEnv(env_args)+ try:+ yield env+ finally:+ env.close()@pytest.mark.slowdef test_init_swe_env(test_env_args):- env = SWEEnv(test_env_args)- env.reset()+ with swe_env_context(test_env_args) as env:+ env.reset()@pytest.mark.slow-def test_init_swe_env_persistent(test_env_args):- test_env_args = dataclasses.replace(test_env_args, container_name="test-container")- env = SWEEnv(test_env_args)- 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()@pytest.mark.slow@@ -50,8 +61,8 @@ def test_execute_setup_script(tmp_path, test_env_args):script_path = Path(tmp_path / "test_script.sh")script_path.write_text(test_script)test_env_args = dataclasses.replace(test_env_args, environment_setup=script_path)- env = SWEEnv(test_env_args)- env.reset()+ with swe_env_context(test_env_args) as env:+ env.reset()@pytest.mark.slow@@ -65,30 +76,31 @@ def test_execute_environment(tmp_path, test_env_args):env_config_path = Path(tmp_path / "env_config.yml")env_config_path.write_text(yaml.dump(test_env))test_env_args = dataclasses.replace(test_env_args, environment_setup=env_config_path)- env = SWEEnv(test_env_args)- env.reset()+ with swe_env_context(test_env_args) as env:+ env.reset()@pytest.mark.slowdef test_open_pr(test_env_args):- env = SWEEnv(test_env_args)- env.reset()- env.open_pr(_dry_run=True, trajectory=[])+ test_env_args = dataclasses.replace(test_env_args, data_path="https://github.com/klieret/swe-agent-test-repo/issues/1", repo_path="")+ with swe_env_context(test_env_args) as env:+ env.reset()+ env.open_pr(_dry_run=True, trajectory=[])@pytest.mark.slowdef test_interrupt_close(test_env_args):- env = SWEEnv(test_env_args)- env.interrupt()- env.close()+ with swe_env_context(test_env_args) as env:+ env.reset()+ env.interrupt()@pytest.mark.slowdef test_communicate_old(test_env_args):del os.environ["SWE_AGENT_EXPERIMENTAL_COMMUNICATE"]try:- env = SWEEnv(test_env_args)- env.reset()+ with swe_env_context(test_env_args) as env:+ env.reset()except:raisefinally: