def test_subset_hook_caller(pm: PluginManager) -> None: class Hooks: @hookspec def he_method1(self, arg): pass pm.add_hookspecs(Hooks) out = [] class Plugin1: @hookimpl def he_method1(self, arg): out.append(arg) class Plugin2: @hookimpl def he_method1(self, arg): out.append(arg * 10) class PluginNo: pass plugin1, plugin2, plugin3 = Plugin1(), Plugin2(), PluginNo() pm.register(plugin1) pm.register(plugin2) pm.register(plugin3) pm.hook.he_method1(arg=1) assert out == [10, 1] out[:] = [] hc = pm.subset_hook_caller("he_method1", [plugin1]) hc(arg=2) assert out == [20] out[:] = [] hc = pm.subset_hook_caller("he_method1", [plugin2]) hc(arg=2) assert out == [2] out[:] = [] pm.unregister(plugin1) hc(arg=2) assert out == [] out[:] = [] pm.hook.he_method1(arg=1) assert out == [10] assert repr(hc) == "<_SubsetHookCaller 'he_method1'>"
def test_historic_with_subset_hook_caller(pm: PluginManager) -> None: class Hooks: @hookspec(historic=True) def he_method1(self, arg): ... pm.add_hookspecs(Hooks) out = [] class Plugin: @hookimpl def he_method1(self, arg): out.append(arg) plugin = Plugin() pm.register(plugin) class Plugin2: @hookimpl def he_method1(self, arg): out.append(arg * 10) shc = pm.subset_hook_caller("he_method1", remove_plugins=[plugin]) shc.call_historic(kwargs=dict(arg=1)) pm.register(Plugin2()) assert out == [10] pm.register(Plugin()) assert out == [10, 1]