|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Convert an Xcode .xcresult coverage archive to LCOV format. |
| 4 | +
|
| 5 | +This script consumes the JSON output produced by `xcrun xccov` and emits |
| 6 | +LCOV records so downstream tooling (e.g. Codecov) can ingest coverage. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import json |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | + |
| 17 | +def run_xccov(args: list[str]) -> str: |
| 18 | + """Execute xccov with the provided arguments and return stdout.""" |
| 19 | + result = subprocess.run( |
| 20 | + ["xcrun", "xccov", *args], |
| 21 | + check=True, |
| 22 | + capture_output=True, |
| 23 | + text=True, |
| 24 | + ) |
| 25 | + return result.stdout |
| 26 | + |
| 27 | + |
| 28 | +def generate_lcov(xcresult_path: Path, output_path: Path, repo_root: Path) -> None: |
| 29 | + report = json.loads( |
| 30 | + run_xccov(["view", "--report", "--json", str(xcresult_path)]) |
| 31 | + ) |
| 32 | + |
| 33 | + lines = [] |
| 34 | + for target in report.get("targets", []): |
| 35 | + # Only include the Simmer app target; skip tests and UI harnesses. |
| 36 | + if not target.get("name", "").endswith(".app"): |
| 37 | + continue |
| 38 | + |
| 39 | + for file_entry in target.get("files", []): |
| 40 | + file_path = Path(file_entry.get("path", "")) |
| 41 | + if not file_path.exists(): |
| 42 | + # If the file no longer exists in the workspace, skip it. |
| 43 | + continue |
| 44 | + |
| 45 | + try: |
| 46 | + rel_path = file_path.relative_to(repo_root) |
| 47 | + except ValueError: |
| 48 | + # Skip files outside the repository (e.g. system headers). |
| 49 | + continue |
| 50 | + |
| 51 | + file_json = json.loads( |
| 52 | + run_xccov( |
| 53 | + [ |
| 54 | + "view", |
| 55 | + "--archive", |
| 56 | + "--file", |
| 57 | + str(file_path), |
| 58 | + "--json", |
| 59 | + str(xcresult_path), |
| 60 | + ] |
| 61 | + ) |
| 62 | + ) |
| 63 | + file_key = next(iter(file_json.keys())) |
| 64 | + coverage_entries = file_json[file_key] |
| 65 | + |
| 66 | + lines.append(f"TN:{target['name']}") |
| 67 | + lines.append(f"SF:{rel_path}") |
| 68 | + for entry in coverage_entries: |
| 69 | + if not entry.get("isExecutable", False): |
| 70 | + continue |
| 71 | + line_no = entry["line"] |
| 72 | + exec_count = entry.get("executionCount", 0) |
| 73 | + lines.append(f"DA:{line_no},{exec_count}") |
| 74 | + lines.append("end_of_record") |
| 75 | + |
| 76 | + output_path.write_text("\n".join(lines) + "\n") |
| 77 | + |
| 78 | + |
| 79 | +def main() -> int: |
| 80 | + if len(sys.argv) != 3: |
| 81 | + print( |
| 82 | + "usage: scripts/xccov_to_lcov.py <path/to/result.xcresult> <output.lcov>", |
| 83 | + file=sys.stderr, |
| 84 | + ) |
| 85 | + return 1 |
| 86 | + |
| 87 | + xcresult = Path(sys.argv[1]).resolve() |
| 88 | + output = Path(sys.argv[2]) |
| 89 | + repo_root = Path.cwd() |
| 90 | + |
| 91 | + if not xcresult.exists(): |
| 92 | + print(f"error: xcresult not found at {xcresult}", file=sys.stderr) |
| 93 | + return 1 |
| 94 | + |
| 95 | + output.parent.mkdir(parents=True, exist_ok=True) |
| 96 | + generate_lcov(xcresult, output, repo_root) |
| 97 | + return 0 |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + sys.exit(main()) |
0 commit comments