Skip to content

Commit 841c321

Browse files
ChinmayMadeshicopybara-github
authored andcommitted
Internal Changes
PiperOrigin-RevId: 808012281
1 parent b202efc commit 841c321

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

testing/src/main/java/dev/cel/testing/testrunner/CelCoverageIndex.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.auto.value.AutoValue;
2020
import com.google.common.collect.ImmutableList;
21+
import com.google.common.io.Files;
2122
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2223
import javax.annotation.concurrent.ThreadSafe;
2324
import dev.cel.common.CelAbstractSyntaxTree;
@@ -28,6 +29,8 @@
2829
import dev.cel.common.types.CelKind;
2930
import dev.cel.parser.CelUnparserVisitor;
3031
import dev.cel.runtime.CelEvaluationListener;
32+
import java.io.File;
33+
import java.io.IOException;
3134
import java.io.UnsupportedEncodingException;
3235
import java.net.URLEncoder;
3336
import java.util.Map;
@@ -392,4 +395,20 @@ private String escapeSpecialCharacters(String exprText) {
392395
.replace("{", "\\{")
393396
.replace("}", "\\}");
394397
}
398+
399+
private void writeDotGraphToArtifact(String dotGraph) {
400+
String testOutputsDir = System.getenv("TEST_UNDECLARED_OUTPUTS_DIR");
401+
if (testOutputsDir == null) {
402+
return; // Not running in an environment with TEST_UNDECLARED_OUTPUTS_DIR
403+
}
404+
File outputDir = new File(testOutputsDir, "cel_test_coverage");
405+
if (!outputDir.exists()) {
406+
outputDir.mkdirs();
407+
}
408+
try {
409+
Files.write(dotGraph.getBytes(UTF_8), new File(outputDir, "coverage_graph.txt"));
410+
} catch (IOException e) {
411+
throw new IllegalStateException(e);
412+
}
413+
}
395414
}

testing/src/test/java/dev/cel/testing/testrunner/CelCoverageIndexTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
package dev.cel.testing.testrunner;
1515

1616
import static com.google.common.truth.Truth.assertThat;
17+
import static java.nio.charset.StandardCharsets.UTF_8;
1718

1819
import com.google.common.collect.ImmutableMap;
20+
import com.google.common.io.Files;
1921
import dev.cel.bundle.Cel;
2022
import dev.cel.bundle.CelFactory;
2123
import dev.cel.common.CelAbstractSyntaxTree;
@@ -27,6 +29,7 @@
2729
import dev.cel.runtime.CelEvaluationListener;
2830
import dev.cel.runtime.CelRuntime;
2931
import dev.cel.testing.testrunner.CelCoverageIndex.CoverageReport;
32+
import java.io.File;
3033
import org.junit.Before;
3134
import org.junit.Test;
3235
import org.junit.runner.RunWith;
@@ -125,4 +128,32 @@ public void getCoverageReport_comprehension_generatesDotGraph() throws Exception
125128
.contains("label=\"{<1> exprID: 16 | <2> LoopStep} | <3> @result && i % 2 != 0\"");
126129
assertThat(report.dotGraph()).contains("label=\"{<1> exprID: 17 | <2> Result} | <3> @result\"");
127130
}
131+
132+
@Test
133+
public void getCoverageReport_fullCoverage_writesToUndeclaredOutputs() throws Exception {
134+
// Setup for a more complex graph to write.
135+
cel = CelFactory.standardCelBuilder().build();
136+
CelCompiler compiler =
137+
cel.toCompilerBuilder()
138+
.setOptions(CelOptions.newBuilder().populateMacroCalls(true).build())
139+
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
140+
.addLibraries(CelExtensions.comprehensions())
141+
.build();
142+
ast = compiler.compile("[1, 2, 3].all(i, i % 2 != 0)").getAst();
143+
program = cel.createProgram(ast);
144+
CelCoverageIndex coverageIndex = new CelCoverageIndex();
145+
coverageIndex.init(ast);
146+
CelEvaluationListener listener = coverageIndex.newEvaluationListener();
147+
program.trace(ImmutableMap.of(), listener);
148+
149+
CoverageReport report = coverageIndex.generateCoverageReport();
150+
151+
String undeclaredOutputsDir = System.getenv("TEST_UNDECLARED_OUTPUTS_DIR");
152+
assertThat(undeclaredOutputsDir).isNotNull();
153+
154+
File outputFile = new File(undeclaredOutputsDir, "cel_test_coverage/coverage_graph.txt");
155+
156+
String fileContent = Files.asCharSource(outputFile, UTF_8).read();
157+
assertThat(fileContent).isEqualTo(report.dotGraph());
158+
}
128159
}

0 commit comments

Comments
 (0)