|
31 | 31 | import com.google.protobuf.ExtensionRegistry;
|
32 | 32 | import com.google.protobuf.Message;
|
33 | 33 | import com.google.protobuf.TextFormat;
|
| 34 | +import com.google.testing.junit.runner.util.TestPropertyExporter; |
34 | 35 | import dev.cel.bundle.Cel;
|
35 | 36 | import dev.cel.bundle.CelEnvironment;
|
36 | 37 | import dev.cel.bundle.CelEnvironment.ExtensionConfig;
|
@@ -66,6 +67,16 @@ public final class TestRunnerLibrary {
|
66 | 67 |
|
67 | 68 | private static final Logger logger = Logger.getLogger(TestRunnerLibrary.class.getName());
|
68 | 69 |
|
| 70 | + private static final String ATTR_CEL_EXPR = "Cel Expr"; |
| 71 | + private static final String ATTR_CEL_COVERAGE = "Cel Coverage"; |
| 72 | + private static final String ATTR_AST_NODE_COVERAGE = "Ast Node Coverage"; |
| 73 | + private static final String ATTR_INTERESTING_UNENCOUNTERED_NODES = |
| 74 | + "Interesting Unencountered Nodes"; |
| 75 | + private static final String ATTR_AST_BRANCH_COVERAGE = "Ast Branch Coverage"; |
| 76 | + private static final String ATTR_INTERESTING_UNENCOUNTERED_BRANCH_PATHS = |
| 77 | + "Interesting Unencountered Branch Paths"; |
| 78 | + private static final String ATTR_CEL_TEST_COVERAGE_GRAPH_URL = "Cel Test Coverage Graph URL"; |
| 79 | + |
69 | 80 | /**
|
70 | 81 | * Run the assertions for a given raw/checked expression test case.
|
71 | 82 | *
|
@@ -144,6 +155,15 @@ static void evaluateTestCase(
|
144 | 155 | celCoverageIndex.init(ast);
|
145 | 156 | }
|
146 | 157 | evaluate(ast, testCase, celTestContext, celCoverageIndex);
|
| 158 | + |
| 159 | + // For programmatic tests, if coverage is not enabled via the build macro, update the Sponge |
| 160 | + // properties with the coverage report. |
| 161 | + // This flag does not exist when the test is run via direct invocation of {@link |
| 162 | + // TestRunnerLibrary#runTest} |
| 163 | + String isCoverageEnabled = System.getProperty("is_coverage_enabled"); |
| 164 | + if (isCoverageEnabled == null && celCoverageIndex != null) { |
| 165 | + updateSpongeProperties(celCoverageIndex.generateCoverageReport()); |
| 166 | + } |
147 | 167 | }
|
148 | 168 |
|
149 | 169 | private static CelAbstractSyntaxTree readAstFromCheckedExpression(
|
@@ -403,4 +423,48 @@ private static Object getValueFromBinding(Object value, CelTestContext celTestCo
|
403 | 423 | }
|
404 | 424 | return value;
|
405 | 425 | }
|
| 426 | + |
| 427 | + /** |
| 428 | + * Updates Sponge properties with the provided coverage report. |
| 429 | + * |
| 430 | + * <p>This method is called when {@link TestRunnerLibrary#runTest} is invoked directly to export |
| 431 | + * coverage data. |
| 432 | + */ |
| 433 | + private static void updateSpongeProperties(CelCoverageIndex.CoverageReport report) { |
| 434 | + TestPropertyExporter exporter = TestPropertyExporter.INSTANCE; |
| 435 | + if (report.nodes() == 0) { |
| 436 | + exporter.exportProperty(ATTR_CEL_COVERAGE, "No coverage stats found"); |
| 437 | + } else { |
| 438 | + // CEL expression |
| 439 | + exporter.exportProperty(ATTR_CEL_EXPR, report.celExpression()); |
| 440 | + // Node coverage |
| 441 | + double nodeCoverage = (double) report.coveredNodes() / (double) report.nodes() * 100.0; |
| 442 | + String nodeCoverageString = |
| 443 | + String.format( |
| 444 | + "%.2f%% (%d out of %d nodes covered)", |
| 445 | + nodeCoverage, report.coveredNodes(), report.nodes()); |
| 446 | + exporter.exportProperty(ATTR_AST_NODE_COVERAGE, nodeCoverageString); |
| 447 | + if (!report.unencounteredNodes().isEmpty()) { |
| 448 | + exporter.exportProperty( |
| 449 | + ATTR_INTERESTING_UNENCOUNTERED_NODES, String.join("\n", report.unencounteredNodes())); |
| 450 | + } |
| 451 | + // Branch coverage |
| 452 | + double branchCoverage = 0.0; |
| 453 | + if (report.branches() > 0) { |
| 454 | + branchCoverage = |
| 455 | + (double) report.coveredBooleanOutcomes() / (double) report.branches() * 100.0; |
| 456 | + } |
| 457 | + String branchCoverageString = |
| 458 | + String.format( |
| 459 | + "%.2f%% (%d out of %d branch outcomes covered)", |
| 460 | + branchCoverage, report.coveredBooleanOutcomes(), report.branches()); |
| 461 | + exporter.exportProperty(ATTR_AST_BRANCH_COVERAGE, branchCoverageString); |
| 462 | + if (!report.unencounteredBranches().isEmpty()) { |
| 463 | + exporter.exportProperty( |
| 464 | + ATTR_INTERESTING_UNENCOUNTERED_BRANCH_PATHS, |
| 465 | + String.join("\n", report.unencounteredBranches())); |
| 466 | + } |
| 467 | + exporter.exportProperty(ATTR_CEL_TEST_COVERAGE_GRAPH_URL, report.graphUrl()); |
| 468 | + } |
| 469 | + } |
406 | 470 | }
|
0 commit comments