Skip to content

Commit 479123e

Browse files
wahunekeRonnyPfannschmidt
authored andcommitted
test tracer: add 2 tests, coverage for plugin tracing
1 parent 8706f55 commit 479123e

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

testing/test_tracer.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import pytest
22

3+
from pluggy import HookimplMarker
4+
from pluggy import HookspecMarker
5+
from pluggy import PluginManager
36
from pluggy._tracing import TagTracer
47

58

9+
hookspec = HookspecMarker("example")
10+
hookimpl = HookimplMarker("example")
11+
12+
613
@pytest.fixture
714
def rootlogger() -> TagTracer:
815
return TagTracer()
@@ -75,3 +82,79 @@ def test_setprocessor(rootlogger: TagTracer) -> None:
7582
log2("seen")
7683
tags, args = l2[0]
7784
assert args == ("seen",)
85+
86+
87+
def test_plugin_tracing(pm: PluginManager) -> None:
88+
class Api:
89+
@hookspec
90+
def hello(self, arg: object) -> None:
91+
"api hook 1"
92+
93+
pm.add_hookspecs(Api)
94+
hook = pm.hook
95+
test_hc = hook.hello
96+
97+
class Plugin:
98+
@hookimpl
99+
def hello(self, arg):
100+
return arg + 1
101+
102+
plugin = Plugin()
103+
104+
trace_out: list[str] = []
105+
pm.trace.root.setwriter(trace_out.append)
106+
pm.register(plugin)
107+
pm.enable_tracing()
108+
109+
out = test_hc(arg=3)
110+
assert out == [4]
111+
112+
assert trace_out == [
113+
" hello [hook]\n arg: 3\n",
114+
" finish hello --> [4] [hook]\n",
115+
]
116+
117+
118+
def test_dbl_plugin_tracing(pm: PluginManager) -> None:
119+
class Api:
120+
@hookspec
121+
def hello(self, arg: object) -> None:
122+
"api hook 1"
123+
124+
pm.add_hookspecs(Api)
125+
hook = pm.hook
126+
test_hc = hook.hello
127+
128+
class Plugin:
129+
@hookimpl
130+
def hello(self, arg):
131+
return arg + 1
132+
133+
@hookimpl(specname="hello")
134+
def hello_again(self, arg):
135+
return arg + 100
136+
137+
plugin = Plugin()
138+
139+
trace_out: list[str] = []
140+
pm.trace.root.setwriter(trace_out.append)
141+
pm.register(plugin)
142+
pm.enable_tracing()
143+
144+
out = test_hc(arg=3)
145+
assert out == [103, 4]
146+
147+
assert trace_out == [
148+
" hello [hook]\n arg: 3\n",
149+
" finish hello --> [103, 4] [hook]\n",
150+
]
151+
152+
trace_out.clear()
153+
pm.unregister(plugin)
154+
out = test_hc(arg=3)
155+
assert out == []
156+
157+
assert trace_out == [
158+
" hello [hook]\n arg: 3\n",
159+
" finish hello --> [] [hook]\n",
160+
]

0 commit comments

Comments
 (0)