| 1 |
|
| 2 |
# Testcase related to #58770 and #24570. This is intended to ensure |
| 3 |
# that coverage collection works in situations where we're testing a |
| 4 |
# collection of packages and supplying a -coverpkg pattern that |
| 5 |
# matches some but not all of the collection. In addition, some of the |
| 6 |
# packages have Go code but no tests, and other packages have tests |
| 7 |
# but no Go code. Package breakdown: |
| 8 |
# |
| 9 |
# Package Code? Tests? Stmts Imports |
| 10 |
# a yes yes 2 f |
| 11 |
# b yes yes 1 a, d |
| 12 |
# c yes yes 3 --- |
| 13 |
# d yes no 1 --- |
| 14 |
# e no yes 0 a, b |
| 15 |
# f yes no 3 --- |
| 16 |
# |
| 17 |
|
| 18 |
[short] skip |
| 19 |
|
| 20 |
# Test all packages with -coverpkg=./... |
| 21 |
go test -coverprofile=cov.p -coverpkg=./... ./... |
| 22 |
stdout '^ok\s+M/a\s+\S+\s+coverage: 50.0% of statements in ./...' |
| 23 |
stdout '^ok\s+M/b\s+\S+\s+coverage: 60.0% of statements in ./...' |
| 24 |
stdout '^ok\s+M/c\s+\S+\s+coverage: 30.0% of statements in ./...' |
| 25 |
stdout '^\s*M/d\s+coverage: 0.0% of statements' |
| 26 |
stdout '^\s*M/f\s+coverage: 0.0% of statements' |
| 27 |
|
| 28 |
# Test just the test-only package ./e but with -coverpkg=./... |
| 29 |
# Total number of statements should be 7 (e.g. a/b/d/f but not c) |
| 30 |
# and covered percent should be 6/7 (we hit everything in the |
| 31 |
# coverpkg pattern except the func in "d"). |
| 32 |
go test -coverprofile=bar.p -coverpkg=./... ./e |
| 33 |
stdout '^ok\s+M/e\s+\S+\s+coverage: 85.7% of statements in ./...' |
| 34 |
|
| 35 |
# Test b and f with -coverpkg set to a/d/f. Total of 6 statements |
| 36 |
# in a/d/f, again we hit everything except DFunc. |
| 37 |
go test -coverprofile=baz.p -coverpkg=./a,./d,./f ./b ./f |
| 38 |
stdout '^ok\s+M/b\s+\S+\s+coverage: 83.3% of statements in ./a, ./d, ./f' |
| 39 |
stdout '^\s*M/f\s+coverage: 0.0% of statements' |
| 40 |
|
| 41 |
# This sub-test inspired by issue 65653: if package P is is matched |
| 42 |
# via the package pattern supplied as the argument to "go test -cover" |
| 43 |
# but P is not part of "-coverpkg", then we don't want coverage for P |
| 44 |
# (including the specific case where P has no test files). |
| 45 |
go test -coverpkg=./a ./... |
| 46 |
stdout '^ok\s+M/a\s+\S+\s+coverage: 100.0% of statements in ./a' |
| 47 |
stdout '^\s*\?\s+M/f\s+\[no test files\]' |
| 48 |
|
| 49 |
-- a/a.go -- |
| 50 |
package a |
| 51 |
|
| 52 |
import "M/f" |
| 53 |
|
| 54 |
var G int |
| 55 |
|
| 56 |
func AFunc() int { |
| 57 |
G = 1 |
| 58 |
return f.Id() |
| 59 |
} |
| 60 |
-- a/a_test.go -- |
| 61 |
package a |
| 62 |
|
| 63 |
import "testing" |
| 64 |
|
| 65 |
func TestA(t *testing.T) { |
| 66 |
if AFunc() != 42 { |
| 67 |
t.Fatalf("bad!") |
| 68 |
} |
| 69 |
} |
| 70 |
-- b/b.go -- |
| 71 |
package b |
| 72 |
|
| 73 |
import ( |
| 74 |
"M/a" |
| 75 |
"M/d" |
| 76 |
) |
| 77 |
|
| 78 |
func BFunc() int { |
| 79 |
return -d.FortyTwo + a.AFunc() |
| 80 |
} |
| 81 |
-- b/b_test.go -- |
| 82 |
package b |
| 83 |
|
| 84 |
import "testing" |
| 85 |
|
| 86 |
func TestB(t *testing.T) { |
| 87 |
if BFunc() == 1010101 { |
| 88 |
t.Fatalf("bad!") |
| 89 |
} |
| 90 |
} |
| 91 |
-- c/c.go -- |
| 92 |
package c |
| 93 |
|
| 94 |
var G int |
| 95 |
|
| 96 |
func CFunc(x, y int) int { |
| 97 |
G += x |
| 98 |
G -= y |
| 99 |
return x + y |
| 100 |
} |
| 101 |
-- c/c_test.go -- |
| 102 |
package c |
| 103 |
|
| 104 |
import "testing" |
| 105 |
|
| 106 |
func TestC(t *testing.T) { |
| 107 |
if CFunc(10, 10) == 1010101 { |
| 108 |
t.Fatalf("bad!") |
| 109 |
} |
| 110 |
} |
| 111 |
-- d/d.go -- |
| 112 |
package d |
| 113 |
|
| 114 |
const FortyTwo = 42 |
| 115 |
|
| 116 |
func DFunc() int { |
| 117 |
return FortyTwo |
| 118 |
} |
| 119 |
|
| 120 |
-- e/e_test.go -- |
| 121 |
package e |
| 122 |
|
| 123 |
import ( |
| 124 |
"M/a" |
| 125 |
"M/b" |
| 126 |
"testing" |
| 127 |
) |
| 128 |
|
| 129 |
func TestBlah(t *testing.T) { |
| 130 |
if b.BFunc() == 1010101 { |
| 131 |
t.Fatalf("bad") |
| 132 |
} |
| 133 |
a.AFunc() |
| 134 |
} |
| 135 |
-- f/f.go -- |
| 136 |
package f |
| 137 |
|
| 138 |
var F int |
| 139 |
|
| 140 |
func Id() int { |
| 141 |
F += 9 |
| 142 |
F *= 2 |
| 143 |
return 42 |
| 144 |
} |
| 145 |
-- go.mod -- |
| 146 |
module M |
| 147 |
|
| 148 |
go 1.21 |