Skip to content

Commit 57a6e8d

Browse files
mkruskal-googlecopybara-github
authored andcommitted
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
1 parent f76b28e commit 57a6e8d

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

editions/BUILD

+30
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ bzl_library(
1010
visibility = ["//visibility:public"],
1111
)
1212

13+
# Aggregate all the features owned by the Protobuf repo.
14+
compile_edition_defaults(
15+
name = "protobuf_defaults",
16+
testonly = True,
17+
srcs = [
18+
"//java/core:java_features_proto",
19+
"//src/google/protobuf:cpp_features_proto",
20+
],
21+
maximum_edition = "2023",
22+
minimum_edition = "2023",
23+
)
24+
1325
compile_edition_defaults(
1426
name = "test_defaults_2023",
1527
testonly = True,
@@ -75,18 +87,36 @@ cc_library(
7587
],
7688
)
7789

90+
cc_proto_library(
91+
name = "cpp_features_cc_proto",
92+
testonly = True,
93+
deps = ["//src/google/protobuf:cpp_features_proto"],
94+
)
95+
96+
cc_proto_library(
97+
name = "java_features_cc_proto",
98+
testonly = True,
99+
deps = ["//java/core:java_features_proto"],
100+
)
101+
78102
cc_test(
79103
name = "defaults_test",
80104
srcs = ["defaults_test.cc"],
81105
data = [
106+
":protobuf_defaults",
82107
":test_defaults_2023",
83108
":test_defaults_far_future",
84109
":test_defaults_future",
85110
],
86111
deps = [
112+
":cpp_features_cc_proto",
87113
":defaults_test_embedded",
114+
":java_features_cc_proto",
115+
"//:protobuf",
88116
"//src/google/protobuf",
89117
"//src/google/protobuf:port",
118+
"//src/google/protobuf:protobuf_lite",
119+
"//src/google/protobuf:test_textproto",
90120
"//src/google/protobuf:unittest_features_cc_proto",
91121
"//src/google/protobuf/stubs",
92122
"//src/google/protobuf/testing",

editions/defaults_test.cc

+63
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
#include "absl/strings/escaping.h"
1313
#include "absl/strings/str_cat.h"
1414
#include "absl/strings/string_view.h"
15+
#include "google/protobuf/java_features.pb.h"
16+
#include "google/protobuf/cpp_features.pb.h"
1517
#include "editions/defaults_test_embedded.h"
1618
#include "editions/defaults_test_embedded_base64.h"
19+
#include "google/protobuf/extension_set.h"
20+
#include "google/protobuf/message.h"
21+
#include "google/protobuf/test_textproto.h"
1722
#include "google/protobuf/unittest_features.pb.h"
1823
#include "google/protobuf/stubs/status_macros.h"
1924

25+
2026
// Must be included last.
2127
#include "google/protobuf/port_def.inc"
2228

@@ -182,6 +188,63 @@ TEST(DefaultsTest, EmbeddedBase64) {
182188
pb::VALUE3);
183189
}
184190

191+
// Lock down that overridable defaults never change in released editions. After
192+
// an edition has been released these tests should never need to be touched.
193+
class OverridableDefaultsTest : public ::testing::Test {
194+
public:
195+
OverridableDefaultsTest() = default;
196+
static void SetUpTestSuite() {
197+
google::protobuf::LinkExtensionReflection(pb::cpp);
198+
google::protobuf::LinkExtensionReflection(pb::java);
199+
DescriptorPool::generated_pool();
200+
}
201+
};
202+
203+
// TODO Enable these once they become fixed internally.
204+
TEST_F(OverridableDefaultsTest, Proto2) {
205+
auto feature_defaults = ReadDefaults("protobuf_defaults");
206+
ASSERT_OK(feature_defaults);
207+
ASSERT_GE(feature_defaults->defaults().size(), 1);
208+
const auto& defaults = feature_defaults->defaults(0);
209+
ASSERT_EQ(defaults.edition(), EDITION_PROTO2);
210+
211+
EXPECT_THAT(defaults.overridable_features(), EqualsProto(R"pb([pb.cpp] {}
212+
[pb.java] {}
213+
)pb"));
214+
}
215+
TEST_F(OverridableDefaultsTest, Proto3) {
216+
auto feature_defaults = ReadDefaults("protobuf_defaults");
217+
ASSERT_OK(feature_defaults);
218+
ASSERT_GE(feature_defaults->defaults().size(), 2);
219+
const auto& defaults = feature_defaults->defaults(1);
220+
ASSERT_EQ(defaults.edition(), EDITION_PROTO3);
221+
222+
EXPECT_THAT(defaults.overridable_features(), EqualsProto(R"pb([pb.cpp] {}
223+
[pb.java] {}
224+
)pb"));
225+
}
226+
227+
// Lock down that 2023 overridable defaults never change. Once Edition 2023 has
228+
// been released this test should never need to be touched.
229+
TEST_F(OverridableDefaultsTest, Edition2023) {
230+
auto feature_defaults = ReadDefaults("protobuf_defaults");
231+
ASSERT_OK(feature_defaults);
232+
ASSERT_GE(feature_defaults->defaults().size(), 3);
233+
const auto& defaults = feature_defaults->defaults(2);
234+
ASSERT_EQ(defaults.edition(), EDITION_2023);
235+
236+
EXPECT_THAT(defaults.overridable_features(), EqualsProto(R"pb(
237+
field_presence: EXPLICIT
238+
enum_type: OPEN
239+
repeated_field_encoding: PACKED
240+
utf8_validation: VERIFY
241+
message_encoding: LENGTH_PREFIXED
242+
json_format: ALLOW
243+
[pb.cpp] { legacy_closed_enum: false string_type: STRING }
244+
[pb.java] { legacy_closed_enum: false utf8_validation: DEFAULT }
245+
)pb"));
246+
}
247+
185248
} // namespace
186249
} // namespace protobuf
187250
} // namespace google

java/core/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ proto_library(
187187
strip_import_prefix = "/java/core/src/main/resources",
188188
visibility = [
189189
"//:__pkg__",
190+
"//editions:__pkg__",
190191
"//java/__subpackages__",
191192
"//pkg:__pkg__",
192193
],

0 commit comments

Comments
 (0)