-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
356 lines (309 loc) · 7.6 KB
/
main.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"os"
"os/exec"
"path"
"strings"
"text/tabwriter"
"github.com/pterm/pterm"
"golang.org/x/tools/cover"
)
var fFunc string
var fRunTests bool
var fFileName string
var fLegend bool
var fcoverFilename string
var to = pterm.NewRGB(42, 119, 11) // This RGB value is used as the gradients start point.
var from = pterm.NewRGB(171, 200, 170)
func main() {
flag.StringVar(&fFunc, "func", "", "Show only selected function")
flag.StringVar(&fFileName, "file", "", "show annotated source code for selected file")
flag.StringVar(&fcoverFilename, "coverFilename", "", "Cover profile filename location")
flag.BoolVar(&fLegend, "legend", false, "Print sample colors")
flag.BoolVar(&fRunTests, "runtests", true, "Run tests and generate coverage profile")
flag.Parse()
// set fFileName or fFunc if this is given as argument
// i.e gocover main.go
setFlagsFromArgs()
if fcoverFilename == "" {
outFile, err := ioutil.TempFile("", "coverage")
if err != nil {
log.Fatal(err)
}
fcoverFilename = outFile.Name()
defer func() {
err := os.Remove(fcoverFilename)
if err != nil {
log.Fatal(err)
}
}()
}
if fLegend {
legend(os.Stdout)
os.Exit(0)
}
if fRunTests {
cmd := exec.Command(
"go",
"test",
"-covermode=count",
fmt.Sprintf("-coverprofile=%s", fcoverFilename),
"./...",
)
output, err := cmd.CombinedOutput()
if err != nil {
log.Println(string(output))
log.Fatal(err)
}
}
profiles, err := cover.ParseProfiles(fcoverFilename)
if err != nil {
log.Fatal(err)
}
if fFunc != "" {
err := printfunc(fFunc, profiles)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return
}
if fFileName != "" {
printFile(profiles, fFileName)
return
}
generateReport(profiles)
}
// set fFileName or fFunc if this is given as argument
// i.e gocover main.go
func setFlagsFromArgs() {
args := flag.Args()
if len(args) == 1 {
if strings.HasSuffix(args[0], ".go") && fileExists(args[0]) {
fFileName = args[0]
} else {
// asume a function name was given
fFunc = args[0]
}
}
}
// printfunc finds funcname and prints it with coverage head map
func printfunc(funcname string, profiles []*cover.Profile) error {
for _, profile := range profiles {
fn := profile.FileName
actualfile, err := findFile(fn)
if err != nil {
return err
}
funcs, err := findFuncs(actualfile)
if err != nil {
return err
}
for _, f := range funcs {
if f.name == funcname {
analyzeandprintWithFunc(actualfile, profile, f)
return nil
}
}
}
return fmt.Errorf("could not find function %s", funcname)
}
// run through cover profiles and print coverage of files and functions
func generateReport(profiles []*cover.Profile) {
for _, profile := range profiles {
actualfile, err := findFile(profile.FileName)
if err != nil {
log.Fatal(err)
}
// print file info
printFileAndCoverage(actualfile, percentCovered(profile))
// print function info
printFunctionsAndCoverage(profile, actualfile)
}
cov := totalcoverage(profiles)
fmt.Printf(`-------------------------------------------------
Total covered: %s
`, fadeprint(fmt.Sprintf("%.2f%%", cov), cov))
}
// find filename and print the contents coverage head map
func printFile(profiles []*cover.Profile, filename string) {
for _, profile := range profiles {
actualfile, err := findFile(profile.FileName)
if err != nil {
log.Fatal(err)
}
// print the file
if len(fFileName) > 0 && strings.Contains(actualfile, filename) {
analyzeandprint(actualfile, profile)
}
}
}
func analyzeandprint(actualfile string, profile *cover.Profile) {
src, err := ioutil.ReadFile(actualfile)
if err != nil {
log.Printf("can't read %q: %v", profile.FileName, err)
os.Exit(0)
}
paintpoints, err := generatePaintPoints(src, profile.Boundaries(src))
if err != nil {
log.Fatal(err)
}
printCoverage(paintpoints, src, 0, len(src))
}
func analyzeandprintWithFunc(actualfile string, profile *cover.Profile, fun *FuncExtent) {
src, err := ioutil.ReadFile(actualfile)
if err != nil {
log.Printf("can't read %q: %v", profile.FileName, err)
os.Exit(0)
}
pp, err := generatePaintPoints(src, profile.Boundaries(src))
if err != nil {
log.Fatal(err)
}
start, stop := findstartstop(src, fun)
printCoverage(pp, src, start, stop)
}
func findstartstop(s []byte, fun *FuncExtent) (start, stop int) {
line := 1
posinline := 0
for i, v := range s {
posinline++
if line == fun.startLine && posinline == fun.startCol {
start = i
}
if line == fun.endLine && posinline == fun.endCol {
stop = i
return
}
if v == '\n' {
line++
posinline = 0
}
}
log.Fatal("could not find start/stop")
return
}
// find functions in file and print names and coverage
func printFunctionsAndCoverage(profile *cover.Profile, file string) error {
funcs, err := findFuncs(file)
if err != nil {
return err
}
tabber := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0)
defer tabber.Flush()
var total, covered int64
// Now match up functions and profile blocks.
for _, f := range funcs {
c, t := f.coverage(profile)
cov := 100.0 * float64(c) / float64(t)
fmt.Fprintf(tabber, "%s:%d:\t%s\t%s\n",
path.Base(file),
f.startLine,
f.name,
fadeprint(fmt.Sprintf("%.1f%%", cov), cov))
total += t
covered += c
}
fmt.Fprint(tabber, "\n")
return nil
}
func fadeprint(s string, cov float64) string {
if cov == 0 {
return pterm.FgRed.Sprint(s)
}
if cov > 99 {
return to.Sprint(pterm.NewStyle(pterm.Bold).Sprint(s))
}
return from.Fade(0, float32(10), float32(int(cov/10)), to).Sprint(s)
}
func printFileAndCoverage(filename string, cov float64) {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
fn := strings.Replace(filename, fmt.Sprintf("%s/", wd), "", 1)
pterm.NewStyle(pterm.Bold).Printf("# %-20s ", fn)
fmt.Print(fadeprint(fmt.Sprintf("%.1f%%\n", cov), cov))
fmt.Println("---------------------------")
}
func totalcoverage(profiles []*cover.Profile) float64 {
var total, covered int64
for _, p := range profiles {
for _, b := range p.Blocks {
total += int64(b.NumStmt)
if b.Count > 0 {
covered += int64(b.NumStmt)
}
}
}
if total == 0 {
return 0
}
return float64(covered) / float64(total) * 100
}
func percentCovered(p *cover.Profile) float64 {
var total, covered int64
for _, b := range p.Blocks {
total += int64(b.NumStmt)
if b.Count > 0 {
covered += int64(b.NumStmt)
}
}
if total == 0 {
return 0
}
return float64(covered) / float64(total) * 100
}
func printCoverage(pp []paintpoint, src []byte, start, stop int) {
pi := 0 // paintpoint index
var w bytes.Buffer
for i := start; i < stop; i++ {
if pi < len(pp) && i >= pp[pi].start && i <= pp[pi].stop {
fmt.Fprint(&w, fadeprint(string(src[i]), float64(pp[pi].cov)))
} else {
fmt.Fprint(&w, pterm.FgLightWhite.Sprint(string(src[i])))
}
if pi < len(pp) && i >= pp[pi].stop {
pi++
}
}
fmt.Println(w.String())
}
// paintpoint represent a chunk of code with start/stop byte index in
// sourcefile, coverage and statement count
type paintpoint struct {
cov int
start int
stop int
count int
}
func generatePaintPoints(src []byte, boundaries []cover.Boundary) ([]paintpoint, error) {
paintboard := make([]paintpoint, 0)
pp := paintpoint{}
for i := range src {
for len(boundaries) > 0 && boundaries[0].Offset == i {
b := boundaries[0]
if b.Start {
n := 0
if b.Count > 0 {
n = int(math.Floor(b.Norm*99)) + 1
}
pp.start = i
pp.cov = n
pp.count = b.Count
} else {
pp.stop = i
paintboard = append(paintboard, pp)
pp = paintpoint{}
}
boundaries = boundaries[1:]
}
}
return paintboard, nil
}