-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_logisolation.py
More file actions
117 lines (82 loc) · 4.14 KB
/
test_logisolation.py
File metadata and controls
117 lines (82 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""Tests for our tests helpers. 8-}."""
import logging
import tempfile
from pathlib import Path
from cli_test_helpers import ArgvContext, EnvironContext, RandomDirectoryContext
def test_argv_context_logging_isolation():
"""
Does ArgvContext isolate logging configuration?
Verifies that ArgvContext temporarily clears logging handlers, allowing
code under test to call logging.basicConfig() successfully even when
test frameworks have already configured logging.
"""
original_handlers = logging.root.handlers[:]
mock_handler = logging.NullHandler()
logging.root.handlers.append(mock_handler)
try:
assert mock_handler in logging.root.handlers, "Test setup failed"
with tempfile.TemporaryDirectory() as tmpdir:
logfile = Path(tmpdir) / "test.log"
with ArgvContext("test_script.py", str(logfile)):
handlers_before = logging.root.handlers[:]
assert not handlers_before, "Handlers not cleared"
logging.basicConfig(filename=str(logfile), level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info("Test message")
assert mock_handler in logging.root.handlers, "Handlers not restored"
assert logfile.exists(), "Log file should have been created"
log_content = logfile.read_text()
assert "Test message" in log_content, "Unexpected log file content"
finally:
logging.root.handlers = original_handlers
def test_environ_context_logging_isolation():
"""
Does EnvironContext isolate logging configuration?
Verifies that EnvironContext temporarily clears logging handlers, allowing
code under test to call logging.basicConfig() successfully even when test
frameworks have already configured logging.
"""
original_handlers = logging.root.handlers[:]
mock_handler = logging.NullHandler()
logging.root.handlers.append(mock_handler)
try:
assert mock_handler in logging.root.handlers, "Test setup failed"
with tempfile.TemporaryDirectory() as tmpdir:
logfile = Path(tmpdir) / "environ_test.log"
with EnvironContext(TEST_VAR="test_value"):
handlers_before = logging.root.handlers[:]
assert not handlers_before, "Handlers not cleared"
logging.basicConfig(filename=str(logfile), level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info("Test message from EnvironContext")
assert mock_handler in logging.root.handlers, "Handlers not restored"
assert logfile.exists(), "Log file should have been created"
log_content = logfile.read_text()
assert "Test message" in log_content, "Unexpected log file content"
finally:
logging.root.handlers = original_handlers
def test_random_directory_context_logging_isolation():
"""
Does RandomDirectoryContext isolate logging configuration?
Verifies that RandomDirectoryContext temporarily clears logging handlers,
allowing code under test to call logging.basicConfig() successfully even
when test frameworks have already configured logging.
"""
original_handlers = logging.root.handlers[:]
mock_handler = logging.NullHandler()
logging.root.handlers.append(mock_handler)
try:
assert mock_handler in logging.root.handlers, "Test setup failed"
with RandomDirectoryContext():
logfile = Path("random_dir_test.log")
handlers_before = logging.root.handlers[:]
assert not handlers_before, "Handlers not cleared"
logging.basicConfig(filename=str(logfile), level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info("Test message from RandomDirectoryContext")
assert logfile.exists(), "Log file should have been created"
log_content = logfile.read_text()
assert "Test message" in log_content, "Unexpected log file content"
assert mock_handler in logging.root.handlers, "Handlers not restored"
finally:
logging.root.handlers = original_handlers