* fix capturing and unicode printing in tests

* introduce "_encoding" to py/io/terminalwriter writing
* beautify a few __repr__ for better internal debugging

--HG--
branch : 1.0.x
This commit is contained in:
holger krekel
2009-08-06 14:34:19 +02:00
parent 91597f4100
commit 8fcdac9dd6
11 changed files with 133 additions and 53 deletions

View File

@@ -145,11 +145,9 @@ class SlaveNode(object):
if call.excinfo:
# likely it is not collectable here because of
# platform/import-dependency induced skips
# XXX somewhat ugly shortcuts - also makes a collection
# failure into an ItemTestReport - this might confuse
# pytest_runtest_logreport hooks
# we fake a setup-error report with the obtained exception
# and do not care about capturing or non-runner hooks
rep = self.runner.pytest_runtest_makereport(item=item, call=call)
self.pytest_runtest_logreport(rep)
return
item.config.hook.pytest_runtest_protocol(item=item)

View File

@@ -107,11 +107,24 @@ class CaptureManager:
def __init__(self):
self._method2capture = {}
def _maketempfile(self):
f = py.std.tempfile.TemporaryFile()
newf = py.io.dupfile(f)
f.close()
return ustream(newf)
def _makestringio(self):
return py.std.StringIO.StringIO()
def _startcapture(self, method):
if method == "fd":
return py.io.StdCaptureFD()
return py.io.StdCaptureFD(
out=self._maketempfile(), err=self._maketempfile()
)
elif method == "sys":
return py.io.StdCapture()
return py.io.StdCapture(
out=self._makestringio(), err=self._makestringio()
)
else:
raise ValueError("unknown capturing method: %r" % method)
@@ -252,3 +265,13 @@ class CaptureFuncarg:
def close(self):
self.capture.reset()
del self.capture
def ustream(f):
import codecs
encoding = getattr(f, 'encoding', None) or "UTF-8"
reader = codecs.getreader(encoding)
writer = codecs.getwriter(encoding)
srw = codecs.StreamReaderWriter(f, reader, writer)
srw.encoding = encoding
return srw

View File

@@ -108,6 +108,13 @@ class CallInfo:
except:
self.excinfo = py.code.ExceptionInfo()
def __repr__(self):
if self.excinfo:
status = "exception: %s" % str(self.excinfo.value)
else:
status = "result: %r" % (self.result,)
return "<CallInfo when=%r %s>" % (self.when, status)
def forked_run_report(item):
# for now, we run setup/teardown in the subprocess
# XXX optionally allow sharing of setup/teardown

View File

@@ -1,5 +1,5 @@
import py, os, sys
from py.__.test.plugin.pytest_capture import CaptureManager
from py.__.test.plugin.pytest_capture import CaptureManager, ustream
class TestCaptureManager:
@@ -54,6 +54,29 @@ class TestCaptureManager:
finally:
capouter.reset()
@py.test.mark.multi(method=['fd', 'sys'])
def test_capturing_unicode(testdir, method):
testdir.makepyfile("""
# taken from issue 227 from nosests
def test_unicode():
import sys
print sys.stdout
print u'b\\u00f6y'
""")
result = testdir.runpytest("--capture=%s" % method)
result.stdout.fnmatch_lines([
"*1 passed*"
])
def test_ustream_helper(testdir):
p = testdir.makepyfile("hello")
f = p.open('w')
#f.encoding = "utf8"
x = ustream(f)
x.write(u'b\\00f6y')
x.close()
def test_collect_capturing(testdir):
p = testdir.makepyfile("""
print "collect %s failure" % 13

View File

@@ -271,3 +271,13 @@ def test_functional_boxed(testdir):
"*1 failed*"
])
def test_callinfo():
ci = runner.CallInfo(lambda: 0, '123')
assert ci.when == "123"
assert ci.result == 0
assert "result" in repr(ci)
ci = runner.CallInfo(lambda: 0/0, '123')
assert ci.when == "123"
assert not hasattr(ci, 'result')
assert ci.excinfo
assert "exc" in repr(ci)