neotest: distinguish coverage modes

Use calls frequency calculated by executor in the final coverage
profile for `atomic` cover mode. Support only `set` cover mode for now
due to #3587.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2024-10-14 18:16:30 +03:00
parent a9242535db
commit 2dc588ea95

View file

@ -16,13 +16,22 @@ import (
) )
const ( const (
// goCoverProfileFlag specifies the name of `go test` flag that tells it where to save coverage data. // goCoverProfileFlag specifies the name of `go test` command flag `coverprofile`
// Neotest coverage can be enabled only when this flag is set. // that tells it where to save coverage data. Neotest coverage can be enabled
// only when this flag is set.
goCoverProfileFlag = "test.coverprofile" goCoverProfileFlag = "test.coverprofile"
// goCoverModeFlag specifies the name of `go test` command flag `covermode` that
// specifies the coverage calculation mode.
goCoverModeFlag = "test.covermode"
// disableNeotestCover is name of the environmental variable used to explicitly disable neotest coverage. // disableNeotestCover is name of the environmental variable used to explicitly disable neotest coverage.
disableNeotestCover = "DISABLE_NEOTEST_COVER" disableNeotestCover = "DISABLE_NEOTEST_COVER"
) )
const (
// goCoverModeSet is the name of "set" go test coverage mode.
goCoverModeSet = "set"
)
var ( var (
// coverageLock protects all vars below from concurrent modification when tests are run in parallel. // coverageLock protects all vars below from concurrent modification when tests are run in parallel.
coverageLock sync.Mutex coverageLock sync.Mutex
@ -34,6 +43,8 @@ var (
coverageEnabled bool coverageEnabled bool
// coverProfile specifies the file all coverage data is written to, unless empty. // coverProfile specifies the file all coverage data is written to, unless empty.
coverProfile = "" coverProfile = ""
// coverMode is the mode of go coverage collection.
coverMode = goCoverModeSet
) )
type scriptRawCoverage struct { type scriptRawCoverage struct {
@ -83,11 +94,17 @@ func isCoverageEnabled() bool {
goToolCoverageEnabled = true goToolCoverageEnabled = true
coverProfile = f.Value.String() coverProfile = f.Value.String()
} }
if f.Name == goCoverModeFlag && f.Value != nil && f.Value.String() != "" {
coverMode = f.Value.String()
}
}) })
coverageEnabled = !disabledByEnvironment && goToolCoverageEnabled coverageEnabled = !disabledByEnvironment && goToolCoverageEnabled
if coverageEnabled { if coverageEnabled {
if coverMode != goCoverModeSet {
t.Fatalf("coverage: only '%s' cover mode is currently supported (#3587), got '%s'", goCoverModeSet, coverMode)
}
// This is needed so go cover tool doesn't overwrite // This is needed so go cover tool doesn't overwrite
// the file with our coverage when all tests are done. // the file with our coverage when all tests are done.
err := flag.Set(goCoverProfileFlag, "") err := flag.Set(goCoverProfileFlag, "")
@ -119,19 +136,19 @@ func reportCoverage(t testing.TB) {
} }
func writeCoverageReport(w io.Writer) { func writeCoverageReport(w io.Writer) {
fmt.Fprintf(w, "mode: set\n") fmt.Fprintf(w, "mode: %s\n", coverMode)
cover := processCover() cover := processCover()
for name, blocks := range cover { for name, blocks := range cover {
for _, b := range blocks { for _, b := range blocks {
c := 0 var counts = b.counts
if b.counts > 0 { if coverMode == goCoverModeSet && counts > 0 {
c = 1 counts = 1
} }
fmt.Fprintf(w, "%s:%d.%d,%d.%d %d %d\n", name, fmt.Fprintf(w, "%s:%d.%d,%d.%d %d %d\n", name,
b.startLine, b.startCol, b.startLine, b.startCol,
b.endLine, b.endCol, b.endLine, b.endCol,
b.stmts, b.stmts,
c, counts,
) )
} }
} }