Skip to content

Commit

Permalink
[ObjC] Enable editions conformance tests.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 590328722
  • Loading branch information
thomasvl authored and copybara-github committed Dec 12, 2023
1 parent ddc5406 commit 6c7c5a5
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 17 deletions.
2 changes: 2 additions & 0 deletions conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ objc_library(
":conformance_objc_proto",
"//:test_messages_proto2_objc_proto",
"//:test_messages_proto3_objc_proto",
"//src/google/protobuf/editions:test_messages_proto2_editions_objc_proto",
"//src/google/protobuf/editions:test_messages_proto3_editions_objc_proto",
],
)

Expand Down
29 changes: 17 additions & 12 deletions conformance/conformance_objc.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#import "Conformance.pbobjc.h"
#import "google/protobuf/TestMessagesProto2.pbobjc.h"
#import "google/protobuf/TestMessagesProto3.pbobjc.h"
#import "google/protobuf/editions/golden/TestMessagesProto2Editions.pbobjc.h"
#import "google/protobuf/editions/golden/TestMessagesProto3Editions.pbobjc.h"

static void Die(NSString *format, ...) __dead2;

Expand Down Expand Up @@ -49,22 +51,25 @@ static void Die(NSString *format, ...) {
break;

case ConformanceRequest_Payload_OneOfCase_ProtobufPayload: {
Class msgClass = nil;
if ([request.messageType isEqual:@"protobuf_test_messages.proto3.TestAllTypesProto3"]) {
msgClass = [Proto3TestAllTypesProto3 class];
} else if ([request.messageType
isEqual:@"protobuf_test_messages.proto2.TestAllTypesProto2"]) {
msgClass = [Proto2TestAllTypesProto2 class];
NSDictionary *mappings = @{
@"protobuf_test_messages.proto2.TestAllTypesProto2" : [Proto2TestAllTypesProto2 class],
@"protobuf_test_messages.proto3.TestAllTypesProto3" : [Proto3TestAllTypesProto3 class],
@"protobuf_test_messages.editions.proto2.TestAllTypesProto2" :
[EditionsProto2TestAllTypesProto2 class],
@"protobuf_test_messages.editions.proto3.TestAllTypesProto3" :
[EditionsProto3TestAllTypesProto3 class],
};
Class msgClass = mappings[request.messageType];
if (msgClass) {
NSError *error = nil;
testMessage = [msgClass parseFromData:request.protobufPayload error:&error];
if (!testMessage) {
response.parseError = [NSString stringWithFormat:@"Parse error: %@", error];
}
} else {
response.runtimeError =
[NSString stringWithFormat:@"Protobuf request had an unknown message_type: %@",
request.messageType];
break;
}
NSError *error = nil;
testMessage = [msgClass parseFromData:request.protobufPayload error:&error];
if (!testMessage) {
response.parseError = [NSString stringWithFormat:@"Parse error: %@", error];
}
break;
}
Expand Down
1 change: 1 addition & 0 deletions objectivec/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ objc_library(
conformance_test(
name = "conformance_test",
failure_list = "//conformance:failure_list_objc.txt",
maximum_edition = "2023",
target_compatible_with = ["@platforms//os:macos"],
testee = "//conformance:conformance_objc",
)
Expand Down
14 changes: 11 additions & 3 deletions protobuf.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def _proto_gen_impl(ctx):
srcs = ctx.files.srcs
langs = ctx.attr.langs or []
out_type = ctx.attr.out_type
enable_editions = ctx.attr.enable_editions
deps = depset(direct = ctx.files.srcs)
source_dir = _SourceDir(ctx)
gen_dir = _GenDir(ctx).rstrip("/")
Expand Down Expand Up @@ -130,6 +131,8 @@ def _proto_gen_impl(ctx):
generated_files = []
for src in srcs:
args = []
if enable_editions:
args.append("--experimental_editions")

in_gen_dir = src.root.path == gen_dir
if in_gen_dir:
Expand Down Expand Up @@ -247,6 +250,7 @@ _proto_gen = rule(
attrs = {
"srcs": attr.label_list(allow_files = True),
"deps": attr.label_list(providers = [ProtoGenInfo]),
"enable_editions": attr.bool(),
"includes": attr.string_list(),
"protoc": attr.label(
cfg = "exec",
Expand Down Expand Up @@ -406,11 +410,11 @@ def internal_objc_proto_library(
includes = ["."],
default_runtime = Label("//:protobuf_objc"),
protoc = Label("//:protoc"),
enable_editions = False,
testonly = None,
visibility = ["//visibility:public"],
**kwargs):
"""Bazel rule to create a Objective-C protobuf library from proto source
files
"""Bazel rule to create a Objective-C protobuf library from proto sources
NOTE: the rule is only an internal workaround to generate protos. The
interface may change and the rule may be removed when bazel has introduced
Expand All @@ -423,9 +427,10 @@ def internal_objc_proto_library(
outs: a list of expected output files.
proto_deps: a list of proto file dependencies that don't have a
objc_proto_library rule.
include: a string indicating the include path of the .proto files.
includes: a string indicating the include path of the .proto files.
default_runtime: the Objective-C Protobuf runtime
protoc: the label of the protocol compiler to generate the sources.
enable_editions: if editions should be enabled while invoking the compiler.
testonly: common rule attribute (see:
https://bazel.build/reference/be/common-definitions#common-attributes)
visibility: the visibility of the generated files.
Expand All @@ -440,6 +445,7 @@ def internal_objc_proto_library(
testonly = testonly,
srcs = proto_deps,
protoc = protoc,
enable_editions = enable_editions,
includes = includes,
)
full_deps.append(":%s_deps_genproto" % name)
Expand All @@ -454,6 +460,7 @@ def internal_objc_proto_library(
out_type = "hdrs",
includes = includes,
protoc = protoc,
enable_editions = enable_editions,
testonly = testonly,
visibility = visibility,
tags = ["manual"],
Expand All @@ -467,6 +474,7 @@ def internal_objc_proto_library(
out_type = "srcs",
includes = includes,
protoc = protoc,
enable_editions = enable_editions,
testonly = testonly,
visibility = visibility,
tags = ["manual"],
Expand Down
22 changes: 22 additions & 0 deletions src/google/protobuf/editions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("//bazel:upb_proto_library.bzl", "upb_c_proto_library", "upb_proto_reflecti
load("@rules_cc//cc:defs.bzl", "cc_proto_library")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(":defaults.bzl", "compile_edition_defaults", "embed_edition_defaults")
load("//:protobuf.bzl", "internal_objc_proto_library")

bzl_library(
name = "defaults",
Expand Down Expand Up @@ -100,6 +101,14 @@ cc_proto_library(
deps = [":test_messages_proto2_editions_proto"],
)

internal_objc_proto_library(
name = "test_messages_proto2_editions_objc_proto",
testonly = True,
srcs = ["golden/test_messages_proto2_editions.proto"],
enable_editions = True,
visibility = ["//conformance:__pkg__"],
)

py_proto_library(
name = "test_messages_proto2_editions_py_pb2",
testonly = True,
Expand Down Expand Up @@ -142,6 +151,19 @@ cc_proto_library(
deps = [":test_messages_proto3_editions_proto"],
)

internal_objc_proto_library(
name = "test_messages_proto3_editions_objc_proto",
testonly = True,
srcs = ["golden/test_messages_proto3_editions.proto"],
enable_editions = True,
includes = [
".",
"src",
],
proto_deps = ["//:well_known_type_protos"],
visibility = ["//conformance:__pkg__"],
)

py_proto_library(
name = "test_messages_proto3_editions_py_pb2",
testonly = True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ option features.enum_type = CLOSED;
option features.repeated_field_encoding = EXPANDED;
option features.utf8_validation = NONE;
option java_package = "com.google.protobuf_test_messages.editions.proto2";
option objc_class_prefix = "Proto2";
option objc_class_prefix = "EditionsProto2";

// This is the default, but we specify it here explicitly.
option optimize_for = SPEED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import "google/protobuf/wrappers.proto";

option features.field_presence = IMPLICIT;
option java_package = "com.google.protobuf_test_messages.editions.proto3";
option objc_class_prefix = "Proto3";
option objc_class_prefix = "EditionsProto3";

// This is the default, but we specify it here explicitly.
option optimize_for = SPEED;
Expand Down

0 comments on commit 6c7c5a5

Please sign in to comment.