forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover.go
295 lines (257 loc) · 7.46 KB
/
cover.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
// Copyright 2018 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// Package cover reports coverage on modules.
package cover
import (
"fmt"
"math"
"sort"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/topdown"
)
// Cover computes and reports on coverage.
type Cover struct {
hits map[string]map[Position]struct{}
}
// New returns a new Cover object.
func New() *Cover {
return &Cover{
hits: map[string]map[Position]struct{}{},
}
}
// Enabled returns true if coverage is enabled.
func (c *Cover) Enabled() bool {
return true
}
// Config returns the standard Tracer configuration for the Cover tracer
func (c *Cover) Config() topdown.TraceConfig {
return topdown.TraceConfig{
PlugLocalVars: false, // Event variable metadata is not required for the Coverage report
}
}
// Report returns a coverage Report for the given modules.
func (c *Cover) Report(modules map[string]*ast.Module) (report Report) {
report.Files = map[string]*FileReport{}
for file, hits := range c.hits {
covered := make(PositionSlice, 0, len(hits))
for pos := range hits {
covered = append(covered, pos)
}
covered.Sort()
fr, ok := report.Files[file]
if !ok {
fr = &FileReport{}
report.Files[file] = fr
}
fr.Covered = sortedPositionSliceToRangeSlice(covered)
}
for file, module := range modules {
notCovered := PositionSlice{}
ast.WalkRules(module, func(x *ast.Rule) bool {
if hasFileLocation(x.Head.Location) {
if !report.IsCovered(x.Location.File, x.Location.Row) {
notCovered = append(notCovered, Position{x.Head.Location.Row})
}
}
return false
})
ast.WalkExprs(module, func(x *ast.Expr) bool {
if includeExprInCoverage(x) {
if !report.IsCovered(x.Location.File, x.Location.Row) {
notCovered = append(notCovered, Position{x.Location.Row})
}
}
return false
})
notCovered.Sort()
fr, ok := report.Files[file]
if !ok {
fr = &FileReport{}
report.Files[file] = fr
}
fr.NotCovered = sortedPositionSliceToRangeSlice(notCovered)
}
var coveredLoc, notCoveredLoc int
var overallCoverage float64
for _, fr := range report.Files {
fr.Coverage = fr.computeCoveragePercentage()
fr.CoveredLines = fr.locCovered()
fr.NotCoveredLines = fr.locNotCovered()
coveredLoc += fr.CoveredLines
notCoveredLoc += fr.NotCoveredLines
}
totalLoc := coveredLoc + notCoveredLoc
if totalLoc != 0 {
overallCoverage = 100.0 * float64(coveredLoc) / float64(totalLoc)
}
report.CoveredLines = coveredLoc
report.NotCoveredLines = notCoveredLoc
report.Coverage = round(overallCoverage, 2)
return
}
// Trace updates the coverage state.
// Deprecated: Use TraceEvent instead.
func (c *Cover) Trace(event *topdown.Event) {
c.TraceEvent(*event)
}
// TraceEvent updates the coverage state.
func (c *Cover) TraceEvent(event topdown.Event) {
switch event.Op {
case topdown.ExitOp:
if rule, ok := event.Node.(*ast.Rule); ok {
c.setHit(rule.Head.Location)
}
case topdown.EvalOp:
if expr := event.Node.(*ast.Expr); expr != nil {
c.setHit(expr.Location)
}
}
}
func (c *Cover) setHit(loc *ast.Location) {
if hasFileLocation(loc) {
hits, ok := c.hits[loc.File]
if !ok {
hits = map[Position]struct{}{}
c.hits[loc.File] = hits
}
hits[Position{loc.Row}] = struct{}{}
}
}
// Position represents a file location.
type Position struct {
Row int `json:"row"`
}
// PositionSlice is a collection of position that can be sorted.
type PositionSlice []Position
// Sort sorts the slice by line number.
func (sl PositionSlice) Sort() {
sort.Slice(sl, func(i, j int) bool {
return sl[i].Row < sl[j].Row
})
}
// Range represents a range of positions in a file.
type Range struct {
Start Position `json:"start"`
End Position `json:"end"`
}
// In returns true if the row is inside the range.
func (r Range) In(row int) bool {
return row >= r.Start.Row && row <= r.End.Row
}
// FileReport represents a coverage report for a single file.
type FileReport struct {
Covered []Range `json:"covered,omitempty"`
NotCovered []Range `json:"not_covered,omitempty"`
CoveredLines int `json:"covered_lines,omitempty"`
NotCoveredLines int `json:"not_covered_lines,omitempty"`
Coverage float64 `json:"coverage,omitempty"`
}
// IsCovered returns true if the row is marked as covered in the report.
func (fr *FileReport) IsCovered(row int) bool {
if fr == nil {
return false
}
for _, r := range fr.Covered {
if r.In(row) {
return true
}
}
return false
}
// IsNotCovered returns true if the row is marked as NOT covered in the report.
// This is not the same as simply not being reported. For example, certain
// statements like imports are not included in the report.
func (fr *FileReport) IsNotCovered(row int) bool {
if fr == nil {
return false
}
for _, r := range fr.NotCovered {
if r.In(row) {
return true
}
}
return false
}
// locCovered returns the number of lines of code covered by tests
func (fr *FileReport) locCovered() (loc int) {
for _, r := range fr.Covered {
loc += r.End.Row - r.Start.Row + 1
}
return
}
// locNotCovered returns the number of lines of code not covered by tests
func (fr *FileReport) locNotCovered() (loc int) {
for _, r := range fr.NotCovered {
loc += r.End.Row - r.Start.Row + 1
}
return
}
// computeCoveragePercentage returns the code coverage percentage of the file
func (fr *FileReport) computeCoveragePercentage() float64 {
coveredLoc := fr.locCovered()
notCoveredLoc := fr.locNotCovered()
totalLoc := coveredLoc + notCoveredLoc
if totalLoc == 0 {
return 0.0
}
return round(100.0*float64(coveredLoc)/float64(totalLoc), 2)
}
// Report represents a coverage report for a set of files.
type Report struct {
Files map[string]*FileReport `json:"files"`
CoveredLines int `json:"covered_lines"`
NotCoveredLines int `json:"not_covered_lines"`
Coverage float64 `json:"coverage"`
}
// IsCovered returns true if the row in the given file is covered.
func (r Report) IsCovered(file string, row int) bool {
return r.Files[file].IsCovered(row)
}
// CoverageThresholdError represents an error raised when the global
// code coverage percentage is lower than the specified threshold.
type CoverageThresholdError struct {
Coverage float64
Threshold float64
}
func (e *CoverageThresholdError) Error() string {
return fmt.Sprintf(
"Code coverage threshold not met: got %.2f instead of %.2f",
e.Coverage,
e.Threshold)
}
func sortedPositionSliceToRangeSlice(sorted []Position) (result []Range) {
if len(sorted) == 0 {
return
}
start, end := sorted[0], sorted[0]
for i := 1; i < len(sorted); i++ {
curr := sorted[i]
switch {
case curr.Row == end.Row: // skip
case curr.Row == end.Row+1:
end = curr
default:
result = append(result, Range{start, end})
start, end = curr, curr
}
}
result = append(result, Range{start, end})
return
}
func hasFileLocation(loc *ast.Location) bool {
return loc != nil && loc.File != ""
}
// round returns the number with the specified precision.
func round(number float64, precision int) float64 {
return math.Round(number*10*float64(precision)) / (10.0 * float64(precision))
}
// Check the expression and return true if it should be included in the coverage report
func includeExprInCoverage(x *ast.Expr) bool {
includeExprType := true
switch x.Terms.(type) {
case *ast.SomeDecl:
includeExprType = false
}
return includeExprType && hasFileLocation(x.Location)
}