|
| 1 | +"""LCOV reporting for slipcover""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import sys |
| 6 | +from collections import defaultdict |
| 7 | +from typing import TYPE_CHECKING, Dict, List, Optional, Tuple |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from typing import IO |
| 11 | + |
| 12 | + from .schemas import Coverage, CoverageFile |
| 13 | + |
| 14 | + |
| 15 | +def get_branch_info( |
| 16 | + file_data: CoverageFile, |
| 17 | +) -> Dict[int, List[Tuple[int, bool]]]: |
| 18 | + """Get information about branches for LCOV format. |
| 19 | +
|
| 20 | + Returns a dict mapping line numbers to a list of (branch_dest, was_taken) tuples. |
| 21 | +
|
| 22 | + """ |
| 23 | + # Branches may arrive as lists (e.g. after a JSON round-trip via --merge) |
| 24 | + # rather than tuples, so normalize before hashing into a set. |
| 25 | + missing_branches = {tuple(b) for b in file_data.get("missing_branches", [])} |
| 26 | + all_branches = sorted( |
| 27 | + tuple(b) for b in file_data.get("executed_branches", []) + file_data.get("missing_branches", []) |
| 28 | + ) |
| 29 | + |
| 30 | + # Group branches by their source line |
| 31 | + branches_by_line: Dict[int, List[Tuple[int, bool]]] = defaultdict(list) |
| 32 | + |
| 33 | + for branch in all_branches: |
| 34 | + src_line, dest_line = branch |
| 35 | + is_taken = branch not in missing_branches |
| 36 | + branches_by_line[src_line].append((dest_line, is_taken)) |
| 37 | + |
| 38 | + return branches_by_line |
| 39 | + |
| 40 | + |
| 41 | +class LcovReporter: |
| 42 | + """A reporter for writing LCOV-style coverage results.""" |
| 43 | + |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + coverage: Coverage, |
| 47 | + with_branches: bool, |
| 48 | + test_name: Optional[str] = None, |
| 49 | + comments: Optional[List[str]] = None, |
| 50 | + ) -> None: |
| 51 | + self.coverage = coverage |
| 52 | + self.with_branches = with_branches |
| 53 | + self.test_name = test_name |
| 54 | + self.comments = comments or [] |
| 55 | + |
| 56 | + def report(self, outfile: IO[str] | None = None) -> None: |
| 57 | + """Generate an LCOV-compatible coverage report. |
| 58 | +
|
| 59 | + `outfile` is a file object to write the LCOV data to. |
| 60 | +
|
| 61 | + """ |
| 62 | + outfile = outfile or sys.stdout |
| 63 | + |
| 64 | + for comment in self.comments: |
| 65 | + outfile.write(f"# {comment}\n") |
| 66 | + |
| 67 | + for file_path, file_data in sorted(self.coverage["files"].items()): |
| 68 | + self._write_file_coverage(outfile, file_path, file_data) |
| 69 | + |
| 70 | + def _write_file_coverage( |
| 71 | + self, outfile: IO[str], file_path: str, file_data: CoverageFile |
| 72 | + ) -> None: |
| 73 | + """Write LCOV coverage data for a single file.""" |
| 74 | + |
| 75 | + # TN: Test Name (optional) |
| 76 | + if self.test_name is not None: |
| 77 | + outfile.write(f"TN:{self.test_name}\n") |
| 78 | + |
| 79 | + # SF: Source File |
| 80 | + sf_path = file_path.replace('\\', '/') |
| 81 | + outfile.write(f"SF:{sf_path}\n") |
| 82 | + |
| 83 | + # Get all lines (both executed and missing) |
| 84 | + all_lines = sorted(file_data["executed_lines"] + file_data["missing_lines"]) |
| 85 | + |
| 86 | + # Write branch coverage data if enabled |
| 87 | + if self.with_branches and (file_data.get("executed_branches") or file_data.get("missing_branches")): |
| 88 | + branch_info = get_branch_info(file_data) |
| 89 | + |
| 90 | + # BRDA: Branch data |
| 91 | + # Format: BRDA:<line number>,<block number>,<branch number>,<taken count or '-'> |
| 92 | + for line_num in sorted(branch_info.keys()): |
| 93 | + branches = branch_info[line_num] |
| 94 | + # Use line number as block number for simplicity |
| 95 | + block_num = 0 |
| 96 | + for branch_num, (dest, is_taken) in enumerate(branches): |
| 97 | + taken_str = "1" if is_taken else "-" |
| 98 | + outfile.write(f"BRDA:{line_num},{block_num},{branch_num},{taken_str}\n") |
| 99 | + |
| 100 | + # BRF: Branches Found |
| 101 | + total_branches = len(file_data.get("executed_branches", [])) + len(file_data.get("missing_branches", [])) |
| 102 | + outfile.write(f"BRF:{total_branches}\n") |
| 103 | + |
| 104 | + # BRH: Branches Hit |
| 105 | + branches_hit = len(file_data.get("executed_branches", [])) |
| 106 | + outfile.write(f"BRH:{branches_hit}\n") |
| 107 | + |
| 108 | + # DA: Line coverage data |
| 109 | + # Format: DA:<line number>,<execution count> |
| 110 | + for line in all_lines: |
| 111 | + hit_count = 1 if line in file_data["executed_lines"] else 0 |
| 112 | + outfile.write(f"DA:{line},{hit_count}\n") |
| 113 | + |
| 114 | + # LF: Lines Found (total instrumented lines) |
| 115 | + total_lines = len(all_lines) |
| 116 | + outfile.write(f"LF:{total_lines}\n") |
| 117 | + |
| 118 | + # LH: Lines Hit (covered lines) |
| 119 | + lines_hit = len(file_data["executed_lines"]) |
| 120 | + outfile.write(f"LH:{lines_hit}\n") |
| 121 | + |
| 122 | + # end_of_record: End of record marker |
| 123 | + outfile.write("end_of_record\n") |
0 commit comments