jw-devtest/src/python/devtest/os/tcf/CaseDir.py

63 lines
2.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import glob
import importlib.util
from abc import abstractmethod
import asyncio
from jwutils.log import *
from jwutils.misc import *
from ..TestCaseFactory import TestCaseFactory
from ..TestCase import TestCase as TestCaseBase
from ..TestPhases import TestPhases
from ..Connection import Connection
from pathlib import PurePath
class CaseDir(TestCaseFactory): # export
# just to identify test cases as to be instantiated by this factory
class Case(TestCaseBase):
def __init__(self):
super().__init__()
class CaseWrapper(Case):
def __init__(self, case):
self.__case = case
async def _run(self, env, machine, phase):
return await self.__case.run(env, machine, phase)
def __init__(self, path):
super().__init__(path)
self.__cases = []
for p in path:
slog(INFO, "importing test cases from {}".format(p))
for phase in TestPhases.Phase:
pattern = p + '/' + phase.name.lower() + '/*.py'
slog(INFO, " importing test cases for phase {:<8} from {}".format(phase.name, pattern))
sources = sorted(glob.glob(pattern))
for s in sources:
slog(DEBUG, " found source", s)
spec = importlib.util.spec_from_file_location(s, s) # args are (name, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
members = inspect.getmembers(mod, inspect.isclass)
for name, c in members:
slog(DEBUG, " found member", name)
if c.__module__ != s:
slog(DEBUG, " is not defined in", s, "ignoring")
continue
if inspect.isabstract(c):
slog(DEBUG, " is abstract, ignoring")
continue
if not CaseDir.Case in inspect.getmro(c):
slog(DEBUG, " is not a CaseDir.Case, ignoring")
continue
case = c()
case.phases = [phase]
case.source = s
case.name = name + ' from ' + s
case.short_name = name + '@' + PurePath(s).stem
self.__cases.append(case)
slog(INFO, "adding test cases:", ', '.join([c.name for c in self.__cases]))
def test_cases(self):
return self.__cases