提交 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 ...@@ -26,6 +26,18 @@ import sys
from ..utils import data_structures 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): def modules_imported_as(module):
""" """
Returns a mapping of imported module names whether using aliases or not Returns a mapping of imported module names whether using aliases or not
...@@ -193,10 +205,7 @@ def find_avocado_tests(path, class_name=None): ...@@ -193,10 +205,7 @@ def find_avocado_tests(path, class_name=None):
force-disabled. force-disabled.
:rtype: tuple :rtype: tuple
""" """
# The name used, in case of 'from avocado import Test as AvocadoTest' module = AvocadoModule(path)
test_import = ""
# If the "avocado" module itself was imported
mod_import = ""
# The resulting test classes # The resulting test classes
result = collections.OrderedDict() result = collections.OrderedDict()
disabled = set() disabled = set()
...@@ -215,9 +224,9 @@ def find_avocado_tests(path, class_name=None): ...@@ -215,9 +224,9 @@ def find_avocado_tests(path, class_name=None):
for name in statement.names: for name in statement.names:
if name.name == 'Test': if name.name == 'Test':
if name.asname is not None: if name.asname is not None:
test_import = name.asname module.test_import = name.asname
else: else:
test_import = name.name module.test_import = name.name
break break
# Looking for a 'import avocado' # Looking for a 'import avocado'
...@@ -225,9 +234,9 @@ def find_avocado_tests(path, class_name=None): ...@@ -225,9 +234,9 @@ def find_avocado_tests(path, class_name=None):
for name in statement.names: for name in statement.names:
if name.name == 'avocado': if name.name == 'avocado':
if name.asname is not None: if name.asname is not None:
mod_import = name.nasname module.mod_import = name.nasname
else: else:
mod_import = name.name module.mod_import = name.name
# Looking for a 'class Anything(anything):' # Looking for a 'class Anything(anything):'
elif isinstance(statement, ast.ClassDef): elif isinstance(statement, ast.ClassDef):
...@@ -328,22 +337,22 @@ def find_avocado_tests(path, class_name=None): ...@@ -328,22 +337,22 @@ def find_avocado_tests(path, class_name=None):
continue continue
if test_import: if module.test_import:
base_ids = [base.id for base in statement.bases base_ids = [base.id for base in statement.bases
if hasattr(base, 'id')] if hasattr(base, 'id')]
# Looking for a 'class FooTest(Test):' # 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, info = get_methods_info(statement.body,
cl_tags) cl_tags)
result[statement.name] = info result[statement.name] = info
continue continue
# Looking for a 'class FooTest(avocado.Test):' # Looking for a 'class FooTest(avocado.Test):'
if mod_import: if module.mod_import:
for base in statement.bases: for base in statement.bases:
module = base.value.id cls_module = base.value.id
klass = base.attr 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, info = get_methods_info(statement.body,
cl_tags) cl_tags)
result[statement.name] = info result[statement.name] = info
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册