grim/pyscovery
Clone
Summary
Browse
Changes
Graph
Fixed the last of the known bugs for Loader class
2013-05-26, Gary Kramlich
19570d2d0da1
Fixed the last of the known bugs for Loader class
# pyscovery - A python plugin finder
# Copyright (C) 2013 Gary Kramlich <grim@reaperworld.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Unit tests for pyscovery
"""
import
inspect
import
unittest
import
pyscovery
from
tests.modules
import
create
# pylint:disable-msg=R0913
class
Plugin
(
object
):
# pylint:disable-msg=R0903
"""
The class all the test plugins descend from
"""
pass
def
_test_module
(
test
,
module
,
count
,
recurse
=
False
,
instantiate
=
False
):
"""
Given a module, test that we find count plugins
"""
pyscovery
.
add_module
(
module
)
gen
=
pyscovery
.
find
(
Plugin
,
recurse
,
instantiate
)
test
.
assertTrue
(
inspect
.
isgenerator
(
gen
))
found
=
list
(
gen
)
test
.
assertEqual
(
len
(
found
),
count
,
'plugins found:
{}
'
.
format
(
len
(
found
)))
if
instantiate
:
for
plugin
in
found
:
test
.
assertIsInstance
(
plugin
,
Plugin
)
return
found
def
_test_module_count
(
test
,
count
):
"""
Asserts if the number of paths doesn't match count
"""
modules
=
pyscovery
.
get_modules
()
test
.
assertEqual
(
len
(
modules
),
count
,
'search paths
{}
'
.
format
(
modules
))
class
TestModules
(
unittest
.
TestCase
):
# pylint:disable-msg=R0904
"""
Unit tests for path manipulation
"""
module
=
'test'
def
setUp
(
self
):
# pylint:disable-msg=C0103
pyscovery
.
clear_modules
()
def
tearDown
(
self
):
# pylint:disable-msg=C0103
_test_module_count
(
self
,
0
)
pyscovery
.
clear_modules
()
def
test_add_remove
(
self
):
"""
Add and remove a single plugin path
"""
pyscovery
.
add_module
(
TestModules
.
module
)
_test_module_count
(
self
,
1
)
pyscovery
.
remove_module
(
TestModules
.
module
)
def
test_add_existing
(
self
):
"""
Add the same plugin path twice, and make sure it's only in there once
"""
pyscovery
.
add_module
(
TestModules
.
module
)
pyscovery
.
add_module
(
TestModules
.
module
)
_test_module_count
(
self
,
1
)
pyscovery
.
remove_module
(
TestModules
.
module
)
def
test_remove_nonexistant
(
self
):
# pylint:disable-msg=R0201
"""
Try to remove a non-existant plugin path
"""
pyscovery
.
remove_module
(
TestModules
.
module
)
def
test_add_multiple
(
self
):
"""
Add multiple paths to the search paths
"""
second
=
'
{}
.1'
.
format
(
TestModules
.
module
)
pyscovery
.
add_module
(
TestModules
.
module
)
_test_module_count
(
self
,
1
)
pyscovery
.
add_module
(
second
)
_test_module_count
(
self
,
2
)
pyscovery
.
remove_module
(
TestModules
.
module
)
pyscovery
.
remove_module
(
second
)
class
TestFind
(
unittest
.
TestCase
):
# pylint:disable-msg=R0904
"""
Basic tests for the find function
"""
def
test_none
(
self
):
"""
Test that a TypeError is raised when find is called with None
"""
self
.
assertRaises
(
TypeError
,
pyscovery
.
find
(
None
,
False
,
False
))
def
test_string
(
self
):
"""
Test that a TypeError is raised when find is called with a string
"""
self
.
assertRaises
(
TypeError
,
pyscovery
.
find
(
''
,
False
,
False
))
def
test_int
(
self
):
"""
Test that a TypeError is raised when find is called with an int
"""
self
.
assertRaises
(
TypeError
,
pyscovery
.
find
(
0
,
False
,
False
))
def
test_old_style_class
(
self
):
# pylint:disable-msg=R0201
"""
Test that a TypeError is NOT raised when find is called with an old
style class
"""
class
Test
:
# pylint:disable-msg=R0903,W0232
""" old style class """
pass
pyscovery
.
find
(
Test
,
False
,
False
)
def
test_new_style_class
(
self
):
# pylint:disable-msg=R0201
"""
Test that a TypeError is NOT raised when find is called with an new
style class
"""
class
Test
(
object
):
# pylint:disable-msg=R0903
""" new style class """
pass
pyscovery
.
find
(
Test
,
False
,
False
)
class
TestModule
(
unittest
.
TestCase
):
# pylint:disable-msg=R0904
""" Tests the discovery method of pyscovery """
def
setUp
(
self
):
# pylint:disable-msg=C0103
pyscovery
.
clear_modules
()
def
tearDown
(
self
):
# pylint:disable-msg=C0103
pyscovery
.
clear_modules
()
def
test_single
(
self
):
""" Test a module with a single plugin """
_test_module
(
self
,
'tests.modules.single'
,
1
)
def
test_multiple
(
self
):
""" Test a module with multiple plugins """
_test_module
(
self
,
'tests.modules.multiple'
,
2
)
def
test_mixed
(
self
):
""" Test a module with more than just plugins """
_test_module
(
self
,
'tests.modules.mixed'
,
2
)
class
TestPackage
(
unittest
.
TestCase
):
# pylint:disable-msg=R0904
""" Tests the find method of pyscovery on a package """
def
setUp
(
self
):
# pylint:disable-msg=C0103
pyscovery
.
clear_modules
()
def
tearDown
(
self
):
# pylint:disable-msg=C0103
pyscovery
.
clear_modules
()
def
test_package_without_recurse
(
self
):
""" Test a package without recursion """
_test_module
(
self
,
'tests.modules'
,
0
)
def
test_package_recursion
(
self
):
""" Test a package with recursion """
_test_module
(
self
,
'tests.modules.deep'
,
2
,
recurse
=
True
)
def
test_package_deep_recursion
(
self
):
""" Test a package with recursion """
_test_module
(
self
,
'tests.modules'
,
7
,
recurse
=
True
)
class
TestCreate
(
unittest
.
TestCase
):
# pylint:disable-msg=R0904
""" Tests the find method with the create option on """
def
setUp
(
self
):
# pylint:disable-msg=C0103
pyscovery
.
clear_modules
()
def
test_create_without_recurse
(
self
):
""" Test a package with create but without recursion """
_test_module
(
self
,
'tests.modules.deep'
,
1
,
instantiate
=
True
)
def
test_create_with_recursion
(
self
):
""" Test a package with create and recursion """
_test_module
(
self
,
'tests.modules.deep'
,
2
,
recurse
=
True
,
instantiate
=
True
)
def
test_create_with_deep_recursion
(
self
):
""" Test a packge with create and deep recursion """
_test_module
(
self
,
'tests.modules'
,
7
,
recurse
=
True
,
instantiate
=
True
)
class
TestCreateWithParameters
(
unittest
.
TestCase
):
# pylint:disable-msg=R0904
""" Tests for finding and creating plugins that take arguments """
def
setUp
(
self
):
# pylint:disable-msg=C0103
pyscovery
.
clear_modules
()
pyscovery
.
add_module
(
'tests.modules.create'
)
def
test_create_args
(
self
):
""" Tests that create works with args """
for
plugin
in
pyscovery
.
find
(
create
.
ArgsPlugin
,
False
,
True
,
'one'
,
'two'
):
self
.
assertTupleEqual
(
plugin
.
args
,
(
'one'
,
'two'
))
def
test_create_kwargs
(
self
):
""" Tests that create works with kwargs """
for
plugin
in
pyscovery
.
find
(
create
.
KwArgsPlugin
,
False
,
True
,
one
=
1
,
two
=
2
):
self
.
assertDictEqual
(
plugin
.
kwargs
,
{
'one'
:
1
,
'two'
:
2
})
def
test_both_args
(
self
):
""" Tests the create works with args and kwargs """
for
plugin
in
pyscovery
.
find
(
create
.
BothArgsPlugin
,
False
,
True
,
'one'
,
'two'
,
one
=
1
,
two
=
2
):
self
.
assertTupleEqual
(
plugin
.
args
,
(
'one'
,
'two'
))
self
.
assertDictEqual
(
plugin
.
kwargs
,
{
'one'
:
1
,
'two'
:
2
})