From 57a6e8df1c8dd44962b4d3357ba93e7291d79191 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Wed, 17 Apr 2024 16:31:41 -0700 Subject: [PATCH] Add dedicated tests to lock down that overridable defaults never change. Once an edition is released these should be fixed forever. On the other hand, the fixed defaults may see new features included as we change legacy behaviors in future editions. PiperOrigin-RevId: 625842071 --- editions/BUILD | 30 +++++++++++++++++++ editions/defaults_test.cc | 63 +++++++++++++++++++++++++++++++++++++++ java/core/BUILD.bazel | 1 + 3 files changed, 94 insertions(+) diff --git a/editions/BUILD b/editions/BUILD index fc85f4fa18af9..2cfd511e61fd6 100644 --- a/editions/BUILD +++ b/editions/BUILD @@ -10,6 +10,18 @@ bzl_library( visibility = ["//visibility:public"], ) +# Aggregate all the features owned by the Protobuf repo. +compile_edition_defaults( + name = "protobuf_defaults", + testonly = True, + srcs = [ + "//java/core:java_features_proto", + "//src/google/protobuf:cpp_features_proto", + ], + maximum_edition = "2023", + minimum_edition = "2023", +) + compile_edition_defaults( name = "test_defaults_2023", testonly = True, @@ -75,18 +87,36 @@ cc_library( ], ) +cc_proto_library( + name = "cpp_features_cc_proto", + testonly = True, + deps = ["//src/google/protobuf:cpp_features_proto"], +) + +cc_proto_library( + name = "java_features_cc_proto", + testonly = True, + deps = ["//java/core:java_features_proto"], +) + cc_test( name = "defaults_test", srcs = ["defaults_test.cc"], data = [ + ":protobuf_defaults", ":test_defaults_2023", ":test_defaults_far_future", ":test_defaults_future", ], deps = [ + ":cpp_features_cc_proto", ":defaults_test_embedded", + ":java_features_cc_proto", + "//:protobuf", "//src/google/protobuf", "//src/google/protobuf:port", + "//src/google/protobuf:protobuf_lite", + "//src/google/protobuf:test_textproto", "//src/google/protobuf:unittest_features_cc_proto", "//src/google/protobuf/stubs", "//src/google/protobuf/testing", diff --git a/editions/defaults_test.cc b/editions/defaults_test.cc index 99432c8c7812d..fb87038294664 100644 --- a/editions/defaults_test.cc +++ b/editions/defaults_test.cc @@ -12,11 +12,17 @@ #include "absl/strings/escaping.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" +#include "google/protobuf/java_features.pb.h" +#include "google/protobuf/cpp_features.pb.h" #include "editions/defaults_test_embedded.h" #include "editions/defaults_test_embedded_base64.h" +#include "google/protobuf/extension_set.h" +#include "google/protobuf/message.h" +#include "google/protobuf/test_textproto.h" #include "google/protobuf/unittest_features.pb.h" #include "google/protobuf/stubs/status_macros.h" + // Must be included last. #include "google/protobuf/port_def.inc" @@ -182,6 +188,63 @@ TEST(DefaultsTest, EmbeddedBase64) { pb::VALUE3); } +// Lock down that overridable defaults never change in released editions. After +// an edition has been released these tests should never need to be touched. +class OverridableDefaultsTest : public ::testing::Test { + public: + OverridableDefaultsTest() = default; + static void SetUpTestSuite() { + google::protobuf::LinkExtensionReflection(pb::cpp); + google::protobuf::LinkExtensionReflection(pb::java); + DescriptorPool::generated_pool(); + } +}; + +// TODO Enable these once they become fixed internally. +TEST_F(OverridableDefaultsTest, Proto2) { + auto feature_defaults = ReadDefaults("protobuf_defaults"); + ASSERT_OK(feature_defaults); + ASSERT_GE(feature_defaults->defaults().size(), 1); + const auto& defaults = feature_defaults->defaults(0); + ASSERT_EQ(defaults.edition(), EDITION_PROTO2); + + EXPECT_THAT(defaults.overridable_features(), EqualsProto(R"pb([pb.cpp] {} + [pb.java] {} + )pb")); +} +TEST_F(OverridableDefaultsTest, Proto3) { + auto feature_defaults = ReadDefaults("protobuf_defaults"); + ASSERT_OK(feature_defaults); + ASSERT_GE(feature_defaults->defaults().size(), 2); + const auto& defaults = feature_defaults->defaults(1); + ASSERT_EQ(defaults.edition(), EDITION_PROTO3); + + EXPECT_THAT(defaults.overridable_features(), EqualsProto(R"pb([pb.cpp] {} + [pb.java] {} + )pb")); +} + +// Lock down that 2023 overridable defaults never change. Once Edition 2023 has +// been released this test should never need to be touched. +TEST_F(OverridableDefaultsTest, Edition2023) { + auto feature_defaults = ReadDefaults("protobuf_defaults"); + ASSERT_OK(feature_defaults); + ASSERT_GE(feature_defaults->defaults().size(), 3); + const auto& defaults = feature_defaults->defaults(2); + ASSERT_EQ(defaults.edition(), EDITION_2023); + + EXPECT_THAT(defaults.overridable_features(), EqualsProto(R"pb( + field_presence: EXPLICIT + enum_type: OPEN + repeated_field_encoding: PACKED + utf8_validation: VERIFY + message_encoding: LENGTH_PREFIXED + json_format: ALLOW + [pb.cpp] { legacy_closed_enum: false string_type: STRING } + [pb.java] { legacy_closed_enum: false utf8_validation: DEFAULT } + )pb")); +} + } // namespace } // namespace protobuf } // namespace google diff --git a/java/core/BUILD.bazel b/java/core/BUILD.bazel index 93c3315594c47..683b0654acf42 100644 --- a/java/core/BUILD.bazel +++ b/java/core/BUILD.bazel @@ -187,6 +187,7 @@ proto_library( strip_import_prefix = "/java/core/src/main/resources", visibility = [ "//:__pkg__", + "//editions:__pkg__", "//java/__subpackages__", "//pkg:__pkg__", ],