-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split tests into files and change flake8/pytest setup (#35)
* extends tox and travis config to more python versions * lines up closer with pep8 * fixes #32
- Loading branch information
1 parent
aef418c
commit 8e0c74d
Showing
13 changed files
with
1,249 additions
and
1,157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,5 +43,6 @@ def main(): | |
py_modules=['pluggy'], | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture( | ||
params=[ | ||
lambda spec: spec, | ||
lambda spec: spec() | ||
], | ||
ids=[ | ||
"spec-is-class", | ||
"spec-is-instance" | ||
], | ||
) | ||
def he_pm(request, pm): | ||
from pluggy import HookspecMarker | ||
hookspec = HookspecMarker("example") | ||
|
||
class Hooks: | ||
@hookspec | ||
def he_method1(self, arg): | ||
return arg + 1 | ||
|
||
pm.add_hookspecs(request.param(Hooks)) | ||
return pm | ||
|
||
|
||
@pytest.fixture | ||
def pm(): | ||
from pluggy import PluginManager | ||
return PluginManager("example") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from pluggy import PluginManager, HookimplMarker, HookspecMarker | ||
|
||
|
||
hookspec = HookspecMarker("example") | ||
hookimpl = HookimplMarker("example") | ||
|
||
|
||
def test_parse_hookimpl_override(): | ||
class MyPluginManager(PluginManager): | ||
def parse_hookimpl_opts(self, module_or_class, name): | ||
opts = PluginManager.parse_hookimpl_opts( | ||
self, module_or_class, name) | ||
if opts is None: | ||
if name.startswith("x1"): | ||
opts = {} | ||
return opts | ||
|
||
class Plugin: | ||
def x1meth(self): | ||
pass | ||
|
||
@hookimpl(hookwrapper=True, tryfirst=True) | ||
def x1meth2(self): | ||
pass | ||
|
||
class Spec: | ||
@hookspec | ||
def x1meth(self): | ||
pass | ||
|
||
@hookspec | ||
def x1meth2(self): | ||
pass | ||
|
||
pm = MyPluginManager(hookspec.project_name) | ||
pm.register(Plugin()) | ||
pm.add_hookspecs(Spec) | ||
assert not pm.hook.x1meth._nonwrappers[0].hookwrapper | ||
assert not pm.hook.x1meth._nonwrappers[0].tryfirst | ||
assert not pm.hook.x1meth._nonwrappers[0].trylast | ||
assert not pm.hook.x1meth._nonwrappers[0].optionalhook | ||
|
||
assert pm.hook.x1meth2._wrappers[0].tryfirst | ||
assert pm.hook.x1meth2._wrappers[0].hookwrapper | ||
|
||
|
||
def test_plugin_getattr_raises_errors(): | ||
"""Pluggy must be able to handle plugins which raise weird exceptions | ||
when getattr() gets called (#11). | ||
""" | ||
class DontTouchMe: | ||
def __getattr__(self, x): | ||
raise Exception('cant touch me') | ||
|
||
class Module: | ||
pass | ||
|
||
module = Module() | ||
module.x = DontTouchMe() | ||
|
||
pm = PluginManager(hookspec.project_name) | ||
# register() would raise an error | ||
pm.register(module, 'donttouch') | ||
assert pm.get_plugin('donttouch') is module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from pluggy import _formatdef, varnames | ||
|
||
|
||
def test_varnames(): | ||
def f(x): | ||
i = 3 # noqa | ||
|
||
class A: | ||
def f(self, y): | ||
pass | ||
|
||
class B(object): | ||
def __call__(self, z): | ||
pass | ||
|
||
assert varnames(f) == ("x",) | ||
assert varnames(A().f) == ('y',) | ||
assert varnames(B()) == ('z',) | ||
|
||
|
||
def test_varnames_default(): | ||
def f(x, y=3): | ||
pass | ||
|
||
assert varnames(f) == ("x",) | ||
|
||
|
||
def test_varnames_class(): | ||
class C: | ||
def __init__(self, x): | ||
pass | ||
|
||
class D: | ||
pass | ||
|
||
class E(object): | ||
def __init__(self, x): | ||
pass | ||
|
||
class F(object): | ||
pass | ||
|
||
assert varnames(C) == ("x",) | ||
assert varnames(D) == () | ||
assert varnames(E) == ("x",) | ||
assert varnames(F) == () | ||
|
||
|
||
def test_formatdef(): | ||
def function1(): | ||
pass | ||
|
||
assert _formatdef(function1) == 'function1()' | ||
|
||
def function2(arg1): | ||
pass | ||
|
||
assert _formatdef(function2) == "function2(arg1)" | ||
|
||
def function3(arg1, arg2="qwe"): | ||
pass | ||
|
||
assert _formatdef(function3) == "function3(arg1, arg2='qwe')" | ||
|
||
def function4(arg1, *args, **kwargs): | ||
pass | ||
|
||
assert _formatdef(function4) == "function4(arg1, *args, **kwargs)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import pytest | ||
from pluggy import PluginValidationError, HookimplMarker, HookspecMarker | ||
|
||
|
||
hookspec = HookspecMarker("example") | ||
hookimpl = HookimplMarker("example") | ||
|
||
|
||
def test_happypath(pm): | ||
class Api: | ||
@hookspec | ||
def hello(self, arg): | ||
"api hook 1" | ||
|
||
pm.add_hookspecs(Api) | ||
hook = pm.hook | ||
assert hasattr(hook, 'hello') | ||
assert repr(hook.hello).find("hello") != -1 | ||
|
||
class Plugin: | ||
@hookimpl | ||
def hello(self, arg): | ||
return arg + 1 | ||
|
||
plugin = Plugin() | ||
pm.register(plugin) | ||
l = hook.hello(arg=3) | ||
assert l == [4] | ||
assert not hasattr(hook, 'world') | ||
pm.unregister(plugin) | ||
assert hook.hello(arg=3) == [] | ||
|
||
|
||
def test_argmismatch(pm): | ||
class Api: | ||
@hookspec | ||
def hello(self, arg): | ||
"api hook 1" | ||
|
||
pm.add_hookspecs(Api) | ||
|
||
class Plugin: | ||
@hookimpl | ||
def hello(self, argwrong): | ||
pass | ||
|
||
with pytest.raises(PluginValidationError) as exc: | ||
pm.register(Plugin()) | ||
|
||
assert "argwrong" in str(exc.value) | ||
|
||
|
||
def test_only_kwargs(pm): | ||
class Api: | ||
@hookspec | ||
def hello(self, arg): | ||
"api hook 1" | ||
|
||
pm.add_hookspecs(Api) | ||
pytest.raises(TypeError, lambda: pm.hook.hello(3)) | ||
|
||
|
||
def test_firstresult_definition(pm): | ||
class Api: | ||
@hookspec(firstresult=True) | ||
def hello(self, arg): | ||
"api hook 1" | ||
|
||
pm.add_hookspecs(Api) | ||
|
||
class Plugin: | ||
@hookimpl | ||
def hello(self, arg): | ||
return arg + 1 | ||
|
||
pm.register(Plugin()) | ||
res = pm.hook.hello(arg=3) | ||
assert res == 4 |
Oops, something went wrong.