diff --git a/py/io/test/test_fdcapture.py b/py/io/test/test_fdcapture.py new file mode 100644 index 000000000..eee7a1579 --- /dev/null +++ b/py/io/test/test_fdcapture.py @@ -0,0 +1,59 @@ +import os, sys +import py + +class TestFDCapture: + def test_basic(self): + tmpfile = py.std.os.tmpfile() + fd = tmpfile.fileno() + cap = py.io.FDCapture(fd) + os.write(fd, "hello") + f = cap.done() + s = f.read() + assert s == "hello" + + def test_stderr(self): + cap = py.io.FDCapture(2) + cap.setasfile('stderr') + print >>sys.stderr, "hello" + f = cap.done() + s = f.read() + assert s == "hello\n" + + def test_stdin(self): + f = os.tmpfile() + print >>f, "3" + f.seek(0) + cap = py.io.FDCapture(0, tmpfile=f) + # check with os.read() directly instead of raw_input(), because + # sys.stdin itself may be redirected (as py.test now does by default) + x = os.read(0, 100).strip() + f = cap.done() + assert x == "3" + + def test_writeorg(self): + tmppath = py.test.ensuretemp('test_writeorg').ensure('stderr', + file=True) + tmpfp = tmppath.open('w+b') + try: + cap = py.io.FDCapture(tmpfp.fileno()) + print >>tmpfp, 'foo' + cap.writeorg('bar\n') + finally: + tmpfp.close() + f = cap.done() + scap = f.read() + assert scap == 'foo\n' + stmp = tmppath.read() + assert stmp == "bar\n" + + def test_writeorg_wrongtype(self): + tmppath = py.test.ensuretemp('test_writeorg').ensure('stdout', + file=True) + tmpfp = tmppath.open('r') + try: + cap = py.io.FDCapture(tmpfp.fileno()) + py.test.raises(IOError, "cap.writeorg('bar\\n')") + finally: + tmpfp.close() + f = cap.done() + diff --git a/py/io/test/test_capture.py b/py/io/test/test_stdcapture.py similarity index 68% rename from py/io/test/test_capture.py rename to py/io/test/test_stdcapture.py index 565c6561c..c2f815e99 100644 --- a/py/io/test/test_capture.py +++ b/py/io/test/test_stdcapture.py @@ -1,62 +1,6 @@ import os, sys import py -class TestFDCapture: - def test_basic(self): - tmpfile = py.std.os.tmpfile() - fd = tmpfile.fileno() - cap = py.io.FDCapture(fd) - os.write(fd, "hello") - f = cap.done() - s = f.read() - assert s == "hello" - - def test_stderr(self): - cap = py.io.FDCapture(2) - cap.setasfile('stderr') - print >>sys.stderr, "hello" - f = cap.done() - s = f.read() - assert s == "hello\n" - - def test_stdin(self): - f = os.tmpfile() - print >>f, "3" - f.seek(0) - cap = py.io.FDCapture(0, tmpfile=f) - # check with os.read() directly instead of raw_input(), because - # sys.stdin itself may be redirected (as py.test now does by default) - x = os.read(0, 100).strip() - f = cap.done() - assert x == "3" - - def test_writeorg(self): - tmppath = py.test.ensuretemp('test_writeorg').ensure('stderr', - file=True) - tmpfp = tmppath.open('w+b') - try: - cap = py.io.FDCapture(tmpfp.fileno()) - print >>tmpfp, 'foo' - cap.writeorg('bar\n') - finally: - tmpfp.close() - f = cap.done() - scap = f.read() - assert scap == 'foo\n' - stmp = tmppath.read() - assert stmp == "bar\n" - - def test_writeorg_wrongtype(self): - tmppath = py.test.ensuretemp('test_writeorg').ensure('stdout', - file=True) - tmpfp = tmppath.open('r') - try: - cap = py.io.FDCapture(tmpfp.fileno()) - py.test.raises(IOError, "cap.writeorg('bar\\n')") - finally: - tmpfp.close() - f = cap.done() - class TestStdCapture: def getcapture(self, **kw): return py.io.StdCapture(**kw)