"""Run every daedalus check: sanity, syntax, lint, and pytest with coverage. Single entry point for local runs and CI. Usage: python autotest.py # every check python autotest.py pytest json # only the named checks """ import argparse import concurrent.futures import functools import logging import subprocess import sys import time from ci import checks from ci import sanity logger = logging.getLogger(__name__) MIN_COVERAGE_PERCENT = 75 COVERAGE_SOURCES = ["ci", "daedalus", "python/src"] def run_each(check, suffixes: tuple[str, ...]) -> int: """Apply a per-file check to every repo file with a suffix.""" return sum(check(path) for path in checks.iter_files(checks.REPO_ROOT, suffixes)) def run_shellcheck() -> int: """shellcheck every shell script, including suffixless launchers.""" candidates = checks.iter_files(checks.REPO_ROOT, (*checks.SHELL_SUFFIXES, "")) return sum(checks.check_shell(path) for path in candidates if checks.is_shell_script(path)) def run_on_all(check, suffixes: tuple[str, ...]) -> int: """Apply a list check to every repo file with a suffix.""" return check(checks.iter_files(checks.REPO_ROOT, suffixes)) def run_pytest() -> int: """Run pytest in a subprocess so coverage is not skewed by imports here.""" cmd = [sys.executable, "-m", "pytest", "--cov-report=term-missing:skip-covered", f"--cov-fail-under={MIN_COVERAGE_PERCENT}"] cmd += [f"--cov={source}" for source in COVERAGE_SOURCES] return subprocess.run(cmd, cwd=checks.REPO_ROOT, check=False).returncode CHECKS = { "sanity": sanity.main, "python_syntax": functools.partial(run_each, checks.check_python_syntax, (".py",)), "pycodestyle": functools.partial(run_on_all, checks.check_pycodestyle, (".py",)), "shellcheck": run_shellcheck, "json": functools.partial(run_each, checks.check_json, (".json",)), "yaml": functools.partial(run_each, checks.check_yaml, checks.YAML_SUFFIXES), "markdown": functools.partial(run_on_all, checks.check_markdown, (".md",)), "pytest": run_pytest, } def timed(check) -> tuple[int, float]: """Run a check and return its error count and elapsed seconds.""" start = time.monotonic() return check(), time.monotonic() - start def main(names: list[str]) -> int: """Run the named checks in parallel, print a summary, return 1 on any failure.""" with concurrent.futures.ThreadPoolExecutor() as executor: futures = {name: executor.submit(timed, CHECKS[name]) for name in names} results = {name: future.result() for name, future in futures.items()} print("\n########################################") print("autotest results") print("########################################") for name, (errors, elapsed) in results.items(): print(f"[ {'PASS' if errors == 0 else 'FAIL'} ] {elapsed:6.2f}s {name}") return int(any(errors != 0 for errors, _ in results.values())) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("checks", nargs="*", metavar="CHECK", help=f"checks to run (default: all): {', '.join(CHECKS)}") selected = parser.parse_args().checks or list(CHECKS) unknown = sorted(set(selected) - set(CHECKS)) if unknown: parser.error(f"unknown check(s): {', '.join(unknown)}") logging.basicConfig(level=logging.INFO, format="%(levelname)-5s %(message)s") sys.exit(main(selected))