Skip to content

Commit

Permalink
Add dedicated tests to lock down that overridable defaults never change.
Browse files Browse the repository at this point in the history
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: 625823442
  • Loading branch information
mkruskal-google authored and copybara-github committed Apr 17, 2024
1 parent 1bc6d6d commit 879c34b
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
31 changes: 31 additions & 0 deletions editions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
"//src/google/protobuf:cpp_features_proto",
"//third_party/java/protobuf:java_features_proto",
],
maximum_edition = "2023",
minimum_edition = "2023",
)

compile_edition_defaults(
name = "test_defaults_2023",
testonly = True,
Expand Down Expand Up @@ -75,18 +87,37 @@ cc_library(
],
)

cc_proto_library(
name = "java_features_cc_proto",
testonly = True,
deps = ["//third_party/java/protobuf:java_features_proto"],
)

cc_proto_library(
name = "proto1_features_cc_proto",
testonly = True,
deps = ["//net/proto:proto1_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 = [
":defaults_test_embedded",
":java_features_cc_proto",
":proto1_features_cc_proto",
"//:protobuf",
"//src/google/protobuf",
"//src/google/protobuf:cpp_features_cc_proto",
"//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",
Expand Down
83 changes: 83 additions & 0 deletions editions/defaults_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "tools/cpp/runfiles/runfiles.h"
#include "google/protobuf/testing/file.h"
#include "google/protobuf/testing/file.h"
#include "net/proto/proto1_features.pb.h"
#include "google/protobuf/descriptor.pb.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
Expand All @@ -12,8 +13,13 @@
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "third_party/java/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"

Expand Down Expand Up @@ -182,6 +188,83 @@ 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);
google::protobuf::LinkExtensionReflection(pb::proto1);
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_PROTO2);

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);

#ifndef PROTO2_OPENSOURCE
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
large_enum: false
}
[pb.proto1] { legacy_packed: false }
)pb"));
#include "google/protobuf/stubs/common.h"

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"));
#endif
}

} // namespace
} // namespace protobuf
} // namespace google

0 comments on commit 879c34b

Please sign in to comment.