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]

Add test for default_utils.py

Kilian Lieretdb88452613ef
diff --git a/tests/tools/conftest.py b/tests/tools/conftest.py
new file mode 100644
index 00000000..bc1c8819
--- /dev/null
+++ b/tests/tools/conftest.py
@@ -0,0 +1,12 @@
+from unittest import mock
+
+import pytest
+
+
+def with_tmp_env_file(tmp_path):
+ env_file = tmp_path / ".swe-agent-env"
+ env_file.write_text("{}")
+ with mock.patch.dict("os.environ", {"SWE_AGENT_ENV_FILE": str(env_file)}, clear=True):
+ yield env_file
+ env_file.unlink()
diff --git a/tests/tools/test_default_utils.py b/tests/tools/test_default_utils.py
new file mode 100644
index 00000000..3c4b1f32
--- /dev/null
+++ b/tests/tools/test_default_utils.py
@@ -0,0 +1,32 @@
+import json
+import os
+from pathlib import Path
+
+from tests.utils import make_python_tool_importable
+
+make_python_tool_importable("tools/defaults/lib/default_utils.py", "default_utils")
+import default_utils # type: ignore
+from default_utils import ( # type: ignore
+ get_current_file_with_line_range,
+)
+
+
+def test_env_file_override(with_tmp_env_file):
+ assert Path(os.getenv("SWE_AGENT_ENV_FILE")).name == ".swe-agent-env" # type: ignore
+
+
+def test_get_current_file_with_line_range(with_tmp_env_file):
+ default_utils.SWE_AGENT_ENV_FILE = with_tmp_env_file # type: ignore
+ test_path = with_tmp_env_file.parent.joinpath("test.py")
+ registry = {
+ "CURRENT_FILE": str(test_path),
+ "CURRENT_LINE": "10",
+ "WINDOW": "10",
+ }
+ with_tmp_env_file.write_text(json.dumps(registry))
+ test_path.write_text("\n".join(map(str, range(20))))
+ file_path, start_line, end_line = get_current_file_with_line_range()
+ print(file_path, start_line, end_line)
+ assert file_path == test_path
+ assert start_line == 4
+ assert end_line == 14
diff --git a/tests/tools/test_edit_replace.py b/tests/tools/test_edit_replace.py
new file mode 100644
index 00000000..e69de29b