|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Pytest discovery helper for Kubeflow components and pipelines. |
| 3 | +
|
| 4 | +This script discovers `tests/` directories under the provided component or |
| 5 | +pipeline paths and runs pytest with a two-minute timeout per test. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import sys |
| 12 | +import warnings |
| 13 | +from pathlib import Path |
| 14 | +from typing import List, Sequence |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from ..utils import get_repo_root, normalize_targets |
| 19 | + |
| 20 | +REPO_ROOT = get_repo_root() |
| 21 | +TIMEOUT_SECONDS = 120 |
| 22 | + |
| 23 | + |
| 24 | +def parse_args() -> argparse.Namespace: |
| 25 | + """Parse command-line arguments. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + Parsed command-line arguments. |
| 29 | + """ |
| 30 | + parser = argparse.ArgumentParser( |
| 31 | + description=( |
| 32 | + "Discover tests/ directories for the specified components or pipelines " |
| 33 | + "and execute pytest with a two-minute timeout per test." |
| 34 | + ) |
| 35 | + ) |
| 36 | + parser.add_argument( |
| 37 | + "paths", |
| 38 | + metavar="PATH", |
| 39 | + nargs="*", |
| 40 | + help=( |
| 41 | + "Component or pipeline directories (or files within them) to test. " |
| 42 | + "If omitted, all components and pipelines are scanned." |
| 43 | + ), |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "--timeout", |
| 47 | + type=int, |
| 48 | + default=TIMEOUT_SECONDS, |
| 49 | + help="Per-test timeout in seconds (default: 120).", |
| 50 | + ) |
| 51 | + parser.add_argument( |
| 52 | + "--verbose", |
| 53 | + action="store_true", |
| 54 | + help="Pass the -vv flag to pytest for more detailed output.", |
| 55 | + ) |
| 56 | + return parser.parse_args() |
| 57 | + |
| 58 | + |
| 59 | +def discover_test_dirs(targets: Sequence[Path]) -> List[Path]: |
| 60 | + """Discover tests/ directories under the given targets. |
| 61 | +
|
| 62 | + Args: |
| 63 | + targets: Sequence of component or pipeline paths to search. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + List of discovered tests/ directory paths. |
| 67 | + """ |
| 68 | + discovered: List[Path] = [] |
| 69 | + |
| 70 | + for target in targets: |
| 71 | + search_root = target if target.is_dir() else target.parent |
| 72 | + if not search_root.exists(): |
| 73 | + continue |
| 74 | + |
| 75 | + direct = search_root / "tests" |
| 76 | + if direct.is_dir() and _is_member_of_pipeline_or_component(direct): |
| 77 | + if direct not in discovered: |
| 78 | + discovered.append(direct) |
| 79 | + |
| 80 | + return discovered |
| 81 | + |
| 82 | + |
| 83 | +def _is_member_of_pipeline_or_component(candidate: Path) -> bool: |
| 84 | + """Check if a path is within components/ or pipelines/ directory. |
| 85 | +
|
| 86 | + Args: |
| 87 | + candidate: Path to check. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + True if the path is within components/ or pipelines/, False otherwise. |
| 91 | + """ |
| 92 | + try: |
| 93 | + relative = candidate.relative_to(REPO_ROOT) |
| 94 | + except ValueError: |
| 95 | + warnings.warn( |
| 96 | + f"Unable to determine relative path for {candidate} " f"relative to repo root {REPO_ROOT}. Skipping.", |
| 97 | + ) |
| 98 | + return False |
| 99 | + |
| 100 | + return relative.parts and relative.parts[0] in {"components", "pipelines"} |
| 101 | + |
| 102 | + |
| 103 | +def build_pytest_args( |
| 104 | + test_dirs: Sequence[Path], |
| 105 | + timeout_seconds: int, |
| 106 | + verbose: bool, |
| 107 | +) -> List[str]: |
| 108 | + """Build pytest command-line arguments. |
| 109 | +
|
| 110 | + Args: |
| 111 | + test_dirs: Directories containing tests to run. |
| 112 | + timeout_seconds: Per-test timeout in seconds. |
| 113 | + verbose: Whether to enable verbose pytest output. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + List of pytest command-line arguments. |
| 117 | + """ |
| 118 | + args: List[str] = [ |
| 119 | + f"--timeout={timeout_seconds}", |
| 120 | + "--timeout-method=signal", |
| 121 | + ] |
| 122 | + if verbose: |
| 123 | + args.append("-vv") |
| 124 | + |
| 125 | + args.extend(str(directory) for directory in test_dirs) |
| 126 | + return args |
| 127 | + |
| 128 | + |
| 129 | +def main() -> int: |
| 130 | + """Main entry point for running component/pipeline tests. |
| 131 | +
|
| 132 | + Returns: |
| 133 | + Exit code (0 for success, non-zero for failure). |
| 134 | + """ |
| 135 | + args = parse_args() |
| 136 | + targets = normalize_targets(args.paths) |
| 137 | + test_dirs = discover_test_dirs(targets) |
| 138 | + |
| 139 | + if not test_dirs: |
| 140 | + print("No tests/ directories found under the supplied paths. Nothing to do.") |
| 141 | + return 0 |
| 142 | + |
| 143 | + relative_dirs = ", ".join(str(directory.relative_to(REPO_ROOT)) for directory in test_dirs) |
| 144 | + print(f"Running pytest for: {relative_dirs}") |
| 145 | + |
| 146 | + pytest_args = build_pytest_args( |
| 147 | + test_dirs=test_dirs, |
| 148 | + timeout_seconds=args.timeout, |
| 149 | + verbose=args.verbose, |
| 150 | + ) |
| 151 | + |
| 152 | + exit_code = pytest.main(pytest_args) |
| 153 | + if exit_code == 0: |
| 154 | + print("✅ Pytest completed successfully.") |
| 155 | + else: |
| 156 | + print("❌ Pytest reported failures. See log above for details.") |
| 157 | + |
| 158 | + return exit_code |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + sys.exit(main()) |
0 commit comments