|
| 1 | +"""Branch Coverage Tracking Module""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from typing import Final |
| 7 | + |
| 8 | +BRANCH_COVERAGE: dict[str, set[int]] = {"check_return_stmt": set()} |
| 9 | + |
| 10 | +BRANCH_DESCRIPTIONS: Final[dict[str, dict[int, str]]] = { |
| 11 | + "check_return_stmt": { |
| 12 | + 1: "Function entry", |
| 13 | + 2: "defn is not None - TRUE", |
| 14 | + 3: "defn is not None - FALSE", |
| 15 | + 4: "defn.is_generator - TRUE", |
| 16 | + 5: "defn.is_generator - FALSE (else/elif)", |
| 17 | + 6: "defn.is_coroutine - TRUE", |
| 18 | + 7: "defn.is_coroutine - FALSE (else)", |
| 19 | + 8: "isinstance(return_type, UninhabitedType) - TRUE", |
| 20 | + 9: "isinstance(return_type, UninhabitedType) - FALSE", |
| 21 | + 10: "not is_lambda and not return_type.ambiguous - TRUE", |
| 22 | + 11: "not is_lambda and not return_type.ambiguous - FALSE", |
| 23 | + 12: "s.expr - TRUE (has return value)", |
| 24 | + 13: "s.expr - FALSE (empty return)", |
| 25 | + 14: "isinstance(s.expr, (CallExpr, ...)) or isinstance(s.expr, AwaitExpr) - TRUE", |
| 26 | + 15: "isinstance(s.expr, (CallExpr, ...)) or isinstance(s.expr, AwaitExpr) - FALSE", |
| 27 | + 16: "isinstance(typ, Instance) and typ.type.fullname in NOT_IMPLEMENTED - TRUE", |
| 28 | + 17: "isinstance(typ, Instance) and typ.type.fullname in NOT_IMPLEMENTED - FALSE", |
| 29 | + 18: "defn.is_async_generator - TRUE", |
| 30 | + 19: "defn.is_async_generator - FALSE", |
| 31 | + 20: "isinstance(typ, AnyType) - TRUE", |
| 32 | + 21: "isinstance(typ, AnyType) - FALSE", |
| 33 | + 22: "warn_return_any conditions - TRUE (all conditions met)", |
| 34 | + 23: "warn_return_any conditions - FALSE (at least one condition not met)", |
| 35 | + 24: "declared_none_return - TRUE", |
| 36 | + 25: "declared_none_return - FALSE", |
| 37 | + 26: "is_lambda or isinstance(typ, NoneType) - TRUE", |
| 38 | + 27: "is_lambda or isinstance(typ, NoneType) - FALSE", |
| 39 | + 28: "defn.is_generator and not defn.is_coroutine and isinstance(return_type, AnyType) - TRUE", |
| 40 | + 29: "defn.is_generator and not defn.is_coroutine and isinstance(return_type, AnyType) - FALSE", |
| 41 | + 30: "isinstance(return_type, (NoneType, AnyType)) - TRUE", |
| 42 | + 31: "isinstance(return_type, (NoneType, AnyType)) - FALSE", |
| 43 | + 32: "self.in_checked_function() - TRUE", |
| 44 | + 33: "self.in_checked_function() - FALSE", |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +def record_branch(function_name: str, branch_id: int) -> None: |
| 50 | + """Record that a branch has been executed.""" |
| 51 | + if function_name in BRANCH_COVERAGE: |
| 52 | + BRANCH_COVERAGE[function_name].add(branch_id) |
| 53 | + |
| 54 | + |
| 55 | +def get_coverage_report() -> str: |
| 56 | + """Generate a coverage report as a string.""" |
| 57 | + report: list[str] = [] |
| 58 | + report.append("=" * 80) |
| 59 | + report.append("BRANCH COVERAGE REPORT") |
| 60 | + report.append("=" * 80) |
| 61 | + |
| 62 | + for func_name, covered_branches in BRANCH_COVERAGE.items(): |
| 63 | + report.append(f"\n{'=' * 80}") |
| 64 | + report.append(f"Function: {func_name}") |
| 65 | + report.append(f"{'=' * 80}") |
| 66 | + |
| 67 | + descriptions = BRANCH_DESCRIPTIONS.get(func_name, {}) |
| 68 | + total_branches = len(descriptions) |
| 69 | + covered_count = len(covered_branches) |
| 70 | + |
| 71 | + report.append( |
| 72 | + f"Coverage: {covered_count}/{total_branches} branches ({covered_count / total_branches * 100:.1f}%)" |
| 73 | + ) |
| 74 | + report.append("") |
| 75 | + |
| 76 | + for branch_id in sorted(descriptions.keys()): |
| 77 | + status = "COVERED" if branch_id in covered_branches else "NOT COVERED" |
| 78 | + desc = descriptions[branch_id] |
| 79 | + report.append(f" Branch {branch_id:2d}: {status:15s} | {desc}") |
| 80 | + |
| 81 | + uncovered = set(descriptions.keys()) - covered_branches |
| 82 | + if uncovered: |
| 83 | + report.append("\n" + "=" * 80) |
| 84 | + report.append("UNCOVERED BRANCHES:") |
| 85 | + report.append("=" * 80) |
| 86 | + for branch_id in sorted(uncovered): |
| 87 | + report.append(f" Branch {branch_id:2d}: {descriptions[branch_id]}") |
| 88 | + |
| 89 | + report.append("\n" + "=" * 80) |
| 90 | + return "\n".join(report) |
| 91 | + |
| 92 | + |
| 93 | +def save_coverage_report(filename: str = "branch_coverage_report.txt") -> None: |
| 94 | + """Save the coverage report to a file.""" |
| 95 | + report = get_coverage_report() |
| 96 | + output_path = Path.cwd() / filename |
| 97 | + with open(output_path, "w", encoding="utf-8") as f: |
| 98 | + f.write(report) |
| 99 | + print(f"\nCoverage report saved to: {output_path}") |
0 commit comments