capture: remove some indirection in MultiCapture
Removing this indirection enables some further clean ups.
This commit is contained in:
parent
54ae27f081
commit
491239d9b2
|
@ -71,13 +71,13 @@ def pytest_load_initial_conftests(early_config: Config):
|
||||||
|
|
||||||
def _get_multicapture(method: "_CaptureMethod") -> "MultiCapture":
|
def _get_multicapture(method: "_CaptureMethod") -> "MultiCapture":
|
||||||
if method == "fd":
|
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":
|
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":
|
elif method == "no":
|
||||||
return MultiCapture(out=False, err=False, in_=False)
|
return MultiCapture(in_=None, out=None, err=None)
|
||||||
elif method == "tee-sys":
|
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))
|
raise ValueError("unknown capturing method: {!r}".format(method))
|
||||||
|
|
||||||
|
|
||||||
|
@ -354,7 +354,7 @@ class CaptureFixture:
|
||||||
def _start(self):
|
def _start(self):
|
||||||
if self._capture is None:
|
if self._capture is None:
|
||||||
self._capture = MultiCapture(
|
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()
|
self._capture.start_capturing()
|
||||||
|
|
||||||
|
@ -418,17 +418,13 @@ CaptureResult = collections.namedtuple("CaptureResult", ["out", "err"])
|
||||||
|
|
||||||
|
|
||||||
class MultiCapture:
|
class MultiCapture:
|
||||||
out = err = in_ = None
|
|
||||||
_state = None
|
_state = None
|
||||||
_in_suspended = False
|
_in_suspended = False
|
||||||
|
|
||||||
def __init__(self, out=True, err=True, in_=True, Capture=None):
|
def __init__(self, in_, out, err) -> None:
|
||||||
if in_:
|
self.in_ = in_
|
||||||
self.in_ = Capture(0)
|
self.out = out
|
||||||
if out:
|
self.err = err
|
||||||
self.out = Capture(1)
|
|
||||||
if err:
|
|
||||||
self.err = Capture(2)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<MultiCapture out={!r} err={!r} in_={!r} _state={!r} _in_suspended={!r}>".format(
|
return "<MultiCapture out={!r} err={!r} in_={!r} _state={!r} _in_suspended={!r}>".format(
|
||||||
|
|
|
@ -25,8 +25,7 @@ import py
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from _pytest._code import Source
|
from _pytest._code import Source
|
||||||
from _pytest.capture import MultiCapture
|
from _pytest.capture import _get_multicapture
|
||||||
from _pytest.capture import SysCapture
|
|
||||||
from _pytest.compat import TYPE_CHECKING
|
from _pytest.compat import TYPE_CHECKING
|
||||||
from _pytest.config import _PluggyPlugin
|
from _pytest.config import _PluggyPlugin
|
||||||
from _pytest.config import Config
|
from _pytest.config import Config
|
||||||
|
@ -972,7 +971,7 @@ class Testdir:
|
||||||
if syspathinsert:
|
if syspathinsert:
|
||||||
self.syspathinsert()
|
self.syspathinsert()
|
||||||
now = time.time()
|
now = time.time()
|
||||||
capture = MultiCapture(Capture=SysCapture)
|
capture = _get_multicapture("sys")
|
||||||
capture.start_capturing()
|
capture.start_capturing()
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -19,16 +19,28 @@ from _pytest.config import ExitCode
|
||||||
# pylib 1.4.20.dev2 (rev 13d9af95547e)
|
# pylib 1.4.20.dev2 (rev 13d9af95547e)
|
||||||
|
|
||||||
|
|
||||||
def StdCaptureFD(out=True, err=True, in_=True):
|
def StdCaptureFD(out: bool = True, err: bool = True, in_: bool = True) -> MultiCapture:
|
||||||
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 StdCapture(out=True, err=True, in_=True):
|
def StdCapture(out: bool = True, err: bool = True, in_: bool = True) -> MultiCapture:
|
||||||
return capture.MultiCapture(out, err, in_, Capture=capture.SysCapture)
|
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):
|
def TeeStdCapture(out: bool = True, err: bool = True, in_: bool = True) -> MultiCapture:
|
||||||
return capture.MultiCapture(out, err, in_, Capture=capture.TeeSysCapture)
|
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:
|
class TestCaptureManager:
|
||||||
|
@ -1154,7 +1166,11 @@ class TestStdCaptureFDinvalidFD:
|
||||||
from _pytest import capture
|
from _pytest import capture
|
||||||
|
|
||||||
def StdCaptureFD(out=True, err=True, in_=True):
|
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():
|
def test_stdout():
|
||||||
os.close(1)
|
os.close(1)
|
||||||
|
@ -1284,8 +1300,11 @@ def test_capturing_and_logging_fundamentals(testdir, method):
|
||||||
import sys, os
|
import sys, os
|
||||||
import py, logging
|
import py, logging
|
||||||
from _pytest import capture
|
from _pytest import capture
|
||||||
cap = capture.MultiCapture(out=False, in_=False,
|
cap = capture.MultiCapture(
|
||||||
Capture=capture.%s)
|
in_=None,
|
||||||
|
out=None,
|
||||||
|
err=capture.%s(2),
|
||||||
|
)
|
||||||
cap.start_capturing()
|
cap.start_capturing()
|
||||||
|
|
||||||
logging.warning("hello1")
|
logging.warning("hello1")
|
||||||
|
|
Loading…
Reference in New Issue