* streamline hookrecording and global py._com.comregistry patching

* this fixes a reporting bug where events from inner test runs would
  mix with the outer test run

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-05-21 09:45:43 +02:00
parent dcee9bdd6e
commit a58292b473
7 changed files with 41 additions and 21 deletions

View File

@ -101,7 +101,7 @@ class Hooks:
def __init__(self, hookspecs, registry=None): def __init__(self, hookspecs, registry=None):
self._hookspecs = hookspecs self._hookspecs = hookspecs
if registry is None: if registry is None:
registry = comregistry registry = py._com.comregistry
self.registry = registry self.registry = registry
for name, method in vars(hookspecs).items(): for name, method in vars(hookspecs).items():
if name[:2] != "__": if name[:2] != "__":

View File

@ -117,6 +117,7 @@ class pytest_funcarg__mysetup:
tmp = request.config.mktemp(request.function.__name__, numbered=True) tmp = request.config.mktemp(request.function.__name__, numbered=True)
self.source = tmp.mkdir("source") self.source = tmp.mkdir("source")
self.dest = tmp.mkdir("dest") self.dest = tmp.mkdir("dest")
request.getfuncargvalue("_pytest") # to have patching of py._com.comregistry
class TestHRSync: class TestHRSync:
def test_hrsync_filter(self, mysetup): def test_hrsync_filter(self, mysetup):

View File

@ -174,7 +174,7 @@ class TestDSession:
session.senditems_load([item1, item2]) session.senditems_load([item1, item2])
node = session.item2nodes[item1] [0] node = session.item2nodes[item1] [0]
session.queueevent("pytest_testnodedown", node=node, error=None) session.queueevent("pytest_testnodedown", node=node, error=None)
reprec = testdir.getreportrecorder(session.pluginmanager) reprec = testdir.getreportrecorder(session)
print session.item2nodes print session.item2nodes
loopstate = session._initloopstate([]) loopstate = session._initloopstate([])
session.loop_once(loopstate) session.loop_once(loopstate)
@ -261,7 +261,7 @@ class TestDSession:
session.addnode(node) session.addnode(node)
loopstate = session._initloopstate([]) loopstate = session._initloopstate([])
loopstate.shuttingdown = True loopstate.shuttingdown = True
reprec = testdir.getreportrecorder(session.pluginmanager) reprec = testdir.getreportrecorder(session)
session.queueevent("pytest_itemtestreport", rep=run(item, node)) session.queueevent("pytest_itemtestreport", rep=run(item, node))
session.loop_once(loopstate) session.loop_once(loopstate)
assert not reprec.getcalls("pytest_testnodedown") assert not reprec.getcalls("pytest_testnodedown")
@ -282,7 +282,7 @@ class TestDSession:
dsel = session.filteritems([modcol]) dsel = session.filteritems([modcol])
assert dsel == [modcol] assert dsel == [modcol]
items = modcol.collect() items = modcol.collect()
hookrecorder = testdir.getreportrecorder(session.pluginmanager).hookrecorder hookrecorder = testdir.getreportrecorder(session).hookrecorder
remaining = session.filteritems(items) remaining = session.filteritems(items)
assert remaining == [] assert remaining == []
@ -354,7 +354,7 @@ class TestDSession:
""") """)
config = testdir.parseconfig('-d', p1, '--tx=popen') config = testdir.parseconfig('-d', p1, '--tx=popen')
dsession = DSession(config) dsession = DSession(config)
hookrecorder = testdir.getreportrecorder(config.pluginmanager).hookrecorder hookrecorder = testdir.getreportrecorder(config).hookrecorder
dsession.main([config.getfsnode(p1)]) dsession.main([config.getfsnode(p1)])
rep = hookrecorder.popcall("pytest_itemtestreport").rep rep = hookrecorder.popcall("pytest_itemtestreport").rep
assert rep.passed assert rep.passed

View File

@ -8,6 +8,7 @@ class pytest_funcarg__mysetup:
numbered=True) numbered=True)
self.source = basetemp.mkdir("source") self.source = basetemp.mkdir("source")
self.dest = basetemp.mkdir("dest") self.dest = basetemp.mkdir("dest")
request.getfuncargvalue("_pytest")
class TestNodeManager: class TestNodeManager:
@py.test.mark.xfail("consider / forbid implicit rsyncdirs?") @py.test.mark.xfail("consider / forbid implicit rsyncdirs?")
@ -107,7 +108,7 @@ class TestNodeManager:
config = py.test.config._reparse([source, '--debug']) config = py.test.config._reparse([source, '--debug'])
assert config.option.debug assert config.option.debug
nodemanager = NodeManager(config, specs) nodemanager = NodeManager(config, specs)
sorter = testdir.getreportrecorder(config.pluginmanager).hookrecorder sorter = testdir.getreportrecorder(config).hookrecorder
nodemanager.setup_nodes(putevent=[].append) nodemanager.setup_nodes(putevent=[].append)
for spec in nodemanager.gwmanager.specs: for spec in nodemanager.gwmanager.specs:
l = sorter.getcalls("pytest_trace") l = sorter.getcalls("pytest_trace")

View File

@ -6,11 +6,15 @@ def pytest_funcarg___pytest(request):
class PytestArg: class PytestArg:
def __init__(self, request): def __init__(self, request):
self.request = request self.request = request
self.monkeypatch = self.request.getfuncargvalue("monkeypatch")
self.comregistry = py._com.Registry()
self.monkeypatch.setattr(py._com, 'comregistry', self.comregistry)
def gethookrecorder(self, hookspecs, comregistry=None): def gethookrecorder(self, hookspecs, registry=None):
if comregistry is None: if registry is not None:
comregistry = self.request.config.pluginmanager.comregistry self.monkeypatch.setattr(py._com, 'comregistry', registry)
hookrecorder = HookRecorder(comregistry) self.comregistry = registry
hookrecorder = HookRecorder(self.comregistry)
hookrecorder.start_recording(hookspecs) hookrecorder.start_recording(hookspecs)
self.request.addfinalizer(hookrecorder.finish_recording) self.request.addfinalizer(hookrecorder.finish_recording)
return hookrecorder return hookrecorder
@ -111,6 +115,11 @@ def test_hookrecorder_basic():
assert call._name == "xyz" assert call._name == "xyz"
py.test.raises(ValueError, "rec.popcall('abc')") py.test.raises(ValueError, "rec.popcall('abc')")
reg = py._com.comregistry
def test_functional_default(testdir, _pytest):
assert _pytest.comregistry == py._com.comregistry
assert _pytest.comregistry != reg
def test_functional(testdir, linecomp): def test_functional(testdir, linecomp):
sorter = testdir.inline_runsource(""" sorter = testdir.inline_runsource("""
import py import py

View File

@ -7,7 +7,6 @@ import os
import inspect import inspect
from py.__.test import runner from py.__.test import runner
from py.__.test.config import Config as pytestConfig from py.__.test.config import Config as pytestConfig
from pytest__pytest import HookRecorder
import api import api
@ -40,6 +39,7 @@ class RunResult:
class TmpTestdir: class TmpTestdir:
def __init__(self, request): def __init__(self, request):
self.request = request self.request = request
self._pytest = request.getfuncargvalue("_pytest")
# XXX remove duplication with tmpdir plugin # XXX remove duplication with tmpdir plugin
basetmp = request.config.ensuretemp("testdir") basetmp = request.config.ensuretemp("testdir")
name = request.function.__name__ name = request.function.__name__
@ -73,13 +73,22 @@ class TmpTestdir:
if hasattr(self, '_olddir'): if hasattr(self, '_olddir'):
self._olddir.chdir() self._olddir.chdir()
def getreportrecorder(self, registry): def getreportrecorder(self, obj):
sorter = ReportRecorder(registry) if isinstance(obj, py._com.Registry):
sorter.hookrecorder = HookRecorder(registry) registry = obj
sorter.hookrecorder.start_recording(api.PluginHooks) elif hasattr(obj, 'comregistry'):
sorter.hook = sorter.hookrecorder.hook registry = obj.comregistry
self.request.addfinalizer(sorter.hookrecorder.finish_recording) elif hasattr(obj, 'pluginmanager'):
return sorter registry = obj.pluginmanager.comregistry
elif hasattr(obj, 'config'):
registry = obj.config.pluginmanager.comregistry
else:
raise ValueError("obj %r provides no comregistry" %(obj,))
assert isinstance(registry, py._com.Registry)
reprec = ReportRecorder(registry)
reprec.hookrecorder = self._pytest.gethookrecorder(api.PluginHooks, registry)
reprec.hook = reprec.hookrecorder.hook
return reprec
def chdir(self): def chdir(self):
old = self.tmpdir.chdir() old = self.tmpdir.chdir()
@ -130,7 +139,7 @@ class TmpTestdir:
#config = self.parseconfig(*args) #config = self.parseconfig(*args)
config = self.parseconfig(*args) config = self.parseconfig(*args)
session = config.initsession() session = config.initsession()
rec = self.getreportrecorder(config.pluginmanager) rec = self.getreportrecorder(config)
colitems = [config.getfsnode(arg) for arg in config.args] colitems = [config.getfsnode(arg) for arg in config.args]
items = list(session.genitems(colitems)) items = list(session.genitems(colitems))
return items, rec return items, rec
@ -152,7 +161,7 @@ class TmpTestdir:
config = self.parseconfig(*args) config = self.parseconfig(*args)
config.pluginmanager.do_configure(config) config.pluginmanager.do_configure(config)
session = config.initsession() session = config.initsession()
sorter = self.getreportrecorder(config.pluginmanager) sorter = self.getreportrecorder(config)
session.main() session.main()
config.pluginmanager.do_unconfigure(config) config.pluginmanager.do_unconfigure(config)
return sorter return sorter

View File

@ -208,7 +208,7 @@ class TestRunnerPlugin:
item = testdir.getitem("""def test_func(): pass""") item = testdir.getitem("""def test_func(): pass""")
plugin = RunnerPlugin() plugin = RunnerPlugin()
plugin.pytest_configure(item.config) plugin.pytest_configure(item.config)
sorter = testdir.getreportrecorder(item.config.pluginmanager) sorter = testdir.getreportrecorder(item)
plugin.pytest_item_setup_and_runtest(item) plugin.pytest_item_setup_and_runtest(item)
rep = sorter.getcall("pytest_itemtestreport").rep rep = sorter.getcall("pytest_itemtestreport").rep
assert rep.passed assert rep.passed