run_mediacrawler_quality.py 1.84 KB
"""Run isolated MediaCrawler smoke tests from the BettaFish repository root."""

from __future__ import annotations

import argparse
import subprocess
import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[2]
VENDOR_ROOT = REPO_ROOT / "vendor" / "mediacrawler"
DEFAULT_TESTS = [
    "test/test_utils.py",
    "test/test_expiring_local_cache.py",
    "tests/test_store_factory.py",
    "tests/test_excel_store.py",
]


def parse_args(argv: list[str]) -> tuple[argparse.Namespace, list[str]]:
    parser = argparse.ArgumentParser(
        description="Run the isolated MediaCrawler smoke-test subset."
    )
    parser.add_argument(
        "--print-only",
        action="store_true",
        help="Print the resolved pytest command without executing it.",
    )
    return parser.parse_known_args(argv)


def build_command(extra_args: list[str]) -> list[str]:
    return [
        sys.executable,
        "-m",
        "pytest",
        "--rootdir",
        ".",
        *DEFAULT_TESTS,
        *extra_args,
    ]


def main(argv: list[str]) -> int:
    args, extra_args = parse_args(argv)

    if not VENDOR_ROOT.exists():
        print(f"MediaCrawler vendor directory not found: {VENDOR_ROOT}", file=sys.stderr)
        return 2

    if sys.version_info[:2] != (3, 11):
        print(
            "Warning: vendor/mediacrawler smoke tests are maintained against Python 3.11. "
            f"Current interpreter is {sys.version_info.major}.{sys.version_info.minor}.",
            file=sys.stderr,
        )

    command = build_command(extra_args)
    print(f"MediaCrawler root: {VENDOR_ROOT}")
    print("Pytest command:", " ".join(command))

    if args.print_only:
        return 0

    completed = subprocess.run(command, cwd=VENDOR_ROOT, check=False)
    return completed.returncode


if __name__ == "__main__":
    raise SystemExit(main(sys.argv[1:]))