|
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 |
|
| 5 | +from pluggy import HookimplMarker |
| 6 | +from pluggy import HookspecMarker |
| 7 | +from pluggy import PluginManager |
5 | 8 | from pluggy._tracing import TagTracer
|
6 | 9 |
|
| 10 | +hookspec = HookspecMarker("example") |
| 11 | +hookimpl = HookimplMarker("example") |
| 12 | + |
7 | 13 |
|
8 | 14 | @pytest.fixture
|
9 | 15 | def rootlogger() -> TagTracer:
|
@@ -77,3 +83,79 @@ def test_setprocessor(rootlogger: TagTracer) -> None:
|
77 | 83 | log2("seen")
|
78 | 84 | tags, args = l2[0]
|
79 | 85 | 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