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
|
import pytest
from pluggy import HookCallError, HookspecMarker, HookimplMarker
from pluggy.hooks import HookImpl
from pluggy.callers import _multicall, _legacymulticall
hookspec = HookspecMarker("example")
hookimpl = HookimplMarker("example")
def MC(methods, kwargs, firstresult=False):
caller = _multicall
hookfuncs = []
for method in methods:
f = HookImpl(None, "<temp>", method, method.example_impl)
hookfuncs.append(f)
if "__multicall__" in f.argnames:
caller = _legacymulticall
return caller(hookfuncs, kwargs, firstresult=firstresult)
def test_call_passing():
class P1(object):
@hookimpl
def m(self, __multicall__, x):
assert len(__multicall__.results) == 1
assert not __multicall__.hook_impls
return 17
class P2(object):
@hookimpl
def m(self, __multicall__, x):
assert __multicall__.results == []
assert __multicall__.hook_impls
return 23
p1 = P1()
p2 = P2()
reslist = MC([p1.m, p2.m], {"x": 23})
assert len(reslist) == 2
# ensure reversed order
assert reslist == [23, 17]
def test_keyword_args():
@hookimpl
def f(x):
return x + 1
class A(object):
@hookimpl
def f(self, x, y):
return x + y
reslist = MC([f, A().f], dict(x=23, y=24))
assert reslist == [24 + 23, 24]
def test_keyword_args_with_defaultargs():
@hookimpl
def f(x, z=1):
return x + z
reslist = MC([f], dict(x=23, y=24))
assert reslist == [24]
def test_tags_call_error():
@hookimpl
def f(x):
return x
with pytest.raises(HookCallError):
MC([f], {})
def test_call_subexecute():
@hookimpl
def m(__multicall__):
subresult = __multicall__.execute()
return subresult + 1
@hookimpl
def n():
return 1
res = MC([n, m], {}, firstresult=True)
assert res == 2
def test_call_none_is_no_result():
@hookimpl
def m1():
return 1
@hookimpl
def m2():
return None
res = MC([m1, m2], {}, firstresult=True)
assert res == 1
res = MC([m1, m2], {}, {})
assert res == [1]
def test_hookwrapper():
out = []
@hookimpl(hookwrapper=True)
def m1():
out.append("m1 init")
yield None
out.append("m1 finish")
@hookimpl
def m2():
out.append("m2")
return 2
res = MC([m2, m1], {})
assert res == [2]
assert out == ["m1 init", "m2", "m1 finish"]
out[:] = []
res = MC([m2, m1], {}, firstresult=True)
assert res == 2
assert out == ["m1 init", "m2", "m1 finish"]
def test_hookwrapper_order():
out = []
@hookimpl(hookwrapper=True)
def m1():
out.append("m1 init")
yield 1
out.append("m1 finish")
@hookimpl(hookwrapper=True)
def m2():
out.append("m2 init")
yield 2
out.append("m2 finish")
res = MC([m2, m1], {})
assert res == []
assert out == ["m1 init", "m2 init", "m2 finish", "m1 finish"]
def test_hookwrapper_not_yield():
@hookimpl(hookwrapper=True)
def m1():
pass
with pytest.raises(TypeError):
MC([m1], {})
def test_hookwrapper_too_many_yield():
@hookimpl(hookwrapper=True)
def m1():
yield 1
yield 2
with pytest.raises(RuntimeError) as ex:
MC([m1], {})
assert "m1" in str(ex.value)
assert (__file__ + ":") in str(ex.value)
@pytest.mark.parametrize("exc", [ValueError, SystemExit])
def test_hookwrapper_exception(exc):
out = []
@hookimpl(hookwrapper=True)
def m1():
out.append("m1 init")
yield None
out.append("m1 finish")
@hookimpl
def m2():
raise exc
with pytest.raises(exc):
MC([m2, m1], {})
assert out == ["m1 init", "m1 finish"]
|