提交 a79efb9a 编写于 作者: L Lukáš Doktor 提交者: Cleber Rosa

safeloader: Support "multiple import" with different name

This allows us to detect when someone imports avocado multiple times
with different names.
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
Signed-off-by: NCleber Rosa <crosa@redhat.com>
上级 6ae8da64
......@@ -30,12 +30,12 @@ class AvocadoModule(object):
"""
Representation of a module that might contain avocado.Test tests
"""
__slots__ = 'path', 'test_import', 'mod_import', 'mod'
__slots__ = 'path', 'test_imports', 'mod_imports', 'mod'
def __init__(self, path, test_import=False, mod_import=False):
def __init__(self, path):
self.path = path
self.test_import = test_import
self.mod_import = mod_import
self.test_imports = set()
self.mod_imports = set()
if os.path.isdir(path):
self.path = os.path.join(path, "__init__.py")
else:
......@@ -55,9 +55,9 @@ class AvocadoModule(object):
for name in statement.names:
if name.name == 'Test':
if name.asname is not None:
self.test_import = name.asname
self.test_imports.add(name.asname)
else:
self.test_import = name.name
self.test_imports.add(name.name)
break
# Looking for a 'import avocado'
......@@ -65,9 +65,9 @@ class AvocadoModule(object):
for name in statement.names:
if name.name == 'avocado':
if name.asname is not None:
self.mod_import = name.asname
self.mod_imports.add(name.asname)
else:
self.mod_import = name.name
self.mod_imports.add(name.name)
# Looking for a 'class Anything(anything):'
elif isinstance(statement, ast.ClassDef):
......@@ -347,25 +347,25 @@ def find_avocado_tests(path, class_name=None):
continue
# Looking for a 'class FooTest(Test):'
if module.test_import:
if module.test_imports:
base_ids = [base.id for base in klass.bases
if isinstance(base, ast.Name)]
# Looking for a 'class FooTest(Test):'
if module.test_import in base_ids:
if not module.test_imports.isdisjoint(base_ids):
info = get_methods_info(klass.body,
cl_tags)
result[klass.name] = info
continue
# Looking for a 'class FooTest(avocado.Test):'
if module.mod_import:
if module.mod_imports:
for base in klass.bases:
if not isinstance(base, ast.Attribute):
# Check only 'module.Class' bases
continue
cls_module = base.value.id
cls_name = base.attr
if cls_module == module.mod_import and cls_name == 'Test':
if cls_module in module.mod_imports and cls_name == 'Test':
info = get_methods_info(klass.body,
cl_tags)
result[klass.name] = info
......
# This currently only discovers 2 tests in avocado due to bug
import avocado as foo
import avocado as bar # pylint: disable=W0404
......
......@@ -511,12 +511,12 @@ class LoaderTest(unittest.TestCase):
self.assertEqual(os.path.abspath(tst[1]['name']), os.path.abspath(exp[1]))
def test_double_import(self):
# This is currently broken in Avocado, so let's just document the
# current behavior.
path = os.path.join(os.path.dirname(os.path.dirname(__file__)),
'.data', 'loader_instrumented', 'double_import.py')
tests = self.loader.discover(path)
exps = [('Test2', 'selftests/.data/loader_instrumented/double_import.py:Test2.test2'),
exps = [('Test1', 'selftests/.data/loader_instrumented/double_import.py:Test1.test1'),
('Test2', 'selftests/.data/loader_instrumented/double_import.py:Test2.test2'),
('Test3', 'selftests/.data/loader_instrumented/double_import.py:Test3.test3'),
('Test4', 'selftests/.data/loader_instrumented/double_import.py:Test4.test4')]
self._check_discovery(exps, tests)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册