Skip to content

Commit

Permalink
close() should never be called on an installed LogCapture()
Browse files Browse the repository at this point in the history
  • Loading branch information
cjw296 committed Jun 7, 2024
1 parent 25a3607 commit 2686329
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 9 additions & 0 deletions testfixtures/logcapture.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,15 @@ def __exit__(self, type, value, traceback):
self.uninstall()
self.ensure_checked()

def close(self):
super().close()
if self in self.instances:
raise AssertionError(
'LogCapture instance closed while still installed, '
'loggers captured:\n'
'%s' % ('\n'.join((str(i.names) for i in self.instances)))
)


class LogCaptureForDecorator(LogCapture):

Expand Down
19 changes: 15 additions & 4 deletions testfixtures/tests/test_logcapture.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from logging import getLogger, ERROR, Filter
from logging import getLogger, ERROR, Filter, shutdown
from textwrap import dedent
from unittest import TestCase
from warnings import catch_warnings

from testfixtures.shouldraise import ShouldAssert
from testfixtures.mock import Mock

from testfixtures import Replacer, LogCapture, compare, Replace
from testfixtures.mock import Mock
from testfixtures.shouldraise import ShouldAssert

root = getLogger()
one = getLogger('one')
Expand Down Expand Up @@ -371,6 +370,18 @@ def test_truthiness_when_empty(self):
handler = LogCapture(install=False)
assert handler
assert bool(handler)

def test_shutdown_while_installed(self):
with LogCapture():
with ShouldAssert(
'LogCapture instance closed while still installed, loggers captured:\n'
'(None,)'
):
shutdown()
# second shutdown should be fine:
shutdown()


class TestCheckPresent:

def test_order_matters_ok(self):
Expand Down

0 comments on commit 2686329

Please sign in to comment.