#!/usr/bin/env python3 """Fail closed when a test-shaped file is absent from the reviewed CI inventory. This is an inventory check, not evidence that a referenced command executes the intended test. Dynamic runners and test-level selections still need their own contracts. The workflow and scripts listed below are checked as literal evidence and are not parsed as a shell or workflow program. """ from __future__ import annotations import argparse from functools import cache import json from pathlib import Path, PurePosixPath import re import sys ROOT = Path(__file__).resolve().parent.parent INVENTORY = ROOT / 'tests/gates/ci-test-inventory.json' PROJECT = 'hf' if (ROOT / 'pyrightconfig.json').is_file() else 'github' KINDS = {'direct', 'script', 'manifest', 'helper', 'excluded'} SUFFIXES = {'.py', '.js', '.mjs'} def clean_path(value, parent=None): if not isinstance(value, str) or not value or '\\' in value: raise ValueError('path must be a nonempty POSIX string') path = PurePosixPath(value) if path.is_absolute() or any(part in ('.', '..') for part in value.split('/')) or str(path) != value: raise ValueError(f'unsafe path: {value}') if parent and path.parts[0] != parent: raise ValueError(f'expected {parent}/ path: {value}') return path def test_files(root): directory = root / 'tests' if not directory.is_dir() or directory.is_symlink(): raise ValueError('tests directory missing or symlinked') files = set() for path in directory.rglob('*'): if path.is_symlink() and (path.is_dir() or (path.name.startswith('test_') and path.suffix in SUFFIXES)): raise ValueError(f'symlinked test path: {path.relative_to(root)}') if path.is_file() and path.name.startswith('test_') and path.suffix in SUFFIXES: files.add(path.relative_to(root).as_posix()) if not files: raise ValueError('no tests found') return files def uncomment(text): return '\n'.join(line for line in text.splitlines() if not line.lstrip().startswith('#')) def referenced(path, text): dotted = path[:-3].replace('/', '.') if path.endswith('.py') else '' return any(re.search(r'(?