Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion testing/testrunner/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ cc_library(
name = "runner",
srcs = ["runner_bin.cc"],
deps = [
":cel_expression_source",
":cel_test_context",
":cel_test_factories",
":coverage_index",
":runner_lib",
"//eval/public:cel_expression",
"//internal:status_macros",
"//internal:testing_no_main",
"//runtime",
"@com_google_absl//absl/flags:flag",
Expand All @@ -130,9 +132,9 @@ cc_library(
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_cel_spec//proto/cel/expr:checked_cc_proto",
"@com_google_cel_spec//proto/cel/expr/conformance/test:suite_cc_proto",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//src/google/protobuf/io",
],
alwayslink = True,
)
Expand Down
44 changes: 42 additions & 2 deletions testing/testrunner/cel_cc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@

"""Rules for triggering the cc impl of the CEL test runner."""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_cc//cc:cc_test.bzl", "cc_test")

def cel_cc_test(
name,
test_suite = "",
cel_expr = "",
is_raw_expr = False,
filegroup = "",
deps = [],
enable_coverage = False,
Expand All @@ -33,6 +36,10 @@ def cel_cc_test(
name: str name for the generated artifact
test_suite: str label of a file containing a test suite. The file should have a
.textproto extension.
cel_expr: The CEL expression source. The meaning of this argument depends on `is_raw_expr`.
is_raw_expr: bool whether the cel_expr is a raw expression string. If False,
cel_expr is treated as a file path. The file type (.cel or .textproto)
is inferred from the extension.
filegroup: str label of a filegroup containing the test suite, the config and the checked
expression.
deps: list of dependencies for the cc_test rule.
Expand All @@ -41,7 +48,14 @@ def cel_cc_test(
test_data_path: absolute path of the directory containing the test files. This is needed only
if the test files are not located in the same directory as the BUILD file.
"""
data, test_data_path = _update_data_with_test_files(data, filegroup, test_data_path, test_suite)
data, test_data_path = _update_data_with_test_files(
data,
filegroup,
test_data_path,
test_suite,
cel_expr,
is_raw_expr,
)
args = []

test_data_path = test_data_path.lstrip("/")
Expand All @@ -52,23 +66,49 @@ def cel_cc_test(

args.append("--collect_coverage=" + str(enable_coverage))

if cel_expr != "":
expression_kind = ""
expr_source = ""
if is_raw_expr:
expression_kind = "raw"
expr_source = "\"" + cel_expr + "\""
else:
_, ext = paths.split_extension(cel_expr)
if ext == ".cel":
expression_kind = "file"
expr_source = test_data_path + "/" + cel_expr
else:
expression_kind = "checked"
expr_source = "$(location " + cel_expr + ")"

args.append("--expression_kind=" + expression_kind)
args.append("--expr_source=" + expr_source)

cc_test(
name = name,
data = data,
args = args,
deps = ["//testing/testrunner:runner"] + deps,
)

def _update_data_with_test_files(data, filegroup, test_data_path, test_suite):
def _update_data_with_test_files(data, filegroup, test_data_path, test_suite, cel_expr, is_raw_expr):
"""Updates the data with the test files."""

if filegroup != "":
data = data + [filegroup]
elif test_data_path != "" and test_data_path != native.package_name():
if test_suite != "":
data = data + [test_data_path + ":" + test_suite]
if cel_expr != "" and not is_raw_expr:
_, ext = paths.split_extension(cel_expr)
if ext == ".cel":
data = data + [test_data_path + ":" + cel_expr]
else:
data = data + [cel_expr]
else:
test_data_path = native.package_name()
if test_suite != "":
data = data + [test_suite]
if cel_expr != "" and not is_raw_expr:
data = data + [cel_expr]
return data, test_data_path
17 changes: 11 additions & 6 deletions testing/testrunner/cel_test_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ namespace cel::test {

// Struct to hold optional parameters for `CelTestContext`.
struct CelTestContextOptions {
// The source for the CEL expression to be evaluated in the test.
std::optional<CelExpressionSource> expression_source;

// An optional CEL compiler. This is required for test cases where
// input or output values are themselves CEL expressions that need to be
// resolved at runtime or cel expression source is raw string or cel file.
Expand Down Expand Up @@ -105,16 +102,21 @@ class CelTestContext {
}

const CelExpressionSource* absl_nullable expression_source() const {
return cel_test_context_options_.expression_source.has_value()
? &cel_test_context_options_.expression_source.value()
: nullptr;
return expression_source_.get();
}

const absl::flat_hash_map<std::string, cel::expr::Value>&
custom_bindings() const {
return cel_test_context_options_.custom_bindings;
}

// Allows the runner to inject the expression source
// parsed from command-line flags.
void SetExpressionSource(CelExpressionSource source) {
expression_source_ =
std::make_unique<CelExpressionSource>(std::move(source));
}

private:
// Delete copy and move constructors.
CelTestContext(const CelTestContext&) = delete;
Expand All @@ -135,6 +137,9 @@ class CelTestContext {
: cel_test_context_options_(std::move(options)),
runtime_(std::move(runtime)) {}

// The source for the CEL expression to be evaluated in the test.
std::unique_ptr<CelExpressionSource> expression_source_;

// Configuration for the expression to be executed.
CelTestContextOptions cel_test_context_options_;

Expand Down
4 changes: 3 additions & 1 deletion testing/testrunner/resources/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package(default_visibility = ["//visibility:public"])

exports_files(
["test.cel"],
[
"test.cel",
],
)

filegroup(
Expand Down
15 changes: 15 additions & 0 deletions testing/testrunner/resources/test_environment.textproto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# proto-file: third_party/cel/go/tools/compilecli/compile_input.proto
# proto-message: Environment

declarations: {
name: "x"
ident: {
type: { primitive: INT64 }
}
}
declarations: {
name: "y"
ident: {
type: { primitive: INT64 }
}
}
Loading