Skip to content

Commit 52d8e31

Browse files
committed
test tracer: add 2 tests, coverage for plugin tracing
1 parent 43624d9 commit 52d8e31

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

testing/test_tracer.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import pytest
44

5+
from pluggy import HookimplMarker
6+
from pluggy import HookspecMarker
7+
from pluggy import PluginManager
58
from pluggy._tracing import TagTracer
69

10+
hookspec = HookspecMarker("example")
11+
hookimpl = HookimplMarker("example")
12+
713

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

0 commit comments

Comments
 (0)