github.com/tiagovtristao/plz@v13.4.0+incompatible/src/test/go_coverage.go (about)

     1  // Code for parsing Go's coverage output.
     2  //
     3  // Go comes with a built-in coverage tool and a package to parse its output format. <3
     4  // Its format is actually rather richer than ours and can handle sub-line coverage etc.
     5  // We may look into taking more advantage of that later...
     6  
     7  package test
     8  
     9  import "bytes"
    10  import "golang.org/x/tools/cover"
    11  
    12  import "github.com/thought-machine/please/src/core"
    13  
    14  func looksLikeGoCoverageResults(results []byte) bool {
    15  	return bytes.HasPrefix(results, []byte("mode: "))
    16  }
    17  
    18  func parseGoCoverageResults(target *core.BuildTarget, coverage *core.TestCoverage, filename string) error {
    19  	profiles, err := cover.ParseProfiles(filename)
    20  	if err != nil {
    21  		return err
    22  	}
    23  	for _, profile := range profiles {
    24  		coverage.Files[profile.FileName] = parseBlocks(profile.Blocks)
    25  	}
    26  	coverage.Tests[target.Label] = coverage.Files
    27  	return nil
    28  }
    29  
    30  func parseBlocks(blocks []cover.ProfileBlock) []core.LineCoverage {
    31  	if len(blocks) == 0 {
    32  		return nil
    33  	}
    34  	lastLine := blocks[len(blocks)-1].EndLine
    35  	ret := make([]core.LineCoverage, lastLine)
    36  	for _, block := range blocks {
    37  		for line := block.StartLine - 1; line < block.EndLine; line++ {
    38  			if block.Count > 0 {
    39  				ret[line] = core.Covered
    40  			} else {
    41  				ret[line] = core.Uncovered
    42  			}
    43  		}
    44  	}
    45  	return ret
    46  }