mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-15 23:31:29 +00:00
Replaces the unused exception-based run_audit/LedgerDriftError with a FunctionReport value so a missing ledger is modeled as data, not an error.
123 lines
4.4 KiB
Python
123 lines
4.4 KiB
Python
from __future__ import annotations
|
|
|
|
import ast
|
|
import re
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
from .ledger import TestLedger, ledger_path_for, load_ledger
|
|
|
|
REPO_ROOT = Path(__file__).parent.parent.parent
|
|
|
|
_RUST_TEST_PATTERN = re.compile(
|
|
r"#\[(?:test|tokio::test)\][^\n]*\n(?:[^\n]*\n)*?\s*(?:async\s+)?fn\s+(\w+)\s*\("
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class AuditReport:
|
|
missing_python_tests: tuple[str, ...]
|
|
stale_python_tests: tuple[str, ...]
|
|
missing_rust_tests: tuple[str, ...]
|
|
stale_rust_tests: tuple[str, ...]
|
|
|
|
@property
|
|
def is_clean(self) -> bool:
|
|
return not (
|
|
self.missing_python_tests
|
|
or self.stale_python_tests
|
|
or self.missing_rust_tests
|
|
or self.stale_rust_tests
|
|
)
|
|
|
|
|
|
def _enumerate_python_tests(repo_root: Path, relative_path: str) -> frozenset[str]:
|
|
source = (repo_root / relative_path).read_text(encoding="utf-8")
|
|
tree = ast.parse(source, filename=relative_path)
|
|
|
|
module_level: list[str] = []
|
|
for node in ast.iter_child_nodes(tree):
|
|
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name.startswith("test_"):
|
|
module_level.append(node.name)
|
|
elif isinstance(node, ast.ClassDef):
|
|
for child in ast.iter_child_nodes(node):
|
|
if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)) and child.name.startswith(
|
|
"test_"
|
|
):
|
|
module_level.append(f"{node.name}::{child.name}")
|
|
|
|
return frozenset(module_level)
|
|
|
|
|
|
def _enumerate_rust_tests(repo_root: Path, relative_path: str) -> frozenset[str]:
|
|
source = (repo_root / relative_path).read_text(encoding="utf-8")
|
|
return frozenset(match.group(1) for match in _RUST_TEST_PATTERN.finditer(source))
|
|
|
|
|
|
def _ledger_python_tests_by_file(ledger: TestLedger) -> dict[str, set[str]]:
|
|
grouping: dict[str, set[str]] = {path: set() for path in ledger.python_scope}
|
|
for entry in ledger.entries:
|
|
grouping.setdefault(entry.python_file, set()).add(entry.python_test)
|
|
return grouping
|
|
|
|
|
|
def _ledger_rust_tests_by_file(ledger: TestLedger) -> dict[str, set[str]]:
|
|
grouping: dict[str, set[str]] = {path: set() for path in ledger.rust_scope}
|
|
for entry in ledger.entries:
|
|
if entry.status == "mapped":
|
|
grouping.setdefault(entry.rust_file, set()).add(entry.rust_test)
|
|
for rust_only in ledger.rust_only_tests:
|
|
grouping.setdefault(rust_only.rust_file, set()).add(rust_only.rust_test)
|
|
return grouping
|
|
|
|
|
|
def audit_ledger(ledger: TestLedger, repo_root: Path = REPO_ROOT) -> AuditReport:
|
|
missing_python: list[str] = []
|
|
stale_python: list[str] = []
|
|
for python_file, ledger_tests in _ledger_python_tests_by_file(ledger).items():
|
|
actual_tests = _enumerate_python_tests(repo_root, python_file)
|
|
for missing in sorted(ledger_tests - actual_tests):
|
|
missing_python.append(f"{python_file}:{missing}")
|
|
for stale in sorted(actual_tests - ledger_tests):
|
|
stale_python.append(f"{python_file}:{stale}")
|
|
|
|
missing_rust: list[str] = []
|
|
stale_rust: list[str] = []
|
|
for rust_file, ledger_tests in _ledger_rust_tests_by_file(ledger).items():
|
|
actual_tests = _enumerate_rust_tests(repo_root, rust_file)
|
|
for missing in sorted(ledger_tests - actual_tests):
|
|
missing_rust.append(f"{rust_file}:{missing}")
|
|
for stale in sorted(actual_tests - ledger_tests):
|
|
stale_rust.append(f"{rust_file}:{stale}")
|
|
|
|
return AuditReport(
|
|
missing_python_tests=tuple(missing_python),
|
|
stale_python_tests=tuple(stale_python),
|
|
missing_rust_tests=tuple(missing_rust),
|
|
stale_rust_tests=tuple(stale_rust),
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class FunctionReport:
|
|
sdk_function: str
|
|
ledger: TestLedger | None
|
|
audit: AuditReport | None
|
|
|
|
@property
|
|
def has_ledger(self) -> bool:
|
|
return self.ledger is not None
|
|
|
|
@property
|
|
def is_clean(self) -> bool:
|
|
return self.audit is None or self.audit.is_clean
|
|
|
|
|
|
def build_function_report(sdk_function: str, repo_root: Path = REPO_ROOT) -> FunctionReport:
|
|
path = ledger_path_for(sdk_function)
|
|
if not path.exists():
|
|
return FunctionReport(sdk_function=sdk_function, ledger=None, audit=None)
|
|
ledger = load_ledger(path)
|
|
return FunctionReport(
|
|
sdk_function=sdk_function, ledger=ledger, audit=audit_ledger(ledger, repo_root)
|
|
)
|