...

Source file src/github.com/bazelbuild/rules_go/go/tools/bzltestutil/lcov.go

Documentation: github.com/bazelbuild/rules_go/go/tools/bzltestutil

     1  // Copyright 2022 The Bazel Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bzltestutil
    16  
    17  import (
    18  	"bufio"
    19  	"flag"
    20  	"fmt"
    21  	"io"
    22  	"log"
    23  	"os"
    24  	"regexp"
    25  	"sort"
    26  	"strconv"
    27  	"strings"
    28  	"testing/internal/testdeps"
    29  )
    30  
    31  // Lock in the COVERAGE_DIR during test setup in case the test uses e.g. os.Clearenv.
    32  var coverageDir = os.Getenv("COVERAGE_DIR")
    33  
    34  // ConvertCoverToLcov converts the go coverprofile file coverage.dat.cover to
    35  // the expectedLcov format and stores it in coverage.dat, where it is picked up by
    36  // Bazel.
    37  // The conversion emits line and branch coverage, but not function coverage.
    38  func ConvertCoverToLcov() error {
    39  	inPath := flag.Lookup("test.coverprofile").Value.String()
    40  	in, err := os.Open(inPath)
    41  	if err != nil {
    42  		// This can happen if there are no tests and should not be an error.
    43  		log.Printf("Not collecting coverage: %s has not been created: %s", inPath, err)
    44  		return nil
    45  	}
    46  	defer in.Close()
    47  
    48  	if coverageDir == "" {
    49  		log.Printf("Not collecting coverage: COVERAGE_DIR is not set")
    50  		return nil
    51  	}
    52  	// All *.dat files in $COVERAGE_DIR will be merged by Bazel's lcov_merger tool.
    53  	out, err := os.CreateTemp(coverageDir, "go_coverage.*.dat")
    54  	if err != nil {
    55  		return err
    56  	}
    57  	defer out.Close()
    58  
    59  	return convertCoverToLcov(in, out)
    60  }
    61  
    62  var _coverLinePattern = regexp.MustCompile(`^(?P<path>.+):(?P<startLine>\d+)\.(?P<startColumn>\d+),(?P<endLine>\d+)\.(?P<endColumn>\d+) (?P<numStmt>\d+) (?P<count>\d+)$`)
    63  
    64  const (
    65  	_pathIdx      = 1
    66  	_startLineIdx = 2
    67  	_endLineIdx   = 4
    68  	_countIdx     = 7
    69  )
    70  
    71  func convertCoverToLcov(coverReader io.Reader, lcovWriter io.Writer) error {
    72  	cover := bufio.NewScanner(coverReader)
    73  	lcov := bufio.NewWriter(lcovWriter)
    74  	defer lcov.Flush()
    75  	currentPath := ""
    76  	var lineCounts map[uint32]uint32
    77  	for cover.Scan() {
    78  		l := cover.Text()
    79  		m := _coverLinePattern.FindStringSubmatch(l)
    80  		if m == nil {
    81  			if strings.HasPrefix(l, "mode: ") {
    82  				continue
    83  			}
    84  			return fmt.Errorf("invalid go cover line: %s", l)
    85  		}
    86  
    87  		if m[_pathIdx] != currentPath {
    88  			if currentPath != "" {
    89  				if err := emitLcovLines(lcov, currentPath, lineCounts); err != nil {
    90  					return err
    91  				}
    92  			}
    93  			currentPath = m[_pathIdx]
    94  			lineCounts = make(map[uint32]uint32)
    95  		}
    96  
    97  		startLine, err := strconv.ParseUint(m[_startLineIdx], 10, 32)
    98  		if err != nil {
    99  			return err
   100  		}
   101  		endLine, err := strconv.ParseUint(m[_endLineIdx], 10, 32)
   102  		if err != nil {
   103  			return err
   104  		}
   105  		count, err := strconv.ParseUint(m[_countIdx], 10, 32)
   106  		if err != nil {
   107  			return err
   108  		}
   109  		for line := uint32(startLine); line <= uint32(endLine); line++ {
   110  			prevCount, ok := lineCounts[line]
   111  			if !ok || uint32(count) > prevCount {
   112  				lineCounts[line] = uint32(count)
   113  			}
   114  		}
   115  	}
   116  	if currentPath != "" {
   117  		if err := emitLcovLines(lcov, currentPath, lineCounts); err != nil {
   118  			return err
   119  		}
   120  	}
   121  	return nil
   122  }
   123  
   124  func emitLcovLines(lcov io.StringWriter, path string, lineCounts map[uint32]uint32) error {
   125  	_, err := lcov.WriteString(fmt.Sprintf("SF:%s\n", path))
   126  	if err != nil {
   127  		return err
   128  	}
   129  
   130  	// Emit the coverage counters for the individual source lines.
   131  	sortedLines := make([]uint32, 0, len(lineCounts))
   132  	for line := range lineCounts {
   133  		sortedLines = append(sortedLines, line)
   134  	}
   135  	sort.Slice(sortedLines, func(i, j int) bool { return sortedLines[i] < sortedLines[j] })
   136  	numCovered := 0
   137  	for _, line := range sortedLines {
   138  		count := lineCounts[line]
   139  		if count > 0 {
   140  			numCovered++
   141  		}
   142  		_, err := lcov.WriteString(fmt.Sprintf("DA:%d,%d\n", line, count))
   143  		if err != nil {
   144  			return err
   145  		}
   146  	}
   147  	// Emit a summary containing the number of all/covered lines and end the info for the current source file.
   148  	_, err = lcov.WriteString(fmt.Sprintf("LH:%d\nLF:%d\nend_of_record\n", numCovered, len(sortedLines)))
   149  	if err != nil {
   150  		return err
   151  	}
   152  	return nil
   153  }
   154  
   155  // LcovTestDeps is a patched version of testdeps.TestDeps that allows to
   156  // hook into the SetPanicOnExit0 call happening right before testing.M.Run
   157  // returns.
   158  // This trick relies on the testDeps interface defined in this package being
   159  // identical to the actual testing.testDeps interface, which differs between
   160  // major versions of Go.
   161  type LcovTestDeps struct {
   162  	testdeps.TestDeps
   163  	OriginalPanicOnExit bool
   164  }
   165  
   166  // SetPanicOnExit0 is called with true by m.Run() before running all tests,
   167  // and with false right before returning -- after writing all coverage
   168  // profiles.
   169  // https://cs.opensource.google/go/go/+/refs/tags/go1.18.1:src/testing/testing.go;l=1921-1931;drc=refs%2Ftags%2Fgo1.18.1
   170  //
   171  // This gives us a good place to intercept the os.Exit(m.Run()) with coverage
   172  // data already available.
   173  func (ltd LcovTestDeps) SetPanicOnExit0(panicOnExit bool) {
   174  	if !panicOnExit {
   175  		lcovAtExitHook()
   176  	}
   177  	ltd.TestDeps.SetPanicOnExit0(ltd.OriginalPanicOnExit)
   178  }
   179  
   180  func lcovAtExitHook() {
   181  	if err := ConvertCoverToLcov(); err != nil {
   182  		log.Printf("Failed to collect coverage: %s", err)
   183  		os.Exit(TestWrapperAbnormalExit)
   184  	}
   185  }
   186  

View as plain text