From 698c4e75fd6e5346cb8a90b61fa228ce79406e32 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 10 Apr 2019 23:07:29 +0200 Subject: [PATCH] capture: track current state in _state attributes This is meant for debugging, and making assertions later. --- src/_pytest/capture.py | 37 +++++++++++++++++++++++++++++++++---- testing/test_capture.py | 9 ++++----- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index 95d6e363e..25eab7fdf 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -456,6 +456,7 @@ CaptureResult = collections.namedtuple("CaptureResult", ["out", "err"]) class MultiCapture(object): out = err = in_ = None + _state = None def __init__(self, out=True, err=True, in_=True, Capture=None): if in_: @@ -466,9 +467,16 @@ class MultiCapture(object): self.err = Capture(2) def __repr__(self): - return "" % (self.out, self.err, self.in_) + return "" % ( + self.out, + self.err, + self.in_, + self._state, + getattr(self, "_in_suspended", ""), + ) def start_capturing(self): + self._state = "started" if self.in_: self.in_.start() if self.out: @@ -486,6 +494,7 @@ class MultiCapture(object): return out, err def suspend_capturing(self, in_=False): + self._state = "suspended" if self.out: self.out.suspend() if self.err: @@ -495,6 +504,7 @@ class MultiCapture(object): self._in_suspended = True def resume_capturing(self): + self._state = "resumed" if self.out: self.out.resume() if self.err: @@ -505,9 +515,9 @@ class MultiCapture(object): def stop_capturing(self): """ stop capturing and reset capturing streams """ - if hasattr(self, "_reset"): + if self._state == "stopped": raise ValueError("was already stopped") - self._reset = True + self._state = "stopped" if self.out: self.out.done() if self.err: @@ -535,6 +545,7 @@ class FDCaptureBinary(object): """ EMPTY_BUFFER = b"" + _state = None def __init__(self, targetfd, tmpfile=None): self.targetfd = targetfd @@ -561,9 +572,10 @@ class FDCaptureBinary(object): self.tmpfile_fd = tmpfile.fileno() def __repr__(self): - return "" % ( + return "" % ( self.targetfd, getattr(self, "targetfd_save", None), + self._state, ) def start(self): @@ -574,6 +586,7 @@ class FDCaptureBinary(object): raise ValueError("saved filedescriptor not valid anymore") os.dup2(self.tmpfile_fd, self.targetfd) self.syscapture.start() + self._state = "started" def snap(self): self.tmpfile.seek(0) @@ -590,14 +603,17 @@ class FDCaptureBinary(object): os.close(targetfd_save) self.syscapture.done() _attempt_to_close_capture_file(self.tmpfile) + self._state = "done" def suspend(self): self.syscapture.suspend() os.dup2(self.targetfd_save, self.targetfd) + self._state = "suspended" def resume(self): self.syscapture.resume() os.dup2(self.tmpfile_fd, self.targetfd) + self._state = "resumed" def writeorg(self, data): """ write to original file descriptor. """ @@ -625,6 +641,7 @@ class FDCapture(FDCaptureBinary): class SysCapture(object): EMPTY_BUFFER = str() + _state = None def __init__(self, fd, tmpfile=None): name = patchsysdict[fd] @@ -637,8 +654,17 @@ class SysCapture(object): tmpfile = CaptureIO() self.tmpfile = tmpfile + def __repr__(self): + return "" % ( + self.name, + self._old, + self.tmpfile, + self._state, + ) + def start(self): setattr(sys, self.name, self.tmpfile) + self._state = "started" def snap(self): res = self.tmpfile.getvalue() @@ -650,12 +676,15 @@ class SysCapture(object): setattr(sys, self.name, self._old) del self._old _attempt_to_close_capture_file(self.tmpfile) + self._state = "done" def suspend(self): setattr(sys, self.name, self._old) + self._state = "suspended" def resume(self): setattr(sys, self.name, self.tmpfile) + self._state = "resumed" def writeorg(self, data): self._old.write(data) diff --git a/testing/test_capture.py b/testing/test_capture.py index c3881128f..2b450c189 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -1243,25 +1243,24 @@ class TestStdCaptureFDinvalidFD(object): from _pytest import capture def StdCaptureFD(out=True, err=True, in_=True): - return capture.MultiCapture(out, err, in_, - Capture=capture.FDCapture) + return capture.MultiCapture(out, err, in_, Capture=capture.FDCapture) def test_stdout(): os.close(1) cap = StdCaptureFD(out=True, err=False, in_=False) - assert repr(cap.out) == "" + assert repr(cap.out) == "" cap.stop_capturing() def test_stderr(): os.close(2) cap = StdCaptureFD(out=False, err=True, in_=False) - assert repr(cap.err) == "" + assert repr(cap.err) == "" cap.stop_capturing() def test_stdin(): os.close(0) cap = StdCaptureFD(out=False, err=False, in_=True) - assert repr(cap.in_) == "" + assert repr(cap.in_) == "" cap.stop_capturing() """ )