|
| 1 | +"""Supplemental checks: SWE-bench 5.0.2 ships no tests for its GitPython callers. |
| 2 | +
|
| 3 | +Import the ordinary upstream utility module. The minimal environment only needs |
| 4 | +chardet and GitPython; no SWE-bench code or imported module is replaced. |
| 5 | +""" |
| 6 | + |
| 7 | +import subprocess |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | +from git import Repo |
| 12 | +from swebench.inference.make_datasets.utils import ( |
| 13 | + AutoContextManager, |
| 14 | + ingest_directory_contents, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize("object_format", ["sha1", "sha256"]) |
| 19 | +@pytest.mark.parametrize("ref_format", ["files", "reftable"]) |
| 20 | +def test_auto_context_manager_clones_and_resets(tmp_path, monkeypatch, object_format, ref_format): |
| 21 | + """Clone, revisit commits and reuse the checkout with real GitPython.""" |
| 22 | + origin = tmp_path / "origin" |
| 23 | + origin.mkdir() |
| 24 | + config = tmp_path / "gitconfig" |
| 25 | + config.write_text("[user]\n name = GitPython Compatibility\n email = tests@example.invalid\n") |
| 26 | + monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(config)) |
| 27 | + monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1") |
| 28 | + monkeypatch.setenv("GIT_DEFAULT_REF_FORMAT", ref_format) |
| 29 | + monkeypatch.setenv("GIT_ALLOW_PROTOCOL", "file") |
| 30 | + monkeypatch.setenv("GIT_TERMINAL_PROMPT", "0") |
| 31 | + |
| 32 | + def git(*args): |
| 33 | + return subprocess.run( |
| 34 | + ["git", "-C", str(origin), *args], check=True, text=True, capture_output=True |
| 35 | + ).stdout.strip() |
| 36 | + |
| 37 | + git("init", "-b", "main", f"--object-format={object_format}", f"--ref-format={ref_format}") |
| 38 | + (origin / "README.md").write_text("Local fixture\n") |
| 39 | + (origin / "module.py").write_text("value = 1\n") |
| 40 | + git("add", "--", "README.md", "module.py") |
| 41 | + git("commit", "-m", "initial") |
| 42 | + first_commit = git("rev-parse", "HEAD") |
| 43 | + (origin / "module.py").write_text("value = 2\n") |
| 44 | + git("commit", "-am", "second") |
| 45 | + second_commit = git("rev-parse", "HEAD") |
| 46 | + |
| 47 | + # Preserve SWE-bench's ordinary HTTPS URL construction while routing the |
| 48 | + # transport to our local fixture. No network access or credentials are used. |
| 49 | + git( |
| 50 | + "config", |
| 51 | + "--global", |
| 52 | + f"url.{origin.as_uri()}.insteadOf", |
| 53 | + "https://gitpython-tests@github.com/swe-bench-repos/fixture__project.git", |
| 54 | + ) |
| 55 | + git("config", "--global", "protocol.file.allow", "always") |
| 56 | + root = tmp_path / "checkouts" |
| 57 | + root.mkdir() |
| 58 | + instance = {"repo": "fixture/project", "base_commit": first_commit} |
| 59 | + original_cwd = Path.cwd() |
| 60 | + manager = AutoContextManager(instance, root_dir=str(root), token="gitpython-tests") |
| 61 | + checkout = Path(manager.repo_path) |
| 62 | + with manager as context: |
| 63 | + assert Path.cwd() == checkout |
| 64 | + assert context.get_readme_files() == ["README.md"] |
| 65 | + assert ingest_directory_contents(checkout, include_tests=True) == {"module.py": "value = 1\n"} |
| 66 | + with Repo(checkout) as repo: |
| 67 | + assert repo.head.commit.hexsha == first_commit |
| 68 | + assert repo.git.rev_parse("--show-object-format") == object_format |
| 69 | + assert repo.git.rev_parse("--show-ref-format") == ref_format |
| 70 | + assert Path.cwd() == original_cwd |
| 71 | + |
| 72 | + # Reuse the local clone even when its source no longer exists. Reset must |
| 73 | + # remove both modified tracked contents and untracked files from a prior run. |
| 74 | + origin.rename(tmp_path / "unavailable-origin") |
| 75 | + (checkout / "module.py").write_text("dirty = True\n") |
| 76 | + (checkout / "untracked.py").write_text("untracked = True\n") |
| 77 | + instance["base_commit"] = second_commit |
| 78 | + with AutoContextManager(instance, root_dir=str(root), token="gitpython-tests") as context: |
| 79 | + assert Path(context.repo_path) == checkout |
| 80 | + assert ingest_directory_contents(checkout, include_tests=True) == {"module.py": "value = 2\n"} |
| 81 | + assert Path.cwd() == original_cwd |
0 commit comments