[svn r37741] monster checking for

* unifying IO capturing methods
* py.io.StdCapture and py.io.StdCaptureFD
  (and both have a classmethod 'call' that is
  a shortcut for capturing output while
  executing a function)
* removing lots of duplicate code
* providing some examples in py/doc/io.txt

at least tests on win32 and linux seem
to pass all for me.

--HG--
branch : trunk
This commit is contained in:
hpk
2007-02-01 16:20:39 +01:00
parent d9572239a8
commit b706ec2f95
18 changed files with 126 additions and 178 deletions
+5 -5
View File
@@ -59,7 +59,7 @@ class TestFDCapture:
class TestCapturing:
def getcapture(self):
return py.io.OutErrCapture()
return py.io.StdCaptureFD()
def test_capturing_simple(self):
cap = self.getcapture()
@@ -120,13 +120,13 @@ def test_callcapture():
print >>py.std.sys.stderr, y
return 42
res, out, err = py.io.callcapture(func, 3, y=4)
res, out, err = py.io.StdCaptureFD.call(func, 3, y=4)
assert res == 42
assert out.startswith("3")
assert err.startswith("4")
def test_just_out_capture():
cap = py.io.OutErrCapture(out=True, err=False)
cap = py.io.StdCaptureFD(out=True, err=False)
print >>sys.stdout, "hello"
print >>sys.stderr, "world"
out, err = cap.reset()
@@ -134,7 +134,7 @@ def test_just_out_capture():
assert not err
def test_just_err_capture():
cap = py.io.OutErrCapture(out=False, err=True)
cap = py.io.StdCaptureFD(out=False, err=True)
print >>sys.stdout, "hello"
print >>sys.stderr, "world"
out, err = cap.reset()
@@ -142,7 +142,7 @@ def test_just_err_capture():
assert not out
def test_capture_no_sys():
cap = py.io.OutErrCapture(patchsys=False)
cap = py.io.StdCaptureFD(patchsys=False)
print >>sys.stdout, "hello"
print >>sys.stderr, "world"
os.write(1, "1")
+67
View File
@@ -0,0 +1,67 @@
import os, sys
import py
class TestCapturingOnSys:
def getcapture(self):
return py.io.StdCapture()
def test_capturing_simple(self):
cap = self.getcapture()
print "hello world"
print >>sys.stderr, "hello error"
out, err = cap.reset()
assert out == "hello world\n"
assert err == "hello error\n"
def test_capturing_twice_error(self):
cap = self.getcapture()
print "hello"
cap.reset()
py.test.raises(AttributeError, "cap.reset()")
def test_capturing_modify_sysouterr_in_between(self):
oldout = sys.stdout
olderr = sys.stderr
cap = self.getcapture()
print "hello",
print >>sys.stderr, "world",
sys.stdout = py.std.StringIO.StringIO()
sys.stderr = py.std.StringIO.StringIO()
print "not seen"
print >>sys.stderr, "not seen"
out, err = cap.reset()
assert out == "hello"
assert err == "world"
assert sys.stdout == oldout
assert sys.stderr == olderr
def test_capturing_error_recursive(self):
cap1 = self.getcapture()
print "cap1"
cap2 = self.getcapture()
print "cap2"
out2, err2 = cap2.reset()
py.test.raises(AttributeError, "cap2.reset()")
out1, err1 = cap1.reset()
assert out1 == "cap1\n"
assert out2 == "cap2\n"
def test_reading_stdin_while_captured_doesnt_hang(self):
cap = self.getcapture()
try:
py.test.raises(IOError, raw_input)
finally:
cap.reset()
def test_callcapture_nofd():
def func(x, y):
print x
print >>py.std.sys.stderr, y
return 42
res, out, err = py.io.StdCapture.call(func, 3, y=4)
assert res == 42
assert out.startswith("3")
assert err.startswith("4")