Rotary Logger  1.0.2
The middleware rotary logger
Loading...
Searching...
No Matches
conftest.py
Go to the documentation of this file.
1"""
2Pytest configuration and shared fixtures for rotary_logger tests.
3"""
4
5import sys
6
7import pytest
8from rotary_logger.tee_stream import TeeStream
9
10
11@pytest.fixture(autouse=True)
13 """Restore sys.stdout/stderr before and after each test for proper isolation.
14
15 When running with pytest -s (no capture), this fixture ensures that if any
16 test leaves sys.stdout or sys.stderr as TeeStream instances (indicating
17 cleanup failed), those streams are restored before the next test runs.
18 This prevents test isolation failures that are hidden when using pytest -q
19 (which has its own capture mechanism that resets streams).
20 """
21 stdout_before = sys.stdout
22 stderr_before = sys.stderr
23
24 # Check if previous test left broken streams and restore if needed
25 if isinstance(sys.stdout, TeeStream) or isinstance(sys.stderr, TeeStream):
26 # Get a reference to the original streams by checking pytest's internals
27 # If stdout/stderr are TeeStream, try to get the wrapped stream
28 if isinstance(sys.stdout, TeeStream):
29 sys.stdout = getattr(
30 sys.stdout, '_get_stream_if_present', lambda: sys.stdout)()
31 if isinstance(sys.stderr, TeeStream):
32 sys.stderr = getattr(
33 sys.stderr, '_get_stream_if_present', lambda: sys.stderr)()
34
35 yield
36
37 # After test, restore if it left broken streams
38 if isinstance(sys.stdout, TeeStream) or isinstance(sys.stderr, TeeStream):
39 sys.stdout = stdout_before
40 sys.stderr = stderr_before