From 5c000e57f13a423632c6f3f86e7c16da2ab5c83f Mon Sep 17 00:00:00 2001 From: hpk Date: Sun, 11 Feb 2007 17:25:01 +0100 Subject: [PATCH] [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 --- py/test/collect.py | 9 ++------- py/test/config.py | 18 ++++++++++++++++++ py/test/defaultconftest.py | 3 +-- py/test/item.py | 9 ++------- py/test/testing/test_config.py | 24 ++++++++++++++++++++++++ py/test/testing/test_session.py | 7 ++----- 6 files changed, 49 insertions(+), 21 deletions(-) diff --git a/py/test/collect.py b/py/test/collect.py index 8f23d6857..961a058da 100644 --- a/py/test/collect.py +++ b/py/test/collect.py @@ -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) diff --git a/py/test/config.py b/py/test/config.py index a0d757a64..cdcc51b9e 100644 --- a/py/test/config.py +++ b/py/test/config.py @@ -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() diff --git a/py/test/defaultconftest.py b/py/test/defaultconftest.py index 058c1b177..300585a91 100644 --- a/py/test/defaultconftest.py +++ b/py/test/defaultconftest.py @@ -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 diff --git a/py/test/item.py b/py/test/item.py index ed36c560b..1add3c54f 100644 --- a/py/test/item.py +++ b/py/test/item.py @@ -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 diff --git a/py/test/testing/test_config.py b/py/test/testing/test_config.py index 275f57503..e1e2db068 100644 --- a/py/test/testing/test_config.py +++ b/py/test/testing/test_config.py @@ -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__) diff --git a/py/test/testing/test_session.py b/py/test/testing/test_session.py index 5e3a39f81..013d185c9 100644 --- a/py/test/testing/test_session.py +++ b/py/test/testing/test_session.py @@ -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()