simplify some capturing tests

This commit is contained in:
holger krekel 2014-03-28 07:55:07 +01:00
parent 859915dc5e
commit 0b340aa1f6
1 changed files with 62 additions and 79 deletions

View File

@ -757,32 +757,35 @@ class TestFDCapture:
class TestStdCapture: class TestStdCapture:
captureclass = staticmethod(StdCapture)
@contextlib.contextmanager
def getcapture(self, **kw): def getcapture(self, **kw):
cap = StdCapture(**kw) cap = self.__class__.captureclass(**kw)
cap.start_capturing() cap.start_capturing()
return cap try:
yield cap
finally:
cap.stop_capturing()
def test_capturing_done_simple(self): def test_capturing_done_simple(self):
cap = self.getcapture() with self.getcapture() as cap:
sys.stdout.write("hello") sys.stdout.write("hello")
sys.stderr.write("world") sys.stderr.write("world")
out, err = cap.readouterr() out, err = cap.readouterr()
cap.stop_capturing()
assert out == "hello" assert out == "hello"
assert err == "world" assert err == "world"
def test_capturing_reset_simple(self): def test_capturing_reset_simple(self):
cap = self.getcapture() with self.getcapture() as cap:
print("hello world") print("hello world")
sys.stderr.write("hello error\n") sys.stderr.write("hello error\n")
out, err = cap.readouterr() out, err = cap.readouterr()
cap.stop_capturing()
assert out == "hello world\n" assert out == "hello world\n"
assert err == "hello error\n" assert err == "hello error\n"
def test_capturing_readouterr(self): def test_capturing_readouterr(self):
cap = self.getcapture() with self.getcapture() as cap:
try:
print ("hello world") print ("hello world")
sys.stderr.write("hello error\n") sys.stderr.write("hello error\n")
out, err = cap.readouterr() out, err = cap.readouterr()
@ -790,34 +793,27 @@ class TestStdCapture:
assert err == "hello error\n" assert err == "hello error\n"
sys.stderr.write("error2") sys.stderr.write("error2")
out, err = cap.readouterr() out, err = cap.readouterr()
finally:
cap.stop_capturing()
assert err == "error2" assert err == "error2"
def test_capturing_readouterr_unicode(self): def test_capturing_readouterr_unicode(self):
cap = self.getcapture() with self.getcapture() as cap:
try:
print ("hx\xc4\x85\xc4\x87") print ("hx\xc4\x85\xc4\x87")
out, err = cap.readouterr() out, err = cap.readouterr()
finally:
cap.stop_capturing()
assert out == py.builtin._totext("hx\xc4\x85\xc4\x87\n", "utf8") assert out == py.builtin._totext("hx\xc4\x85\xc4\x87\n", "utf8")
@pytest.mark.skipif('sys.version_info >= (3,)', @pytest.mark.skipif('sys.version_info >= (3,)',
reason='text output different for bytes on python3') reason='text output different for bytes on python3')
def test_capturing_readouterr_decode_error_handling(self): def test_capturing_readouterr_decode_error_handling(self):
cap = self.getcapture() with self.getcapture() as cap:
# triggered a internal error in pytest # triggered a internal error in pytest
print('\xa6') print('\xa6')
out, err = cap.readouterr() out, err = cap.readouterr()
assert out == py.builtin._totext('\ufffd\n', 'unicode-escape') assert out == py.builtin._totext('\ufffd\n', 'unicode-escape')
cap.stop_capturing()
def test_reset_twice_error(self): def test_reset_twice_error(self):
cap = self.getcapture() with self.getcapture() as cap:
print ("hello") print ("hello")
out, err = cap.readouterr() out, err = cap.readouterr()
cap.stop_capturing()
pytest.raises(ValueError, cap.stop_capturing) pytest.raises(ValueError, cap.stop_capturing)
assert out == "hello\n" assert out == "hello\n"
assert not err assert not err
@ -825,55 +821,49 @@ class TestStdCapture:
def test_capturing_modify_sysouterr_in_between(self): def test_capturing_modify_sysouterr_in_between(self):
oldout = sys.stdout oldout = sys.stdout
olderr = sys.stderr olderr = sys.stderr
cap = self.getcapture() with self.getcapture() as cap:
sys.stdout.write("hello") sys.stdout.write("hello")
sys.stderr.write("world") sys.stderr.write("world")
sys.stdout = capture.TextIO() sys.stdout = capture.TextIO()
sys.stderr = capture.TextIO() sys.stderr = capture.TextIO()
print ("not seen") print ("not seen")
sys.stderr.write("not seen\n") sys.stderr.write("not seen\n")
out, err = cap.readouterr() out, err = cap.readouterr()
cap.stop_capturing()
assert out == "hello" assert out == "hello"
assert err == "world" assert err == "world"
assert sys.stdout == oldout assert sys.stdout == oldout
assert sys.stderr == olderr assert sys.stderr == olderr
def test_capturing_error_recursive(self): def test_capturing_error_recursive(self):
cap1 = self.getcapture() with self.getcapture() as cap1:
print ("cap1") print ("cap1")
cap2 = self.getcapture() with self.getcapture() as cap2:
print ("cap2") print ("cap2")
out2, err2 = cap2.readouterr() out2, err2 = cap2.readouterr()
out1, err1 = cap1.readouterr() out1, err1 = cap1.readouterr()
cap2.stop_capturing()
cap1.stop_capturing()
assert out1 == "cap1\n" assert out1 == "cap1\n"
assert out2 == "cap2\n" assert out2 == "cap2\n"
def test_just_out_capture(self): def test_just_out_capture(self):
cap = self.getcapture(out=True, err=False) with self.getcapture(out=True, err=False) as cap:
sys.stdout.write("hello") sys.stdout.write("hello")
sys.stderr.write("world") sys.stderr.write("world")
out, err = cap.readouterr() out, err = cap.readouterr()
cap.stop_capturing()
assert out == "hello" assert out == "hello"
assert not err assert not err
def test_just_err_capture(self): def test_just_err_capture(self):
cap = self.getcapture(out=False, err=True) with self.getcapture(out=False, err=True) as cap:
sys.stdout.write("hello") sys.stdout.write("hello")
sys.stderr.write("world") sys.stderr.write("world")
out, err = cap.readouterr() out, err = cap.readouterr()
cap.stop_capturing()
assert err == "world" assert err == "world"
assert not out assert not out
def test_stdin_restored(self): def test_stdin_restored(self):
old = sys.stdin old = sys.stdin
cap = self.getcapture(in_=True) with self.getcapture(in_=True) as cap:
newstdin = sys.stdin newstdin = sys.stdin
cap.stop_capturing()
assert newstdin != sys.stdin assert newstdin != sys.stdin
assert sys.stdin is old assert sys.stdin is old
@ -881,18 +871,13 @@ class TestStdCapture:
print ("XXX this test may well hang instead of crashing") print ("XXX this test may well hang instead of crashing")
print ("XXX which indicates an error in the underlying capturing") print ("XXX which indicates an error in the underlying capturing")
print ("XXX mechanisms") print ("XXX mechanisms")
cap = self.getcapture() with self.getcapture() as cap:
pytest.raises(IOError, "sys.stdin.read()") pytest.raises(IOError, "sys.stdin.read()")
cap.stop_capturing()
class TestStdCaptureFD(TestStdCapture): class TestStdCaptureFD(TestStdCapture):
pytestmark = needsosdup pytestmark = needsosdup
captureclass = staticmethod(StdCaptureFD)
def getcapture(self, **kw):
cap = StdCaptureFD(**kw)
cap.start_capturing()
return cap
def test_simple_only_fd(self, testdir): def test_simple_only_fd(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
@ -909,17 +894,16 @@ class TestStdCaptureFD(TestStdCapture):
""") """)
def test_intermingling(self): def test_intermingling(self):
cap = self.getcapture() with self.getcapture() as cap:
oswritebytes(1, "1") oswritebytes(1, "1")
sys.stdout.write(str(2)) sys.stdout.write(str(2))
sys.stdout.flush() sys.stdout.flush()
oswritebytes(1, "3") oswritebytes(1, "3")
oswritebytes(2, "a") oswritebytes(2, "a")
sys.stderr.write("b") sys.stderr.write("b")
sys.stderr.flush() sys.stderr.flush()
oswritebytes(2, "c") oswritebytes(2, "c")
out, err = cap.readouterr() out, err = cap.readouterr()
cap.stop_capturing()
assert out == "123" assert out == "123"
assert err == "abc" assert err == "abc"
@ -930,7 +914,6 @@ class TestStdCaptureFD(TestStdCapture):
cap.stop_capturing() cap.stop_capturing()
class TestStdCaptureFDinvalidFD: class TestStdCaptureFDinvalidFD:
pytestmark = needsosdup pytestmark = needsosdup