diff --git a/cover/cover.go b/cover/cover.go index 4cdd625b47..be7ace4133 100644 --- a/cover/cover.go +++ b/cover/cover.go @@ -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) +} diff --git a/cover/cover_test.go b/cover/cover_test.go index 63cc468a4e..cd5df63ff3 100644 --- a/cover/cover_test.go +++ b/cover/cover_test.go @@ -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 {