|
| 1 | +""" |
| 2 | +UCKN Quality Metrics Dashboard |
| 3 | +
|
| 4 | +- Summarizes test results and coverage |
| 5 | +- Enforces quality gates |
| 6 | +- Generates reports for CI and local use |
| 7 | +""" |
| 8 | + |
| 9 | +import argparse |
| 10 | +import json |
| 11 | +import os |
| 12 | +import sys |
| 13 | +from typing import Dict, Any |
| 14 | + |
| 15 | +from tests.quality_metrics.coverage_analysis import ( |
| 16 | + load_coverage_json, |
| 17 | + extract_coverage_metrics, |
| 18 | + print_coverage_trend, |
| 19 | + generate_markdown_summary, |
| 20 | +) |
| 21 | + |
| 22 | +PYTEST_JSON = os.environ.get("UCKN_PYTEST_JSON", "pytest-report.json") |
| 23 | +COVERAGE_JSON = os.environ.get("UCKN_COVERAGE_JSON", "coverage.json") |
| 24 | +DEFAULT_FAIL_UNDER = 90 |
| 25 | + |
| 26 | + |
| 27 | +def load_pytest_json(path: str = PYTEST_JSON) -> Dict[str, Any]: |
| 28 | + if not os.path.exists(path): |
| 29 | + return {} |
| 30 | + with open(path) as f: |
| 31 | + return json.load(f) |
| 32 | + |
| 33 | + |
| 34 | +def summarize_pytest_results(pytest_json: Dict[str, Any]) -> Dict[str, Any]: |
| 35 | + summary = pytest_json.get("summary", {}) |
| 36 | + return { |
| 37 | + "passed": summary.get("passed", 0), |
| 38 | + "failed": summary.get("failed", 0), |
| 39 | + "skipped": summary.get("skipped", 0), |
| 40 | + "errors": summary.get("errors", 0), |
| 41 | + "duration": summary.get("duration", 0.0), |
| 42 | + "total": sum(summary.get(k, 0) for k in ["passed", "failed", "skipped", "errors"]), |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +def print_summary(pytest_metrics: Dict[str, Any], coverage_metrics: Dict[str, Any]): |
| 47 | + print("==== UCKN Quality Metrics Summary ====") |
| 48 | + print(f"Tests: {pytest_metrics['total']} | Passed: {pytest_metrics['passed']} | Failed: {pytest_metrics['failed']} | Skipped: {pytest_metrics['skipped']} | Errors: {pytest_metrics['errors']}") |
| 49 | + print(f"Test Duration: {pytest_metrics['duration']:.2f}s") |
| 50 | + print(f"Coverage: {coverage_metrics['percent_covered']}% lines, {coverage_metrics.get('percent_branches_covered', 'N/A')}% branches") |
| 51 | + print(f"Statements: {coverage_metrics['num_statements']} | Covered: {coverage_metrics['covered_lines']} | Missing: {coverage_metrics['missing_lines']}") |
| 52 | + print("======================================") |
| 53 | + |
| 54 | + |
| 55 | +def check_quality_gate(coverage_metrics: Dict[str, Any], fail_under: int = DEFAULT_FAIL_UNDER) -> bool: |
| 56 | + percent = coverage_metrics.get("percent_covered", 0) |
| 57 | + if percent is None: |
| 58 | + print("Coverage percent not found.") |
| 59 | + return False |
| 60 | + if percent < fail_under: |
| 61 | + print(f"FAIL: Coverage {percent}% is below threshold {fail_under}%") |
| 62 | + return False |
| 63 | + print(f"PASS: Coverage {percent}% meets threshold {fail_under}%") |
| 64 | + return True |
| 65 | + |
| 66 | + |
| 67 | +def main(): |
| 68 | + parser = argparse.ArgumentParser(description="UCKN Quality Metrics Dashboard") |
| 69 | + parser.add_argument("--summary", action="store_true", help="Print summary of test and coverage metrics") |
| 70 | + parser.add_argument("--check-gate", action="store_true", help="Check quality gate and exit nonzero if failed") |
| 71 | + parser.add_argument("--fail-under", type=int, default=DEFAULT_FAIL_UNDER, help="Coverage threshold for quality gate") |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + pytest_json = load_pytest_json() |
| 75 | + pytest_metrics = summarize_pytest_results(pytest_json) |
| 76 | + coverage_json = load_coverage_json() |
| 77 | + if not coverage_json: |
| 78 | + print("No coverage.json found.") |
| 79 | + sys.exit(1) |
| 80 | + coverage_metrics = extract_coverage_metrics(coverage_json) |
| 81 | + |
| 82 | + if args.summary: |
| 83 | + print_summary(pytest_metrics, coverage_metrics) |
| 84 | + print("Coverage trend:") |
| 85 | + print_trend = True |
| 86 | + try: |
| 87 | + print_trend = True |
| 88 | + print() |
| 89 | + print_coverage_trend() |
| 90 | + except Exception: |
| 91 | + print_trend = False |
| 92 | + if not print_trend: |
| 93 | + print("No coverage trend available.") |
| 94 | + |
| 95 | + if args.check_gate: |
| 96 | + ok = check_quality_gate(coverage_metrics, args.fail_under) |
| 97 | + if not ok: |
| 98 | + sys.exit(2) |
| 99 | + else: |
| 100 | + print("Quality gate passed.") |
| 101 | + |
| 102 | + if not args.summary and not args.check_gate: |
| 103 | + print_summary(pytest_metrics, coverage_metrics) |
| 104 | + print("Tip: Use --summary or --check-gate for more options.") |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments