From 468b3bc4d5c57603e44699117b683ea1b8fa5aac Mon Sep 17 00:00:00 2001 From: Dmitri Plotnikov Date: Thu, 19 Nov 2026 12:43:57 -0800 Subject: [PATCH] Add expression and result to the env test case PiperOrigin-RevId: 1025818932 --- conformance/env/BUILD | 83 ++++++++++ conformance/env/env_conformance_test.bzl | 46 ++++++ conformance/env/env_conformance_test.cc | 197 +++++++++++++++++++++++ conformance/env/normalize_yaml.cc | 147 +++++++++++++++++ conformance/env/normalize_yaml.h | 34 ++++ conformance/env/normalize_yaml_test.cc | 79 +++++++++ 6 files changed, 586 insertions(+) create mode 100644 conformance/env/BUILD create mode 100644 conformance/env/env_conformance_test.bzl create mode 100644 conformance/env/env_conformance_test.cc create mode 100644 conformance/env/normalize_yaml.cc create mode 100644 conformance/env/normalize_yaml.h create mode 100644 conformance/env/normalize_yaml_test.cc diff --git a/conformance/env/BUILD b/conformance/env/BUILD new file mode 100644 index 000000000..a2412a23d --- /dev/null +++ b/conformance/env/BUILD @@ -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, +) diff --git a/conformance/env/env_conformance_test.bzl b/conformance/env/env_conformance_test.bzl new file mode 100644 index 000000000..9ec4bd4c2 --- /dev/null +++ b/conformance/env/env_conformance_test.bzl @@ -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 + ) diff --git a/conformance/env/env_conformance_test.cc b/conformance/env/env_conformance_test.cc new file mode 100644 index 000000000..6e3a5fe79 --- /dev/null +++ b/conformance/env/env_conformance_test.cc @@ -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 +#include +#include +#include +#include +#include + +#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, test_suites, {}, + "Comma-separated list of test suite textproto runfiles paths."); +ABSL_FLAG(std::vector, 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 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_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 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 suite_paths = absl::GetFlag(FLAGS_test_suites); + std::vector 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(); +} diff --git a/conformance/env/normalize_yaml.cc b/conformance/env/normalize_yaml.cc new file mode 100644 index 000000000..b44b9a782 --- /dev/null +++ b/conformance/env/normalize_yaml.cc @@ -0,0 +1,147 @@ +// 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 "conformance/env/normalize_yaml.h" + +#include +#include +#include + +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "absl/strings/strip.h" + +namespace cel { +namespace { + +// Unquotes a single- or double-quoted YAML scalar value so that cosmetic +// quoting differences (e.g. `int` vs `"int"` vs `'int'`, or `"~T\\.U"` vs +// `'~T\.U'`) normalize to the same string while preserving the exact scalar +// contents (including any internal whitespace). +std::string UnquoteYamlScalar(absl::string_view value) { + // Double-quoted scalars: + // "int" -> int + // "a\"b\\c" -> a"b\c + // "~T\\.U" -> ~T\.U + if (value.size() >= 2 && value.front() == '"' && value.back() == '"') { + absl::string_view inner = value.substr(1, value.size() - 2); + std::string out; + out.reserve(inner.size()); + for (size_t i = 0; i < inner.size(); ++i) { + if (inner[i] == '\\' && i + 1 < inner.size()) { + ++i; + switch (inner[i]) { + case '\\': + out.push_back('\\'); + break; + case '"': + out.push_back('"'); + break; + case 'n': + out.push_back('\n'); + break; + case 'r': + out.push_back('\r'); + break; + case 't': + out.push_back('\t'); + break; + default: + out.push_back('\\'); + out.push_back(inner[i]); + break; + } + } else { + out.push_back(inner[i]); + } + } + return out; + } + + // Single-quoted scalars: + // 'int' -> int + // 'it''s' -> it's + // '~T\.U' -> ~T\.U + if (value.size() >= 2 && value.front() == '\'' && value.back() == '\'') { + absl::string_view inner = value.substr(1, value.size() - 2); + std::string out; + out.reserve(inner.size()); + for (size_t i = 0; i < inner.size(); ++i) { + if (inner[i] == '\'' && i + 1 < inner.size() && inner[i + 1] == '\'') { + out.push_back('\''); + ++i; + } else { + out.push_back(inner[i]); + } + } + return out; + } + + // Unquoted scalars are returned verbatim, e.g. int -> int. + return std::string(value); +} + +} // namespace + +std::string NormalizeYaml(absl::string_view yaml) { + std::vector normalized_lines; + for (absl::string_view line : absl::StrSplit(yaml, '\n')) { + // Normalize CRLF -> LF and strip trailing spaces/tabs. + absl::ConsumeSuffix(&line, "\r"); + while (!line.empty() && (line.back() == ' ' || line.back() == '\t')) { + line.remove_suffix(1); + } + if (line.empty()) { + continue; + } + size_t colon_space = line.find(": "); + if (colon_space != absl::string_view::npos) { + // Mapping entry with a scalar value: + // type: "int" -> type: int + // type: 'bool' -> type: bool + absl::string_view key_prefix = line.substr(0, colon_space + 2); + absl::string_view value = line.substr(key_prefix.size()); + while (!value.empty() && value.front() == ' ') { + value.remove_prefix(1); + } + normalized_lines.push_back( + absl::StrCat(key_prefix, UnquoteYamlScalar(value))); + } else { + // `line` is non-empty and has no trailing spaces, so `first_non_space` is + // guaranteed to be a valid index. + size_t first_non_space = line.find_first_not_of(' '); + if (absl::StartsWith(line.substr(first_non_space), "- ")) { + // Sequence item with a scalar value: + // - "int" -> - int + // - 'bool' -> - bool + absl::string_view item_prefix = line.substr(0, first_non_space + 2); + absl::string_view value = line.substr(item_prefix.size()); + while (!value.empty() && value.front() == ' ') { + value.remove_prefix(1); + } + normalized_lines.push_back( + absl::StrCat(item_prefix, UnquoteYamlScalar(value))); + } else { + // Structural lines such as `variables:` are kept as-is. + normalized_lines.push_back(std::string(line)); + } + } + } + return absl::StrJoin(normalized_lines, "\n"); +} + +} // namespace cel diff --git a/conformance/env/normalize_yaml.h b/conformance/env/normalize_yaml.h new file mode 100644 index 000000000..9daacafc8 --- /dev/null +++ b/conformance/env/normalize_yaml.h @@ -0,0 +1,34 @@ +// 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. + +#ifndef THIRD_PARTY_CEL_CPP_CONFORMANCE_ENV_NORMALIZE_YAML_H_ +#define THIRD_PARTY_CEL_CPP_CONFORMANCE_ENV_NORMALIZE_YAML_H_ + +#include + +#include "absl/strings/string_view.h" + +namespace cel { + +// Performs textual normalization on properly formatted YAML: +// - Normalizes CRLF line endings and trims trailing whitespace/blank lines. +// - Unquotes scalar values on `key: value` and `- value` sequence item lines +// (e.g., `type: "int"` -> `type: int`, `- "int"` -> `- int`) so quoting +// style differences do not cause false mismatches while preserving exact +// scalar values. +std::string NormalizeYaml(absl::string_view yaml); + +} // namespace cel + +#endif // THIRD_PARTY_CEL_CPP_CONFORMANCE_ENV_NORMALIZE_YAML_H_ diff --git a/conformance/env/normalize_yaml_test.cc b/conformance/env/normalize_yaml_test.cc new file mode 100644 index 000000000..a94e0ea6d --- /dev/null +++ b/conformance/env/normalize_yaml_test.cc @@ -0,0 +1,79 @@ +// 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 "conformance/env/normalize_yaml.h" + +#include "internal/testing.h" + +namespace cel { +namespace { + +TEST(NormalizeYamlTest, NormalizesLineEndingsWhitespaceAndBlankLines) { + EXPECT_EQ(NormalizeYaml( + "variables: \r\n\r\n - name: x\t \r\n\n type: bool \n"), + "variables:\n - name: x\n type: bool"); +} + +TEST(NormalizeYamlTest, UnquotesDoubleQuotedMappingScalars) { + EXPECT_EQ(NormalizeYaml("variables:\n" + " - name: \"x\"\n" + " type: \"map\"\n"), + "variables:\n" + " - name: x\n" + " type: map"); +} + +TEST(NormalizeYamlTest, DecodesDoubleQuotedEscapeSequences) { + EXPECT_EQ(NormalizeYaml(R"(a: "hello\"world\\path\n\r\t\x")"), + "a: hello\"world\\path\n\r\t\\x"); +} + +TEST(NormalizeYamlTest, UnquotesSingleQuotedMappingScalars) { + EXPECT_EQ(NormalizeYaml("a: 'int'\nb: 'it''s'\nc: '~T\\.U'\nd: 'it's'\n"), + "a: int\nb: it's\nc: ~T\\.U\nd: it's"); +} + +TEST(NormalizeYamlTest, DoubleAndSingleQuotedEscapesMatch) { + EXPECT_EQ(NormalizeYaml(R"(type: "~T\\.U")"), + NormalizeYaml(R"(type: '~T\.U')")); +} + +TEST(NormalizeYamlTest, CollapsesExtraSpacesAfterColon) { + EXPECT_EQ(NormalizeYaml("type: \"int\"\n"), "type: int"); +} + +TEST(NormalizeYamlTest, UnquotesSequenceItemScalars) { + EXPECT_EQ(NormalizeYaml("params:\n" + " - \"int\"\n" + " - 'bool'\n" + " - \"string\"\n" + " - double\n"), + "params:\n" + " - int\n" + " - bool\n" + " - string\n" + " - double"); +} + +TEST(NormalizeYamlTest, HandlesEmptyAndUnclosedQuotes) { + EXPECT_EQ(NormalizeYaml("a: \"\"\nb: ''\nc: \"\nd: '\ne: \"unclosed\n"), + "a: \nb: \nc: \"\nd: '\ne: \"unclosed"); +} + +TEST(NormalizeYamlTest, PreservesNonSequenceStructuralLines) { + EXPECT_EQ(NormalizeYaml("x\nab \"c\"\n"), "x\nab \"c\""); +} + +} // namespace +} // namespace cel