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
83 changes: 83 additions & 0 deletions conformance/env/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")
load(
"//conformance/env:env_conformance_test.bzl",
"cel_env_conformance_test",
)

package(default_visibility = ["//visibility:public"])

cc_library(
name = "normalize_yaml",
testonly = True,
srcs = ["normalize_yaml.cc"],
hdrs = ["normalize_yaml.h"],
deps = [
"@com_google_absl//absl/strings",
],
)

cc_test(
name = "normalize_yaml_test",
srcs = ["normalize_yaml_test.cc"],
deps = [
":normalize_yaml",
"//internal:testing",
],
)

cc_library(
name = "env_conformance_test_lib",
testonly = True,
srcs = ["env_conformance_test.cc"],
deps = [
":normalize_yaml",
"//env:config",
"//env:env_yaml",
"//internal:runfiles",
"//internal:testing_no_main",
"@cel_conformance//proto/cel/expr/conformance/test/env:suite_cc_proto",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//src/google/protobuf/io",
],
alwayslink = True,
)

_ALL_TESTS = [
"@cel_conformance//tests/env:testdata/type_signatures.textproto",
]

cel_env_conformance_test(
name = "env_conformance_test",
test_files = _ALL_TESTS,
)

cel_env_conformance_test(
name = "env_conformance_skip_test",
skip_tests = [
"type_signatures/primitives/bool",
"type_signatures/timestamp",
],
test_files = _ALL_TESTS,
)
46 changes: 46 additions & 0 deletions conformance/env/env_conformance_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This module contains build rules for generating env conformance test targets.
"""

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

def cel_env_conformance_test(name, test_files, skip_tests = [], **kwargs):
"""Generates an env conformance test target.

Args:
name: Name of the test target.
test_files: List of targets or files representing the test suite textprotos.
skip_tests: List of test cases (or suite/section prefixes) to skip.
**kwargs: Additional arguments passed to the underlying cc_test.
"""
args = [
]
paths = ["$(rlocationpath {})".format(f) for f in test_files]
args.append("--test_suites={}".format(",".join(paths)))

if skip_tests:
args.append("--skip_tests=" + ",".join(skip_tests))

cc_test(
name = name,
data = test_files,
deps = [
"//conformance/env:env_conformance_test_lib",
],
args = args,
**kwargs
)
197 changes: 197 additions & 0 deletions conformance/env/env_conformance_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <fstream>
#include <ios>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include "absl/flags/flag.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/status/status_matchers.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "absl/types/span.h"
#include "cel/expr/conformance/test/env/suite.pb.h"
#include "conformance/env/normalize_yaml.h"
#include "env/config.h"
#include "env/env_yaml.h"
#include "internal/runfiles.h"
#include "internal/testing.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/text_format.h"

ABSL_FLAG(std::vector<std::string>, test_suites, {},
"Comma-separated list of test suite textproto runfiles paths.");
ABSL_FLAG(std::vector<std::string>, skip_tests, {},
"Comma-separated list of tests to skip.");

namespace cel {
namespace {

using absl_testing::IsOk;
using cel::expr::conformance::test::env::TestCase;
using cel::expr::conformance::test::env::TestOutput;
using cel::expr::conformance::test::env::TestSuite;
using testing::HasSubstr;
using testing::Not;

bool ShouldSkipTest(absl::Span<const std::string> tests_to_skip,
absl::string_view name) {
for (absl::string_view test_to_skip : tests_to_skip) {
absl::string_view consumed_name = name;
if (absl::ConsumePrefix(&consumed_name, test_to_skip) &&
(consumed_name.empty() || absl::StartsWith(consumed_name, "/"))) {
return true;
}
}
return false;
}

class EnvConformanceTest : public testing::Test {
public:
EnvConformanceTest(TestCase test_case, std::string full_test_name, bool skip)
: test_case_(std::move(test_case)),
full_test_name_(std::move(full_test_name)),
skip_(skip) {}

void TestBody() override {
if (skip_) {
GTEST_SKIP() << "Skipping test: " << full_test_name_;
}

absl::StatusOr<Config> config_or;
switch (test_case_.env_config_case()) {
case TestCase::kEnvYaml:
config_or = EnvConfigFromYaml(test_case_.env_yaml());
break;
case TestCase::kEnv:
FAIL() << "Proto Environment input is not yet supported in "
<< full_test_name_;
case TestCase::ENV_CONFIG_NOT_SET:
FAIL() << "No env_config specified in " << full_test_name_;
}

const TestOutput& output = test_case_.output();
const bool has_exported_yaml = !output.exported_env_yaml().empty();
const bool has_expected_errors = !output.errors().empty();

if (!has_exported_yaml && !has_expected_errors) {
FAIL() << "No expected output specified in " << full_test_name_;
}

if (has_expected_errors) {
ASSERT_THAT(config_or, Not(IsOk()))
<< "Expected error in " << full_test_name_ << ", but succeeded.";
for (absl::string_view expected_err : output.errors()) {
if (!expected_err.empty()) {
EXPECT_THAT(config_or.status().message(), HasSubstr(expected_err));
}
}
return;
}

ASSERT_THAT(config_or, IsOk())
<< "Failed to parse env_yaml in " << full_test_name_;

if (has_exported_yaml) {
std::ostringstream actual_stream;
EnvConfigToYamlOptions options;
options.use_type_signatures = true;
EnvConfigToYaml(*config_or, actual_stream, options);
std::string actual_yaml = actual_stream.str();

EXPECT_EQ(NormalizeYaml(actual_yaml),
NormalizeYaml(output.exported_env_yaml()))
<< "Env YAML mismatch in " << full_test_name_ << "\nExpected:\n"
<< output.exported_env_yaml() << "\nActual:\n"
<< actual_yaml;
}
}

private:
TestCase test_case_;
std::string full_test_name_;
bool skip_;
};

absl::Status RegisterTestsFromFile(absl::Span<const std::string> skip_tests,
absl::string_view runfile_path) {
std::string abs_path = cel::internal::ResolveRunfilesPath(runfile_path);
if (abs_path.empty()) {
return absl::NotFoundError(
absl::StrCat("could not resolve runfiles path: ", runfile_path));
}

TestSuite suite;
{
std::ifstream in(abs_path, std::ios_base::in | std::ios_base::binary);
if (!in.is_open()) {
return absl::UnknownError(
absl::StrCat("failed to open test suite file: ", abs_path));
}
google::protobuf::io::IstreamInputStream stream(&in);
if (!google::protobuf::TextFormat::Parse(&stream, &suite)) {
return absl::InvalidArgumentError(
absl::StrCat("failed to parse test suite textproto: ", abs_path));
}
}

const std::string& suite_name = suite.name();
for (const auto& section : suite.sections()) {
const std::string& section_name = section.name();
for (const auto& test : section.tests()) {
const std::string& test_name = test.name();
std::string full_test_name =
absl::StrCat(suite_name, "/", section_name, "/", test_name);
bool skip = ShouldSkipTest(skip_tests, full_test_name);

testing::RegisterTest(
suite_name.c_str(),
absl::StrCat(section_name, "/", test_name).c_str(), nullptr, nullptr,
__FILE__, __LINE__, [test, full_test_name, skip]() {
return new EnvConformanceTest(test, full_test_name, skip);
});
}
}
return absl::OkStatus();
}

void RegisterAllTests() {
std::vector<std::string> suite_paths = absl::GetFlag(FLAGS_test_suites);
std::vector<std::string> skip_tests = absl::GetFlag(FLAGS_skip_tests);

ABSL_CHECK(!suite_paths.empty())
<< "No test suites specified in --test_suites flag.";

for (const std::string& suite_path : suite_paths) {
ABSL_CHECK_OK(RegisterTestsFromFile(skip_tests, suite_path));
}
}

} // namespace
} // namespace cel

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);

cel::RegisterAllTests();
return RUN_ALL_TESTS();
}
Loading
Loading