Skip to content

Commit e96b2df

Browse files
committed
test(core): add atomic pytest unit tests for logging configuration
1 parent 11ecb86 commit e96b2df

7 files changed

Lines changed: 176 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import logging
2+
3+
import pytest
4+
5+
from CodeEntropy.core.logging import LoggingConfig
6+
7+
8+
@pytest.fixture(autouse=True)
9+
def _isolate_global_logging():
10+
"""
11+
LoggingConfig modifies global loggers. Keep tests atomic by clearing handlers
12+
after each test so tests don't leak state into each other.
13+
"""
14+
yield
15+
for name in ("", "commands", "MDAnalysis"):
16+
lg = logging.getLogger(name)
17+
lg.handlers.clear()
18+
lg.propagate = True
19+
20+
21+
@pytest.fixture()
22+
def config(tmp_path):
23+
return LoggingConfig(folder=str(tmp_path), level=logging.INFO)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import logging
2+
3+
4+
def test_configure_attaches_handlers(config):
5+
config.configure()
6+
7+
root = logging.getLogger()
8+
assert config.handlers["rich"] in root.handlers
9+
assert config.handlers["main"] in root.handlers
10+
assert config.handlers["error"] in root.handlers
11+
12+
13+
def test_configure_commands_logger_non_propagating_with_handler(config):
14+
config.configure()
15+
16+
commands_logger = logging.getLogger("commands")
17+
assert commands_logger.propagate is False
18+
assert config.handlers["command"] in commands_logger.handlers
19+
20+
21+
def test_configure_mdanalysis_logger_non_propagating_with_handler(config):
22+
config.configure()
23+
24+
mda_logger = logging.getLogger("MDAnalysis")
25+
assert mda_logger.propagate is False
26+
assert config.handlers["mdanalysis"] in mda_logger.handlers
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import logging
2+
3+
from CodeEntropy.core.logging import ErrorFilter
4+
5+
6+
def test_error_filter_allows_error_and_critical():
7+
f = ErrorFilter()
8+
9+
record_error = logging.LogRecord("x", logging.ERROR, "f.py", 1, "msg", (), None)
10+
record_crit = logging.LogRecord("x", logging.CRITICAL, "f.py", 1, "msg", (), None)
11+
12+
assert f.filter(record_error) is True
13+
assert f.filter(record_crit) is True
14+
15+
16+
def test_error_filter_blocks_below_error():
17+
f = ErrorFilter()
18+
19+
record_warn = logging.LogRecord("x", logging.WARNING, "f.py", 1, "msg", (), None)
20+
record_info = logging.LogRecord("x", logging.INFO, "f.py", 1, "msg", (), None)
21+
22+
assert f.filter(record_warn) is False
23+
assert f.filter(record_info) is False
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
from unittest.mock import MagicMock
3+
4+
5+
def test_export_console_writes_recorded_output(config):
6+
# Make export_text deterministic
7+
config.console.export_text = MagicMock(return_value="HELLO")
8+
9+
config.export_console("out.txt")
10+
11+
out_path = os.path.join(config.log_dir, "out.txt")
12+
assert os.path.exists(out_path)
13+
14+
with open(out_path, "r", encoding="utf-8") as f:
15+
assert f.read() == "HELLO"
16+
17+
config.console.export_text.assert_called_once()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from CodeEntropy.core.logging import LoggingConfig
2+
3+
4+
def test_get_console_returns_singleton():
5+
# Reset singleton to make the test independent
6+
LoggingConfig._console = None
7+
8+
c1 = LoggingConfig.get_console()
9+
c2 = LoggingConfig.get_console()
10+
11+
assert c1 is c2
12+
13+
14+
def test_get_console_records_output_enabled():
15+
LoggingConfig._console = None
16+
c = LoggingConfig.get_console()
17+
18+
# Rich Console uses 'record' attribute when recording is enabled
19+
assert getattr(c, "record", False) is True
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import logging
2+
import os
3+
4+
from rich.logging import RichHandler
5+
6+
from CodeEntropy.core.logging import LoggingConfig
7+
8+
9+
def test_init_creates_log_dir(tmp_path):
10+
cfg = LoggingConfig(folder=str(tmp_path))
11+
assert os.path.isdir(cfg.log_dir)
12+
13+
14+
def test_setup_handlers_creates_expected_handlers(config):
15+
assert set(config.handlers.keys()) == {
16+
"rich",
17+
"main",
18+
"error",
19+
"command",
20+
"mdanalysis",
21+
}
22+
23+
assert isinstance(config.handlers["rich"], RichHandler)
24+
assert isinstance(config.handlers["main"], logging.FileHandler)
25+
assert isinstance(config.handlers["error"], logging.FileHandler)
26+
assert isinstance(config.handlers["command"], logging.FileHandler)
27+
assert isinstance(config.handlers["mdanalysis"], logging.FileHandler)
28+
29+
30+
def test_handler_paths_match_expected_filenames(config):
31+
expected = {
32+
"main": "program.log",
33+
"error": "program.err",
34+
"command": "program.com",
35+
"mdanalysis": "mdanalysis.log",
36+
}
37+
38+
for handler_key, filename in expected.items():
39+
handler = config.handlers[handler_key]
40+
assert os.path.basename(handler.baseFilename) == filename
41+
assert os.path.dirname(handler.baseFilename) == config.log_dir
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import logging
2+
3+
4+
def test_set_level_updates_root_and_named_loggers(config):
5+
config.configure()
6+
7+
config.set_level(logging.DEBUG)
8+
9+
root = logging.getLogger()
10+
assert root.level == logging.DEBUG
11+
12+
assert logging.getLogger("commands").level == logging.DEBUG
13+
assert logging.getLogger("MDAnalysis").level == logging.DEBUG
14+
15+
16+
def test_set_level_sets_filehandlers_to_log_level_and_other_handlers_to_info(config):
17+
config.configure()
18+
19+
config.set_level(logging.DEBUG)
20+
21+
root = logging.getLogger()
22+
23+
for h in root.handlers:
24+
if isinstance(h, logging.FileHandler):
25+
assert h.level == logging.DEBUG
26+
else:
27+
assert h.level == logging.INFO

0 commit comments

Comments
 (0)