Skip to content

Commit b093f9e

Browse files
author
Tyler Goodlet
committed
Move manager related tests to proper module
1 parent 32a0287 commit b093f9e

File tree

2 files changed

+151
-150
lines changed

2 files changed

+151
-150
lines changed

testing/test_hookcaller.py

Lines changed: 1 addition & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import pytest
2-
import sys
3-
import types
42

5-
from pluggy import PluginManager, HookimplMarker, HookspecMarker
3+
from pluggy import HookimplMarker, HookspecMarker
64
from pluggy.hooks import HookImpl
75

86
hookspec = HookspecMarker("example")
@@ -206,148 +204,3 @@ def hello(self, arg):
206204
assert not hasattr(hook, 'world')
207205
pm.unregister(plugin)
208206
assert hook.hello(arg=3) == []
209-
210-
211-
def test_load_setuptools_instantiation(monkeypatch, pm):
212-
pkg_resources = pytest.importorskip("pkg_resources")
213-
214-
def my_iter(name):
215-
assert name == "hello"
216-
217-
class EntryPoint(object):
218-
name = "myname"
219-
dist = None
220-
221-
def load(self):
222-
class PseudoPlugin(object):
223-
x = 42
224-
return PseudoPlugin()
225-
226-
return iter([EntryPoint()])
227-
228-
monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter)
229-
num = pm.load_setuptools_entrypoints("hello")
230-
assert num == 1
231-
plugin = pm.get_plugin("myname")
232-
assert plugin.x == 42
233-
assert pm.list_plugin_distinfo() == [(plugin, None)]
234-
235-
236-
def test_load_setuptools_not_installed(monkeypatch, pm):
237-
monkeypatch.setitem(
238-
sys.modules, 'pkg_resources',
239-
types.ModuleType("pkg_resources"))
240-
241-
with pytest.raises(ImportError):
242-
pm.load_setuptools_entrypoints("qwe")
243-
244-
245-
def test_add_tracefuncs(he_pm):
246-
out = []
247-
248-
class api1(object):
249-
@hookimpl
250-
def he_method1(self):
251-
out.append("he_method1-api1")
252-
253-
class api2(object):
254-
@hookimpl
255-
def he_method1(self):
256-
out.append("he_method1-api2")
257-
258-
he_pm.register(api1())
259-
he_pm.register(api2())
260-
261-
def before(hook_name, hook_impls, kwargs):
262-
out.append((hook_name, list(hook_impls), kwargs))
263-
264-
def after(outcome, hook_name, hook_impls, kwargs):
265-
out.append((outcome, hook_name, list(hook_impls), kwargs))
266-
267-
undo = he_pm.add_hookcall_monitoring(before, after)
268-
269-
he_pm.hook.he_method1(arg=1)
270-
assert len(out) == 4
271-
assert out[0][0] == "he_method1"
272-
assert len(out[0][1]) == 2
273-
assert isinstance(out[0][2], dict)
274-
assert out[1] == "he_method1-api2"
275-
assert out[2] == "he_method1-api1"
276-
assert len(out[3]) == 4
277-
assert out[3][1] == out[0][0]
278-
279-
undo()
280-
he_pm.hook.he_method1(arg=1)
281-
assert len(out) == 4 + 2
282-
283-
284-
def test_hook_tracing(he_pm):
285-
saveindent = []
286-
287-
class api1(object):
288-
@hookimpl
289-
def he_method1(self):
290-
saveindent.append(he_pm.trace.root.indent)
291-
292-
class api2(object):
293-
@hookimpl
294-
def he_method1(self):
295-
saveindent.append(he_pm.trace.root.indent)
296-
raise ValueError()
297-
298-
he_pm.register(api1())
299-
out = []
300-
he_pm.trace.root.setwriter(out.append)
301-
undo = he_pm.enable_tracing()
302-
try:
303-
indent = he_pm.trace.root.indent
304-
he_pm.hook.he_method1(arg=1)
305-
assert indent == he_pm.trace.root.indent
306-
assert len(out) == 2
307-
assert 'he_method1' in out[0]
308-
assert 'finish' in out[1]
309-
310-
out[:] = []
311-
he_pm.register(api2())
312-
313-
with pytest.raises(ValueError):
314-
he_pm.hook.he_method1(arg=1)
315-
assert he_pm.trace.root.indent == indent
316-
assert saveindent[0] > indent
317-
finally:
318-
undo()
319-
320-
321-
@pytest.mark.parametrize('include_hookspec', [True, False])
322-
def test_prefix_hookimpl(include_hookspec):
323-
with pytest.deprecated_call():
324-
pm = PluginManager(hookspec.project_name, "hello_")
325-
326-
if include_hookspec:
327-
class HookSpec(object):
328-
@hookspec
329-
def hello_myhook(self, arg1):
330-
""" add to arg1 """
331-
332-
pm.add_hookspecs(HookSpec)
333-
334-
class Plugin(object):
335-
def hello_myhook(self, arg1):
336-
return arg1 + 1
337-
338-
with pytest.deprecated_call():
339-
pm.register(Plugin())
340-
pm.register(Plugin())
341-
results = pm.hook.hello_myhook(arg1=17)
342-
assert results == [18, 18]
343-
344-
345-
def test_prefix_hookimpl_dontmatch_module():
346-
with pytest.deprecated_call():
347-
pm = PluginManager(hookspec.project_name, "hello_")
348-
349-
class BadPlugin(object):
350-
hello_module = __import__('email')
351-
352-
pm.register(BadPlugin())
353-
pm.check_pending()

testing/test_pluginmanager.py

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import pytest
22
import types
3-
from pluggy import (PluginValidationError,
4-
HookCallError, HookimplMarker, HookspecMarker)
3+
import sys
4+
from pluggy import (
5+
PluginManager, PluginValidationError,
6+
HookCallError, HookimplMarker, HookspecMarker
7+
)
58

69

710
hookspec = HookspecMarker("example")
@@ -418,3 +421,148 @@ def m(self, x):
418421
pm.register(p1)
419422
with pytest.deprecated_call():
420423
pm.hook.m.call_historic(kwargs=dict(x=10), proc=lambda res: res)
424+
425+
426+
def test_load_setuptools_instantiation(monkeypatch, pm):
427+
pkg_resources = pytest.importorskip("pkg_resources")
428+
429+
def my_iter(name):
430+
assert name == "hello"
431+
432+
class EntryPoint(object):
433+
name = "myname"
434+
dist = None
435+
436+
def load(self):
437+
class PseudoPlugin(object):
438+
x = 42
439+
return PseudoPlugin()
440+
441+
return iter([EntryPoint()])
442+
443+
monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter)
444+
num = pm.load_setuptools_entrypoints("hello")
445+
assert num == 1
446+
plugin = pm.get_plugin("myname")
447+
assert plugin.x == 42
448+
assert pm.list_plugin_distinfo() == [(plugin, None)]
449+
450+
451+
def test_load_setuptools_not_installed(monkeypatch, pm):
452+
monkeypatch.setitem(
453+
sys.modules, 'pkg_resources',
454+
types.ModuleType("pkg_resources"))
455+
456+
with pytest.raises(ImportError):
457+
pm.load_setuptools_entrypoints("qwe")
458+
459+
460+
def test_add_tracefuncs(he_pm):
461+
out = []
462+
463+
class api1(object):
464+
@hookimpl
465+
def he_method1(self):
466+
out.append("he_method1-api1")
467+
468+
class api2(object):
469+
@hookimpl
470+
def he_method1(self):
471+
out.append("he_method1-api2")
472+
473+
he_pm.register(api1())
474+
he_pm.register(api2())
475+
476+
def before(hook_name, hook_impls, kwargs):
477+
out.append((hook_name, list(hook_impls), kwargs))
478+
479+
def after(outcome, hook_name, hook_impls, kwargs):
480+
out.append((outcome, hook_name, list(hook_impls), kwargs))
481+
482+
undo = he_pm.add_hookcall_monitoring(before, after)
483+
484+
he_pm.hook.he_method1(arg=1)
485+
assert len(out) == 4
486+
assert out[0][0] == "he_method1"
487+
assert len(out[0][1]) == 2
488+
assert isinstance(out[0][2], dict)
489+
assert out[1] == "he_method1-api2"
490+
assert out[2] == "he_method1-api1"
491+
assert len(out[3]) == 4
492+
assert out[3][1] == out[0][0]
493+
494+
undo()
495+
he_pm.hook.he_method1(arg=1)
496+
assert len(out) == 4 + 2
497+
498+
499+
def test_hook_tracing(he_pm):
500+
saveindent = []
501+
502+
class api1(object):
503+
@hookimpl
504+
def he_method1(self):
505+
saveindent.append(he_pm.trace.root.indent)
506+
507+
class api2(object):
508+
@hookimpl
509+
def he_method1(self):
510+
saveindent.append(he_pm.trace.root.indent)
511+
raise ValueError()
512+
513+
he_pm.register(api1())
514+
out = []
515+
he_pm.trace.root.setwriter(out.append)
516+
undo = he_pm.enable_tracing()
517+
try:
518+
indent = he_pm.trace.root.indent
519+
he_pm.hook.he_method1(arg=1)
520+
assert indent == he_pm.trace.root.indent
521+
assert len(out) == 2
522+
assert 'he_method1' in out[0]
523+
assert 'finish' in out[1]
524+
525+
out[:] = []
526+
he_pm.register(api2())
527+
528+
with pytest.raises(ValueError):
529+
he_pm.hook.he_method1(arg=1)
530+
assert he_pm.trace.root.indent == indent
531+
assert saveindent[0] > indent
532+
finally:
533+
undo()
534+
535+
536+
@pytest.mark.parametrize('include_hookspec', [True, False])
537+
def test_prefix_hookimpl(include_hookspec):
538+
with pytest.deprecated_call():
539+
pm = PluginManager(hookspec.project_name, "hello_")
540+
541+
if include_hookspec:
542+
class HookSpec(object):
543+
@hookspec
544+
def hello_myhook(self, arg1):
545+
""" add to arg1 """
546+
547+
pm.add_hookspecs(HookSpec)
548+
549+
class Plugin(object):
550+
def hello_myhook(self, arg1):
551+
return arg1 + 1
552+
553+
with pytest.deprecated_call():
554+
pm.register(Plugin())
555+
pm.register(Plugin())
556+
results = pm.hook.hello_myhook(arg1=17)
557+
assert results == [18, 18]
558+
559+
560+
def test_prefix_hookimpl_dontmatch_module():
561+
with pytest.deprecated_call():
562+
pm = PluginManager(hookspec.project_name, "hello_")
563+
564+
class BadPlugin(object):
565+
hello_module = __import__('email')
566+
567+
pm.register(BadPlugin())
568+
pm.check_pending()

0 commit comments

Comments
 (0)