test_hookcaller.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. from typing import Callable
  2. from typing import List
  3. from typing import Sequence
  4. from typing import TypeVar
  5. import pytest
  6. from pluggy import HookimplMarker
  7. from pluggy import HookspecMarker
  8. from pluggy import PluginManager
  9. from pluggy import PluginValidationError
  10. from pluggy._hooks import _HookCaller
  11. from pluggy._hooks import HookImpl
  12. hookspec = HookspecMarker("example")
  13. hookimpl = HookimplMarker("example")
  14. @pytest.fixture
  15. def hc(pm: PluginManager) -> _HookCaller:
  16. class Hooks:
  17. @hookspec
  18. def he_method1(self, arg: object) -> None:
  19. pass
  20. pm.add_hookspecs(Hooks)
  21. return pm.hook.he_method1
  22. FuncT = TypeVar("FuncT", bound=Callable[..., object])
  23. class AddMeth:
  24. def __init__(self, hc: _HookCaller) -> None:
  25. self.hc = hc
  26. def __call__(
  27. self,
  28. tryfirst: bool = False,
  29. trylast: bool = False,
  30. hookwrapper: bool = False,
  31. wrapper: bool = False,
  32. ) -> Callable[[FuncT], FuncT]:
  33. def wrap(func: FuncT) -> FuncT:
  34. hookimpl(
  35. tryfirst=tryfirst,
  36. trylast=trylast,
  37. hookwrapper=hookwrapper,
  38. wrapper=wrapper,
  39. )(func)
  40. self.hc._add_hookimpl(
  41. HookImpl(None, "<temp>", func, func.example_impl), # type: ignore[attr-defined]
  42. )
  43. return func
  44. return wrap
  45. @pytest.fixture
  46. def addmeth(hc: _HookCaller) -> AddMeth:
  47. return AddMeth(hc)
  48. def funcs(hookmethods: Sequence[HookImpl]) -> List[Callable[..., object]]:
  49. return [hookmethod.function for hookmethod in hookmethods]
  50. def test_adding_nonwrappers(hc: _HookCaller, addmeth: AddMeth) -> None:
  51. @addmeth()
  52. def he_method1() -> None:
  53. pass
  54. @addmeth()
  55. def he_method2() -> None:
  56. pass
  57. @addmeth()
  58. def he_method3() -> None:
  59. pass
  60. assert funcs(hc.get_hookimpls()) == [he_method1, he_method2, he_method3]
  61. def test_adding_nonwrappers_trylast(hc: _HookCaller, addmeth: AddMeth) -> None:
  62. @addmeth()
  63. def he_method1_middle() -> None:
  64. pass
  65. @addmeth(trylast=True)
  66. def he_method1() -> None:
  67. pass
  68. @addmeth()
  69. def he_method1_b() -> None:
  70. pass
  71. assert funcs(hc.get_hookimpls()) == [he_method1, he_method1_middle, he_method1_b]
  72. def test_adding_nonwrappers_trylast3(hc: _HookCaller, addmeth: AddMeth) -> None:
  73. @addmeth()
  74. def he_method1_a() -> None:
  75. pass
  76. @addmeth(trylast=True)
  77. def he_method1_b() -> None:
  78. pass
  79. @addmeth()
  80. def he_method1_c() -> None:
  81. pass
  82. @addmeth(trylast=True)
  83. def he_method1_d() -> None:
  84. pass
  85. assert funcs(hc.get_hookimpls()) == [
  86. he_method1_d,
  87. he_method1_b,
  88. he_method1_a,
  89. he_method1_c,
  90. ]
  91. def test_adding_nonwrappers_trylast2(hc: _HookCaller, addmeth: AddMeth) -> None:
  92. @addmeth()
  93. def he_method1_middle() -> None:
  94. pass
  95. @addmeth()
  96. def he_method1_b() -> None:
  97. pass
  98. @addmeth(trylast=True)
  99. def he_method1() -> None:
  100. pass
  101. assert funcs(hc.get_hookimpls()) == [he_method1, he_method1_middle, he_method1_b]
  102. def test_adding_nonwrappers_tryfirst(hc: _HookCaller, addmeth: AddMeth) -> None:
  103. @addmeth(tryfirst=True)
  104. def he_method1() -> None:
  105. pass
  106. @addmeth()
  107. def he_method1_middle() -> None:
  108. pass
  109. @addmeth()
  110. def he_method1_b() -> None:
  111. pass
  112. assert funcs(hc.get_hookimpls()) == [he_method1_middle, he_method1_b, he_method1]
  113. def test_adding_wrappers_ordering(hc: _HookCaller, addmeth: AddMeth) -> None:
  114. @addmeth(hookwrapper=True)
  115. def he_method1():
  116. yield
  117. @addmeth(wrapper=True)
  118. def he_method1_fun():
  119. yield
  120. @addmeth()
  121. def he_method1_middle():
  122. return
  123. @addmeth(hookwrapper=True)
  124. def he_method3_fun():
  125. yield
  126. @addmeth(hookwrapper=True)
  127. def he_method3():
  128. yield
  129. assert funcs(hc.get_hookimpls()) == [
  130. he_method1_middle,
  131. he_method1,
  132. he_method1_fun,
  133. he_method3_fun,
  134. he_method3,
  135. ]
  136. def test_adding_wrappers_ordering_tryfirst(hc: _HookCaller, addmeth: AddMeth) -> None:
  137. @addmeth(hookwrapper=True, tryfirst=True)
  138. def he_method1():
  139. yield
  140. @addmeth(hookwrapper=True)
  141. def he_method2():
  142. yield
  143. @addmeth(wrapper=True, tryfirst=True)
  144. def he_method3():
  145. yield
  146. assert funcs(hc.get_hookimpls()) == [he_method2, he_method1, he_method3]
  147. def test_adding_wrappers_complex(hc: _HookCaller, addmeth: AddMeth) -> None:
  148. assert funcs(hc.get_hookimpls()) == []
  149. @addmeth(hookwrapper=True, trylast=True)
  150. def m1():
  151. yield
  152. assert funcs(hc.get_hookimpls()) == [m1]
  153. @addmeth()
  154. def m2() -> None:
  155. ...
  156. assert funcs(hc.get_hookimpls()) == [m2, m1]
  157. @addmeth(trylast=True)
  158. def m3() -> None:
  159. ...
  160. assert funcs(hc.get_hookimpls()) == [m3, m2, m1]
  161. @addmeth(hookwrapper=True)
  162. def m4() -> None:
  163. ...
  164. assert funcs(hc.get_hookimpls()) == [m3, m2, m1, m4]
  165. @addmeth(wrapper=True, tryfirst=True)
  166. def m5():
  167. yield
  168. assert funcs(hc.get_hookimpls()) == [m3, m2, m1, m4, m5]
  169. @addmeth(tryfirst=True)
  170. def m6() -> None:
  171. ...
  172. assert funcs(hc.get_hookimpls()) == [m3, m2, m6, m1, m4, m5]
  173. @addmeth()
  174. def m7() -> None:
  175. ...
  176. assert funcs(hc.get_hookimpls()) == [m3, m2, m7, m6, m1, m4, m5]
  177. @addmeth(wrapper=True)
  178. def m8():
  179. yield
  180. assert funcs(hc.get_hookimpls()) == [m3, m2, m7, m6, m1, m4, m8, m5]
  181. @addmeth(trylast=True)
  182. def m9() -> None:
  183. ...
  184. assert funcs(hc.get_hookimpls()) == [m9, m3, m2, m7, m6, m1, m4, m8, m5]
  185. @addmeth(tryfirst=True)
  186. def m10() -> None:
  187. ...
  188. assert funcs(hc.get_hookimpls()) == [m9, m3, m2, m7, m6, m10, m1, m4, m8, m5]
  189. @addmeth(hookwrapper=True, trylast=True)
  190. def m11() -> None:
  191. ...
  192. assert funcs(hc.get_hookimpls()) == [m9, m3, m2, m7, m6, m10, m11, m1, m4, m8, m5]
  193. @addmeth(wrapper=True)
  194. def m12():
  195. yield
  196. assert funcs(hc.get_hookimpls()) == [
  197. m9,
  198. m3,
  199. m2,
  200. m7,
  201. m6,
  202. m10,
  203. m11,
  204. m1,
  205. m4,
  206. m8,
  207. m12,
  208. m5,
  209. ]
  210. @addmeth()
  211. def m13() -> None:
  212. ...
  213. assert funcs(hc.get_hookimpls()) == [
  214. m9,
  215. m3,
  216. m2,
  217. m7,
  218. m13,
  219. m6,
  220. m10,
  221. m11,
  222. m1,
  223. m4,
  224. m8,
  225. m12,
  226. m5,
  227. ]
  228. def test_hookspec(pm: PluginManager) -> None:
  229. class HookSpec:
  230. @hookspec()
  231. def he_myhook1(arg1) -> None:
  232. pass
  233. @hookspec(firstresult=True)
  234. def he_myhook2(arg1) -> None:
  235. pass
  236. @hookspec(firstresult=False)
  237. def he_myhook3(arg1) -> None:
  238. pass
  239. pm.add_hookspecs(HookSpec)
  240. assert pm.hook.he_myhook1.spec is not None
  241. assert not pm.hook.he_myhook1.spec.opts["firstresult"]
  242. assert pm.hook.he_myhook2.spec is not None
  243. assert pm.hook.he_myhook2.spec.opts["firstresult"]
  244. assert pm.hook.he_myhook3.spec is not None
  245. assert not pm.hook.he_myhook3.spec.opts["firstresult"]
  246. @pytest.mark.parametrize("name", ["hookwrapper", "optionalhook", "tryfirst", "trylast"])
  247. @pytest.mark.parametrize("val", [True, False])
  248. def test_hookimpl(name: str, val: bool) -> None:
  249. @hookimpl(**{name: val}) # type: ignore[misc,call-overload]
  250. def he_myhook1(arg1) -> None:
  251. pass
  252. if val:
  253. assert he_myhook1.example_impl.get(name)
  254. else:
  255. assert not hasattr(he_myhook1, name)
  256. def test_hookrelay_registry(pm: PluginManager) -> None:
  257. """Verify hook caller instances are registered by name onto the relay
  258. and can be likewise unregistered."""
  259. class Api:
  260. @hookspec
  261. def hello(self, arg: object) -> None:
  262. "api hook 1"
  263. pm.add_hookspecs(Api)
  264. hook = pm.hook
  265. assert hasattr(hook, "hello")
  266. assert repr(hook.hello).find("hello") != -1
  267. class Plugin:
  268. @hookimpl
  269. def hello(self, arg):
  270. return arg + 1
  271. plugin = Plugin()
  272. pm.register(plugin)
  273. out = hook.hello(arg=3)
  274. assert out == [4]
  275. assert not hasattr(hook, "world")
  276. pm.unregister(plugin)
  277. assert hook.hello(arg=3) == []
  278. def test_hookrelay_registration_by_specname(pm: PluginManager) -> None:
  279. """Verify hook caller instances may also be registered by specifying a
  280. specname option to the hookimpl"""
  281. class Api:
  282. @hookspec
  283. def hello(self, arg: object) -> None:
  284. "api hook 1"
  285. pm.add_hookspecs(Api)
  286. hook = pm.hook
  287. assert hasattr(hook, "hello")
  288. assert len(pm.hook.hello.get_hookimpls()) == 0
  289. class Plugin:
  290. @hookimpl(specname="hello")
  291. def foo(self, arg: int) -> int:
  292. return arg + 1
  293. plugin = Plugin()
  294. pm.register(plugin)
  295. out = hook.hello(arg=3)
  296. assert out == [4]
  297. def test_hookrelay_registration_by_specname_raises(pm: PluginManager) -> None:
  298. """Verify using specname still raises the types of errors during registration as it
  299. would have without using specname."""
  300. class Api:
  301. @hookspec
  302. def hello(self, arg: object) -> None:
  303. "api hook 1"
  304. pm.add_hookspecs(Api)
  305. # make sure a bad signature still raises an error when using specname
  306. class Plugin:
  307. @hookimpl(specname="hello")
  308. def foo(self, arg: int, too, many, args) -> int:
  309. return arg + 1
  310. with pytest.raises(PluginValidationError):
  311. pm.register(Plugin())
  312. # make sure check_pending still fails if specname doesn't have a
  313. # corresponding spec. EVEN if the function name matches one.
  314. class Plugin2:
  315. @hookimpl(specname="bar")
  316. def hello(self, arg: int) -> int:
  317. return arg + 1
  318. pm.register(Plugin2())
  319. with pytest.raises(PluginValidationError):
  320. pm.check_pending()
  321. def test_hook_conflict(pm: PluginManager) -> None:
  322. class Api1:
  323. @hookspec
  324. def conflict(self) -> None:
  325. """Api1 hook"""
  326. class Api2:
  327. @hookspec
  328. def conflict(self) -> None:
  329. """Api2 hook"""
  330. pm.add_hookspecs(Api1)
  331. with pytest.raises(ValueError) as exc:
  332. pm.add_hookspecs(Api2)
  333. assert str(exc.value) == (
  334. "Hook 'conflict' is already registered within namespace "
  335. "<class '__tests__.test_hookcaller.test_hook_conflict.<locals>.Api1'>"
  336. )