|
| 1 | +import logging |
1 | 2 | import os |
2 | 3 | import shutil |
3 | 4 | from collections import namedtuple |
@@ -278,6 +279,33 @@ def test_startup_commands(executor): |
278 | 279 | # implement tests on executions of the startupcommands |
279 | 280 |
|
280 | 281 |
|
| 282 | +def test_initialize_logging_expands_user_log_file(monkeypatch, tmp_path): |
| 283 | + home = tmp_path / "home" |
| 284 | + log_file = home / ".cache" / "litecli" / "log" |
| 285 | + monkeypatch.setenv("HOME", str(home)) |
| 286 | + monkeypatch.setenv("USERPROFILE", str(home)) |
| 287 | + |
| 288 | + m = object.__new__(LiteCli) |
| 289 | + m.config = {"main": {"log_file": "~/.cache/litecli/log", "log_level": "INFO"}} |
| 290 | + echo_messages = [] |
| 291 | + m.echo = lambda *args, **kwargs: echo_messages.append((args, kwargs)) |
| 292 | + |
| 293 | + root_logger = logging.getLogger("litecli") |
| 294 | + original_handlers = list(root_logger.handlers) |
| 295 | + try: |
| 296 | + m.initialize_logging() |
| 297 | + |
| 298 | + added_handlers = [handler for handler in root_logger.handlers if handler not in original_handlers] |
| 299 | + assert log_file.exists() |
| 300 | + assert not echo_messages |
| 301 | + assert any(isinstance(handler, logging.FileHandler) and handler.baseFilename == str(log_file) for handler in added_handlers) |
| 302 | + finally: |
| 303 | + for handler in root_logger.handlers[:]: |
| 304 | + if handler not in original_handlers: |
| 305 | + root_logger.removeHandler(handler) |
| 306 | + handler.close() |
| 307 | + |
| 308 | + |
281 | 309 | @patch("litecli.main.datetime") # Adjust if your module path is different |
282 | 310 | def test_get_prompt(mock_datetime): |
283 | 311 | # We'll freeze time at 2025-01-20 13:37:42 for comedic effect. |
|
0 commit comments