|
1 | 1 | import pytest
|
2 |
| -import sys |
3 |
| -import types |
4 | 2 |
|
5 |
| -from pluggy import PluginManager, HookimplMarker, HookspecMarker |
| 3 | +from pluggy import HookimplMarker, HookspecMarker |
6 | 4 | from pluggy.hooks import HookImpl
|
7 | 5 |
|
8 | 6 | hookspec = HookspecMarker("example")
|
|
11 | 9 |
|
12 | 10 | @pytest.fixture
|
13 | 11 | def hc(pm):
|
| 12 | + |
14 | 13 | class Hooks(object):
|
15 | 14 | @hookspec
|
16 | 15 | def he_method1(self, arg):
|
17 | 16 | pass
|
| 17 | + |
18 | 18 | pm.add_hookspecs(Hooks)
|
19 | 19 | return pm.hook.he_method1
|
20 | 20 |
|
@@ -179,146 +179,28 @@ def he_myhook1(arg1):
|
179 | 179 | assert not hasattr(he_myhook1, name)
|
180 | 180 |
|
181 | 181 |
|
182 |
| -def test_load_setuptools_instantiation(monkeypatch, pm): |
183 |
| - pkg_resources = pytest.importorskip("pkg_resources") |
184 |
| - |
185 |
| - def my_iter(name): |
186 |
| - assert name == "hello" |
187 |
| - |
188 |
| - class EntryPoint(object): |
189 |
| - name = "myname" |
190 |
| - dist = None |
191 |
| - |
192 |
| - def load(self): |
193 |
| - class PseudoPlugin(object): |
194 |
| - x = 42 |
195 |
| - return PseudoPlugin() |
196 |
| - |
197 |
| - return iter([EntryPoint()]) |
198 |
| - |
199 |
| - monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter) |
200 |
| - num = pm.load_setuptools_entrypoints("hello") |
201 |
| - assert num == 1 |
202 |
| - plugin = pm.get_plugin("myname") |
203 |
| - assert plugin.x == 42 |
204 |
| - assert pm.list_plugin_distinfo() == [(plugin, None)] |
205 |
| - |
206 |
| - |
207 |
| -def test_load_setuptools_not_installed(monkeypatch, pm): |
208 |
| - monkeypatch.setitem( |
209 |
| - sys.modules, 'pkg_resources', |
210 |
| - types.ModuleType("pkg_resources")) |
211 |
| - |
212 |
| - with pytest.raises(ImportError): |
213 |
| - pm.load_setuptools_entrypoints("qwe") |
214 |
| - |
215 |
| - |
216 |
| -def test_add_tracefuncs(he_pm): |
217 |
| - out = [] |
218 |
| - |
219 |
| - class api1(object): |
220 |
| - @hookimpl |
221 |
| - def he_method1(self): |
222 |
| - out.append("he_method1-api1") |
223 |
| - |
224 |
| - class api2(object): |
225 |
| - @hookimpl |
226 |
| - def he_method1(self): |
227 |
| - out.append("he_method1-api2") |
228 |
| - |
229 |
| - he_pm.register(api1()) |
230 |
| - he_pm.register(api2()) |
231 |
| - |
232 |
| - def before(hook_name, hook_impls, kwargs): |
233 |
| - out.append((hook_name, list(hook_impls), kwargs)) |
234 |
| - |
235 |
| - def after(outcome, hook_name, hook_impls, kwargs): |
236 |
| - out.append((outcome, hook_name, list(hook_impls), kwargs)) |
237 |
| - |
238 |
| - undo = he_pm.add_hookcall_monitoring(before, after) |
239 |
| - |
240 |
| - he_pm.hook.he_method1(arg=1) |
241 |
| - assert len(out) == 4 |
242 |
| - assert out[0][0] == "he_method1" |
243 |
| - assert len(out[0][1]) == 2 |
244 |
| - assert isinstance(out[0][2], dict) |
245 |
| - assert out[1] == "he_method1-api2" |
246 |
| - assert out[2] == "he_method1-api1" |
247 |
| - assert len(out[3]) == 4 |
248 |
| - assert out[3][1] == out[0][0] |
249 |
| - |
250 |
| - undo() |
251 |
| - he_pm.hook.he_method1(arg=1) |
252 |
| - assert len(out) == 4 + 2 |
253 |
| - |
254 |
| - |
255 |
| -def test_hook_tracing(he_pm): |
256 |
| - saveindent = [] |
257 |
| - |
258 |
| - class api1(object): |
259 |
| - @hookimpl |
260 |
| - def he_method1(self): |
261 |
| - saveindent.append(he_pm.trace.root.indent) |
| 182 | +def test_hookrelay_registry(pm): |
| 183 | + """Verify hook caller instances are registered by name onto the relay |
| 184 | + and can be likewise unregistered.""" |
| 185 | + class Api(object): |
| 186 | + @hookspec |
| 187 | + def hello(self, arg): |
| 188 | + "api hook 1" |
262 | 189 |
|
263 |
| - class api2(object): |
264 |
| - @hookimpl |
265 |
| - def he_method1(self): |
266 |
| - saveindent.append(he_pm.trace.root.indent) |
267 |
| - raise ValueError() |
268 |
| - |
269 |
| - he_pm.register(api1()) |
270 |
| - out = [] |
271 |
| - he_pm.trace.root.setwriter(out.append) |
272 |
| - undo = he_pm.enable_tracing() |
273 |
| - try: |
274 |
| - indent = he_pm.trace.root.indent |
275 |
| - he_pm.hook.he_method1(arg=1) |
276 |
| - assert indent == he_pm.trace.root.indent |
277 |
| - assert len(out) == 2 |
278 |
| - assert 'he_method1' in out[0] |
279 |
| - assert 'finish' in out[1] |
280 |
| - |
281 |
| - out[:] = [] |
282 |
| - he_pm.register(api2()) |
283 |
| - |
284 |
| - with pytest.raises(ValueError): |
285 |
| - he_pm.hook.he_method1(arg=1) |
286 |
| - assert he_pm.trace.root.indent == indent |
287 |
| - assert saveindent[0] > indent |
288 |
| - finally: |
289 |
| - undo() |
290 |
| - |
291 |
| - |
292 |
| -@pytest.mark.parametrize('include_hookspec', [True, False]) |
293 |
| -def test_prefix_hookimpl(include_hookspec): |
294 |
| - with pytest.deprecated_call(): |
295 |
| - pm = PluginManager(hookspec.project_name, "hello_") |
296 |
| - |
297 |
| - if include_hookspec: |
298 |
| - class HookSpec(object): |
299 |
| - @hookspec |
300 |
| - def hello_myhook(self, arg1): |
301 |
| - """ add to arg1 """ |
302 |
| - |
303 |
| - pm.add_hookspecs(HookSpec) |
| 190 | + pm.add_hookspecs(Api) |
| 191 | + hook = pm.hook |
| 192 | + assert hasattr(hook, 'hello') |
| 193 | + assert repr(hook.hello).find("hello") != -1 |
304 | 194 |
|
305 | 195 | class Plugin(object):
|
306 |
| - def hello_myhook(self, arg1): |
307 |
| - return arg1 + 1 |
308 |
| - |
309 |
| - with pytest.deprecated_call(): |
310 |
| - pm.register(Plugin()) |
311 |
| - pm.register(Plugin()) |
312 |
| - results = pm.hook.hello_myhook(arg1=17) |
313 |
| - assert results == [18, 18] |
314 |
| - |
315 |
| - |
316 |
| -def test_prefix_hookimpl_dontmatch_module(): |
317 |
| - with pytest.deprecated_call(): |
318 |
| - pm = PluginManager(hookspec.project_name, "hello_") |
319 |
| - |
320 |
| - class BadPlugin(object): |
321 |
| - hello_module = __import__('email') |
322 |
| - |
323 |
| - pm.register(BadPlugin()) |
324 |
| - pm.check_pending() |
| 196 | + @hookimpl |
| 197 | + def hello(self, arg): |
| 198 | + return arg + 1 |
| 199 | + |
| 200 | + plugin = Plugin() |
| 201 | + pm.register(plugin) |
| 202 | + out = hook.hello(arg=3) |
| 203 | + assert out == [4] |
| 204 | + assert not hasattr(hook, 'world') |
| 205 | + pm.unregister(plugin) |
| 206 | + assert hook.hello(arg=3) == [] |
0 commit comments