-
-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optionally convert cover report to lcov
With the new --//go/config:lcov flag, go cover coverage reports are converted to lcov. When combined with Bazel's --combined_report=lcov, this allows for Go coverage to show up in cross-language, cross-target coverage reports. The combined report can be converted to HTML via e.g. genhtml --rc genhtml_branch_coverage=1 \ --rc genhtml_function_coverage=0 \ bazel-out/_coverage/_coverage_report.dat
- Loading branch information
Showing
11 changed files
with
408 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// Copyright 2022 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package bzltestutil | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
"regexp" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// ConvertCoverToLcov converts the go coverprofile file coverage.dat.cover to | ||
// the lcov format and stores it in coverage.dat, where it is picked up by | ||
// Bazel. | ||
// The conversion emits line and branch coverage, but not function coverage. | ||
func ConvertCoverToLcov() error { | ||
inPath := flag.Lookup("test.coverprofile").Value.String() | ||
outPath := strings.TrimSuffix(inPath, ".cover") | ||
|
||
in, err := os.Open(inPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer in.Close() | ||
|
||
out, err := os.Create(outPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
|
||
return convertCoverToLcov(bufio.NewScanner(in), out) | ||
} | ||
|
||
var coverLinePattern = regexp.MustCompile(`^(?P<path>.*):(?P<startLine>\d+)\.(?P<startColumn>\d+),(?P<endLine>\d+)\.(?P<endColumn>\d+) (?P<numStmt>\d+) (?P<count>\d+)$`) | ||
const pathIdx = 1 | ||
const startLineIdx = 2 | ||
const endLineIdx = 4 | ||
const countIdx = 7 | ||
|
||
type blockInfo struct { | ||
midLine uint32 | ||
count uint32 | ||
} | ||
|
||
func convertCoverToLcov(cover *bufio.Scanner, lcov io.StringWriter) error { | ||
currentPath := "" | ||
var lineCounts map[uint32]uint32 | ||
var blockInfos []blockInfo | ||
for cover.Scan() { | ||
l := cover.Text() | ||
m := coverLinePattern.FindStringSubmatch(l) | ||
if m == nil { | ||
if strings.HasPrefix(l, "mode: ") { | ||
continue | ||
} | ||
return fmt.Errorf("invalid go cover line: %s", l) | ||
} | ||
|
||
if m[pathIdx] != currentPath { | ||
if currentPath != "" { | ||
if err := emitLcovLines(lcov, currentPath, lineCounts, blockInfos); err != nil { | ||
return err | ||
} | ||
} | ||
currentPath = m[pathIdx] | ||
lineCounts = make(map[uint32]uint32) | ||
blockInfos = nil | ||
} | ||
|
||
startLine, err := strconv.ParseUint(m[startLineIdx], 10, 32) | ||
if err != nil { | ||
return err | ||
} | ||
endLine, err := strconv.ParseUint(m[endLineIdx], 10, 32) | ||
if err != nil { | ||
return err | ||
} | ||
count, err := strconv.ParseUint(m[countIdx], 10, 32) | ||
if err != nil { | ||
return err | ||
} | ||
blockInfos = append(blockInfos, blockInfo{ | ||
midLine: uint32((startLine + endLine) / 2), | ||
count: uint32(count), | ||
}) | ||
for line := uint32(startLine); line <= uint32(endLine); line++ { | ||
prevCount, ok := lineCounts[line] | ||
if !ok || uint32(count) > prevCount { | ||
lineCounts[line] = uint32(count) | ||
} | ||
} | ||
} | ||
if currentPath != "" { | ||
if err := emitLcovLines(lcov, currentPath, lineCounts, blockInfos); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func emitLcovLines(lcov io.StringWriter, path string, lineCounts map[uint32]uint32, blockInfos []blockInfo) error { | ||
_, err := lcov.WriteString(fmt.Sprintf("SF:%s\n", path)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Every individual block instrumented by go cover is reported as a branch | ||
// with its corresponding line chosen as the mid line of the block. | ||
sort.Slice(blockInfos, func(i, j int) bool { return blockInfos[i].midLine < blockInfos[j].midLine }) | ||
blocksCovered := 0 | ||
for i, block := range blockInfos { | ||
taken := "-" | ||
if block.count > 0 { | ||
taken = fmt.Sprint(block.count) | ||
blocksCovered++ | ||
} | ||
_, err = lcov.WriteString(fmt.Sprintf("BRDA:%d,%d,%d,%s\n", block.midLine, i, i, taken)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
// Emit a summary containing the number of all/covered blocks. | ||
_, err = lcov.WriteString(fmt.Sprintf("BRF:%d\nBRH:%d\n", len(blockInfos), blocksCovered)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Emit the coverage counters for the individual source lines. | ||
sortedLines := make([]uint32, len(lineCounts)) | ||
i := 0 | ||
for line := range lineCounts { | ||
sortedLines[i] = line | ||
i++ | ||
} | ||
sort.Slice(sortedLines, func(i, j int) bool { return sortedLines[i] < sortedLines[j] }) | ||
numCovered := 0 | ||
for _, line := range sortedLines { | ||
count := lineCounts[line] | ||
if count > 0 { | ||
numCovered++ | ||
} | ||
_, err := lcov.WriteString(fmt.Sprintf("DA:%d,%d\n", line, count)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
// Emit a summary containing the number of all/covered lines and end the info for the current source file. | ||
_, err = lcov.WriteString(fmt.Sprintf("LH:%d\nLF:%d\nend_of_record\n", numCovered, len(sortedLines))) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.