Skip to content

Commit c51df73

Browse files
committed
Add generated message reflection unittest for import option and option_deps test proto in edition 2024.
PiperOrigin-RevId: 788038187
1 parent 1a7e012 commit c51df73

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""This module defines a wrapper around proto_library for Bazel versions that do not support certain attributes yet."""
2+
3+
load("//bazel:proto_library.bzl", "proto_library")
4+
5+
def protobuf_test_proto_library(**kwattrs):
6+
"""
7+
Creates a proto library, handling any attributes that are not supported by the proto_library rule.
8+
9+
Args:
10+
**kwattrs: Additional arguments to pass to the proto_library rule.
11+
"""
12+
kwargs = dict(kwattrs)
13+
14+
# TODO: Bazel 7's proto_library rule does not support option_deps, so we handle it by putting it in deps instead.
15+
if "option_deps" in kwargs and hasattr(native, "proto_library"):
16+
deps = kwargs.pop("deps", [])
17+
option_deps = kwargs.pop("option_deps")
18+
kwargs["deps"] = deps + option_deps
19+
20+
proto_library(**kwargs)

src/google/protobuf/BUILD.bazel

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ load("//bazel:proto_library.bzl", "proto_library")
99
load("//bazel:upb_c_proto_library.bzl", "upb_c_proto_library")
1010
load("//bazel:upb_minitable_proto_library.bzl", "upb_minitable_proto_library")
1111
load("//bazel:upb_proto_reflection_library.bzl", "upb_proto_reflection_library")
12+
load(
13+
"//bazel/tests:protobuf_test_proto_library.bzl",
14+
"protobuf_test_proto_library",
15+
)
1216
load("//build_defs:cpp_opts.bzl", "COPTS", "LINK_OPTS")
1317
load("//upb/cmake:build_defs.bzl", "staleness_test")
1418

@@ -980,11 +984,13 @@ filegroup(
980984
"map_unittest.proto",
981985
"unittest.proto",
982986
"unittest_custom_options.proto",
987+
"unittest_custom_options_unlinked.proto",
983988
"unittest_embed_optimize_for.proto",
984989
"unittest_empty.proto",
985990
"unittest_enormous_descriptor.proto",
986991
"unittest_features.proto",
987992
"unittest_import.proto",
993+
"unittest_import_option.proto",
988994
"unittest_import_public.proto",
989995
"unittest_invalid_features.proto",
990996
"unittest_lite_imports_nonlite.proto",
@@ -1300,6 +1306,31 @@ cc_proto_library(
13001306
deps = [":unittest_string_view_proto"],
13011307
)
13021308

1309+
proto_library(
1310+
name = "unittest_custom_options_unlinked_proto",
1311+
srcs = ["unittest_custom_options_unlinked.proto"],
1312+
strip_import_prefix = "/src",
1313+
deps = [
1314+
"//:any_proto",
1315+
"//:descriptor_proto",
1316+
],
1317+
)
1318+
1319+
protobuf_test_proto_library(
1320+
name = "unittest_import_option_proto",
1321+
srcs = ["unittest_import_option.proto"],
1322+
option_deps = [
1323+
":test_protos",
1324+
":unittest_custom_options_unlinked_proto",
1325+
],
1326+
strip_import_prefix = "/src",
1327+
)
1328+
1329+
cc_proto_library(
1330+
name = "unittest_import_option_cc_proto",
1331+
deps = [":unittest_import_option_proto"],
1332+
)
1333+
13031334
cc_test(
13041335
name = "string_view_test",
13051336
srcs = ["string_view_test.cc"],
@@ -1755,6 +1786,7 @@ cc_test(
17551786
":port",
17561787
":protobuf",
17571788
":test_util",
1789+
":unittest_import_option_cc_proto",
17581790
":unittest_proto3_cc_proto",
17591791
"//src/google/protobuf/stubs",
17601792
"//src/google/protobuf/testing",

src/google/protobuf/generated_message_reflection_unittest.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
#include "google/protobuf/test_util.h"
4141
#include "google/protobuf/text_format.h"
4242
#include "google/protobuf/unittest.pb.h"
43+
#include "google/protobuf/unittest_custom_options.pb.h"
4344
#include "google/protobuf/unittest_import.pb.h"
45+
#include "google/protobuf/unittest_import_option.pb.h"
4446
#include "google/protobuf/unittest_mset.pb.h"
4547
#include "google/protobuf/unittest_mset_wire_format.pb.h"
4648
#include "google/protobuf/unittest_proto3.pb.h"
@@ -1825,6 +1827,23 @@ TEST(GeneratedMessageReflection, UnvalidatedStringsAreDowngradedToBytes) {
18251827
msg.mutable_optional_extension());
18261828
}
18271829

1830+
TEST(GeneratedMessageReflection, ImportOption) {
1831+
proto2_unittest_import_option::TestMessage message;
1832+
google::protobuf::FileDescriptor const* file_descriptor =
1833+
message.GetDescriptor()->file();
1834+
google::protobuf::Descriptor const* message_descriptor = message.GetDescriptor();
1835+
google::protobuf::FieldDescriptor const* field_descriptor =
1836+
message_descriptor->FindFieldByName("field1");
1837+
1838+
EXPECT_EQ(
1839+
1, file_descriptor->options().GetExtension(proto2_unittest::file_opt1));
1840+
EXPECT_EQ(2, message_descriptor->options().GetExtension(
1841+
proto2_unittest::message_opt1));
1842+
EXPECT_EQ(
1843+
3, field_descriptor->options().GetExtension(proto2_unittest::field_opt1));
1844+
1845+
}
1846+
18281847
} // namespace
18291848
} // namespace protobuf
18301849
} // namespace google
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2008 Google Inc. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
// Author: [email protected] (Benjy Weinberger)
9+
// Based on original Protocol Buffers design by
10+
// Sanjay Ghemawat, Jeff Dean, and others.
11+
//
12+
// A proto file used to test the "custom options" feature when not linked in.
13+
14+
edition = "2024";
15+
16+
// A custom file option (defined below).
17+
option (file_opt1) = 9876543210;
18+
19+
import "google/protobuf/descriptor.proto";
20+
21+
// We don't put this in a package within proto2 because we need to make sure
22+
// that the generated code doesn't depend on being in the proto2 namespace.
23+
package proto2_unittest_unlinked;
24+
25+
// Some simple test custom options of various types.
26+
27+
extend google.protobuf.FileOptions {
28+
uint64 file_opt1 = 7736975;
29+
}
30+
31+
extend google.protobuf.MessageOptions {
32+
int32 message_opt1 = 7739037;
33+
}
34+
35+
extend google.protobuf.FieldOptions {
36+
fixed64 field_opt1 = 7740937;
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2008 Google Inc. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
// Author: [email protected] (Kenton Varda)
8+
// Based on original Protocol Buffers design by
9+
// Sanjay Ghemawat, Jeff Dean, and others.
10+
//
11+
// A proto file to test options importing.
12+
edition = "2024";
13+
14+
package proto2_unittest_import_option;
15+
16+
// Test option import
17+
import option "google/protobuf/unittest_custom_options.proto";
18+
import option "google/protobuf/unittest_custom_options_unlinked.proto";
19+
20+
option (proto2_unittest.file_opt1) = 1;
21+
option (proto2_unittest_unlinked.file_opt1) = 1;
22+
23+
message TestMessage {
24+
option (proto2_unittest.message_opt1) = 2;
25+
option (proto2_unittest_unlinked.message_opt1) = 2;
26+
27+
int32 field1 = 1 [
28+
(proto2_unittest.field_opt1) = 3,
29+
(proto2_unittest_unlinked.field_opt1) = 3
30+
];
31+
}

0 commit comments

Comments
 (0)