-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathtest_hookcaller.py
513 lines (387 loc) · 11.7 KB
/
test_hookcaller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
from collections.abc import Generator
from collections.abc import Sequence
from typing import Callable
from typing import TypeVar
import pytest
from pluggy import HookimplMarker
from pluggy import HookspecMarker
from pluggy import PluginManager
from pluggy import PluginValidationError
from pluggy._hooks import HookCaller
from pluggy._hooks import HookImpl
hookspec = HookspecMarker("example")
hookimpl = HookimplMarker("example")
@pytest.fixture
def hc(pm: PluginManager) -> HookCaller:
class Hooks:
@hookspec
def he_method1(self, arg: object) -> None:
pass
pm.add_hookspecs(Hooks)
return pm.hook.he_method1
FuncT = TypeVar("FuncT", bound=Callable[..., object])
class AddMeth:
def __init__(self, hc: HookCaller) -> None:
self.hc = hc
def __call__(
self,
tryfirst: bool = False,
trylast: bool = False,
hookwrapper: bool = False,
wrapper: bool = False,
) -> Callable[[FuncT], FuncT]:
def wrap(func: FuncT) -> FuncT:
hookimpl(
tryfirst=tryfirst,
trylast=trylast,
hookwrapper=hookwrapper,
wrapper=wrapper,
)(func)
self.hc._add_hookimpl(
HookImpl(None, "<temp>", func, func.example_impl), # type: ignore[attr-defined]
)
return func
return wrap
@pytest.fixture
def addmeth(hc: HookCaller) -> AddMeth:
return AddMeth(hc)
def funcs(hookmethods: Sequence[HookImpl]) -> list[Callable[..., object]]:
return [hookmethod.function for hookmethod in hookmethods]
def test_adding_nonwrappers(hc: HookCaller, addmeth: AddMeth) -> None:
@addmeth()
def he_method1() -> None:
pass
@addmeth()
def he_method2() -> None:
pass
@addmeth()
def he_method3() -> None:
pass
assert funcs(hc.get_hookimpls()) == [he_method1, he_method2, he_method3]
def test_adding_nonwrappers_trylast(hc: HookCaller, addmeth: AddMeth) -> None:
@addmeth()
def he_method1_middle() -> None:
pass
@addmeth(trylast=True)
def he_method1() -> None:
pass
@addmeth()
def he_method1_b() -> None:
pass
assert funcs(hc.get_hookimpls()) == [he_method1, he_method1_middle, he_method1_b]
def test_adding_nonwrappers_trylast3(hc: HookCaller, addmeth: AddMeth) -> None:
@addmeth()
def he_method1_a() -> None:
pass
@addmeth(trylast=True)
def he_method1_b() -> None:
pass
@addmeth()
def he_method1_c() -> None:
pass
@addmeth(trylast=True)
def he_method1_d() -> None:
pass
assert funcs(hc.get_hookimpls()) == [
he_method1_d,
he_method1_b,
he_method1_a,
he_method1_c,
]
def test_adding_nonwrappers_trylast2(hc: HookCaller, addmeth: AddMeth) -> None:
@addmeth()
def he_method1_middle() -> None:
pass
@addmeth()
def he_method1_b() -> None:
pass
@addmeth(trylast=True)
def he_method1() -> None:
pass
assert funcs(hc.get_hookimpls()) == [he_method1, he_method1_middle, he_method1_b]
def test_adding_nonwrappers_tryfirst(hc: HookCaller, addmeth: AddMeth) -> None:
@addmeth(tryfirst=True)
def he_method1() -> None:
pass
@addmeth()
def he_method1_middle() -> None:
pass
@addmeth()
def he_method1_b() -> None:
pass
assert funcs(hc.get_hookimpls()) == [he_method1_middle, he_method1_b, he_method1]
def test_adding_wrappers_ordering(hc: HookCaller, addmeth: AddMeth) -> None:
@addmeth(hookwrapper=True)
def he_method1():
yield
@addmeth(wrapper=True)
def he_method1_fun():
yield
@addmeth()
def he_method1_middle():
return
@addmeth(hookwrapper=True)
def he_method3_fun():
yield
@addmeth(hookwrapper=True)
def he_method3():
yield
assert funcs(hc.get_hookimpls()) == [
he_method1_middle,
he_method1,
he_method1_fun,
he_method3_fun,
he_method3,
]
def test_adding_wrappers_ordering_tryfirst(hc: HookCaller, addmeth: AddMeth) -> None:
@addmeth(hookwrapper=True, tryfirst=True)
def he_method1():
yield
@addmeth(hookwrapper=True)
def he_method2():
yield
@addmeth(wrapper=True, tryfirst=True)
def he_method3():
yield
assert funcs(hc.get_hookimpls()) == [he_method2, he_method1, he_method3]
def test_adding_wrappers_complex(hc: HookCaller, addmeth: AddMeth) -> None:
assert funcs(hc.get_hookimpls()) == []
@addmeth(hookwrapper=True, trylast=True)
def m1():
yield
assert funcs(hc.get_hookimpls()) == [m1]
@addmeth()
def m2() -> None: ...
assert funcs(hc.get_hookimpls()) == [m2, m1]
@addmeth(trylast=True)
def m3() -> None: ...
assert funcs(hc.get_hookimpls()) == [m3, m2, m1]
@addmeth(hookwrapper=True)
def m4() -> None: ...
assert funcs(hc.get_hookimpls()) == [m3, m2, m1, m4]
@addmeth(wrapper=True, tryfirst=True)
def m5():
yield
assert funcs(hc.get_hookimpls()) == [m3, m2, m1, m4, m5]
@addmeth(tryfirst=True)
def m6() -> None: ...
assert funcs(hc.get_hookimpls()) == [m3, m2, m6, m1, m4, m5]
@addmeth()
def m7() -> None: ...
assert funcs(hc.get_hookimpls()) == [m3, m2, m7, m6, m1, m4, m5]
@addmeth(wrapper=True)
def m8():
yield
assert funcs(hc.get_hookimpls()) == [m3, m2, m7, m6, m1, m4, m8, m5]
@addmeth(trylast=True)
def m9() -> None: ...
assert funcs(hc.get_hookimpls()) == [m9, m3, m2, m7, m6, m1, m4, m8, m5]
@addmeth(tryfirst=True)
def m10() -> None: ...
assert funcs(hc.get_hookimpls()) == [m9, m3, m2, m7, m6, m10, m1, m4, m8, m5]
@addmeth(hookwrapper=True, trylast=True)
def m11() -> None: ...
assert funcs(hc.get_hookimpls()) == [m9, m3, m2, m7, m6, m10, m11, m1, m4, m8, m5]
@addmeth(wrapper=True)
def m12():
yield
assert funcs(hc.get_hookimpls()) == [
m9,
m3,
m2,
m7,
m6,
m10,
m11,
m1,
m4,
m8,
m12,
m5,
]
@addmeth()
def m13() -> None: ...
assert funcs(hc.get_hookimpls()) == [
m9,
m3,
m2,
m7,
m13,
m6,
m10,
m11,
m1,
m4,
m8,
m12,
m5,
]
def test_hookspec(pm: PluginManager) -> None:
class HookSpec:
@hookspec()
def he_myhook1(arg1) -> None:
pass
@hookspec(firstresult=True)
def he_myhook2(arg1) -> None:
pass
@hookspec(firstresult=False)
def he_myhook3(arg1) -> None:
pass
pm.add_hookspecs(HookSpec)
assert pm.hook.he_myhook1.spec is not None
assert not pm.hook.he_myhook1.spec.opts["firstresult"]
assert pm.hook.he_myhook2.spec is not None
assert pm.hook.he_myhook2.spec.opts["firstresult"]
assert pm.hook.he_myhook3.spec is not None
assert not pm.hook.he_myhook3.spec.opts["firstresult"]
@pytest.mark.parametrize("name", ["hookwrapper", "optionalhook", "tryfirst", "trylast"])
@pytest.mark.parametrize("val", [True, False])
def test_hookimpl(name: str, val: bool) -> None:
@hookimpl(**{name: val}) # type: ignore[misc,call-overload]
def he_myhook1(arg1) -> None:
pass
if val:
assert he_myhook1.example_impl.get(name)
else:
assert not hasattr(he_myhook1, name)
def test_hookrelay_registry(pm: PluginManager) -> None:
"""Verify hook caller instances are registered by name onto the relay
and can be likewise unregistered."""
class Api:
@hookspec
def hello(self, arg: object) -> None:
"api hook 1"
pm.add_hookspecs(Api)
hook = pm.hook
assert hasattr(hook, "hello")
assert repr(hook.hello).find("hello") != -1
class Plugin:
@hookimpl
def hello(self, arg):
return arg + 1
plugin = Plugin()
pm.register(plugin)
out = hook.hello(arg=3)
assert out == [4]
assert not hasattr(hook, "world")
pm.unregister(plugin)
assert hook.hello(arg=3) == []
def test_hookrelay_registration_by_specname(pm: PluginManager) -> None:
"""Verify hook caller instances may also be registered by specifying a
specname option to the hookimpl"""
class Api:
@hookspec
def hello(self, arg: object) -> None:
"api hook 1"
pm.add_hookspecs(Api)
hook = pm.hook
assert hasattr(hook, "hello")
assert len(pm.hook.hello.get_hookimpls()) == 0
class Plugin:
@hookimpl(specname="hello")
def foo(self, arg: int) -> int:
return arg + 1
plugin = Plugin()
pm.register(plugin)
out = hook.hello(arg=3)
assert out == [4]
def test_hookrelay_registration_by_specname_raises(pm: PluginManager) -> None:
"""Verify using specname still raises the types of errors during registration as it
would have without using specname."""
class Api:
@hookspec
def hello(self, arg: object) -> None:
"api hook 1"
pm.add_hookspecs(Api)
# make sure a bad signature still raises an error when using specname
class Plugin:
@hookimpl(specname="hello")
def foo(self, arg: int, too, many, args) -> int:
return arg + 1
with pytest.raises(PluginValidationError):
pm.register(Plugin())
# make sure check_pending still fails if specname doesn't have a
# corresponding spec. EVEN if the function name matches one.
class Plugin2:
@hookimpl(specname="bar")
def hello(self, arg: int) -> int:
return arg + 1
pm.register(Plugin2())
with pytest.raises(PluginValidationError):
pm.check_pending()
def test_hook_conflict(pm: PluginManager) -> None:
class Api1:
@hookspec
def conflict(self) -> None:
"""Api1 hook"""
class Api2:
@hookspec
def conflict(self) -> None:
"""Api2 hook"""
pm.add_hookspecs(Api1)
with pytest.raises(ValueError) as exc:
pm.add_hookspecs(Api2)
assert str(exc.value) == (
"Hook 'conflict' is already registered within namespace "
"<class 'test_hookcaller.test_hook_conflict.<locals>.Api1'>"
)
def test_call_extra_hook_order(hc: HookCaller, addmeth: AddMeth) -> None:
"""Ensure that call_extra is calling hooks in the right order."""
order = []
@addmeth(tryfirst=True)
def method1() -> str:
order.append("1")
return "1"
@addmeth()
def method2() -> str:
order.append("2")
return "2"
@addmeth(trylast=True)
def method3() -> str:
order.append("3")
return "3"
@addmeth(wrapper=True, tryfirst=True)
def method4() -> Generator[None, str, str]:
order.append("4pre")
result = yield
order.append("4post")
return result
@addmeth(wrapper=True)
def method5() -> Generator[None, str, str]:
order.append("5pre")
result = yield
order.append("5post")
return result
@addmeth(wrapper=True, trylast=True)
def method6() -> Generator[None, str, str]:
order.append("6pre")
result = yield
order.append("6post")
return result
def extra1() -> str:
order.append("extra1")
return "extra1"
def extra2() -> str:
order.append("extra2")
return "extra2"
result = hc.call_extra([extra1, extra2], {"arg": "test"})
assert order == [
"4pre",
"5pre",
"6pre",
"1",
"extra2",
"extra1",
"2",
"3",
"6post",
"5post",
"4post",
]
assert result == [
"1",
"extra2",
"extra1",
"2",
"3",
]