Skip to content

Commit c15e794

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

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

testing/test_tracer.py

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

33
import pytest
44

5+
from pluggy import PluginManager, HookspecMarker, HookimplMarker
56
from pluggy._tracing import TagTracer
67

8+
hookspec = HookspecMarker("example")
9+
hookimpl = HookimplMarker("example")
10+
711

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

0 commit comments

Comments
 (0)