Skip to content

Commit

Permalink
fix(coverage): Account for SHA in lcov DA parsing (#6463)
Browse files Browse the repository at this point in the history
  • Loading branch information
blorente authored Jun 6, 2024
1 parent 37f515b commit ff5d037
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ private static TIntIntHashMap parseHits(BufferedReader reader) throws IOExceptio
return hits;
}
if (line.startsWith(DA)) {
// DA:line,hits
int comma = line.indexOf(',');
// DA:line,hits,sha
String[] segments = line.substring(DA.length()).split(",");
if (segments.length < 2) {
logger.warn(String.format("Cannot parse LCOV line: Expected entry to have format DA:<number>,<number>, was: %s", line));
continue;
}
try {
hits.put(
Integer.parseInt(line.substring(DA.length(), comma)),
Integer.parseInt(line.substring(comma + 1)));
Integer.parseInt(segments[0]),
Integer.parseInt(segments[1]));
} catch (NumberFormatException e) {
logger.warn("Cannot parse LCOV line: " + line, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ public void testEmptyFilesIgnored() throws IOException {
assertThat(data.perFileData.keySet()).containsExactly("path/to/another/file.txt");
}

/**
* Some test runners, such as the one for `py_test` in rules_python, append extra data to DA entries.
* This test data is taken from a run of one such `py_test`.
* @throws IOException
*/
@Test
public void testDALinesWithShaCanBeParsed() throws IOException {
BlazeCoverageData data =
BlazeCoverageData.parse(
inputStream(
"SF:path/to/file.txt",
"DA:1,1,CjTuYq8+gnNfbQNgi09Ocg",
"DA:40,1,E/tvV9JPVDhEcTCkgrwOFw",
"DA:42,1,SZ/sLwIPxdnoU2xnoUB7pg",
"end_of_record"
));

assertThat(data.perFileData).hasSize(1);
FileData fileData = data.perFileData.get("path/to/file.txt");
assertThat(fileData).isNotNull();
assertThat(fileData.source).isEqualTo("path/to/file.txt");
assertThat(toMap(fileData.lineHits)).containsExactly(1, 1, 40, 1, 42, 1);
}

private static ImmutableMap<Integer, Integer> toMap(TIntIntHashMap troveMap) {
return Arrays.stream(troveMap.keys())
.boxed()
Expand Down

0 comments on commit ff5d037

Please sign in to comment.