capture: remove some indirection in MultiCapture

Removing this indirection enables some further clean ups.
This commit is contained in:
Ran Benita 2020-04-12 20:47:28 +03:00
parent 54ae27f081
commit 491239d9b2
3 changed files with 39 additions and 25 deletions

View File

@ -71,13 +71,13 @@ def pytest_load_initial_conftests(early_config: Config):
def _get_multicapture(method: "_CaptureMethod") -> "MultiCapture":
if method == "fd":
return MultiCapture(out=True, err=True, Capture=FDCapture)
return MultiCapture(in_=FDCapture(0), out=FDCapture(1), err=FDCapture(2))
elif method == "sys":
return MultiCapture(out=True, err=True, Capture=SysCapture)
return MultiCapture(in_=SysCapture(0), out=SysCapture(1), err=SysCapture(2))
elif method == "no":
return MultiCapture(out=False, err=False, in_=False)
return MultiCapture(in_=None, out=None, err=None)
elif method == "tee-sys":
return MultiCapture(out=True, err=True, in_=False, Capture=TeeSysCapture)
return MultiCapture(in_=None, out=TeeSysCapture(1), err=TeeSysCapture(2))
raise ValueError("unknown capturing method: {!r}".format(method))
@ -354,7 +354,7 @@ class CaptureFixture:
def _start(self):
if self._capture is None:
self._capture = MultiCapture(
out=True, err=True, in_=False, Capture=self.captureclass
in_=None, out=self.captureclass(1), err=self.captureclass(2),
)
self._capture.start_capturing()
@ -418,17 +418,13 @@ CaptureResult = collections.namedtuple("CaptureResult", ["out", "err"])
class MultiCapture:
out = err = in_ = None
_state = None
_in_suspended = False
def __init__(self, out=True, err=True, in_=True, Capture=None):
if in_:
self.in_ = Capture(0)
if out:
self.out = Capture(1)
if err:
self.err = Capture(2)
def __init__(self, in_, out, err) -> None:
self.in_ = in_
self.out = out
self.err = err
def __repr__(self):
return "<MultiCapture out={!r} err={!r} in_={!r} _state={!r} _in_suspended={!r}>".format(

View File

@ -25,8 +25,7 @@ import py
import pytest
from _pytest._code import Source
from _pytest.capture import MultiCapture
from _pytest.capture import SysCapture
from _pytest.capture import _get_multicapture
from _pytest.compat import TYPE_CHECKING
from _pytest.config import _PluggyPlugin
from _pytest.config import Config
@ -972,7 +971,7 @@ class Testdir:
if syspathinsert:
self.syspathinsert()
now = time.time()
capture = MultiCapture(Capture=SysCapture)
capture = _get_multicapture("sys")
capture.start_capturing()
try:
try:

View File

@ -19,16 +19,28 @@ from _pytest.config import ExitCode
# pylib 1.4.20.dev2 (rev 13d9af95547e)
def StdCaptureFD(out=True, err=True, in_=True):
return capture.MultiCapture(out, err, in_, Capture=capture.FDCapture)
def StdCaptureFD(out: bool = True, err: bool = True, in_: bool = True) -> MultiCapture:
return capture.MultiCapture(
in_=capture.FDCapture(0) if in_ else None,
out=capture.FDCapture(1) if out else None,
err=capture.FDCapture(2) if err else None,
)
def StdCapture(out=True, err=True, in_=True):
return capture.MultiCapture(out, err, in_, Capture=capture.SysCapture)
def StdCapture(out: bool = True, err: bool = True, in_: bool = True) -> MultiCapture:
return capture.MultiCapture(
in_=capture.SysCapture(0) if in_ else None,
out=capture.SysCapture(1) if out else None,
err=capture.SysCapture(2) if err else None,
)
def TeeStdCapture(out=True, err=True, in_=True):
return capture.MultiCapture(out, err, in_, Capture=capture.TeeSysCapture)
def TeeStdCapture(out: bool = True, err: bool = True, in_: bool = True) -> MultiCapture:
return capture.MultiCapture(
in_=capture.TeeSysCapture(0) if in_ else None,
out=capture.TeeSysCapture(1) if out else None,
err=capture.TeeSysCapture(2) if err else None,
)
class TestCaptureManager:
@ -1154,7 +1166,11 @@ class TestStdCaptureFDinvalidFD:
from _pytest import capture
def StdCaptureFD(out=True, err=True, in_=True):
return capture.MultiCapture(out, err, in_, Capture=capture.FDCapture)
return capture.MultiCapture(
in_=capture.FDCapture(0) if in_ else None,
out=capture.FDCapture(1) if out else None,
err=capture.FDCapture(2) if err else None,
)
def test_stdout():
os.close(1)
@ -1284,8 +1300,11 @@ def test_capturing_and_logging_fundamentals(testdir, method):
import sys, os
import py, logging
from _pytest import capture
cap = capture.MultiCapture(out=False, in_=False,
Capture=capture.%s)
cap = capture.MultiCapture(
in_=None,
out=None,
err=capture.%s(2),
)
cap.start_capturing()
logging.warning("hello1")