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]
Make tests for cloning of conda envs more precise (#552)
Kilian Lieret4ea4c8784b33
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.pyindex 6bd0a5c1..1e6b6e1c 100644--- a/sweagent/agent/agents.py+++ b/sweagent/agent/agents.py@@ -662,7 +662,7 @@ class Agent:)return self.check_format_and_requery(output)- def init_environment_vars(self, env):+ def init_environment_vars(self, env: SWEEnv):self.set_environment_vars(env, self.config.env_variables)def set_environment_vars(self, env: SWEEnv, env_variables: dict[str, Any]) -> None:diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex 95b331a5..929997c8 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -96,6 +96,9 @@ class EnvironmentArguments(FrozenSerializable):"(probably doesn't make sense and takes excessive space).")raise ValueError(msg)+ if self.container_name is not None and self.container_name.strip() == "":+ msg = "Set container_name to None if you don't want to use a persistent container."+ raise ValueError(msg)class EnvHook:@@ -936,6 +939,7 @@ class SWEEnv(gym.Env):)self.logger.debug("Cloned python conda environment")else:+ self.logger.debug(f"Could not find {python_env}, creating new environment")self.communicate_with_handling(f"conda create -n {env_name} python={install_configs['python']} -y",error_msg="Failed to create conda environment",diff --git a/tests/test_env.py b/tests/test_env.pyindex 397d40cb..e2eecaea 100644--- a/tests/test_env.py+++ b/tests/test_env.py@@ -11,6 +11,7 @@ import pytestimport yamlimport docker+import docker.errorsfrom sweagent import CONFIG_DIRfrom sweagent.environment.swe_env import EnvHook, EnvironmentArguments, SWEEnv@@ -34,8 +35,14 @@ def 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)+ # fixme (?): What happens if user changed container_name?+ try:+ container = client.containers.get(test_env_args.container_name)+ container.remove(force=True)+ except docker.errors.NotFound:+ # Can happen if this fixture never runs because we only do a partial+ # test run+ pass@contextmanager@@ -109,25 +116,33 @@ def test_execute_setup_script(tmp_path, test_env_args):@pytest.mark.slow()-def test_execute_environment(tmp_path, test_env_args):+def test_execute_environment(tmp_path, test_env_args, capsys):test_env = {"python": "3.6","packages": "pytest","pip_packages": ["tox"],- "install": "echo 'installing'",+ "install": "python -m pip install --upgrade pip && python -m pip install -e .",}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)+ # Make sure we don't use persistent container, else we might have already installed the conda environment+ test_env_args = dataclasses.replace(test_env_args, environment_setup=env_config_path, container_name=None)with swe_env_context(test_env_args) as env:env.reset()+ out = capsys.readouterr().out+ print(out)+ assert "Cloned python conda environment" not in out@pytest.mark.slow()def test_execute_environment_default(test_env_args):env_config_paths = (CONFIG_DIR / "environment_setup").iterdir()assert env_config_paths+ # Make sure we don't use persistent container, else we might have already installed the conda environment+ test_env_args = dataclasses.replace(test_env_args, container_name=None)for env_config_path in env_config_paths:+ if env_config_path.suffix not in [".yaml", ".yml", ".sh"]:+ continueprint(env_config_path)test_env_args = dataclasses.replace(test_env_args, environment_setup=env_config_path)with swe_env_context(test_env_args) as env:@@ -135,18 +150,23 @@ def test_execute_environment_default(test_env_args):@pytest.mark.slow()-def test_execute_environment_clone_python(tmp_path, test_env_args):+def test_execute_environment_clone_python(tmp_path, test_env_args, capsys):"""This should clone the existing python 3.10 conda environment for speedup"""test_env = {"python": "3.10","packages": "pytest","pip_packages": ["tox"],+ "install": "python -m pip install --upgrade pip && python -m pip install -e .",}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)+ # Make sure we don't use persistent container, else we might have already installed the conda environment+ test_env_args = dataclasses.replace(test_env_args, environment_setup=env_config_path, container_name=None)with swe_env_context(test_env_args) as env:env.reset()+ out = capsys.readouterr().out+ print(out)+ assert "Cloned python conda environment" in out@pytest.mark.slow()