Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dedicated tests to lock down that overridable defaults never change. #16553

Merged
merged 1 commit into from
Apr 17, 2024
Merged
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
30 changes: 30 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 = [
"//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,
Expand Down Expand Up @@ -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",
Expand Down
63 changes: 63 additions & 0 deletions editions/defaults_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions java/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ proto_library(
strip_import_prefix = "/java/core/src/main/resources",
visibility = [
"//:__pkg__",
"//editions:__pkg__",
"//java/__subpackages__",
"//pkg:__pkg__",
],
Expand Down
Loading