Skip to content

Commit

Permalink
capture: fix disabled()/global_and_fixture_disabled() enabling captur…
Browse files Browse the repository at this point in the history
…ing when it was disabled

The `CaptureManager.global_and_fixture_disabled()` context manager (and
`CaptureFixture.disabled()` which calls it) did `suspend(); ...;
resume()` but if the capturing was already suspended, the `resume()`
would resume it when it shouldn't.

This caused caused some messages to be swallowed when `--log-cli` is
used because it uses `global_and_fixture_disabled` when capturing is not
necessarily resumed.
  • Loading branch information
bluetech committed Aug 15, 2020
1 parent d426a79 commit e10eae8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/7148.bugfix.rst
@@ -0,0 +1 @@
Fixed ``--log-cli`` potentially causing unrelated ``print`` output to be swallowed.
24 changes: 21 additions & 3 deletions src/_pytest/capture.py
Expand Up @@ -592,7 +592,7 @@ def suspend_capturing(self, in_: bool = False) -> None:
self._in_suspended = True

def resume_capturing(self) -> None:
self._state = "resumed"
self._state = "started"
if self.out:
self.out.resume()
if self.err:
Expand All @@ -613,6 +613,10 @@ def stop_capturing(self) -> None:
if self.in_:
self.in_.done()

def is_started(self) -> bool:
"""Whether actively capturing -- not suspended or stopped."""
return self._state == "started"

def readouterr(self) -> CaptureResult[AnyStr]:
if self.out:
out = self.out.snap()
Expand Down Expand Up @@ -757,11 +761,19 @@ def resume_fixture(self) -> None:
@contextlib.contextmanager
def global_and_fixture_disabled(self) -> Generator[None, None, None]:
"""Context manager to temporarily disable global and current fixture capturing."""
self.suspend()
do_fixture = self._capture_fixture and self._capture_fixture._is_started()
if do_fixture:
self.suspend_fixture()
do_global = self._global_capturing and self._global_capturing.is_started()
if do_global:
self.suspend_global_capture()
try:
yield
finally:
self.resume()
if do_global:
self.resume_global_capture()
if do_fixture:
self.resume_fixture()

@contextlib.contextmanager
def item_capture(self, when: str, item: Item) -> Generator[None, None, None]:
Expand Down Expand Up @@ -871,6 +883,12 @@ def _resume(self) -> None:
if self._capture is not None:
self._capture.resume_capturing()

def _is_started(self) -> bool:
"""Whether actively capturing -- not disabled or closed."""
if self._capture is not None:
return self._capture.is_started()
return False

@contextlib.contextmanager
def disabled(self) -> Generator[None, None, None]:
"""Temporarily disable capturing while inside the ``with`` block."""
Expand Down

0 comments on commit e10eae8

Please sign in to comment.