github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/cov/covcmd/cmddefs.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package covcmd
     6  
     7  import (
     8  	"crypto/sha256"
     9  	"fmt"
    10  
    11  	"github.com/go-asm/go/coverage"
    12  )
    13  
    14  // CoverPkgConfig is a bundle of information passed from the Go
    15  // command to the cover command during "go build -cover" runs. The
    16  // Go command creates and fills in a struct as below, then passes
    17  // file containing the encoded JSON for the struct to the "cover"
    18  // tool when instrumenting the source files in a Go package.
    19  type CoverPkgConfig struct {
    20  	// File into which cmd/cover should emit summary info
    21  	// when instrumentation is complete.
    22  	OutConfig string
    23  
    24  	// Import path for the package being instrumented.
    25  	PkgPath string
    26  
    27  	// Package name.
    28  	PkgName string
    29  
    30  	// Instrumentation granularity: one of "perfunc" or "perblock" (default)
    31  	Granularity string
    32  
    33  	// Module path for this package (empty if no go.mod in use)
    34  	ModulePath string
    35  
    36  	// Local mode indicates we're doing a coverage build or test of a
    37  	// package selected via local import path, e.g. "./..." or
    38  	// "./foo/bar" as opposed to a non-relative import path. See the
    39  	// corresponding field in cmd/go's PackageInternal struct for more
    40  	// info.
    41  	Local bool
    42  
    43  	// EmitMetaFile if non-empty is the path to which the cover tool should
    44  	// directly emit a coverage meta-data file for the package, if the
    45  	// package has any functions in it. The go command will pass in a value
    46  	// here if we've been asked to run "go test -cover" on a package that
    47  	// doesn't have any *_test.go files.
    48  	EmitMetaFile string
    49  }
    50  
    51  // CoverFixupConfig contains annotations/notes generated by the
    52  // cmd/cover tool (during instrumentation) to be passed on to the
    53  // compiler when the instrumented code is compiled. The cmd/cover tool
    54  // creates a struct of this type, JSON-encodes it, and emits the
    55  // result to a file, which the Go command then passes to the compiler
    56  // when the instrumented package is built.
    57  type CoverFixupConfig struct {
    58  	// Name of the variable (created by cmd/cover) containing the
    59  	// encoded meta-data for the package.
    60  	MetaVar string
    61  
    62  	// Length of the meta-data.
    63  	MetaLen int
    64  
    65  	// Hash computed by cmd/cover of the meta-data.
    66  	MetaHash string
    67  
    68  	// Instrumentation strategy. For now this is always set to
    69  	// "normal", but in the future we may add new values (for example,
    70  	// if panic paths are instrumented, or if the instrumenter
    71  	// eliminates redundant counters).
    72  	Strategy string
    73  
    74  	// Prefix assigned to the names of counter variables generated
    75  	// during instrumentation by cmd/cover.
    76  	CounterPrefix string
    77  
    78  	// Name chosen for the package ID variable generated during
    79  	// instrumentation.
    80  	PkgIdVar string
    81  
    82  	// Counter mode (e.g. set/count/atomic)
    83  	CounterMode string
    84  
    85  	// Counter granularity (perblock or perfunc).
    86  	CounterGranularity string
    87  }
    88  
    89  // MetaFileForPackage returns the expected name of the meta-data file
    90  // for the package whose import path is 'importPath' in cases where
    91  // we're using meta-data generated by the cover tool, as opposed to a
    92  // meta-data file created at runtime.
    93  func MetaFileForPackage(importPath string) string {
    94  	var r [32]byte
    95  	sum := sha256.Sum256([]byte(importPath))
    96  	copy(r[:], sum[:])
    97  	return coverage.MetaFilePref + fmt.Sprintf(".%x", r)
    98  }