grim/pyscovery
Clone
Summary
Browse
Changes
Graph
pylint cleanups..
2013-04-14, Gary Kramlich
4430918d4bf7
pylint cleanups..
# 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
def
_test_module
(
test
,
module
,
count
,
recurse
=
False
):
"""
Given a module, test that we find count plugins
"""
pyscovery
.
add_module
(
module
)
gen
=
pyscovery
.
find
(
Plugin
,
recurse
=
recurse
)
test
.
assertTrue
(
inspect
.
isgenerator
(
gen
))
found
=
list
(
gen
)
test
.
assertEqual
(
len
(
found
),
count
,
'plugins found:
{}
'
.
format
(
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
Plugin
(
object
):
# pylint:disable-msg=R0903
"""
The class all the test plugins descend from
"""
pass
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
))
def
test_string
(
self
):
"""
Test that a TypeError is raised when find is called with a string
"""
self
.
assertRaises
(
TypeError
,
pyscovery
.
find
(
''
))
def
test_int
(
self
):
"""
Test that a TypeError is raised when find is called with an int
"""
self
.
assertRaises
(
TypeError
,
pyscovery
.
find
(
0
))
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
)
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
)
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
)