...
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=./...
21go test -coverprofile=cov.p -coverpkg=./... ./...
22stdout '^ok\s+M/a\s+\S+\s+coverage: 50.0% of statements in ./...'
23stdout '^ok\s+M/b\s+\S+\s+coverage: 60.0% of statements in ./...'
24stdout '^ok\s+M/c\s+\S+\s+coverage: 30.0% of statements in ./...'
25stdout '^\s*M/d\s+coverage: 0.0% of statements'
26stdout '^\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").
32go test -coverprofile=bar.p -coverpkg=./... ./e
33stdout '^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.
37go test -coverprofile=baz.p -coverpkg=./a,./d,./f ./b ./f
38stdout '^ok\s+M/b\s+\S+\s+coverage: 83.3% of statements in ./a, ./d, ./f'
39stdout '^\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).
45go test -coverpkg=./a ./...
46stdout '^ok\s+M/a\s+\S+\s+coverage: 100.0% of statements in ./a'
47stdout '^\s*\?\s+M/f\s+\[no test files\]'
48
49-- a/a.go --
50package a
51
52import "M/f"
53
54var G int
55
56func AFunc() int {
57 G = 1
58 return f.Id()
59}
60-- a/a_test.go --
61package a
62
63import "testing"
64
65func TestA(t *testing.T) {
66 if AFunc() != 42 {
67 t.Fatalf("bad!")
68 }
69}
70-- b/b.go --
71package b
72
73import (
74 "M/a"
75 "M/d"
76)
77
78func BFunc() int {
79 return -d.FortyTwo + a.AFunc()
80}
81-- b/b_test.go --
82package b
83
84import "testing"
85
86func TestB(t *testing.T) {
87 if BFunc() == 1010101 {
88 t.Fatalf("bad!")
89 }
90}
91-- c/c.go --
92package c
93
94var G int
95
96func CFunc(x, y int) int {
97 G += x
98 G -= y
99 return x + y
100}
101-- c/c_test.go --
102package c
103
104import "testing"
105
106func TestC(t *testing.T) {
107 if CFunc(10, 10) == 1010101 {
108 t.Fatalf("bad!")
109 }
110}
111-- d/d.go --
112package d
113
114const FortyTwo = 42
115
116func DFunc() int {
117 return FortyTwo
118}
119
120-- e/e_test.go --
121package e
122
123import (
124 "M/a"
125 "M/b"
126 "testing"
127)
128
129func TestBlah(t *testing.T) {
130 if b.BFunc() == 1010101 {
131 t.Fatalf("bad")
132 }
133 a.AFunc()
134}
135-- f/f.go --
136package f
137
138var F int
139
140func Id() int {
141 F += 9
142 F *= 2
143 return 42
144}
145-- go.mod --
146module M
147
148go 1.21
View as plain text