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