"testing"
)
-// Path to unit test executable to be used as standin for 'go tool covdata'
-var testcovdata string
+// testcovdata returns the path to the unit test executable to be used as
+// standin for 'go tool covdata'.
+func testcovdata(t testing.TB) string {
+ exe, err := os.Executable()
+ if err != nil {
+ t.Helper()
+ t.Fatal(err)
+ }
+ return exe
+}
// Top level tempdir for test.
var testTempDir string
fmt.Fprintf(os.Stderr, "debug: preserving tmpdir %s\n", topTmpdir)
}
os.Setenv("CMDCOVDATA_TEST_RUN_MAIN", "true")
- testExe, err := os.Executable()
- if err != nil {
- log.Fatal(err)
- }
- testcovdata = testExe
os.Exit(m.Run())
}
}
func buildProg(t *testing.T, prog string, dir string, tag string, flags []string) (string, string) {
-
// Create subdirs.
subdir := filepath.Join(dir, prog+"dir"+tag)
if err := os.Mkdir(subdir, 0777); err != nil {
s.exepath3, s.exedir3 = buildProg(t, "prog1", dir, "atomic", flags)
// Reuse unit test executable as tool to be tested.
- s.tool = testcovdata
+ s.tool = testcovdata(t)
// Create a few coverage output dirs.
for i := 0; i < 4; i++ {
testdata = "testdata"
)
-var (
- // The cmd/cover binary that we are going to test. At one point
- // this was created via "go build"; we now reuse the unit test
- // executable itself.
- testcover string
-
- // testTempDir is a temporary directory created in TestMain.
- testTempDir string
-)
+// testcover returns the path to the cmd/cover binary that we are going to
+// test. At one point this was created via "go build"; we now reuse the unit
+// test executable itself.
+func testcover(t testing.TB) string {
+ exe, err := os.Executable()
+ if err != nil {
+ t.Helper()
+ t.Fatal(err)
+ }
+ return exe
+}
+
+// testTempDir is a temporary directory created in TestMain.
+var testTempDir string
// If set, this will preserve all the tmpdir files from the test run.
var debug = flag.Bool("debug", false, "keep tmpdir files for debugging")
fmt.Fprintf(os.Stderr, "debug: preserving tmpdir %s\n", topTmpdir)
}
os.Setenv("CMDCOVER_TEST_RUN_MAIN", "normal")
- testExe, err := os.Executable()
- if err != nil {
- log.Fatal(err)
- }
- testcover = testExe
os.Exit(m.Run())
}
// "-toolexec" wrapper program to invoke the cover test executable
// itself via "go test -cover".
func TestCoverWithToolExec(t *testing.T) {
+ testenv.MustHaveExec(t)
- toolexecArg := "-toolexec=" + testcover
+ toolexecArg := "-toolexec=" + testcover(t)
t.Run("CoverHTML", func(t *testing.T) {
testCoverHTML(t, toolexecArg)
// go run ./testdata/main.go ./testdata/test.go
func TestCover(t *testing.T) {
testenv.MustHaveGoRun(t)
-
- dir := tempDir(t)
-
t.Parallel()
+ dir := tempDir(t)
// Read in the test file (testTest) and write it, with LINEs specified, to coverInput.
testTest := filepath.Join(testdata, "test.go")
// testcover -mode=count -var=thisNameMustBeVeryLongToCauseOverflowOfCounterIncrementStatementOntoNextLineForTest -o ./testdata/test_cover.go testdata/test_line.go
coverOutput := filepath.Join(dir, "test_cover.go")
- cmd := exec.Command(testcover, "-mode=count", "-var=thisNameMustBeVeryLongToCauseOverflowOfCounterIncrementStatementOntoNextLineForTest", "-o", coverOutput, coverInput)
+ cmd := exec.Command(testcover(t), "-mode=count", "-var=thisNameMustBeVeryLongToCauseOverflowOfCounterIncrementStatementOntoNextLineForTest", "-o", coverOutput, coverInput)
run(cmd, t)
- cmd = exec.Command(testcover, "-mode=set", "-var=Not_an-identifier", "-o", coverOutput, coverInput)
+ cmd = exec.Command(testcover(t), "-mode=set", "-var=Not_an-identifier", "-o", coverOutput, coverInput)
err = cmd.Run()
if err == nil {
t.Error("Expected cover to fail with an error")
// above those declarations, even if they are not part of the block of
// documentation comments.
func TestDirectives(t *testing.T) {
-
+ testenv.MustHaveExec(t)
t.Parallel()
// Read the source file and find all the directives. We'll keep
sourceDirectives := findDirectives(source)
// testcover -mode=atomic ./testdata/directives.go
- cmd := exec.Command(testcover, "-mode=atomic", testDirectives)
+ cmd := exec.Command(testcover(t), "-mode=atomic", testDirectives)
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
// Makes sure that `cover -func=profile.cov` reports accurate coverage.
// Issue #20515.
func TestCoverFunc(t *testing.T) {
- t.Parallel()
+ testenv.MustHaveExec(t)
// testcover -func ./testdata/profile.cov
coverProfile := filepath.Join(testdata, "profile.cov")
- cmd := exec.Command(testcover, "-func", coverProfile)
+ cmd := exec.Command(testcover(t), "-func", coverProfile)
out, err := cmd.Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
run(cmd, t)
// testcover -html testdata/html/html.cov -o testdata/html/html.html
htmlHTML := filepath.Join(dir, "html.html")
- cmd = exec.Command(testcover, "-html", htmlProfile, "-o", htmlHTML)
+ cmd = exec.Command(testcover(t), "-html", htmlProfile, "-o", htmlHTML)
run(cmd, t)
// Extract the parts of the HTML with comment markers,
run(cmd, t)
// testcover -html TMPDIR/htmlunformatted.cov -o unformatted.html
- cmd = exec.Command(testcover, "-html", htmlUProfile, "-o", htmlUHTML)
+ cmd = exec.Command(testcover(t), "-html", htmlUProfile, "-o", htmlUHTML)
cmd.Dir = htmlUDir
run(cmd, t)
}
run(cmd, t)
// testcover -func=TMPDIR/linedup.out
- cmd = exec.Command(testcover, "-func", lineDupProfile)
+ cmd = exec.Command(testcover(t), "-func", lineDupProfile)
cmd.Dir = lineDupDir
run(cmd, t)
}