refactor tests
Publish container image / Build and push (push) Successful in 32s

This commit is contained in:
2026-07-26 23:49:40 +02:00
parent 5ef10d28fe
commit ce9f1e3d20
32 changed files with 1546 additions and 1923 deletions
+64 -51
View File
@@ -1,4 +1,7 @@
import asyncio
from collections.abc import Sequence
from pathlib import Path
from typing import Any
import pytest
@@ -14,53 +17,75 @@ class FakeProcess:
return b"", self.stderr
async def test_initializes_incomplete_index_and_excludes_it_from_git(
tmp_path: Path, monkeypatch
) -> None:
workspace = tmp_path / "repo"
(workspace / ".git" / "info").mkdir(parents=True)
(workspace / ".codegraph").mkdir()
calls: list[tuple[object, ...]] = []
class CodeGraphProcessRecorder:
def __init__(self, outcomes: Sequence[FakeProcess | OSError]) -> None:
self.outcomes = list(outcomes)
self.calls: list[tuple[tuple[Any, ...], dict[str, Any]]] = []
async def create_subprocess_exec(*args, **_kwargs):
calls.append(args)
return FakeProcess()
async def __call__(self, *args: Any, **kwargs: Any) -> FakeProcess:
self.calls.append((args, kwargs))
outcome = self.outcomes.pop(0)
if isinstance(outcome, OSError):
raise outcome
return outcome
@property
def commands(self) -> list[tuple[Any, ...]]:
return [args for args, _ in self.calls]
def install_recorder(
monkeypatch: pytest.MonkeyPatch,
outcomes: Sequence[FakeProcess | OSError],
) -> CodeGraphProcessRecorder:
recorder = CodeGraphProcessRecorder(outcomes)
monkeypatch.setattr(
"agentci.integrations.codegraph.asyncio.create_subprocess_exec",
create_subprocess_exec,
recorder,
)
await CodeGraph().prepare(workspace)
assert calls == [("codegraph", "init", str(workspace))]
assert (workspace / ".git" / "info" / "exclude").read_text() == ".codegraph/\n"
return recorder
async def test_syncs_an_existing_index_without_duplicating_exclude(
tmp_path: Path, monkeypatch
@pytest.mark.parametrize(
("database_exists", "initial_exclude", "expected_command", "expected_exclude"),
[
pytest.param(False, None, "init", ".codegraph/\n", id="incomplete-index"),
pytest.param(
True,
"# local excludes\n.codegraph/\n",
"sync",
"# local excludes\n.codegraph/\n",
id="existing-index",
),
],
)
async def test_prepare_selects_init_or_sync_and_updates_git_exclude(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
database_exists: bool,
initial_exclude: str | None,
expected_command: str,
expected_exclude: str,
) -> None:
workspace = tmp_path / "repo"
(workspace / ".git" / "info").mkdir(parents=True)
(workspace / ".codegraph").mkdir()
(workspace / ".codegraph" / "codegraph.db").touch()
if database_exists:
(workspace / ".codegraph" / "codegraph.db").touch()
exclude = workspace / ".git" / "info" / "exclude"
exclude.write_text("# local excludes\n.codegraph/\n")
calls: list[tuple[object, ...]] = []
async def create_subprocess_exec(*args, **_kwargs):
calls.append(args)
return FakeProcess()
monkeypatch.setattr(
"agentci.integrations.codegraph.asyncio.create_subprocess_exec",
create_subprocess_exec,
)
if initial_exclude is not None:
exclude.write_text(initial_exclude)
recorder = install_recorder(monkeypatch, [FakeProcess()])
await CodeGraph().prepare(workspace)
assert calls == [("codegraph", "sync", str(workspace))]
assert exclude.read_text() == "# local excludes\n.codegraph/\n"
assert recorder.commands == [("codegraph", expected_command, str(workspace))]
kwargs = recorder.calls[0][1]
assert kwargs["cwd"] == workspace
assert kwargs["env"]["CODEGRAPH_TELEMETRY"] == "0"
assert kwargs["stdout"] is asyncio.subprocess.PIPE
assert kwargs["stderr"] is asyncio.subprocess.PIPE
assert exclude.read_text() == expected_exclude
@pytest.mark.parametrize(
@@ -82,13 +107,7 @@ async def test_handles_exclude_file_boundaries(
exclude.parent.mkdir(parents=True)
exclude.write_text(initial)
async def create_subprocess_exec(*_args, **_kwargs):
return FakeProcess()
monkeypatch.setattr(
"agentci.integrations.codegraph.asyncio.create_subprocess_exec",
create_subprocess_exec,
)
install_recorder(monkeypatch, [FakeProcess()])
await CodeGraph().prepare(workspace)
@@ -99,12 +118,9 @@ async def test_reports_missing_executable(tmp_path: Path, monkeypatch: pytest.Mo
workspace = tmp_path / "repo"
workspace.mkdir()
async def create_subprocess_exec(*_args, **_kwargs):
raise FileNotFoundError(2, "No such file or directory", "codegraph")
monkeypatch.setattr(
"agentci.integrations.codegraph.asyncio.create_subprocess_exec",
create_subprocess_exec,
install_recorder(
monkeypatch,
[FileNotFoundError(2, "No such file or directory", "codegraph")],
)
with pytest.raises(CodeGraphError, match="Could not run CodeGraph:.*codegraph") as raised:
@@ -121,12 +137,9 @@ async def test_reports_nonzero_exit_with_bounded_non_utf8_stderr(
workspace.mkdir()
stderr = b"discarded-prefix" + (b"x" * 1200) + b"\xff useful-tail"
async def create_subprocess_exec(*_args, **_kwargs):
return FakeProcess(returncode=7, stderr=stderr)
monkeypatch.setattr(
"agentci.integrations.codegraph.asyncio.create_subprocess_exec",
create_subprocess_exec,
install_recorder(
monkeypatch,
[FakeProcess(returncode=7, stderr=stderr)],
)
with pytest.raises(CodeGraphError, match="codegraph init failed") as raised: