[svn r38474] make io capturing configurable per e.g.
conf_iocapture = "sys" by default it has "fd" for performing fd-based capturing --HG-- branch : trunk
This commit is contained in:
parent
245aa057b4
commit
5c000e57f1
|
@ -362,15 +362,10 @@ class Module(FSCollector, PyCollectorMixin):
|
|||
return res
|
||||
|
||||
def startcapture(self):
|
||||
if not self._config.option.nocapture:
|
||||
assert not hasattr(self, '_capture')
|
||||
self._capture = py.io.StdCaptureFD()
|
||||
self._config._startcapture(self, path=self.fspath)
|
||||
|
||||
def finishcapture(self):
|
||||
if hasattr(self, '_capture'):
|
||||
capture = self._capture
|
||||
del self._capture
|
||||
self._captured_out, self._captured_err = capture.reset()
|
||||
self._config._finishcapture(self)
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s %r>" % (self.__class__.__name__, self.name)
|
||||
|
|
|
@ -252,6 +252,24 @@ class Config(object):
|
|||
%(chain[0], self.topdir))
|
||||
return relpath, tuple([x.name for x in chain[1:]])
|
||||
|
||||
def _startcapture(self, colitem, path=None):
|
||||
if not self.option.nocapture:
|
||||
assert not hasattr(colitem, '_capture')
|
||||
iocapture = self.getvalue("conf_iocapture", path=path)
|
||||
if iocapture == "fd":
|
||||
capture = py.io.StdCaptureFD()
|
||||
elif iocapture == "sys":
|
||||
capture = py.io.StdCapture()
|
||||
else:
|
||||
raise ValueError("unknown io capturing: " + iocapture)
|
||||
colitem._capture = capture
|
||||
|
||||
def _finishcapture(self, colitem):
|
||||
if hasattr(colitem, '_capture'):
|
||||
capture = colitem._capture
|
||||
del colitem._capture
|
||||
colitem._captured_out, colitem._captured_err = capture.reset()
|
||||
|
||||
# this is the one per-process instance of py.test configuration
|
||||
config_per_process = Config()
|
||||
|
||||
|
|
|
@ -8,8 +8,7 @@ Generator = py.test.collect.Generator
|
|||
Function = py.test.Function
|
||||
Instance = py.test.collect.Instance
|
||||
|
||||
additionalinfo = None
|
||||
|
||||
conf_iocapture = "fd" # overridable from conftest.py
|
||||
|
||||
# ===================================================
|
||||
# Distributed testing specific options
|
||||
|
|
|
@ -32,15 +32,10 @@ class SetupState(object):
|
|||
|
||||
class Item(py.test.collect.Collector):
|
||||
def startcapture(self):
|
||||
if not self._config.option.nocapture:
|
||||
self._capture = py.io.StdCaptureFD()
|
||||
self._config._startcapture(self, path=self.fspath)
|
||||
|
||||
def finishcapture(self):
|
||||
if hasattr(self, '_capture'):
|
||||
capture = self._capture
|
||||
del self._capture
|
||||
self._captured_out, self._captured_err = capture.reset()
|
||||
|
||||
self._config._finishcapture(self)
|
||||
|
||||
class Function(Item):
|
||||
""" a Function Item is responsible for setting up
|
||||
|
|
|
@ -289,6 +289,30 @@ class TestSessionAndOptions:
|
|||
assert pl[0] == tmpdir
|
||||
assert pl[1] == somepath
|
||||
|
||||
def test_config_iocapturing(self):
|
||||
self.tmpdir
|
||||
config = py.test.config._reparse([self.tmpdir])
|
||||
assert config.getvalue("conf_iocapture")
|
||||
tmpdir = self.tmpdir.ensure("sub-with-conftest", dir=1)
|
||||
tmpdir.join("conftest.py").write(py.code.Source("""
|
||||
conf_iocapture = "sys"
|
||||
"""))
|
||||
config = py.test.config._reparse([tmpdir])
|
||||
assert config.getvalue("conf_iocapture") == "sys"
|
||||
class dummy: pass
|
||||
config._startcapture(dummy)
|
||||
print 42
|
||||
py.std.os.write(1, "23")
|
||||
config._finishcapture(dummy)
|
||||
assert dummy._captured_out.strip() == "42"
|
||||
|
||||
config = py.test.config._reparse([tmpdir.dirpath()])
|
||||
config._startcapture(dummy, path=tmpdir)
|
||||
print 42
|
||||
py.std.os.write(1, "23")
|
||||
config._finishcapture(dummy)
|
||||
assert dummy._captured_out.strip() == "42"
|
||||
|
||||
class TestConfigColitems:
|
||||
def setup_class(cls):
|
||||
cls.tmproot = py.test.ensuretemp(cls.__name__)
|
||||
|
|
|
@ -194,10 +194,10 @@ class TestTerminalSession:
|
|||
import py
|
||||
class Function(py.test.Function):
|
||||
def startcapture(self):
|
||||
self._mycapture = py.io.StdCaptureFD()
|
||||
self._mycapture = None
|
||||
|
||||
def finishcapture(self):
|
||||
self._testmycapture = self._mycapture.reset()
|
||||
self._testmycapture = None
|
||||
"""))
|
||||
session = self.mainsession(o)
|
||||
l = session.getitemoutcomepairs(Passed)
|
||||
|
@ -205,9 +205,6 @@ class TestTerminalSession:
|
|||
item = l[0][0]
|
||||
assert hasattr(item, '_testmycapture')
|
||||
print item._testmycapture
|
||||
out, err = item._testmycapture
|
||||
assert int(out.strip()) == 42
|
||||
assert int(err.strip()) == 23
|
||||
|
||||
assert isinstance(item.parent, py.test.collect.Module)
|
||||
out, err = item.parent._getouterr()
|
||||
|
|
Loading…
Reference in New Issue