Skip to content

Commit 25df363

Browse files
authored
test: Make tests/forges/test_github_api_url.py::test_with_no_setting_the_forge_calls_github deterministic (#983)
The tests come first and fail until the implementation. `agent_build_kit.settings.reload` writes a fresh read of the environment into the one process-wide `settings` object, in place, field by field (`src/agent_build_kit/settings.py:228`). Done when the doctor-with-setting test followed by the failing test passes in one process with no workers (on the base the second fails with `NotFound: POST /api/v3/repos/example/app/pulls`), no… Unit: fix-test-tests-forges-test-github-api-url-py-test-with-no-set-fb2349ad/1
1 parent a43bcb7 commit 25df363

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎tests/conftest.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from agent_build_kit import claude_account, forges, orchestration, sandboxes
3535
from agent_build_kit import config as config_module
3636
from agent_build_kit import installation as installation_module
37+
from agent_build_kit import settings as settings_module
3738
from agent_build_kit.cli import doctor
3839
from agent_build_kit.cli import pipeline as pipeline_cli
3940
from agent_build_kit.config import RepoConfig, WorkspaceConfig
@@ -83,6 +84,22 @@ def build_session(new_session_id: Callable[[], str]) -> str:
8384
return new_session_id()
8485

8586

87+
@pytest.fixture(autouse=True)
88+
def settings_restored() -> Iterator[None]:
89+
"""Every test ends with the settings it started with.
90+
91+
`settings.reload` writes the process-wide object in place, field by field, and loading an
92+
installation or running the doctor calls it. `monkeypatch.setenv` restores the environment
93+
but not the fields a reload wrote from it, so a later test on the same worker would read
94+
the earlier one's value."""
95+
before = settings_module.settings.model_dump()
96+
env_file = settings_module.loaded_env_file
97+
yield
98+
for name, value in before.items():
99+
setattr(settings_module.settings, name, value)
100+
settings_module.loaded_env_file = env_file
101+
102+
86103
@pytest.fixture(autouse=True)
87104
def no_checkout_env_file(monkeypatch: pytest.MonkeyPatch) -> None:
88105
"""A developer's ignored `.env` is not part of the suite: the lookup treats the checkout's

‎tests/forges/test_github_api_url.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
44
Read off the requests that reach the stand-in host (their URLs). The forge reads the
55
setting from the settings object; the doctor loads the installation, which re-reads
6-
the settings from the environment, so its tests set the environment variable.
6+
the settings from the environment, so its tests set the environment variable. The `token`
7+
fixture undoes that environment and reloads the settings afterwards, so a doctor test
8+
leaves nothing for a forge test.
79
"""
810

911
from __future__ import annotations
@@ -19,7 +21,7 @@
1921
from agent_build_kit.forges.base import RepoId
2022
from agent_build_kit.forges.github import GitHubForge
2123
from agent_build_kit.forges.transport import clear_credentials
22-
from agent_build_kit.settings import settings
24+
from agent_build_kit.settings import reload, settings
2325
from tests.cli.test_doctor import Answers, which_all
2426
from tests.factories import init_repo
2527
from tests.forges.conftest import GH_TOKEN
@@ -37,6 +39,8 @@ def token(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
3739
clear_credentials()
3840
yield
3941
clear_credentials()
42+
monkeypatch.undo()
43+
reload(None)
4044

4145

4246
def created() -> httpx.Response:

0 commit comments

Comments
 (0)