cover: Exclude some expressions in coverage report

They would previously show up as not-covered in any report. This
changes to simply omit them. They will never be int the resulting
covered or not-covered lists, or count against the % covered.

Fixes: #1972
Signed-off-by: Patrick East <east.patrick@gmail.com>
This commit is contained in:
Patrick East
2020-02-05 16:15:53 -08:00
committed by Torin Sandall
parent 167f455277
commit 1d9cf35926
2 changed files with 31 additions and 9 deletions
+13 -1
View File
@@ -58,7 +58,7 @@ func (c *Cover) Report(modules map[string]*ast.Module) (report Report) {
return false
})
ast.WalkExprs(module, func(x *ast.Expr) bool {
if hasFileLocation(x.Location) {
if includeExprInCoverage(x) {
if !report.IsCovered(x.Location.File, x.Location.Row) {
notCovered = append(notCovered, Position{x.Location.Row})
}
@@ -258,3 +258,15 @@ func hasFileLocation(loc *ast.Location) bool {
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)
}
+18 -8
View File
@@ -24,6 +24,7 @@ import data.deadbeef # expect not reported
foo {
bar
p
not baz
}
@@ -37,7 +38,14 @@ baz { # expect no exit
true
false # expect eval but fail
true # expect not covered
}`
}
p {
some bar # should not be included in coverage report
bar = 1
bar + 1 == 2
}
`
parsedModule, err := ast.ParseModule("test.rego", module)
if err != nil {
@@ -67,16 +75,18 @@ baz { # expect no exit
}
expectedCovered := []Position{
{5}, // foo head
{6}, {7}, // foo body
{10}, // bar head
{11}, {12}, {13}, // bar body
{17}, {18}, // baz body hits
{5}, // foo head
{6}, {7}, {8}, // foo body
{11}, // bar head
{12}, {13}, {14}, // bar body
{18}, {19}, // baz body hits
{23}, // p head
{25}, {26}, // p body
}
expectedNotCovered := []Position{
{16}, // baz head
{19}, // baz body miss
{17}, // baz head
{20}, // baz body miss
}
for _, exp := range expectedCovered {