Skip to content

Commit 0ea1c60

Browse files
committed
test_fmt(fix[reset_logging]): Fix logging fixture to prevent test interference
why: Tests were failing when run after test_log.py because setup_logger() created StreamHandlers that bypassed pytest's log capture mechanism. what: - Update reset_logging fixture to clear handlers before AND after tests - Re-enable log propagation so pytest can capture logs properly - Add libvcs to list of loggers to clear - Make fixture autouse and remove unnecessary parameters from test methods - Match the pattern used in test_add.py and test_add_from_fs.py refs: Fixes all 7 failing tests in TestFormatConfigFile class
1 parent 9b0a7e7 commit 0ea1c60

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

tests/cli/test_fmt.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,29 @@
1515
from _pytest.logging import LogCaptureFixture
1616

1717

18-
@pytest.fixture
18+
@pytest.fixture(autouse=True)
1919
def reset_logging() -> t.Generator[None, None, None]:
2020
"""Reset logging handlers to prevent duplicate log messages in tests."""
21-
# Store original handlers
22-
original_handlers = logging.root.handlers[:]
23-
# Clear all handlers
24-
logging.root.handlers = []
21+
# Clear handlers from all CLI loggers before AND after test
22+
cli_loggers = [
23+
"vcspull",
24+
"vcspull.cli.add",
25+
"vcspull.cli.add_from_fs",
26+
"vcspull.cli.sync",
27+
"vcspull.cli.fmt",
28+
"libvcs",
29+
]
30+
for logger_name in cli_loggers:
31+
logger = logging.getLogger(logger_name)
32+
logger.handlers.clear()
33+
logger.propagate = True # Re-enable propagation so pytest can capture logs
34+
2535
yield
26-
# Restore original handlers
27-
logging.root.handlers = original_handlers
36+
37+
# Clear again after test
38+
for logger_name in cli_loggers:
39+
logger = logging.getLogger(logger_name)
40+
logger.handlers.clear()
2841

2942

3043
class TestNormalizeRepoConfig:
@@ -181,7 +194,6 @@ def test_format_file_no_write(
181194
self,
182195
tmp_path: pathlib.Path,
183196
caplog: LogCaptureFixture,
184-
reset_logging: None,
185197
) -> None:
186198
"""Test formatting without writing changes."""
187199
config_file = tmp_path / ".vcspull.yaml"
@@ -214,7 +226,6 @@ def test_format_file_with_write(
214226
self,
215227
tmp_path: pathlib.Path,
216228
caplog: LogCaptureFixture,
217-
reset_logging: None,
218229
) -> None:
219230
"""Test formatting with writing changes."""
220231
config_file = tmp_path / ".vcspull.yaml"
@@ -252,7 +263,6 @@ def test_already_formatted(
252263
self,
253264
tmp_path: pathlib.Path,
254265
caplog: LogCaptureFixture,
255-
reset_logging: None,
256266
) -> None:
257267
"""Test when file is already correctly formatted."""
258268
config_file = tmp_path / ".vcspull.yaml"
@@ -277,7 +287,6 @@ def test_nonexistent_file(
277287
self,
278288
tmp_path: pathlib.Path,
279289
caplog: LogCaptureFixture,
280-
reset_logging: None,
281290
) -> None:
282291
"""Test handling of nonexistent config file."""
283292
config_file = tmp_path / "nonexistent.yaml"
@@ -291,7 +300,6 @@ def test_invalid_yaml(
291300
self,
292301
tmp_path: pathlib.Path,
293302
caplog: LogCaptureFixture,
294-
reset_logging: None,
295303
) -> None:
296304
"""Test handling of invalid YAML."""
297305
config_file = tmp_path / ".vcspull.yaml"
@@ -307,7 +315,6 @@ def test_no_config_found(
307315
tmp_path: pathlib.Path,
308316
caplog: LogCaptureFixture,
309317
monkeypatch: pytest.MonkeyPatch,
310-
reset_logging: None,
311318
) -> None:
312319
"""Test when no config file is found."""
313320
monkeypatch.chdir(tmp_path)
@@ -321,7 +328,6 @@ def test_detailed_change_reporting(
321328
self,
322329
tmp_path: pathlib.Path,
323330
caplog: LogCaptureFixture,
324-
reset_logging: None,
325331
) -> None:
326332
"""Test detailed reporting of changes."""
327333
config_file = tmp_path / ".vcspull.yaml"

0 commit comments

Comments
 (0)