Skip to content

Commit

Permalink
CaptureResult: typing, repr
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Mar 8, 2020
1 parent 69919e8 commit 56de2ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/_pytest/capture.py
Expand Up @@ -2,7 +2,6 @@
per-test stdout/stderr capturing mechanism.
"""
import collections
import contextlib
import io
import os
Expand All @@ -12,7 +11,9 @@
from typing import BinaryIO
from typing import Generator
from typing import Iterable
from typing import NamedTuple
from typing import Optional
from typing import Union

import pytest
from _pytest.compat import CaptureAndPassthroughIO
Expand Down Expand Up @@ -439,7 +440,11 @@ def __getattr__(self, name):
return getattr(object.__getattribute__(self, "buffer"), name)


CaptureResult = collections.namedtuple("CaptureResult", ["out", "err"])
# XXX: Generic[AnyStr], and AnyStr as type directly does not work?
# Would be good to ensure out and err are of the same type.
class CaptureResult(NamedTuple):
out: Union[str, bytes]
err: Union[str, bytes]


class MultiCapture:
Expand Down
22 changes: 22 additions & 0 deletions testing/test_capture.py
Expand Up @@ -16,6 +16,7 @@
from _pytest import capture
from _pytest.capture import _get_multicapture
from _pytest.capture import CaptureManager
from _pytest.capture import CaptureResult
from _pytest.capture import MultiCapture
from _pytest.config import ExitCode

Expand All @@ -40,6 +41,27 @@ def TeeStdCapture(out=True, err=True, in_=True):
return capture.MultiCapture(out, err, in_, Capture=capture.TeeSysCapture)


def test_CaptureResult() -> None:
with pytest.raises(TypeError):
CaptureResult() # type: ignore

cr = CaptureResult(out="out", err="err")
repr_ = repr(cr)
assert repr_ == "CaptureResult(out='out', err='err')"
assert str(cr) == repr_

cr = CaptureResult(out=b"out\nout2", err=b"err\nerr2")
repr_ = repr(cr)
assert repr_ == r"CaptureResult(out=b'out\nout2', err=b'err\nerr2')"
assert str(cr) == repr_

# XXX: should cause mypy type error?!
cr = CaptureResult(out="out", err=b"err")
repr_ = repr(cr)
assert repr_ == r"CaptureResult(out='out', err=b'err')"
assert str(cr) == repr_


class TestCaptureManager:
def test_getmethod_default_no_fd(self, monkeypatch):
from _pytest.capture import pytest_addoption
Expand Down

0 comments on commit 56de2ac

Please sign in to comment.