Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/aignostics_foundry_core/foundry.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ def set_context(ctx: FoundryContext) -> None:
_context = ctx


def reset_context() -> None:
"""Reset the process-level context singleton.

Clears the context set by :func:`set_context`, making :func:`get_context`
raise :exc:`RuntimeError` again until :func:`set_context` is called.
Intended for use in test teardown to prevent state leaking between tests.
"""
global _context # noqa: PLW0603
_context = None


def get_context() -> FoundryContext:
"""Return the global project context.

Expand Down
6 changes: 6 additions & 0 deletions tests/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def test_service_initialization(mock_client):
mock_client.assert_called_once()
```

> **Critical rule**: `@patch` is only acceptable for **external libraries and stdlib** (e.g.
> `sentry_sdk`, `importlib.metadata.entry_points`, `sys.exit`). Never patch symbols that live
> inside `aignostics_foundry_core` itself — instead set up the real environment so the code runs
> for real (use `set_context()` / `reset_context()`, set `request.app.state`, etc.) and mark
> those tests `@pytest.mark.integration`.

**Run locally**:

```bash
Expand Down
187 changes: 102 additions & 85 deletions tests/aignostics_foundry_core/api/auth_test.py

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions tests/aignostics_foundry_core/console_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from rich.console import Console

from aignostics_foundry_core.console import console
from aignostics_foundry_core.foundry import FoundryContext, set_context
from aignostics_foundry_core.foundry import FoundryContext, reset_context, set_context

EXPECTED_THEME_KEYS = ["success", "info", "warning", "error", "debug", "logging.level.info"]
CUSTOM_ENV_PREFIX = "TESTPROJ_"
Expand All @@ -18,11 +18,15 @@


@pytest.fixture(autouse=True)
def reset_context(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
"""Reset global _context to None before and after every test."""
monkeypatch.setattr("aignostics_foundry_core.foundry._context", None)
def _reset_context() -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
"""Reset global _context to None before and after every test.

Yields:
None
"""
reset_context()
yield
monkeypatch.setattr("aignostics_foundry_core.foundry._context", None)
reset_context()


class TestConsole:
Expand Down
Loading
Loading