Skip to content

Commit

Permalink
test tracer: add 2 tests, coverage for plugin tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
wahuneke committed Sep 22, 2023
1 parent 43624d9 commit 52d8e31
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions testing/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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",
]

0 comments on commit 52d8e31

Please sign in to comment.