aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/testdata/script/cover_coverpkg_partial.txt
blob: 524024101acdf62d923e819ae651d77070cae24c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Testcase related to #58770 and #24570. This is intended to ensure
# that coverage collection works in situations where we're testing a
# collection of packages and supplying a -coverpkg pattern that
# matches some but not all of the collection. In addition, some of the
# packages have Go code but no tests, and other packages have tests
# but no Go code. Package breakdown:
#
# Package         Code?           Tests?          Stmts           Imports
# a               yes             yes             2               f
# b               yes             yes             1               a, d
# c               yes             yes             3               ---
# d               yes             no              1               ---
# e               no              yes             0               a, b
# f               yes             no              3               ---
#

[short] skip
[!GOEXPERIMENT:coverageredesign] skip

# Test all packages with -coverpkg=./...
go test -coverprofile=cov.p -coverpkg=./... ./...
stdout '^ok\s+M/a\s+\S+\s+coverage: 50.0% of statements in ./...'
stdout '^ok\s+M/b\s+\S+\s+coverage: 60.0% of statements in ./...'
stdout '^ok\s+M/c\s+\S+\s+coverage: 30.0% of statements in ./...'
stdout '^\s*M/d\s+coverage: 0.0% of statements'
stdout '^\s*M/f\s+coverage: 0.0% of statements'

# Test just the test-only package ./e but with -coverpkg=./...
# Total number of statements should be 7 (e.g. a/b/d/f but not c)
# and covered percent should be 6/7 (we hit everything in the
# coverpkg pattern except the func in "d").
go test -coverprofile=bar.p -coverpkg=./... ./e
stdout '^ok\s+M/e\s+\S+\s+coverage: 85.7% of statements in ./...'

# Test b and f with -coverpkg set to a/d/f. Total of 6 statements
# in a/d/f, again we hit everything except DFunc.
go test -coverprofile=baz.p -coverpkg=./a,./d,./f ./b ./f
stdout '^ok\s+M/b\s+\S+\s+coverage: 83.3% of statements in ./a, ./d, ./f'
stdout '^\s*M/f\s+coverage: 0.0% of statements'

-- a/a.go --
package a

import "M/f"

var G int

func AFunc() int {
	G = 1
	return f.Id()
}
-- a/a_test.go --
package a

import "testing"

func TestA(t *testing.T) {
	if AFunc() != 42 {
		t.Fatalf("bad!")
	}
}
-- b/b.go --
package b

import (
	"M/a"
	"M/d"
)

func BFunc() int {
	return -d.FortyTwo + a.AFunc()
}
-- b/b_test.go --
package b

import "testing"

func TestB(t *testing.T) {
	if BFunc() == 1010101 {
		t.Fatalf("bad!")
	}
}
-- c/c.go --
package c

var G int

func CFunc(x, y int) int {
	G += x
	G -= y
	return x + y
}
-- c/c_test.go --
package c

import "testing"

func TestC(t *testing.T) {
	if CFunc(10, 10) == 1010101 {
		t.Fatalf("bad!")
	}
}
-- d/d.go --
package d

const FortyTwo = 42

func DFunc() int {
  return FortyTwo
}

-- e/e_test.go --
package e

import (
	"M/a"
	"M/b"
	"testing"
)

func TestBlah(t *testing.T) {
	if b.BFunc() == 1010101 {
		t.Fatalf("bad")
	}
	a.AFunc()
}
-- f/f.go --
package f

var F int

func Id() int {
	F += 9
	F *= 2
	return 42
}
-- go.mod --
module M

go 1.21