Match a report to a source file
The path inside a coverage artifact rarely matches the path you have in hand. A listing written from the repository root says libs/x/src/math.d; the viewer opened /home/me/repo/libs/x/src/math.d; an LCOV tracefile from CI may say either. CoverageReport.findFile resolves that.
auto report = loadCoverage(artifactPath, contents);
if (auto file = report.value.findFile(documentPath))
attach(planCoverage(*file));What it matches
- Exact equality first.
- Whole-component suffix, in either direction —
src/m.dmatches/repo/src/m.d, and/repo/src/m.dmatches a report that only recordedsrc/m.d.
The boundary matters: m.d does not match stream.d, because the match must begin at a path separator. Both / and \ count, so a report produced on Windows still resolves.
An empty path never matches anything. That sounds obvious, but a DMD listing with no trailer leaves sourcePath empty, and a suffix test that accepted it would make that one record answer every lookup.
When it returns null
null means the report does not describe that file — usually because the artifact is for a different file entirely. Treat it as "no coverage available" and render plainly.
Resist the temptation to fall back to "if the report has exactly one file, use that one". A single-file report is the common case for .lst, so the fallback looks harmless, and then every file you browse to inherits the coverage of whichever artifact is attached.