Skip to content

Commit 9819012

Browse files
ChinmayMadeshicopybara-github
authored andcommitted
Support triggering runner library programmatically.
PiperOrigin-RevId: 773272797
1 parent a8c7479 commit 9819012

14 files changed

Lines changed: 351 additions & 163 deletions

File tree

testing/src/main/java/dev/cel/testing/testrunner/BUILD.bazel

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ java_library(
4242
tags = [
4343
],
4444
deps = [
45+
":cel_expression_source",
4546
":cel_test_context",
4647
":cel_test_suite",
4748
":test_runner_library",
@@ -55,11 +56,11 @@ java_library(
5556
tags = [
5657
],
5758
deps = [
59+
":cel_expression_source",
5860
":cel_test_context",
5961
":cel_test_suite",
6062
":registry_utils",
6163
":result_matcher",
62-
"//:auto_value",
6364
"//bundle:cel",
6465
"//bundle:environment",
6566
"//bundle:environment_yaml_parser",
@@ -129,6 +130,7 @@ java_library(
129130
tags = [
130131
],
131132
deps = [
133+
":cel_expression_source",
132134
":default_result_matcher",
133135
":result_matcher",
134136
"//:auto_value",
@@ -207,6 +209,16 @@ java_library(
207209
],
208210
)
209211

212+
java_library(
213+
name = "cel_expression_source",
214+
srcs = ["CelExpressionSource.java"],
215+
tags = [
216+
],
217+
deps = [
218+
"//:auto_value",
219+
],
220+
)
221+
210222
filegroup(
211223
name = "test_runner_binary",
212224
srcs = [
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
package dev.cel.testing.testrunner;
15+
16+
import com.google.auto.value.AutoValue;
17+
18+
/**
19+
* CelExpressionSource is an encapsulation around cel_expr file format argument accepted in
20+
* cel_java_test/cel_test bzl macro. It either holds a {@link CheckedExpr} in binarypb/textproto
21+
* format, a serialized {@link CelPolicy} file in yaml/celpolicy format or a raw cel expression in
22+
* cel file format or string format.
23+
*/
24+
@AutoValue
25+
public abstract class CelExpressionSource {
26+
27+
public abstract String value();
28+
29+
public abstract ExpressionSourceType type();
30+
31+
public static CelExpressionSource fromSource(String value) {
32+
return new AutoValue_CelExpressionSource(value, ExpressionSourceType.fromSource(value));
33+
}
34+
35+
public static CelExpressionSource fromRawExpr(String value) {
36+
return new AutoValue_CelExpressionSource(value, ExpressionSourceType.fromRawExpr());
37+
}
38+
39+
/**
40+
* ExpressionSourceType is an enumeration of the supported expression file types.
41+
*
42+
* <p>This enumeration is used to determine the type of the expression file based on the file
43+
* extension.
44+
*/
45+
public enum ExpressionSourceType {
46+
BINARYPB,
47+
TEXTPROTO,
48+
POLICY,
49+
CEL,
50+
RAW_EXPR;
51+
52+
private static ExpressionSourceType fromSource(String filePath) {
53+
if (filePath.endsWith(".binarypb")) {
54+
return BINARYPB;
55+
}
56+
if (filePath.endsWith(".textproto")) {
57+
return TEXTPROTO;
58+
}
59+
if (filePath.endsWith(".yaml") || filePath.endsWith(".celpolicy")) {
60+
return POLICY;
61+
}
62+
if (filePath.endsWith(".cel")) {
63+
return CEL;
64+
}
65+
throw new IllegalArgumentException("Unsupported expression file type: " + filePath);
66+
}
67+
68+
private static ExpressionSourceType fromRawExpr() {
69+
return RAW_EXPR;
70+
}
71+
}
72+
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ public abstract class CelTestContext {
7979
*/
8080
public abstract ResultMatcher resultMatcher();
8181

82+
/**
83+
* The CEL expression to be tested. Could be a expression string or a policy/cel file path. This
84+
* should only be used when invoking the runner library directly.
85+
*/
86+
public abstract Optional<CelExpressionSource> celExpression();
87+
88+
/**
89+
* The config file for the CEL test.
90+
*
91+
* <p>The config file is used to provide a custom environment for the CEL test.
92+
*/
93+
public abstract Optional<String> configFile();
94+
95+
/**
96+
* The file descriptor set path for the CEL test.
97+
*
98+
* <p>The file descriptor set path is used to provide proto descriptors for the CEL test.
99+
*/
100+
public abstract Optional<String> fileDescriptorSetPath();
101+
82102
/** Returns a builder for {@link CelTestContext} with the current instance's values. */
83103
public abstract Builder toBuilder();
84104

@@ -107,6 +127,12 @@ public abstract Builder setCelLateFunctionBindings(
107127

108128
public abstract Builder setResultMatcher(ResultMatcher resultMatcher);
109129

130+
public abstract Builder setCelExpression(CelExpressionSource celExpression);
131+
132+
public abstract Builder setConfigFile(String configFile);
133+
134+
public abstract Builder setFileDescriptorSetPath(String fileDescriptorSetPath);
135+
110136
public abstract CelTestContext build();
111137
}
112138
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ private TestSuite parseTestSuite(String textProto) throws IOException {
5353
TypeRegistry typeRegistry = TypeRegistry.getEmptyTypeRegistry();
5454
ExtensionRegistry extensionRegistry = ExtensionRegistry.getEmptyRegistry();
5555
if (fileDescriptorSetPath != null) {
56-
extensionRegistry = RegistryUtils.getExtensionRegistry();
57-
typeRegistry = RegistryUtils.getTypeRegistry();
56+
extensionRegistry = RegistryUtils.getExtensionRegistry(fileDescriptorSetPath);
57+
typeRegistry = RegistryUtils.getTypeRegistry(fileDescriptorSetPath);
5858
}
5959
TextFormat.Parser parser = TextFormat.Parser.newBuilder().setTypeRegistry(typeRegistry).build();
6060
TestSuite.Builder builder = TestSuite.newBuilder();

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ public CelUserTestTemplate(CelTestContext celTestContext) {
3636

3737
@Test
3838
public void test() throws Exception {
39-
TestRunnerLibrary.runTest(testCase, celTestContext);
39+
TestRunnerLibrary.runTest(testCase, updateCelTestContext(celTestContext));
40+
}
41+
42+
/**
43+
* Updates the CEL test context based on the system properties.
44+
*
45+
* <p>This method is used to update the CEL test context based on the system properties. It checks
46+
* if the runner library is triggered via blaze macro or via JUnit and assigns values accordingly.
47+
*
48+
* @param celTestContext The CEL test context to update.
49+
* @return The updated CEL test context.
50+
*/
51+
private CelTestContext updateCelTestContext(CelTestContext celTestContext) {
52+
String celExpr = System.getProperty("cel_expr");
53+
String configPath = System.getProperty("config_path");
54+
String fileDescriptorSetPath = System.getProperty("file_descriptor_set_path");
55+
56+
CelTestContext.Builder celTestContextBuilder = celTestContext.toBuilder();
57+
if (celExpr != null) {
58+
celTestContextBuilder.setCelExpression(CelExpressionSource.fromSource(celExpr));
59+
}
60+
if (configPath != null) {
61+
celTestContextBuilder.setConfigFile(configPath);
62+
}
63+
if (fileDescriptorSetPath != null) {
64+
celTestContextBuilder.setFileDescriptorSetPath(fileDescriptorSetPath);
65+
}
66+
return celTestContextBuilder.build();
4067
}
4168
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ private static void assertExprValue(ExprValue exprValue, ExprValue expectedExprV
8888
assertThat(exprValue)
8989
.ignoringRepeatedFieldOrderOfFieldDescriptors(
9090
MapValue.getDescriptor().findFieldByName("entries"))
91-
.unpackingAnyUsing(RegistryUtils.getTypeRegistry(), RegistryUtils.getExtensionRegistry())
91+
.unpackingAnyUsing(
92+
RegistryUtils.getTypeRegistry(fileDescriptorSetPath),
93+
RegistryUtils.getExtensionRegistry(fileDescriptorSetPath))
9294
.isEqualTo(expectedExprValue);
9395
} else {
9496
assertThat(exprValue)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@
2727
public final class RegistryUtils {
2828

2929
/** Returns the {@link TypeRegistry} for the given file descriptor set. */
30-
public static TypeRegistry getTypeRegistry() throws IOException {
30+
public static TypeRegistry getTypeRegistry(String fileDescriptorSetPath) throws IOException {
3131
return TypeRegistry.newBuilder()
32-
.add(getAllDescriptorsFromJvm().messageTypeDescriptors())
32+
.add(getAllDescriptorsFromJvm(fileDescriptorSetPath).messageTypeDescriptors())
3333
.build();
3434
}
3535

3636
/** Returns the {@link ExtensionRegistry} for the given file descriptor set. */
37-
public static ExtensionRegistry getExtensionRegistry() throws IOException {
37+
public static ExtensionRegistry getExtensionRegistry(String fileDescriptorSetPath)
38+
throws IOException {
3839
ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
3940

40-
getAllDescriptorsFromJvm()
41+
getAllDescriptorsFromJvm(fileDescriptorSetPath)
4142
.extensionDescriptors()
4243
.forEach(
4344
(descriptorName, descriptor) -> {

0 commit comments

Comments
 (0)