github.com/0xKiwi/rules_go@v0.24.3/go/tools/coverdata/coverdata.go (about)

     1  /* Copyright 2018 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  
    16  // Package coverdata provides a registration function for files with
    17  // coverage instrumentation.
    18  //
    19  // This package is part of the Bazel Go rules, and its interface
    20  // should not be considered public. It may change without notice.
    21  package coverdata
    22  
    23  import (
    24  	"fmt"
    25  	"testing"
    26  )
    27  
    28  // Cover contains all coverage data for the program.
    29  var Cover = testing.Cover{
    30  	Mode:            "set",
    31  	CoveredPackages: "",
    32  	Counters:        map[string][]uint32{},
    33  	Blocks:          map[string][]testing.CoverBlock{},
    34  }
    35  
    36  // RegisterFile causes the coverage data recorded for a file to be included
    37  // in program-wide coverage reports. This should be called from init functions
    38  // in packages with coverage instrumentation.
    39  func RegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) {
    40  	if 3*len(counter) != len(pos) || len(counter) != len(numStmts) {
    41  		panic("coverage: mismatched sizes")
    42  	}
    43  	if Cover.Counters[fileName] != nil {
    44  		// Already registered.
    45  		fmt.Printf("Already covered %s\n", fileName)
    46  		return
    47  	}
    48  	Cover.Counters[fileName] = counter
    49  	block := make([]testing.CoverBlock, len(counter))
    50  	for i := range counter {
    51  		block[i] = testing.CoverBlock{
    52  			Line0: pos[3*i+0],
    53  			Col0:  uint16(pos[3*i+2]),
    54  			Line1: pos[3*i+1],
    55  			Col1:  uint16(pos[3*i+2] >> 16),
    56  			Stmts: numStmts[i],
    57  		}
    58  	}
    59  	Cover.Blocks[fileName] = block
    60  }