|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Feature Smoke Test for Coverage Generation |
| 4 | +Safely imports modules from specific feature groups to generate coverage data. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import importlib.util |
| 9 | +import sys |
| 10 | +import pkgutil |
| 11 | +import logging |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(name)s:%(message)s') |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | +def is_excluded(file_path: str, global_excludes: list) -> bool: |
| 18 | + """Checks if a file path should be excluded based on global exclude patterns.""" |
| 19 | + path = Path(file_path).as_posix() |
| 20 | + for exclude in global_excludes: |
| 21 | + if Path(exclude).is_absolute(): |
| 22 | + if Path(path) == Path(exclude) or Path(path).is_relative_to(Path(exclude)): |
| 23 | + return True |
| 24 | + else: |
| 25 | + if exclude.endswith('/') and f"/{exclude}" in path: |
| 26 | + return True |
| 27 | + elif exclude.startswith('*') and path.endswith(exclude[1:]): |
| 28 | + return True |
| 29 | + elif exclude in path: |
| 30 | + return True |
| 31 | + return False |
| 32 | + |
| 33 | +def smoke_import_modules(root_packages: list[str], global_excludes: list[str]): |
| 34 | + """ |
| 35 | + Attempts to import all Python modules under the given root packages safely. |
| 36 | + Logs warnings for failures but does not stop execution. |
| 37 | + """ |
| 38 | + imported_count = 0 |
| 39 | + failed_count = 0 |
| 40 | + total_modules = 0 |
| 41 | + |
| 42 | + # Add current directory to sys.path to ensure local modules are discoverable |
| 43 | + sys.path.insert(0, os.getcwd()) |
| 44 | + |
| 45 | + all_modules = set() |
| 46 | + for root_package in root_packages: |
| 47 | + try: |
| 48 | + # Attempt to find the path for the root package |
| 49 | + spec = importlib.util.find_spec(root_package) |
| 50 | + if spec and spec.submodule_search_locations: |
| 51 | + root_path = spec.submodule_search_locations[0] |
| 52 | + else: |
| 53 | + # Fallback for packages not yet installed or in sys.path |
| 54 | + root_path = os.path.join(os.getcwd(), *root_package.split('.')) |
| 55 | + if not os.path.exists(root_path): |
| 56 | + logger.warning(f"Root package path not found for {root_package}. Skipping.") |
| 57 | + continue |
| 58 | + |
| 59 | + for importer, modname, ispkg in pkgutil.walk_packages([root_path], prefix=f"{root_package}."): |
| 60 | + module_path = os.path.join(importer.path, modname.replace('.', os.sep)) |
| 61 | + if ispkg: |
| 62 | + module_path = os.path.join(module_path, '__init__.py') |
| 63 | + else: |
| 64 | + module_path += '.py' |
| 65 | + |
| 66 | + if is_excluded(module_path, global_excludes): |
| 67 | + logger.debug(f"Excluding module: {modname} ({module_path})") |
| 68 | + continue |
| 69 | + all_modules.add(modname) |
| 70 | + except Exception as e: |
| 71 | + logger.warning(f"Could not walk packages for {root_package}: {e}") |
| 72 | + |
| 73 | + total_modules = len(all_modules) |
| 74 | + logger.info(f"📦 Found {total_modules} modules to import") |
| 75 | + |
| 76 | + for modname in sorted(list(all_modules)): |
| 77 | + try: |
| 78 | + importlib.import_module(modname) |
| 79 | + imported_count += 1 |
| 80 | + except (ImportError, SyntaxError, Exception) as e: |
| 81 | + logger.warning(f"Could not import {modname}: {e}") |
| 82 | + failed_count += 1 |
| 83 | + |
| 84 | + logger.info(f"✅ Successfully imported: {imported_count}") |
| 85 | + logger.warning(f"❌ Failed to import: {failed_count}") |
| 86 | + logger.info(f"📊 Total modules processed: {total_modules}") |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + from pathlib import Path |
| 90 | + REPO_ROOT = Path(__file__).parent.parent.parent |
| 91 | + |
| 92 | + # Define root packages and global excludes as per user's request |
| 93 | + ROOT_PACKAGES = ["stillme_core", "stillme_ethical_core"] |
| 94 | + GLOBAL_EXCLUDES = [ |
| 95 | + ".git/", ".github/", ".venv/", "venv/", "env/", "site-packages/", "dist/", |
| 96 | + "build/", "node_modules/", "artifacts/", "reports/", "htmlcov", |
| 97 | + "__pycache__/", "*.egg-info/", ".sandbox/" |
| 98 | + ] |
| 99 | + |
| 100 | + logger.info("🔥 Starting smoke import for coverage generation...") |
| 101 | + smoke_import_modules(ROOT_PACKAGES, GLOBAL_EXCLUDES) |
0 commit comments