def decorator_test(func): unwrapped = inspect.unwrap(func) module_name: str = unwrapped.__module__ is_home_module: bool = "." not in module_name if is_test_module_name(module_name) and is_home_module: force_path: Path = kwargs.get("_force_path") if force_path: path = force_path.absolute() else: path = get_absolute_path(unwrapped) if hasattr(unwrapped, "ward_meta"): unwrapped.ward_meta.description = description unwrapped.ward_meta.tags = tags unwrapped.ward_meta.path = path else: unwrapped.ward_meta = CollectionMetadata( description=description, tags=tags, path=path, ) collect_into = kwargs.get("_collect_into", COLLECTED_TESTS) collect_into[path].append(unwrapped) @functools.wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper return func
def testable_test(func): """ Decorate a function with this to treat it as a test that doesn't interfere with the "normal" tests, i.e. it collects into a separate location, uses a static path, module name etc. Useful for writing Ward internal tests. """ func.__module__ = "test_x" assert is_test_module_name(func.__module__) return test( "testable test description", _force_path=FORCE_TEST_PATH, _collect_into=defaultdict(list), )(func)
def load_modules(modules: Iterable[pkgutil.ModuleInfo]) -> List[ModuleType]: loaded_modules = [] for m in modules: if hasattr(m, "module_finder"): file_finder: FileFinder = m.module_finder spec: ModuleSpec = file_finder.find_spec(m.name) m = importlib.util.module_from_spec(spec) module_name = m.__name__ if is_test_module_name(module_name): pkg_data = _build_package_data(m) if pkg_data.pkg_root not in sys.path: sys.path.append(str(pkg_data.pkg_root)) m.__package__ = pkg_data.pkg_name m.__loader__.exec_module(m) loaded_modules.append(m) return loaded_modules
def is_test_module(module: pkgutil.ModuleInfo) -> bool: return is_test_module_name(module.name)