Skip to content

Commit faeb07b

Browse files
olivermeyerclaude
andcommitted
fix(console): use ctx.env_prefix for width env var
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4b6e1f7 commit faeb07b

2 files changed

Lines changed: 45 additions & 8 deletions

File tree

src/aignostics_foundry_core/console.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@
99
def _get_console() -> Console:
1010
"""Get a themed rich console.
1111
12-
The console width can be set via the AIGNOSTICS_CONSOLE_WIDTH environment variable.
12+
The console width is controlled by ``{env_prefix}CONSOLE_WIDTH`` when a
13+
``FoundryContext`` is available (e.g. ``MYPROJECT_CONSOLE_WIDTH``).
14+
If no context has been set the width defaults to Rich's auto-detection.
1315
1416
Returns:
1517
Console: The themed rich console.
1618
"""
19+
try:
20+
from aignostics_foundry_core.foundry import get_context # noqa: PLC0415
21+
22+
env_var = f"{get_context().env_prefix}CONSOLE_WIDTH"
23+
width: int | None = int(os.environ.get(env_var, "0")) or None
24+
except RuntimeError:
25+
width = None
26+
1727
return Console(
1828
theme=Theme({
1929
"logging.level.info": "purple4",
@@ -23,7 +33,7 @@ def _get_console() -> Console:
2333
"warning": "yellow1",
2434
"error": "red1",
2535
}),
26-
width=int(os.environ.get("AIGNOSTICS_CONSOLE_WIDTH", "0")) or None, # TODO(oliverm): use ctx.env_prefix
36+
width=width,
2737
legacy_windows=False, # Modern Windows (10+) doesn't need width adjustment
2838
)
2939

tests/aignostics_foundry_core/console_test.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@
22

33
import importlib
44
import sys
5+
from collections.abc import Generator
56

67
import pytest
78
from rich.console import Console
89

910
from aignostics_foundry_core.console import console
11+
from aignostics_foundry_core.foundry import FoundryContext, set_context
1012

1113
EXPECTED_THEME_KEYS = ["success", "info", "warning", "error", "debug", "logging.level.info"]
14+
CUSTOM_ENV_PREFIX = "TESTPROJ_"
15+
CUSTOM_WIDTH = "120"
16+
EXPECTED_CUSTOM_WIDTH = 120
17+
18+
19+
@pytest.fixture(autouse=True)
20+
def reset_context(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
21+
"""Reset global _context to None before and after every test."""
22+
monkeypatch.setattr("aignostics_foundry_core.foundry._context", None)
23+
yield
24+
monkeypatch.setattr("aignostics_foundry_core.foundry._context", None)
1225

1326

1427
class TestConsole:
@@ -22,18 +35,32 @@ def test_console_is_console_instance(self) -> None:
2235
@pytest.mark.unit
2336
@pytest.mark.sequential
2437
def test_console_default_width(self, monkeypatch: pytest.MonkeyPatch) -> None:
25-
"""Module-level console has Rich's default width (80) when env var is not set."""
26-
monkeypatch.delenv("AIGNOSTICS_CONSOLE_WIDTH", raising=False)
38+
"""Module-level console has Rich's default width (80) when no context is set."""
2739
reloaded = importlib.reload(sys.modules["aignostics_foundry_core.console"])
2840
assert reloaded.console.width == 80
2941

3042
@pytest.mark.unit
3143
@pytest.mark.sequential
32-
def test_console_custom_width(self, monkeypatch: pytest.MonkeyPatch) -> None:
33-
"""Module-level console uses width from AIGNOSTICS_CONSOLE_WIDTH env var."""
34-
monkeypatch.setenv("AIGNOSTICS_CONSOLE_WIDTH", "100")
44+
def test_console_width_uses_env_prefix_from_context(self, monkeypatch: pytest.MonkeyPatch) -> None:
45+
"""Console width is read from {env_prefix}CONSOLE_WIDTH when a context is set."""
46+
ctx = FoundryContext(
47+
name="testproj",
48+
version="0.0.0",
49+
version_full="0.0.0",
50+
environment="test",
51+
env_prefix=CUSTOM_ENV_PREFIX,
52+
)
53+
set_context(ctx)
54+
monkeypatch.setenv(f"{CUSTOM_ENV_PREFIX}CONSOLE_WIDTH", CUSTOM_WIDTH)
3555
reloaded = importlib.reload(sys.modules["aignostics_foundry_core.console"])
36-
assert reloaded.console.width == 100
56+
assert reloaded.console.width == EXPECTED_CUSTOM_WIDTH
57+
58+
@pytest.mark.unit
59+
@pytest.mark.sequential
60+
def test_console_width_is_auto_when_no_context(self, monkeypatch: pytest.MonkeyPatch) -> None:
61+
"""Console width defaults to Rich's auto-detection (80 in non-TTY) when no context is set."""
62+
reloaded = importlib.reload(sys.modules["aignostics_foundry_core.console"])
63+
assert reloaded.console.width == 80
3764

3865
@pytest.mark.unit
3966
def test_console_theme_contains_expected_keys(self) -> None:

0 commit comments

Comments
 (0)