|
6 | 6 | from pytest_mock import MockerFixture |
7 | 7 | from rich.logging import RichHandler |
8 | 8 |
|
9 | | -from log import get_logger, resolve_log_level, create_log_handler |
10 | | -from constants import LIGHTSPEED_STACK_LOG_LEVEL_ENV_VAR, DEFAULT_LOG_FORMAT |
| 9 | +from constants import ( |
| 10 | + DEFAULT_LOG_FORMAT, |
| 11 | + LIGHTSPEED_STACK_DISABLE_RICH_HANDLER_ENV_VAR, |
| 12 | + LIGHTSPEED_STACK_LOG_LEVEL_ENV_VAR, |
| 13 | +) |
| 14 | +from log import create_log_handler, get_logger, resolve_log_level |
11 | 15 |
|
12 | 16 |
|
13 | 17 | def test_get_logger() -> None: |
@@ -116,3 +120,36 @@ def test_create_log_handler_non_tty_format(mocker: MockerFixture) -> None: |
116 | 120 | assert handler.formatter is not None |
117 | 121 | # pylint: disable=protected-access |
118 | 122 | assert handler.formatter._fmt == DEFAULT_LOG_FORMAT |
| 123 | + |
| 124 | + |
| 125 | +def test_create_log_handler_disable_rich_with_tty( |
| 126 | + mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch |
| 127 | +) -> None: |
| 128 | + """Test that RichHandler is disabled when env var is set, even with TTY.""" |
| 129 | + mocker.patch("sys.stderr.isatty", return_value=True) |
| 130 | + monkeypatch.setenv(LIGHTSPEED_STACK_DISABLE_RICH_HANDLER_ENV_VAR, "1") |
| 131 | + handler = create_log_handler() |
| 132 | + assert isinstance(handler, logging.StreamHandler) |
| 133 | + assert not isinstance(handler, RichHandler) |
| 134 | + |
| 135 | + |
| 136 | +def test_create_log_handler_disable_rich_format( |
| 137 | + mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch |
| 138 | +) -> None: |
| 139 | + """Test that disabled RichHandler uses DEFAULT_LOG_FORMAT.""" |
| 140 | + mocker.patch("sys.stderr.isatty", return_value=True) |
| 141 | + monkeypatch.setenv(LIGHTSPEED_STACK_DISABLE_RICH_HANDLER_ENV_VAR, "true") |
| 142 | + handler = create_log_handler() |
| 143 | + assert handler.formatter is not None |
| 144 | + # pylint: disable=protected-access |
| 145 | + assert handler.formatter._fmt == DEFAULT_LOG_FORMAT |
| 146 | + |
| 147 | + |
| 148 | +def test_create_log_handler_enable_rich_when_env_var_empty( |
| 149 | + mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch |
| 150 | +) -> None: |
| 151 | + """Test that RichHandler is used when env var is empty string.""" |
| 152 | + mocker.patch("sys.stderr.isatty", return_value=True) |
| 153 | + monkeypatch.setenv(LIGHTSPEED_STACK_DISABLE_RICH_HANDLER_ENV_VAR, "") |
| 154 | + handler = create_log_handler() |
| 155 | + assert isinstance(handler, RichHandler) |
0 commit comments