From a58292b4731c769fc13f28b8d7c29eef93a5f6b8 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 21 May 2009 09:45:43 +0200 Subject: [PATCH] * 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 --- py/_com.py | 2 +- py/execnet/testing/test_gwmanage.py | 1 + py/test/dist/testing/test_dsession.py | 8 +++---- py/test/dist/testing/test_nodemanage.py | 3 ++- py/test/plugin/pytest__pytest.py | 17 +++++++++++---- py/test/plugin/pytest_pytester.py | 29 ++++++++++++++++--------- py/test/plugin/pytest_runner.py | 2 +- 7 files changed, 41 insertions(+), 21 deletions(-) diff --git a/py/_com.py b/py/_com.py index 6c8f83d0b..48d13b17d 100644 --- a/py/_com.py +++ b/py/_com.py @@ -101,7 +101,7 @@ class Hooks: def __init__(self, hookspecs, registry=None): self._hookspecs = hookspecs if registry is None: - registry = comregistry + registry = py._com.comregistry self.registry = registry for name, method in vars(hookspecs).items(): if name[:2] != "__": diff --git a/py/execnet/testing/test_gwmanage.py b/py/execnet/testing/test_gwmanage.py index 8940ab55a..d2d63bdbd 100644 --- a/py/execnet/testing/test_gwmanage.py +++ b/py/execnet/testing/test_gwmanage.py @@ -117,6 +117,7 @@ class pytest_funcarg__mysetup: tmp = request.config.mktemp(request.function.__name__, numbered=True) self.source = tmp.mkdir("source") self.dest = tmp.mkdir("dest") + request.getfuncargvalue("_pytest") # to have patching of py._com.comregistry class TestHRSync: def test_hrsync_filter(self, mysetup): diff --git a/py/test/dist/testing/test_dsession.py b/py/test/dist/testing/test_dsession.py index b712751f2..0c237b263 100644 --- a/py/test/dist/testing/test_dsession.py +++ b/py/test/dist/testing/test_dsession.py @@ -174,7 +174,7 @@ class TestDSession: session.senditems_load([item1, item2]) node = session.item2nodes[item1] [0] session.queueevent("pytest_testnodedown", node=node, error=None) - reprec = testdir.getreportrecorder(session.pluginmanager) + reprec = testdir.getreportrecorder(session) print session.item2nodes loopstate = session._initloopstate([]) session.loop_once(loopstate) @@ -261,7 +261,7 @@ class TestDSession: session.addnode(node) loopstate = session._initloopstate([]) loopstate.shuttingdown = True - reprec = testdir.getreportrecorder(session.pluginmanager) + reprec = testdir.getreportrecorder(session) session.queueevent("pytest_itemtestreport", rep=run(item, node)) session.loop_once(loopstate) assert not reprec.getcalls("pytest_testnodedown") @@ -282,7 +282,7 @@ class TestDSession: dsel = session.filteritems([modcol]) assert dsel == [modcol] items = modcol.collect() - hookrecorder = testdir.getreportrecorder(session.pluginmanager).hookrecorder + hookrecorder = testdir.getreportrecorder(session).hookrecorder remaining = session.filteritems(items) assert remaining == [] @@ -354,7 +354,7 @@ class TestDSession: """) config = testdir.parseconfig('-d', p1, '--tx=popen') dsession = DSession(config) - hookrecorder = testdir.getreportrecorder(config.pluginmanager).hookrecorder + hookrecorder = testdir.getreportrecorder(config).hookrecorder dsession.main([config.getfsnode(p1)]) rep = hookrecorder.popcall("pytest_itemtestreport").rep assert rep.passed diff --git a/py/test/dist/testing/test_nodemanage.py b/py/test/dist/testing/test_nodemanage.py index f4e554d8a..05a9cb6f7 100644 --- a/py/test/dist/testing/test_nodemanage.py +++ b/py/test/dist/testing/test_nodemanage.py @@ -8,6 +8,7 @@ class pytest_funcarg__mysetup: numbered=True) self.source = basetemp.mkdir("source") self.dest = basetemp.mkdir("dest") + request.getfuncargvalue("_pytest") class TestNodeManager: @py.test.mark.xfail("consider / forbid implicit rsyncdirs?") @@ -107,7 +108,7 @@ class TestNodeManager: config = py.test.config._reparse([source, '--debug']) assert config.option.debug nodemanager = NodeManager(config, specs) - sorter = testdir.getreportrecorder(config.pluginmanager).hookrecorder + sorter = testdir.getreportrecorder(config).hookrecorder nodemanager.setup_nodes(putevent=[].append) for spec in nodemanager.gwmanager.specs: l = sorter.getcalls("pytest_trace") diff --git a/py/test/plugin/pytest__pytest.py b/py/test/plugin/pytest__pytest.py index 4959c7b41..86cc58d99 100644 --- a/py/test/plugin/pytest__pytest.py +++ b/py/test/plugin/pytest__pytest.py @@ -6,11 +6,15 @@ def pytest_funcarg___pytest(request): class PytestArg: def __init__(self, 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): - if comregistry is None: - comregistry = self.request.config.pluginmanager.comregistry - hookrecorder = HookRecorder(comregistry) + def gethookrecorder(self, hookspecs, registry=None): + if registry is not None: + self.monkeypatch.setattr(py._com, 'comregistry', registry) + self.comregistry = registry + hookrecorder = HookRecorder(self.comregistry) hookrecorder.start_recording(hookspecs) self.request.addfinalizer(hookrecorder.finish_recording) return hookrecorder @@ -111,6 +115,11 @@ def test_hookrecorder_basic(): assert call._name == "xyz" 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): sorter = testdir.inline_runsource(""" import py diff --git a/py/test/plugin/pytest_pytester.py b/py/test/plugin/pytest_pytester.py index 4a243cd6d..fe0941c12 100644 --- a/py/test/plugin/pytest_pytester.py +++ b/py/test/plugin/pytest_pytester.py @@ -7,7 +7,6 @@ import os import inspect from py.__.test import runner from py.__.test.config import Config as pytestConfig -from pytest__pytest import HookRecorder import api @@ -40,6 +39,7 @@ class RunResult: class TmpTestdir: def __init__(self, request): self.request = request + self._pytest = request.getfuncargvalue("_pytest") # XXX remove duplication with tmpdir plugin basetmp = request.config.ensuretemp("testdir") name = request.function.__name__ @@ -73,13 +73,22 @@ class TmpTestdir: if hasattr(self, '_olddir'): self._olddir.chdir() - def getreportrecorder(self, registry): - sorter = ReportRecorder(registry) - sorter.hookrecorder = HookRecorder(registry) - sorter.hookrecorder.start_recording(api.PluginHooks) - sorter.hook = sorter.hookrecorder.hook - self.request.addfinalizer(sorter.hookrecorder.finish_recording) - return sorter + def getreportrecorder(self, obj): + if isinstance(obj, py._com.Registry): + registry = obj + elif hasattr(obj, 'comregistry'): + registry = obj.comregistry + elif hasattr(obj, 'pluginmanager'): + 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): old = self.tmpdir.chdir() @@ -130,7 +139,7 @@ class TmpTestdir: #config = self.parseconfig(*args) config = self.parseconfig(*args) session = config.initsession() - rec = self.getreportrecorder(config.pluginmanager) + rec = self.getreportrecorder(config) colitems = [config.getfsnode(arg) for arg in config.args] items = list(session.genitems(colitems)) return items, rec @@ -152,7 +161,7 @@ class TmpTestdir: config = self.parseconfig(*args) config.pluginmanager.do_configure(config) session = config.initsession() - sorter = self.getreportrecorder(config.pluginmanager) + sorter = self.getreportrecorder(config) session.main() config.pluginmanager.do_unconfigure(config) return sorter diff --git a/py/test/plugin/pytest_runner.py b/py/test/plugin/pytest_runner.py index 49d38336c..ad467bbdd 100644 --- a/py/test/plugin/pytest_runner.py +++ b/py/test/plugin/pytest_runner.py @@ -208,7 +208,7 @@ class TestRunnerPlugin: item = testdir.getitem("""def test_func(): pass""") plugin = RunnerPlugin() plugin.pytest_configure(item.config) - sorter = testdir.getreportrecorder(item.config.pluginmanager) + sorter = testdir.getreportrecorder(item) plugin.pytest_item_setup_and_runtest(item) rep = sorter.getcall("pytest_itemtestreport").rep assert rep.passed