| 1 |
|
| 2 |
# Testcase inspired by issue #58770, intended to verify that we're |
| 3 |
# doing the right thing when running "go test -coverpkg=./... ./..." |
| 4 |
# on a collection of packages where some have init functions and some |
| 5 |
# do not, some have tests and some do not. |
| 6 |
|
| 7 |
[short] skip |
| 8 |
|
| 9 |
# Verify correct statements percentages. We have a total of 10 |
| 10 |
# statements in the packages matched by "./..."; package "a" (for |
| 11 |
# example) has two statements so we expect 20.0% stmts covered. Go |
| 12 |
# 1.19 would print 50% here (due to force importing of all ./... |
| 13 |
# packages); prior to the fix for #58770 Go 1.20 would show 100% |
| 14 |
# coverage. For packages "x" and "f" (which have no tests), check for |
| 15 |
# 0% stmts covered (as opposed to "no test files"). |
| 16 |
|
| 17 |
go test -count=1 -coverprofile=cov.dat -coverpkg=./... ./... |
| 18 |
stdout '^\s*\?\s+M/n\s+\[no test files\]' |
| 19 |
stdout '^\s*M/x\s+coverage: 0.0% of statements' |
| 20 |
stdout '^\s*M/f\s+coverage: 0.0% of statements' |
| 21 |
stdout '^ok\s+M/a\s+\S+\s+coverage: 30.0% of statements in ./...' |
| 22 |
stdout '^ok\s+M/b\s+\S+\s+coverage: 20.0% of statements in ./...' |
| 23 |
stdout '^ok\s+M/main\s+\S+\s+coverage: 80.0% of statements in ./...' |
| 24 |
|
| 25 |
# Check for selected elements in the collected coverprofile as well. |
| 26 |
|
| 27 |
go tool cover -func=cov.dat |
| 28 |
stdout '^M/x/x.go:3:\s+XFunc\s+0.0%' |
| 29 |
stdout '^M/b/b.go:7:\s+BFunc\s+100.0%' |
| 30 |
stdout '^total:\s+\(statements\)\s+80.0%' |
| 31 |
|
| 32 |
-- go.mod -- |
| 33 |
module M |
| 34 |
|
| 35 |
go 1.21 |
| 36 |
-- a/a.go -- |
| 37 |
package a |
| 38 |
|
| 39 |
import "M/f" |
| 40 |
|
| 41 |
func init() { |
| 42 |
println("package 'a' init: launch the missiles!") |
| 43 |
} |
| 44 |
|
| 45 |
func AFunc() int { |
| 46 |
return f.Id() |
| 47 |
} |
| 48 |
-- a/a_test.go -- |
| 49 |
package a |
| 50 |
|
| 51 |
import "testing" |
| 52 |
|
| 53 |
func TestA(t *testing.T) { |
| 54 |
if AFunc() != 42 { |
| 55 |
t.Fatalf("bad!") |
| 56 |
} |
| 57 |
} |
| 58 |
-- b/b.go -- |
| 59 |
package b |
| 60 |
|
| 61 |
func init() { |
| 62 |
println("package 'b' init: release the kraken") |
| 63 |
} |
| 64 |
|
| 65 |
func BFunc() int { |
| 66 |
return -42 |
| 67 |
} |
| 68 |
-- b/b_test.go -- |
| 69 |
package b |
| 70 |
|
| 71 |
import "testing" |
| 72 |
|
| 73 |
func TestB(t *testing.T) { |
| 74 |
if BFunc() != -42 { |
| 75 |
t.Fatalf("bad!") |
| 76 |
} |
| 77 |
} |
| 78 |
-- f/f.go -- |
| 79 |
package f |
| 80 |
|
| 81 |
func Id() int { |
| 82 |
return 42 |
| 83 |
} |
| 84 |
-- main/main.go -- |
| 85 |
package main |
| 86 |
|
| 87 |
import ( |
| 88 |
"M/a" |
| 89 |
"M/b" |
| 90 |
) |
| 91 |
|
| 92 |
func MFunc() string { |
| 93 |
return "42" |
| 94 |
} |
| 95 |
|
| 96 |
func M2Func() int { |
| 97 |
return a.AFunc() + b.BFunc() |
| 98 |
} |
| 99 |
|
| 100 |
func init() { |
| 101 |
println("package 'main' init") |
| 102 |
} |
| 103 |
|
| 104 |
func main() { |
| 105 |
println(a.AFunc() + b.BFunc()) |
| 106 |
} |
| 107 |
-- main/main_test.go -- |
| 108 |
package main |
| 109 |
|
| 110 |
import "testing" |
| 111 |
|
| 112 |
func TestMain(t *testing.T) { |
| 113 |
if MFunc() != "42" { |
| 114 |
t.Fatalf("bad!") |
| 115 |
} |
| 116 |
if M2Func() != 0 { |
| 117 |
t.Fatalf("also bad!") |
| 118 |
} |
| 119 |
} |
| 120 |
-- n/n.go -- |
| 121 |
package n |
| 122 |
|
| 123 |
type N int |
| 124 |
-- x/x.go -- |
| 125 |
package x |
| 126 |
|
| 127 |
func XFunc() int { |
| 128 |
return 2 * 2 |
| 129 |
} |