提交 a65de386 编写于 作者: L Lukáš Doktor

safeloader: Bundle module-relevant information into an object

We'll need to pass those information to other functions, let's bundle
them to make the code clearer. (keep using "path" internally as
accessing local variable is faster)
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 9f1c097b
......@@ -26,6 +26,18 @@ import sys
from ..utils import data_structures
class AvocadoModule(object):
"""
Representation of a module that might contain avocado.Test tests
"""
__slots__ = 'path', 'test_import', 'mod_import'
def __init__(self, path, test_import=False, mod_import=False):
self.path = path
self.test_import = test_import
self.mod_import = mod_import
def modules_imported_as(module):
"""
Returns a mapping of imported module names whether using aliases or not
......@@ -193,10 +205,7 @@ def find_avocado_tests(path, class_name=None):
force-disabled.
:rtype: tuple
"""
# The name used, in case of 'from avocado import Test as AvocadoTest'
test_import = ""
# If the "avocado" module itself was imported
mod_import = ""
module = AvocadoModule(path)
# The resulting test classes
result = collections.OrderedDict()
disabled = set()
......@@ -215,9 +224,9 @@ def find_avocado_tests(path, class_name=None):
for name in statement.names:
if name.name == 'Test':
if name.asname is not None:
test_import = name.asname
module.test_import = name.asname
else:
test_import = name.name
module.test_import = name.name
break
# Looking for a 'import avocado'
......@@ -225,9 +234,9 @@ def find_avocado_tests(path, class_name=None):
for name in statement.names:
if name.name == 'avocado':
if name.asname is not None:
mod_import = name.nasname
module.mod_import = name.nasname
else:
mod_import = name.name
module.mod_import = name.name
# Looking for a 'class Anything(anything):'
elif isinstance(statement, ast.ClassDef):
......@@ -328,22 +337,22 @@ def find_avocado_tests(path, class_name=None):
continue
if test_import:
if module.test_import:
base_ids = [base.id for base in statement.bases
if hasattr(base, 'id')]
# Looking for a 'class FooTest(Test):'
if test_import in base_ids:
if module.test_import in base_ids:
info = get_methods_info(statement.body,
cl_tags)
result[statement.name] = info
continue
# Looking for a 'class FooTest(avocado.Test):'
if mod_import:
if module.mod_import:
for base in statement.bases:
module = base.value.id
cls_module = base.value.id
klass = base.attr
if module == mod_import and klass == 'Test':
if cls_module == module.mod_import and klass == 'Test':
info = get_methods_info(statement.body,
cl_tags)
result[statement.name] = info
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册