File tree Expand file tree Collapse file tree 3 files changed +50
-0
lines changed
Expand file tree Collapse file tree 3 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
2+ import logging
3+
4+ def main ():
5+ logfile = sys .argv [1 ]
6+ print ("main" )
7+ print (f"Handlers before basicConfig: { logging .root .handlers } " )
8+ logger = logging .getLogger (__name__ )
9+ logging .basicConfig (filename = logfile , encoding = 'utf-8' , level = logging .INFO )
10+ print (f"Handlers after basicConfig: { logging .root .handlers } " )
11+ logger .info ("This is an info message" )
12+
13+ if __name__ == "__main__" :
14+ main ()
Original file line number Diff line number Diff line change 1+ from pathlib import Path
2+ from cli_test_helpers import ArgvContext
3+ import logging
4+
5+ import cli_test_logging
6+
7+ def test_cli ():
8+ print (f"Handlers at test start: { logging .root .handlers } " )
9+ logfile = "test.log"
10+ with ArgvContext ('cli_test_logging.py' , logfile ):
11+ cli_test_logging .main ()
12+ print (f"Handlers after main: { logging .root .handlers } " )
13+ assert Path (logfile ).exists () # fails
Original file line number Diff line number Diff line change 1+ from pathlib import Path
2+ from cli_test_helpers import ArgvContext
3+ import logging
4+
5+ import cli_test_logging
6+
7+ def test_cli_with_workaround ():
8+ """Test with logging handlers reset"""
9+ # Save and clear existing handlers
10+ original_handlers = logging .root .handlers [:]
11+ logging .root .handlers .clear ()
12+
13+ logfile = "test.log"
14+ try :
15+ with ArgvContext ('cli_test_logging.py' , logfile ):
16+ cli_test_logging .main ()
17+ assert Path (logfile ).exists ()
18+ finally :
19+ # Restore original handlers
20+ logging .root .handlers = original_handlers
21+ # Clean up test file
22+ if Path (logfile ).exists ():
23+ Path (logfile ).unlink ()
You can’t perform that action at this time.
0 commit comments