summaryrefslogtreecommitdiffstats
path: root/lang/python/python-pluggy/test.sh
blob: 9d0e193ed21f9b9902abe8037821367743d5d545 (plain)
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
#!/bin/sh

[ "$1" = "python3-pluggy" ] || exit 0

python3 - << EOF
import sys
import pluggy

version = pluggy.__version__
if version != "$2":
    print("Wrong version: " + version)
    sys.exit(1)

# Define a hookspec and hookimpl
hookspec = pluggy.HookspecMarker("myproject")
hookimpl = pluggy.HookimplMarker("myproject")

class MySpec:
    @hookspec
    def my_hook(self, arg):
        """A hook that returns a value."""

class MyPlugin:
    @hookimpl
    def my_hook(self, arg):
        return arg * 2

# Register and call
pm = pluggy.PluginManager("myproject")
pm.add_hookspecs(MySpec)
pm.register(MyPlugin())

results = pm.hook.my_hook(arg=21)
assert results == [42], f"Expected [42], got {results}"

# Multiple plugins, results collected in LIFO order
class AnotherPlugin:
    @hookimpl
    def my_hook(self, arg):
        return arg + 1

pm.register(AnotherPlugin())
results = pm.hook.my_hook(arg=10)
assert results == [11, 20], f"Expected [11, 20], got {results}"

sys.exit(0)
EOF