github.com/lovishpuri/go-40569/src@v0.0.0-20230519171745-f8623e7c56cf/testing/newcover.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  // Support for test coverage with redesigned coverage implementation.
     6  
     7  package testing
     8  
     9  import (
    10  	"fmt"
    11  	"internal/goexperiment"
    12  	"os"
    13  )
    14  
    15  // cover2 variable stores the current coverage mode and a
    16  // tear-down function to be called at the end of the testing run.
    17  var cover2 struct {
    18  	mode     string
    19  	tearDown func(coverprofile string, gocoverdir string) (string, error)
    20  }
    21  
    22  // registerCover2 is invoked during "go test -cover" runs by the test harness
    23  // code in _testmain.go; it is used to record a 'tear down' function
    24  // (to be called when the test is complete) and the coverage mode.
    25  func registerCover2(mode string, tearDown func(coverprofile string, gocoverdir string) (string, error)) {
    26  	cover2.mode = mode
    27  	cover2.tearDown = tearDown
    28  }
    29  
    30  // coverReport2 invokes a callback in _testmain.go that will
    31  // emit coverage data at the point where test execution is complete,
    32  // for "go test -cover" runs.
    33  func coverReport2() {
    34  	if !goexperiment.CoverageRedesign {
    35  		panic("unexpected")
    36  	}
    37  	if errmsg, err := cover2.tearDown(*coverProfile, *gocoverdir); err != nil {
    38  		fmt.Fprintf(os.Stderr, "%s: %v\n", errmsg, err)
    39  		os.Exit(2)
    40  	}
    41  }
    42  
    43  // testGoCoverDir returns the value passed to the -test.gocoverdir
    44  // flag by the Go command, if goexperiment.CoverageRedesign is
    45  // in effect.
    46  func testGoCoverDir() string {
    47  	return *gocoverdir
    48  }