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]
Improve test coverage (#142)
Kilian Lieret2b51d41c8343
diff --git a/.gitignore b/.gitignoreindex 3e85e098..a8653d67 100644--- a/.gitignore+++ b/.gitignore@@ -187,3 +187,5 @@ website/frontend/build# trajectory filestrajectories/*++.vscode/**diff --git a/pyproject.toml b/pyproject.tomlindex d6abdc55..172097b5 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -6,4 +6,8 @@ build-backend = 'setuptools.build_meta'markers = ["slow: marks tests as slow (deselect with '-m \"not slow\"')",]+testpaths = [+ "tests"+]+xfail_strict = truediff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.pyindex 031f3e32..07781302 100644--- a/sweagent/environment/utils.py+++ b/sweagent/environment/utils.py@@ -392,17 +392,15 @@ def get_associated_commit_urls(org: str, repo: str, issue_number: str, *, token:def remove_triple_backticks(text: str) -> str:return "\n".join(line.removeprefix("```") for line in text.splitlines())+_MARKDOWN_TRAJECTORY_EMOJI_MAPPING = {+ "observation": "π",+ "response": "οΈπ§βπ",+ "state": "π§ ",+ "thought": "π‘",+}def format_trajectory_markdown(trajectory: List[Dict[str, str]]):"""Format a trajectory as a markdown string for use in gh PR description."""- emojis = {- "action": "π₯",- "observation": "π",- "response": "οΈπ§βπ",- "state": "π§ ",- "thought": "π‘",-- }prefix = ["<details>","<summary>Thought process ('trajectory') of SWE-agent (click to expand)</summary>",@@ -413,7 +411,7 @@ def format_trajectory_markdown(trajectory: List[Dict[str, str]]):for i, step in enumerate(trajectory):step_strs = []for key, value in step.items():- emoji = emojis.get(key, "")+ emoji = _MARKDOWN_TRAJECTORY_EMOJI_MAPPING.get(key, "")if emoji:emoji += " "step_strs.append(f"**{emoji}{key.capitalize()} ({i})**:")diff --git a/tests/__init__.py b/tests/__init__.pynew file mode 100644index 00000000..e69de29bdiff --git a/tests/conftest.py b/tests/conftest.pyindex d00070ec..042ccd07 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -1,6 +1,9 @@+import jsonimport sysfrom pathlib import Path+from pytest import fixture+# this is a hack and should be removed when we have a better solution_this_dir = Path(__file__).resolve().parentroot_dir = _this_dir.parent@@ -8,3 +11,10 @@ package_dir = root_dir / "sweagent"sys.path.insert(0, str(root_dir))sys.path.insert(1, str(package_dir))print("Adjusted path: ", sys.path)+++@fixture+def test_trajectory() -> Path:+ traj_dir = _this_dir / "test_data/trajectories/gpt4__klieret__swe-agent-test-repo__default_from_url__t-0.00__p-0.95__c-3.00__install-1/klieret__swe-agent-test-repo-i1.traj"+ assert traj_dir.exists()+ return json.loads(traj_dir.read_text())\ No newline at end of filediff --git a/tests/test_replay.py b/tests/test_replay.pyindex 26157485..03681972 100644--- a/tests/test_replay.py+++ b/tests/test_replay.py@@ -1,3 +1,4 @@+import subprocessfrom run_replay import get_args, mainimport pytest@@ -14,4 +15,13 @@ def test_model_replay():"--raise_exceptions=True",]args, remaining_args = get_args(args)- main(**vars(args), forward_args=remaining_args)\ No newline at end of file+ main(**vars(args), forward_args=remaining_args)+++def test_run_cli_help():+ args = [+ "python",+ "run_replay.py",+ "--help",+ ]+ subprocess.run(args, check=True)\ No newline at end of filediff --git a/tests/test_run.py b/tests/test_run.pynew file mode 100644index 00000000..bb868f02--- /dev/null+++ b/tests/test_run.py@@ -0,0 +1,9 @@+import subprocess++def test_run_cli_help():+ args = [+ "python",+ "run.py",+ "--help",+ ]+ subprocess.run(args, check=True)\ No newline at end of filediff --git a/tests/test_utils.py b/tests/test_utils.pynew file mode 100644index 00000000..19d79c5d--- /dev/null+++ b/tests/test_utils.py@@ -0,0 +1,41 @@+from sweagent.environment.utils import format_trajectory_markdown, _MARKDOWN_TRAJECTORY_EMOJI_MAPPING, remove_triple_backticks, parse_gh_repo_url, parse_gh_issue_url, is_from_github_url, get_associated_commit_urls++def test_format_trajectory_markdown(test_trajectory):+ formatted = format_trajectory_markdown(test_trajectory["trajectory"])+ assert formatted.startswith("<details>")+ assert formatted.endswith("</details>")+ for emoji in _MARKDOWN_TRAJECTORY_EMOJI_MAPPING.values():+ assert emoji in formatted+++def test_remove_triple_backticks():+ assert remove_triple_backticks("```") == ""+++def test_parse_gh_repo_url():+ url = "https://github.com/princeton-nlp/SWE-agent"+ owner, repo = parse_gh_repo_url(url)+ assert owner == "princeton-nlp"+ assert repo == "SWE-agent"+++def test_parse_gh_issue_url():+ url = "https://github.com/princeton-nlp/SWE-agent/issues/43"+ owner, repo, no = parse_gh_issue_url(url)+ assert owner == "princeton-nlp"+ assert repo == "SWE-agent"+ assert no == "43"+++def test_is_from_github_url():+ assert not is_from_github_url("")+ assert is_from_github_url("https://github.com/princeton-nlp/SWE-agent/issues/43")+++def test_get_associated_commit_urls():+ assoc = get_associated_commit_urls(+ org="princeton-nlp",+ repo="SWE-agent",+ issue_number="41"+ )+ assert len(assoc) > 0\ No newline at end of file