Skip to content

Commit 4006cdb

Browse files
Copilotbittner
andcommitted
Add return self to __enter__ and clean up test code
Co-authored-by: bittner <[email protected]>
1 parent 7ae6e13 commit 4006cdb

File tree

2 files changed

+2
-13
lines changed

2 files changed

+2
-13
lines changed

cli_test_helpers/decorators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __enter__(self):
3636
sys.argv = self.args
3737
self._old_handlers = logging.root.handlers[:]
3838
logging.root.handlers.clear()
39+
return self
3940

4041
def __exit__(self, exc_type, exc_val, exc_tb):
4142
sys.argv = self._old

tests/test_decorators.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,50 +34,38 @@ def test_argv_context_logging_isolation():
3434
allowing code under test to call logging.basicConfig() successfully even
3535
when test frameworks have already configured logging.
3636
"""
37-
# Setup: Create a mock handler to simulate pytest's handlers
3837
original_handlers = logging.root.handlers[:]
3938
mock_handler = logging.NullHandler()
4039
logging.root.handlers.append(mock_handler)
4140

4241
try:
43-
# Verify the mock handler is present
4442
assert mock_handler in logging.root.handlers, "Test setup failed"
4543

4644
with tempfile.TemporaryDirectory() as tmpdir:
4745
logfile = Path(tmpdir) / "test.log"
4846

4947
with ArgvContext("test_script.py", str(logfile)):
50-
# Inside context: handlers should be cleared
5148
handlers_before = logging.root.handlers[:]
5249
assert len(handlers_before) == 0, (
5350
"Handlers should be cleared inside ArgvContext"
5451
)
5552

56-
# Configure logging (this should work now)
57-
logging.basicConfig(
58-
filename=str(logfile),
59-
level=logging.INFO,
60-
force=False, # Should work without force since handlers are cleared
61-
)
53+
logging.basicConfig(filename=str(logfile), level=logging.INFO)
6254

63-
# Log a message
6455
logger = logging.getLogger(__name__)
6556
logger.info("Test message")
6657

67-
# After context: handlers should be restored
6858
assert mock_handler in logging.root.handlers, (
6959
"Original handlers should be restored after ArgvContext"
7060
)
7161

72-
# Verify the log file was created and contains the message
7362
assert logfile.exists(), "Log file should have been created"
7463
log_content = logfile.read_text()
7564
assert "Test message" in log_content, (
7665
"Log file should contain the test message"
7766
)
7867

7968
finally:
80-
# Cleanup: Restore original handlers
8169
logging.root.handlers = original_handlers
8270

8371

0 commit comments

Comments
 (0)