|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Runtime dependency smoke test for vcspull. |
| 3 | +
|
| 4 | +This script attempts to import every module within the ``vcspull`` package and |
| 5 | +invokes each CLI sub-command with ``--help``. It is intended to run inside an |
| 6 | +environment that only has the package's runtime dependencies installed to catch |
| 7 | +missing dependency declarations (for example, ``typing_extensions``). |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import argparse |
| 13 | +import importlib |
| 14 | +import pkgutil |
| 15 | +import subprocess |
| 16 | + |
| 17 | +ModuleName = str |
| 18 | + |
| 19 | + |
| 20 | +def parse_args() -> argparse.Namespace: |
| 21 | + """Return parsed CLI arguments for the smoke test runner.""" |
| 22 | + parser = argparse.ArgumentParser( |
| 23 | + description=( |
| 24 | + "Probe vcspull's runtime dependencies by importing all modules " |
| 25 | + "and exercising CLI entry points." |
| 26 | + ), |
| 27 | + ) |
| 28 | + parser.add_argument( |
| 29 | + "--package", |
| 30 | + default="vcspull", |
| 31 | + help="Root package name to inspect (defaults to vcspull).", |
| 32 | + ) |
| 33 | + parser.add_argument( |
| 34 | + "--cli-module", |
| 35 | + default="vcspull.cli", |
| 36 | + help="Module that exposes the create_parser helper for CLI discovery.", |
| 37 | + ) |
| 38 | + parser.add_argument( |
| 39 | + "--cli-executable", |
| 40 | + default="vcspull", |
| 41 | + help="Console script to run for CLI smoke checks.", |
| 42 | + ) |
| 43 | + parser.add_argument( |
| 44 | + "--cli-probe-arg", |
| 45 | + action="append", |
| 46 | + dest="cli_probe_args", |
| 47 | + default=None, |
| 48 | + help=( |
| 49 | + "Additional argument(s) appended after each CLI sub-command; " |
| 50 | + "may be repeated. Defaults to --help." |
| 51 | + ), |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + "--skip-imports", |
| 55 | + action="store_true", |
| 56 | + help="Skip module import validation.", |
| 57 | + ) |
| 58 | + parser.add_argument( |
| 59 | + "--skip-cli", |
| 60 | + action="store_true", |
| 61 | + help="Skip CLI command execution.", |
| 62 | + ) |
| 63 | + parser.add_argument( |
| 64 | + "--verbose", |
| 65 | + action="store_true", |
| 66 | + help="Print verbose output for each check.", |
| 67 | + ) |
| 68 | + return parser.parse_args() |
| 69 | + |
| 70 | + |
| 71 | +def discover_modules(package_name: str) -> list[ModuleName]: |
| 72 | + """Return a sorted list of module names within *package_name*.""" |
| 73 | + package = importlib.import_module(package_name) |
| 74 | + module_names: set[str] = {package_name} |
| 75 | + package_path = getattr(package, "__path__", None) |
| 76 | + if package_path is None: |
| 77 | + return sorted(module_names) |
| 78 | + module_names.update( |
| 79 | + module_info.name |
| 80 | + for module_info in pkgutil.walk_packages( |
| 81 | + package_path, |
| 82 | + prefix=f"{package_name}.", |
| 83 | + ) |
| 84 | + ) |
| 85 | + return sorted(module_names) |
| 86 | + |
| 87 | + |
| 88 | +def import_all_modules(module_names: list[ModuleName], verbose: bool) -> list[str]: |
| 89 | + """Attempt to import each module and return a list of failure messages.""" |
| 90 | + failures: list[str] = [] |
| 91 | + for module_name in module_names: |
| 92 | + if verbose: |
| 93 | + pass |
| 94 | + try: |
| 95 | + importlib.import_module(module_name) |
| 96 | + except Exception as exc: # pragma: no cover - reporting only |
| 97 | + detail = f"{module_name}: {exc!r}" |
| 98 | + failures.append(detail) |
| 99 | + return failures |
| 100 | + |
| 101 | + |
| 102 | +def _find_cli_subcommands( |
| 103 | + cli_module_name: str, |
| 104 | +) -> tuple[list[str], argparse.ArgumentParser]: |
| 105 | + """Return CLI sub-command names via vcspull.cli.create_parser.""" |
| 106 | + cli_module = importlib.import_module(cli_module_name) |
| 107 | + try: |
| 108 | + parser = cli_module.create_parser() # type: ignore[attr-defined] |
| 109 | + except AttributeError as exc: # pragma: no cover - defensive |
| 110 | + msg = f"{cli_module_name} does not expose create_parser()" |
| 111 | + raise RuntimeError(msg) from exc |
| 112 | + |
| 113 | + commands: set[str] = set() |
| 114 | + for action in parser._actions: # pragma: no branch - argparse internals |
| 115 | + if isinstance(action, argparse._SubParsersAction): |
| 116 | + commands.update(action.choices.keys()) |
| 117 | + return sorted(commands), parser |
| 118 | + |
| 119 | + |
| 120 | +def run_cli_command( |
| 121 | + executable: str, |
| 122 | + args: list[str], |
| 123 | + *, |
| 124 | + verbose: bool, |
| 125 | +) -> tuple[int, str, str]: |
| 126 | + """Execute CLI command and capture its result.""" |
| 127 | + command = [executable, *args] |
| 128 | + if verbose: |
| 129 | + pass |
| 130 | + result = subprocess.run( |
| 131 | + command, |
| 132 | + capture_output=True, |
| 133 | + text=True, |
| 134 | + check=False, |
| 135 | + ) |
| 136 | + if result.returncode != 0: |
| 137 | + if result.stdout: |
| 138 | + pass |
| 139 | + if result.stderr: |
| 140 | + pass |
| 141 | + return result.returncode, result.stdout, result.stderr |
| 142 | + |
| 143 | + |
| 144 | +def exercise_cli( |
| 145 | + executable: str, |
| 146 | + cli_module_name: str, |
| 147 | + probe_args: list[str], |
| 148 | + verbose: bool, |
| 149 | +) -> list[str]: |
| 150 | + """Run base CLI plus every sub-command, returning failure messages.""" |
| 151 | + failures: list[str] = [] |
| 152 | + subcommands, _parser = _find_cli_subcommands(cli_module_name) |
| 153 | + |
| 154 | + # Always test the base command with --help to verify the entry point. |
| 155 | + base_exit, _, _ = run_cli_command(executable, ["--help"], verbose=verbose) |
| 156 | + if base_exit != 0: |
| 157 | + failures.append(f"{executable} --help (exit code {base_exit})") |
| 158 | + |
| 159 | + for subcommand in subcommands: |
| 160 | + exit_code, _, _ = run_cli_command( |
| 161 | + executable, |
| 162 | + [subcommand, *probe_args], |
| 163 | + verbose=verbose, |
| 164 | + ) |
| 165 | + if exit_code != 0: |
| 166 | + failures.append( |
| 167 | + f"{executable} {subcommand} {' '.join(probe_args)} (exit code {exit_code})", |
| 168 | + ) |
| 169 | + return failures |
| 170 | + |
| 171 | + |
| 172 | +def main() -> int: |
| 173 | + """Entry point for the runtime dependency smoke test.""" |
| 174 | + args = parse_args() |
| 175 | + cli_probe_args = args.cli_probe_args or ["--help"] |
| 176 | + failures: list[str] = [] |
| 177 | + |
| 178 | + if not args.skip_imports: |
| 179 | + modules = discover_modules(args.package) |
| 180 | + failures.extend(import_all_modules(modules, args.verbose)) |
| 181 | + |
| 182 | + if not args.skip_cli: |
| 183 | + failures.extend( |
| 184 | + exercise_cli( |
| 185 | + args.cli_executable, |
| 186 | + args.cli_module, |
| 187 | + cli_probe_args, |
| 188 | + args.verbose, |
| 189 | + ), |
| 190 | + ) |
| 191 | + |
| 192 | + if failures: |
| 193 | + for _failure in failures: |
| 194 | + pass |
| 195 | + return 1 |
| 196 | + |
| 197 | + return 0 |
| 198 | + |
| 199 | + |
| 200 | +if __name__ == "__main__": |
| 201 | + raise SystemExit(main()) |
0 commit comments