From 52d8e318ae7dc455a7973b2985981f9665cae766 Mon Sep 17 00:00:00 2001 From: Bill Huneke Date: Thu, 21 Sep 2023 23:28:28 -0400 Subject: [PATCH] test tracer: add 2 tests, coverage for plugin tracing --- testing/test_tracer.py | 82 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/testing/test_tracer.py b/testing/test_tracer.py index 5e538369..5c2ad9be 100644 --- a/testing/test_tracer.py +++ b/testing/test_tracer.py @@ -2,8 +2,14 @@ import pytest +from pluggy import HookimplMarker +from pluggy import HookspecMarker +from pluggy import PluginManager from pluggy._tracing import TagTracer +hookspec = HookspecMarker("example") +hookimpl = HookimplMarker("example") + @pytest.fixture def rootlogger() -> TagTracer: @@ -77,3 +83,79 @@ def test_setprocessor(rootlogger: TagTracer) -> None: log2("seen") tags, args = l2[0] assert args == ("seen",) + + +def test_plugin_tracing(pm: PluginManager) -> None: + class Api: + @hookspec + def hello(self, arg: object) -> None: + "api hook 1" + + pm.add_hookspecs(Api) + hook = pm.hook + test_hc = hook.hello + + class Plugin: + @hookimpl + def hello(self, arg): + return arg + 1 + + plugin = Plugin() + + trace_out: List[str] = [] + pm.trace.root.setwriter(trace_out.append) + pm.register(plugin) + pm.enable_tracing() + + out = test_hc(arg=3) + assert out == [4] + + assert trace_out == [ + " hello [hook]\n arg: 3\n", + " finish hello --> [4] [hook]\n", + ] + + +def test_dbl_plugin_tracing(pm: PluginManager) -> None: + class Api: + @hookspec + def hello(self, arg: object) -> None: + "api hook 1" + + pm.add_hookspecs(Api) + hook = pm.hook + test_hc = hook.hello + + class Plugin: + @hookimpl + def hello(self, arg): + return arg + 1 + + @hookimpl(specname="hello") + def hello_again(self, arg): + return arg + 100 + + plugin = Plugin() + + trace_out: List[str] = [] + pm.trace.root.setwriter(trace_out.append) + pm.register(plugin) + pm.enable_tracing() + + out = test_hc(arg=3) + assert out == [103, 4] + + assert trace_out == [ + " hello [hook]\n arg: 3\n", + " finish hello --> [103, 4] [hook]\n", + ] + + trace_out.clear() + pm.unregister(plugin) + out = test_hc(arg=3) + assert out == [] + + assert trace_out == [ + " hello [hook]\n arg: 3\n", + " finish hello --> [] [hook]\n", + ]