-
Notifications
You must be signed in to change notification settings - Fork 0
/
deltacoverage.go
279 lines (265 loc) · 7 KB
/
deltacoverage.go
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package deltacoverage
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"gopkg.in/errgo.v2/errors"
)
var (
// ErrMustBeDirectory is the error for when a file is passed instead of a directory.
ErrMustBeDirectory = errors.New("Must be a directory")
// ErrMustBeFile is the error for when a directory is passed instead of a file.
ErrMustBeFile = errors.New("Must be a file")
)
// profileItem is a struct to store a line of a cover profile.
type profileItem struct {
Branch string
Statements int
Visited bool
}
// CoverProfile is a struct to store Go's cover profile state.
type CoverProfile struct {
NumberStatements int
OutputPath string
PackagePath string
Stderr io.Writer
Stdout io.Writer
Tests []string
TestsBranches map[string][]string
UniqueBranches map[string]int
}
// parseProfileLine returns a pointer to ProfileItem type for a given string.
// line example with headers
// branch statements visited
// xyz/xyz.go:3.24,5.2 1 1
func (c CoverProfile) parseProfileLine(line string) (*profileItem, error) {
if strings.HasPrefix(line, "mode:") {
return &profileItem{}, nil
}
items := strings.Fields(line)
branch := items[0]
profItem := &profileItem{
Branch: branch,
}
nrStmt, err := strconv.Atoi(items[1])
if err != nil {
return nil, err
}
profItem.Statements = nrStmt
timesVisited, err := strconv.Atoi(items[2])
if err != nil {
return nil, err
}
if timesVisited > 0 {
profItem.Visited = true
}
return profItem, err
}
// String prints out the deltacoverage percentage for each test
func (c CoverProfile) String() string {
output := []string{}
if len(c.Tests) == 0 {
fmt.Fprintf(c.Stderr, "No tests found\n")
return ""
}
for testName, ids := range c.TestsBranches {
perc := 0.0
testStatements := 0
for _, id := range ids {
statements, ok := c.UniqueBranches[id]
if !ok {
continue
}
testStatements += statements
}
perc = float64(testStatements) / float64(c.NumberStatements) * 100
output = append(output, fmt.Sprintf("%s %.1f%s", testName, perc, "%"))
}
sort.Strings(output)
return strings.Join(output, "\n")
}
// Parse reads all files from a directory with extension
// .coverprofile, parses it, and populate CoverProfile object
// Design tradeoff to get the total number of statements:
// 1. Parse one file just to sum the total statements
// 2. Recalculate total statements each iteration on the loop
// 3. Add a counter and an if to just calculate the total sum in the first
// iteration
// Current implementation is number 3
func (c *CoverProfile) Parse() error {
branchesCount := map[string]int{}
branchesStmts := map[string]int{}
profilesRead := 0
files, err := os.ReadDir(c.OutputPath)
if err != nil {
return err
}
for _, file := range files {
if filepath.Ext(file.Name()) == ".coverprofile" {
profilesRead++
testName := strings.Split(filepath.Base(file.Name()), ".")[0]
f, err := os.Open(c.OutputPath + "/" + file.Name())
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
profileItem, err := c.parseProfileLine(line)
if err != nil {
return fmt.Errorf("cannot parse profile line %q: %+v", line, err)
}
// only sum total statements when reading the first cover profile
if profilesRead == 1 {
c.NumberStatements += profileItem.Statements
}
if !profileItem.Visited {
continue
}
_, ok := branchesCount[profileItem.Branch]
if !ok {
branchesStmts[profileItem.Branch] = profileItem.Statements
}
branchesCount[profileItem.Branch]++
c.TestsBranches[testName] = append(c.TestsBranches[testName], profileItem.Branch)
}
if err := scanner.Err(); err != nil {
return err
}
}
}
for branch, times := range branchesCount {
if times < 2 {
c.UniqueBranches[branch] = branchesStmts[branch]
}
}
return nil
}
// Generate creates a cover profile for each test
func (c *CoverProfile) Generate() error {
err := c.ListTests()
if err != nil {
return err
}
for _, test := range c.Tests {
outputFile := c.OutputPath + "/" + test + ".coverprofile"
cmd := exec.Command("go", "test", "-run", test, "-coverprofile", outputFile)
cmd.Dir = c.PackagePath
cmd.Stderr = c.Stderr
cmd.Stdout = c.Stdout
if err := cmd.Start(); err != nil {
return fmt.Errorf("error starting %q: %v", strings.Join(cmd.Args, " "), err)
}
if err := cmd.Wait(); err != nil {
return fmt.Errorf("error running %q: %v", strings.Join(cmd.Args, " "), err)
}
}
return nil
}
// ListTests set package tests in CoverProfile
func (c *CoverProfile) ListTests() error {
goArgs := []string{"test", "-list", "."}
cmd := exec.Command("go", goArgs...)
cmd.Dir = c.PackagePath
cmd.Stderr = c.Stderr
goTestList, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("error getting pipe for \"go %s\": %v", strings.Join(goArgs, " "), err)
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("error starting \"go %s\": %v", strings.Join(goArgs, " "), err)
}
c.Tests, err = parseListTests(goTestList)
if err != nil {
return fmt.Errorf("error running parseListTests: %v", err)
}
if err := cmd.Wait(); err != nil {
return fmt.Errorf("error running \"go %s\": %v", strings.Join(goArgs, " "), err)
}
return nil
}
// Cleanup delete the OutputPath directory
func (c *CoverProfile) Cleanup() error {
return os.RemoveAll(c.OutputPath)
}
// NewCoverProfile initializes and returns a pointer to CoverProfile
func NewCoverProfile(codePath string) (*CoverProfile, error) {
info, err := os.Stat(codePath)
if err != nil {
return nil, err
}
if !info.IsDir() {
return nil, ErrMustBeDirectory
}
tempDir, err := os.MkdirTemp(os.TempDir(), "deltacoverage")
if err != nil {
return nil, err
}
c := &CoverProfile{
OutputPath: tempDir,
PackagePath: codePath,
Stderr: os.Stderr,
Stdout: io.Discard,
TestsBranches: map[string][]string{},
UniqueBranches: map[string]int{},
}
return c, nil
}
// parseListTests parses the reader and returns a slice containing all strings
// started with "Test"
func parseListTests(r io.Reader) ([]string, error) {
scanner := bufio.NewScanner(r)
testsNames := []string{}
for scanner.Scan() {
text := scanner.Text()
if strings.HasPrefix(text, "Test") {
testsNames = append(testsNames, text)
}
}
if err := scanner.Err(); err != nil {
return []string{}, err
}
return testsNames, nil
}
// Main is the entrypoint for the CLI. It puts all functions together and return
// the CLI exit code
func Main() int {
args := os.Args[1:]
packagePath := "./"
if len(args) > 0 {
packagePath = os.Args[1]
}
c, err := NewCoverProfile(packagePath)
if err != nil {
fmt.Println(err)
return 1
}
err = c.Generate()
if err != nil {
fmt.Println(err)
return 1
}
err = c.Parse()
if err != nil {
fmt.Println(err)
return 1
}
output := c.String()
if output == "" {
return 1
}
fmt.Println(output)
err = c.Cleanup()
if err != nil {
fmt.Println(err)
return 1
}
return 0
}