-
Notifications
You must be signed in to change notification settings - Fork 17.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/go: fix objdir for run actions for -cover no-test packages
As of CL 495447 we now synthesize coverage data (including coverage profiles) for packages that have no tests, if they are included in a "go test -cover" run. The code that set up the "run" actions for such tests wasn't setting the objdir for the action, which meant that the coverage profile temp file fragment ("_cover_.out") was being created in the dir where the test was run, and in addition the same fragment could be written to by more than one package (which could lead to a corrupted file). This CL updates the code to properly set the objdir, and to create the dir when needed. Updates #24570. Fixes #63356. Change-Id: Iffe131cf50f07ce91085b816a039308e0ca84776 Reviewed-on: https://go-review.googlesource.com/c/go/+/532555 Reviewed-by: Russ Cox <rsc@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
- Loading branch information
Showing
2 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
src/cmd/go/testdata/script/cover_coverprofile_multipkg.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
|
||
# Testcase for #63356. In this bug we're doing a "go test -coverprofile" | ||
# run for a collection of packages, mostly independent (hence tests can | ||
# be done in parallel) and in the original bug, temp coverage profile | ||
# files were not being properly qualified and were colliding, resulting | ||
# in a corrupted final profile. Actual content of the packages doesn't | ||
# especially matter as long as we have a mix of packages with tests and | ||
# multiple packages without tests. | ||
|
||
[short] skip | ||
|
||
# Kick off test. | ||
go test -p=10 -vet=off -count=1 -coverprofile=cov.p ./... | ||
|
||
# Make sure resulting profile is digestible. | ||
go tool cover -func=cov.p | ||
|
||
# No extraneous extra files please. | ||
! exists _cover_.out | ||
|
||
-- a/a.go -- | ||
package a | ||
|
||
func init() { | ||
println("package 'a' init: launch the missiles!") | ||
} | ||
|
||
func AFunc() int { | ||
return 42 | ||
} | ||
-- a/a_test.go -- | ||
package a | ||
|
||
import "testing" | ||
|
||
func TestA(t *testing.T) { | ||
if AFunc() != 42 { | ||
t.Fatalf("bad!") | ||
} | ||
} | ||
-- aa/aa.go -- | ||
package aa | ||
|
||
import "M/it" | ||
|
||
func AA(y int) int { | ||
c := it.Conc{} | ||
x := it.Callee(&c) | ||
println(x, y) | ||
return 0 | ||
} | ||
-- aa/aa_test.go -- | ||
package aa | ||
|
||
import "testing" | ||
|
||
func TestMumble(t *testing.T) { | ||
AA(3) | ||
} | ||
-- b/b.go -- | ||
package b | ||
|
||
func init() { | ||
println("package 'b' init: release the kraken") | ||
} | ||
|
||
func BFunc() int { | ||
return -42 | ||
} | ||
-- b/b_test.go -- | ||
package b | ||
|
||
import "testing" | ||
|
||
func TestB(t *testing.T) { | ||
if BFunc() != -42 { | ||
t.Fatalf("bad!") | ||
} | ||
} | ||
-- deadstuff/deadstuff.go -- | ||
package deadstuff | ||
|
||
func downStreamOfPanic(x int) { | ||
panic("bad") | ||
if x < 10 { | ||
println("foo") | ||
} | ||
} | ||
-- deadstuff/deadstuff_test.go -- | ||
package deadstuff | ||
|
||
import "testing" | ||
|
||
func TestMumble(t *testing.T) { | ||
defer func() { | ||
if x := recover(); x != nil { | ||
println("recovered") | ||
} | ||
}() | ||
downStreamOfPanic(10) | ||
} | ||
-- go.mod -- | ||
module M | ||
|
||
go 1.21 | ||
-- it/it.go -- | ||
package it | ||
|
||
type Ctr interface { | ||
Count() int | ||
} | ||
|
||
type Conc struct { | ||
X int | ||
} | ||
|
||
func (c *Conc) Count() int { | ||
return c.X | ||
} | ||
|
||
func DoCall(c *Conc) { | ||
c2 := Callee(c) | ||
println(c2.Count()) | ||
} | ||
|
||
func Callee(ii Ctr) Ctr { | ||
q := ii.Count() | ||
return &Conc{X: q} | ||
} | ||
-- main/main.go -- | ||
package main | ||
|
||
import ( | ||
"M/a" | ||
"M/b" | ||
) | ||
|
||
func MFunc() string { | ||
return "42" | ||
} | ||
|
||
func M2Func() int { | ||
return a.AFunc() + b.BFunc() | ||
} | ||
|
||
func init() { | ||
println("package 'main' init") | ||
} | ||
|
||
func main() { | ||
println(a.AFunc() + b.BFunc()) | ||
} | ||
-- main/main_test.go -- | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestMain(t *testing.T) { | ||
if MFunc() != "42" { | ||
t.Fatalf("bad!") | ||
} | ||
if M2Func() != 0 { | ||
t.Fatalf("also bad!") | ||
} | ||
} | ||
-- n/n.go -- | ||
package n | ||
|
||
type N int | ||
-- onlytest/mumble_test.go -- | ||
package onlytest | ||
|
||
import "testing" | ||
|
||
func TestFoo(t *testing.T) { | ||
t.Logf("Whee\n") | ||
} | ||
-- x/x.go -- | ||
package x | ||
|
||
func XFunc() int { | ||
return 2 * 2 | ||
} | ||
-- xinternal/i.go -- | ||
package i | ||
|
||
func I() int { return 32 } | ||
-- xinternal/q/q.go -- | ||
package q | ||
|
||
func Q() int { | ||
return 42 | ||
} |