|
18 | 18 | import static java.nio.charset.StandardCharsets.UTF_8; |
19 | 19 |
|
20 | 20 | import dev.cel.expr.CheckedExpr; |
| 21 | +import com.google.common.collect.ImmutableList; |
21 | 22 | import com.google.common.collect.ImmutableMap; |
22 | 23 | import com.google.common.io.Files; |
23 | 24 | import com.google.devtools.build.runfiles.AutoBazelRepository; |
|
29 | 30 | import dev.cel.common.CelSource; |
30 | 31 | import dev.cel.expr.conformance.proto3.TestAllTypes; |
31 | 32 | import dev.cel.extensions.CelOptionalLibrary; |
| 33 | +import dev.cel.parser.CelUnparser; |
| 34 | +import dev.cel.parser.CelUnparserFactory; |
32 | 35 | import dev.cel.runtime.CelRuntime; |
33 | 36 | import dev.cel.runtime.CelRuntimeFactory; |
34 | 37 | import java.io.ByteArrayOutputStream; |
|
50 | 53 | @AutoBazelRepository |
51 | 54 | public final class CelPolicyCompilerToolTest { |
52 | 55 |
|
| 56 | + private static final CelUnparser CEL_UNPARSER = CelUnparserFactory.newUnparser(); |
| 57 | + |
53 | 58 | @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); |
54 | 59 |
|
55 | 60 | private Runfiles runfiles; |
@@ -327,6 +332,27 @@ public void compile_withIterationLimitReached_returnsError() throws Exception { |
327 | 332 | assertThat(stdErr).contains("Reason: Unexpected error while composing rules."); |
328 | 333 | } |
329 | 334 |
|
| 335 | + @Test |
| 336 | + public void compile_customFunction_eliminatedByDefault() throws Exception { |
| 337 | + CelAbstractSyntaxTree ast = compileCustomFunctionPolicy(); |
| 338 | + |
| 339 | + assertThat(CEL_UNPARSER.unparse(ast)) |
| 340 | + .isEqualTo( |
| 341 | + "cel.@block([expensive(\"x\")], (@index0 > 1 && @index0 < 10) ? optional.of(true) :" |
| 342 | + + " optional.none())"); |
| 343 | + } |
| 344 | + |
| 345 | + @Test |
| 346 | + public void compile_withNonEliminableFunctions_retainsCalls() throws Exception { |
| 347 | + CelAbstractSyntaxTree ast = |
| 348 | + compileCustomFunctionPolicy("--non_eliminable_functions", "expensive,unused"); |
| 349 | + |
| 350 | + assertThat(CEL_UNPARSER.unparse(ast)) |
| 351 | + .isEqualTo( |
| 352 | + "(expensive(\"x\") > 1 && expensive(\"x\") < 10) ? optional.of(true) :" |
| 353 | + + " optional.none()"); |
| 354 | + } |
| 355 | + |
330 | 356 | @Test |
331 | 357 | public void compile_withOptimizeFieldSelection_rewritesSelectAndEvaluates() throws Exception { |
332 | 358 | String configRlocation = |
@@ -923,6 +949,47 @@ private String createFile(String fileName, String content) throws IOException { |
923 | 949 | return file.getAbsolutePath(); |
924 | 950 | } |
925 | 951 |
|
| 952 | + private CelAbstractSyntaxTree compileCustomFunctionPolicy(String... extraArgs) throws Exception { |
| 953 | + String configPath = |
| 954 | + createFile( |
| 955 | + "config.yaml", |
| 956 | + "name: test-env\n" |
| 957 | + + "functions:\n" |
| 958 | + + " - name: expensive\n" |
| 959 | + + " overloads:\n" |
| 960 | + + " - id: expensive_string\n" |
| 961 | + + " args:\n" |
| 962 | + + " - type_name: string\n" |
| 963 | + + " return:\n" |
| 964 | + + " type_name: int\n"); |
| 965 | + String policyPath = |
| 966 | + createFile( |
| 967 | + "policy.yaml", |
| 968 | + "name: p\n" |
| 969 | + + "rule:\n" |
| 970 | + + " variables:\n" |
| 971 | + + " - name: score\n" |
| 972 | + + " expression: expensive('x')\n" |
| 973 | + + " match:\n" |
| 974 | + + " - condition: variables.score > 1 && variables.score < 10\n" |
| 975 | + + " output: 'true'\n"); |
| 976 | + File outputFile = tempFolder.newFile("output.binarypb"); |
| 977 | + ImmutableList<String> args = |
| 978 | + ImmutableList.<String>builder() |
| 979 | + .add("--policy", policyPath, "--config", configPath) |
| 980 | + .add("--output", outputFile.getAbsolutePath()) |
| 981 | + .add(extraArgs) |
| 982 | + .build(); |
| 983 | + |
| 984 | + int exitCode = |
| 985 | + new CommandLine(new CelPolicyCompilerTool()).execute(args.toArray(new String[0])); |
| 986 | + |
| 987 | + assertThat(exitCode).isEqualTo(0); |
| 988 | + CheckedExpr checkedExpr = |
| 989 | + CheckedExpr.parseFrom(Files.toByteArray(outputFile), ExtensionRegistry.getEmptyRegistry()); |
| 990 | + return CelProtoAbstractSyntaxTree.fromCheckedExpr(checkedExpr).getAst(); |
| 991 | + } |
| 992 | + |
926 | 993 | private static String executeExpectingError(String... args) { |
927 | 994 | StringWriter out = new StringWriter(); |
928 | 995 | PrintWriter pw = new PrintWriter(out); |
|
0 commit comments