Skip to content

Commit e2bc9c9

Browse files
authored
updated test coverage reporter to handle error and unknown test scenarios (#1215)
1 parent b24530c commit e2bc9c9

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

tools/celtest/test_coverage_reporter.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,28 @@ func traverseAndCalculateCoverage(t *testing.T, expr ast.NavigableExpr, p Progra
9696
// Check for Branch Coverage if the node is a boolean type
9797
if interestingBoolNode {
9898
cr.branches += 2
99-
if info, found := p.CoverageStats[nodeID]; !found {
100-
if logUnencountered {
101-
cr.unencounteredBranches = append(cr.unencounteredBranches,
102-
"\n"+preceedingTabs+fmt.Sprintf("Expression ID %d ('%s'): No coverage", nodeID, exprText))
103-
preceedingTabs = preceedingTabs + "\t\t"
104-
}
105-
} else {
106-
if _, ok := info[types.True]; ok {
99+
if info, found := p.CoverageStats[nodeID]; found {
100+
var trueFound, falseFound bool
101+
if _, trueFound = info[types.True]; trueFound {
107102
cr.coveredBooleanOutcomes++
108-
} else if logUnencountered {
109-
cr.unencounteredBranches = append(cr.unencounteredBranches,
110-
"\n"+preceedingTabs+fmt.Sprintf("Expression ID %d ('%s'): lacks 'true' coverage", nodeID, exprText))
111-
preceedingTabs = preceedingTabs + "\t\t"
112-
113103
}
114-
if _, ok := info[types.False]; ok {
104+
if _, falseFound = info[types.False]; falseFound {
115105
cr.coveredBooleanOutcomes++
116-
} else if logUnencountered {
117-
cr.unencounteredBranches = append(cr.unencounteredBranches,
118-
"\n"+preceedingTabs+fmt.Sprintf("Expression ID %d ('%s'): lacks 'false' coverage", nodeID, exprText))
119-
preceedingTabs = preceedingTabs + "\t\t"
106+
}
107+
if logUnencountered && !(trueFound && falseFound) {
108+
if falseFound {
109+
cr.unencounteredBranches = append(cr.unencounteredBranches,
110+
"\n"+preceedingTabs+fmt.Sprintf("Expression ID %d ('%s'): lacks 'true' coverage", nodeID, exprText))
111+
preceedingTabs = preceedingTabs + "\t\t"
112+
} else if trueFound {
113+
cr.unencounteredBranches = append(cr.unencounteredBranches,
114+
"\n"+preceedingTabs+fmt.Sprintf("Expression ID %d ('%s'): lacks 'false' coverage", nodeID, exprText))
115+
preceedingTabs = preceedingTabs + "\t\t"
116+
} else {
117+
cr.unencounteredBranches = append(cr.unencounteredBranches,
118+
"\n"+preceedingTabs+fmt.Sprintf("Expression ID %d ('%s'): No coverage", nodeID, exprText))
119+
preceedingTabs = preceedingTabs + "\t\t"
120+
}
120121
}
121122
}
122123
}

tools/celtest/test_coverage_reporter_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package celtest provides functions for testing CEL policies and expressions.
116
package celtest
217

318
import (

0 commit comments

Comments
 (0)