|
| 1 | +""" |
| 2 | +Validate invariants for conformance result files. |
| 3 | +""" |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +import sys |
| 7 | +import tomllib |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +ALLOWED_RESULT_KEYS = frozenset( |
| 11 | + { |
| 12 | + "conformance_automated", |
| 13 | + "conformant", |
| 14 | + "errors_diff", |
| 15 | + "ignore_errors", |
| 16 | + "notes", |
| 17 | + "output", |
| 18 | + } |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def main() -> int: |
| 23 | + results_dir = Path(__file__).resolve().parent.parent / "results" |
| 24 | + issues: list[str] = [] |
| 25 | + checked = 0 |
| 26 | + |
| 27 | + for type_checker_dir in sorted(results_dir.iterdir()): |
| 28 | + if not type_checker_dir.is_dir(): |
| 29 | + continue |
| 30 | + for file in sorted(type_checker_dir.iterdir()): |
| 31 | + if file.name == "version.toml": |
| 32 | + continue |
| 33 | + checked += 1 |
| 34 | + try: |
| 35 | + with file.open("rb") as f: |
| 36 | + info = tomllib.load(f) |
| 37 | + except Exception as e: |
| 38 | + issues.append(f"{file.relative_to(results_dir)}: failed to parse TOML ({e})") |
| 39 | + continue |
| 40 | + |
| 41 | + issues.extend(_validate_result(file, results_dir, info)) |
| 42 | + |
| 43 | + if issues: |
| 44 | + print(f"Found {len(issues)} invariant violation(s) across {checked} file(s):") |
| 45 | + for issue in issues: |
| 46 | + print(f"- {issue}") |
| 47 | + return 1 |
| 48 | + |
| 49 | + print(f"Validated {checked} conformance result file(s); no invariant violations found.") |
| 50 | + return 0 |
| 51 | + |
| 52 | + |
| 53 | +def _validate_result(file: Path, results_dir: Path, info: dict[str, Any]) -> list[str]: |
| 54 | + issues: list[str] = [] |
| 55 | + rel_path = file.relative_to(results_dir) |
| 56 | + |
| 57 | + unknown_keys = sorted(set(info) - ALLOWED_RESULT_KEYS) |
| 58 | + if unknown_keys: |
| 59 | + issues.append( |
| 60 | + f"{rel_path}: unrecognized key(s): {', '.join(repr(key) for key in unknown_keys)}" |
| 61 | + ) |
| 62 | + |
| 63 | + automated = info.get("conformance_automated") |
| 64 | + if automated not in {"Pass", "Fail"}: |
| 65 | + issues.append( |
| 66 | + f"{rel_path}: conformance_automated must be 'Pass' or 'Fail' (got {automated!r})" |
| 67 | + ) |
| 68 | + return issues |
| 69 | + automated_is_pass = automated == "Pass" |
| 70 | + |
| 71 | + conformant = info.get("conformant") |
| 72 | + if conformant is None: |
| 73 | + if automated_is_pass: |
| 74 | + conformant_is_pass = True |
| 75 | + else: |
| 76 | + issues.append( |
| 77 | + f"{rel_path}: conformant is required when conformance_automated is 'Fail'" |
| 78 | + ) |
| 79 | + return issues |
| 80 | + elif isinstance(conformant, str): |
| 81 | + if conformant not in ("Pass", "Partial", "Unsupported"): |
| 82 | + issues.append(f"{rel_path}: invalid conformance status {conformant!r}") |
| 83 | + conformant_is_pass = conformant == "Pass" |
| 84 | + else: |
| 85 | + issues.append(f"{rel_path}: conformant must be a string when present") |
| 86 | + return issues |
| 87 | + |
| 88 | + if conformant_is_pass != automated_is_pass: |
| 89 | + issues.append( |
| 90 | + f"{rel_path}: conformant={conformant!r} does not match " |
| 91 | + f"conformance_automated={automated!r}" |
| 92 | + ) |
| 93 | + |
| 94 | + if conformant == "Partial": |
| 95 | + notes = info.get("notes", "") |
| 96 | + if not isinstance(notes, str) or not notes.strip(): |
| 97 | + issues.append( |
| 98 | + f"{rel_path}: notes must be present when checker is not fully conformant" |
| 99 | + ) |
| 100 | + |
| 101 | + return issues |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + raise SystemExit(main()) |
0 commit comments