forked from matm/gocov-html
/
report.go
387 lines (344 loc) · 9.65 KB
/
report.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package themes
import (
"encoding/base64"
"encoding/json"
"fmt"
"go/token"
"html"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/CrusoeKe/gocov"
"github.com/rotisserie/eris"
)
// ReportOptions holds various options used when generating the final
// HTML report.
type ReportOptions struct {
// LowCoverageOnTop puts low coverage functions first.
LowCoverageOnTop bool
// Stylesheet is the path to a custom CSS file.
Stylesheet string
// CoverageMin filters out all functions whose code coverage is smaller than it is.
CoverageMin uint8
// CoverageMax filters out all functions whose code coverage is greater than it is.
CoverageMax uint8
// ReportPath is the path to the package report dir.
ReportPath string
}
type report struct {
ReportOptions
packages []*gocov.Package
}
func unmarshalJSON(data []byte) (packages []*gocov.Package, err error) {
result := &struct{ Packages []*gocov.Package }{}
err = json.Unmarshal(data, result)
if err == nil {
packages = result.Packages
}
return
}
type reverse struct {
sort.Interface
}
func (r reverse) Less(i, j int) bool {
return r.Interface.Less(j, i)
}
// NewReport creates a new report.
func newReport() (r *report) {
r = &report{}
return
}
// AddPackage adds a package's coverage information to the report.
func (r *report) addPackage(p *gocov.Package) {
i := sort.Search(len(r.packages), func(i int) bool {
return r.packages[i].Name >= p.Name
})
if i < len(r.packages) && r.packages[i].Name == p.Name {
r.packages[i].Accumulate(p)
} else {
head := r.packages[:i]
tail := append([]*gocov.Package{p}, r.packages[i:]...)
r.packages = append(head, tail...)
}
}
// Clear clears the coverage information from the report.
func (r *report) clear() {
r.packages = nil
}
func buildReportPackage(pkg *gocov.Package, r *report) reportPackage {
rv := reportPackage{
Pkg: pkg,
Functions: make(reportFunctionList, 0),
}
for _, fn := range pkg.Functions {
reached := 0
for _, stmt := range fn.Statements {
if stmt.Reached > 0 {
reached++
}
}
rf := reportFunction{Function: fn, StatementsReached: reached}
covp := rf.CoveragePercent()
if covp >= float64(r.CoverageMin) && covp <= float64(r.CoverageMax) {
rv.Functions = append(rv.Functions, rf)
}
rv.TotalStatements += len(fn.Statements)
rv.ReachedStatements += reached
}
if r.LowCoverageOnTop {
sort.Sort(rv.Functions)
} else {
sort.Sort(reverse{rv.Functions})
}
return rv
}
// printReport prints a coverage report to the given writer.
func printReport(w io.Writer, r *report) error {
data := curTheme.Data()
// Base64 decoding of style data and script.
s, err := base64.StdEncoding.DecodeString(data.Style)
if err != nil {
return eris.Wrap(err, "decode style")
}
css := string(s)
// Decode the script also.
sc, err := base64.StdEncoding.DecodeString(data.Script)
if err != nil {
return eris.Wrap(err, "decode script")
}
if len(r.Stylesheet) > 0 {
// Inline CSS.
f, err := os.Open(r.Stylesheet)
if err != nil {
return eris.Wrap(err, "print report")
}
style, err := ioutil.ReadAll(f)
if err != nil {
return eris.Wrap(err, "read style")
}
css = string(style)
}
reportPackages := make(reportPackageList, len(r.packages))
pkgNames := make([]string, len(r.packages))
for i, pkg := range r.packages {
reportPackages[i] = buildReportPackage(pkg, r)
pkgNames[i] = pkg.Name
}
data.Script = string(sc)
data.Style = css
data.Packages = reportPackages
data.Command = fmt.Sprintf("gocov test %s | gocov-html %s",
strings.Join(pkgNames, " "),
strings.Join(os.Args[1:], " "),
)
if len(reportPackages) > 1 {
rv := reportPackage{
Pkg: &gocov.Package{Name: "Report Total"},
}
for key, rp := range reportPackages {
rv.ReachedStatements += rp.ReachedStatements
rv.TotalStatements += rp.TotalStatements
reportPackages[key].ReachedPercentage = rp.PercentageReached()
reportPackages[key].HtmlFilePath = fmt.Sprintf("%s%s.html", r.ReportPath, strings.ReplaceAll(rp.Pkg.Name, "/", "_"))
reportPackages[key].Style = data.Style
}
data.Overview = &rv
}
origStdout := w
for _, rp := range data.Packages {
// 这个时候是把数据写到了os.Stdout中,所以需要重定向到文件中
// 将os.Stdout中的数据重定向到文件中
f, err := os.OpenFile(rp.HtmlFilePath, os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
return eris.Wrap(err, "create package report file failed")
}
defer f.Close()
w = f
// 新建每个包的html文件
err = curTheme.PackageTemplate().Execute(w, rp)
if err != nil {
return eris.Wrap(err, "execute package template")
}
}
w = origStdout
err = curTheme.Template().Execute(w, data)
return eris.Wrap(err, "execute template")
}
func exists(path string) (bool, error) {
if _, err := os.Stat(path); err != nil {
return false, err
}
return true, nil
}
// HTMLReportCoverage outputs an HTML report on stdout by
// parsing JSON data generated by axw/gocov. The css parameter
// is an absolute path to a custom stylesheet. Use an empty
// string to use the default stylesheet available.
func HTMLReportCoverage(r io.Reader, opts ReportOptions) error {
t0 := time.Now()
report := newReport()
report.ReportOptions = opts
// Custom stylesheet?
stylesheet := ""
if opts.Stylesheet != "" {
if _, err := exists(opts.Stylesheet); err != nil {
return eris.Wrap(err, "stylesheet")
}
stylesheet = opts.Stylesheet
}
report.Stylesheet = stylesheet
data, err := ioutil.ReadAll(r)
if err != nil {
return eris.Wrap(err, "read coverage data")
}
packages, err := unmarshalJSON(data)
if err != nil {
return eris.Wrap(err, "unmarshal coverage data")
}
for _, pkg := range packages {
report.addPackage(pkg)
}
fmt.Println()
err = printReport(os.Stdout, report)
fmt.Fprintf(os.Stderr, "Took %v\n", time.Since(t0))
return eris.Wrap(err, "HTML report")
}
// ProjectURL is the project's site on GitHub.
const ProjectURL = "https://github.com/matm/gocov-html"
const (
hitPrefix = " "
missPrefix = "MISS"
)
type reportPackageList []reportPackage
type reportPackage struct {
Pkg *gocov.Package
Functions reportFunctionList
TotalStatements int
ReachedStatements int
ReachedPercentage float64
HtmlFilePath string
Style string
}
// PercentageReached computes the percentage of reached statements by the tests
// for a package.
func (rp *reportPackage) PercentageReached() float64 {
var rv float64
if rp.TotalStatements > 0 {
rv = float64(rp.ReachedStatements) / float64(rp.TotalStatements) * 100
}
return rv
}
// reportFunction is a gocov Function with some added stats.
type reportFunction struct {
*gocov.Function
StatementsReached int
}
// functionLine holds the line of code, its line number in the source file
// and whether the tests reached it.
type functionLine struct {
Code string
LineNumber int
Missed bool
}
// CoveragePercent is the percentage of code coverage for a function. Returns 100
// if the function has no statement.
func (f reportFunction) CoveragePercent() float64 {
reached := f.StatementsReached
var stmtPercent float64 = 0
if len(f.Statements) > 0 {
stmtPercent = float64(reached) / float64(len(f.Statements)) * 100
} else if len(f.Statements) == 0 {
stmtPercent = 100
}
return stmtPercent
}
// ShortFileName returns the base path of the function's file name. Provided for
// convenience to be used in the HTML template of the theme.
func (f reportFunction) ShortFileName() string {
return filepath.Base(f.File)
}
// Lines returns information about all a function's Lines of code.
func (f reportFunction) Lines() []functionLine {
type annotator struct {
fset *token.FileSet
files map[string]*token.File
}
a := &annotator{}
a.fset = token.NewFileSet()
a.files = make(map[string]*token.File)
// Load the file for line information. Probably overkill, maybe
// just compute the lines from offsets in here.
setContent := false
file := a.files[f.File]
if file == nil {
info, err := os.Stat(f.File)
if err != nil {
panic(err)
}
file = a.fset.AddFile(f.File, a.fset.Base(), int(info.Size()))
setContent = true
}
data, err := ioutil.ReadFile(f.File)
if err != nil {
panic(err)
}
if setContent {
// This processes the content and records line number info.
file.SetLinesForContent(data)
}
statements := f.Statements[:]
lineno := file.Line(file.Pos(f.Start))
lines := strings.Split(string(data)[f.Start:f.End], "\n")
fls := make([]functionLine, len(lines))
for i, line := range lines {
lineno := lineno + i
statementFound := false
hit := false
for j := 0; j < len(statements); j++ {
start := file.Line(file.Pos(statements[j].Start))
if start == lineno {
statementFound = true
if !hit && statements[j].Reached > 0 {
hit = true
}
statements = append(statements[:j], statements[j+1:]...)
}
}
hitmiss := hitPrefix
if statementFound && !hit {
hitmiss = missPrefix
}
fls[i] = functionLine{
Missed: hitmiss == missPrefix,
LineNumber: lineno,
Code: html.EscapeString(strings.Replace(line, "\t", " ", -1)),
}
}
return fls
}
// reportFunctionList is a list of functions for a report.
type reportFunctionList []reportFunction
func (l reportFunctionList) Len() int {
return len(l)
}
// TODO make sort method configurable?
func (l reportFunctionList) Less(i, j int) bool {
var left, right float64
if len(l[i].Statements) > 0 {
left = float64(l[i].StatementsReached) / float64(len(l[i].Statements))
}
if len(l[j].Statements) > 0 {
right = float64(l[j].StatementsReached) / float64(len(l[j].Statements))
}
if left < right {
return true
}
return left == right && len(l[i].Statements) < len(l[j].Statements)
}
func (l reportFunctionList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}