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