Skip to content

Commit 105a57b

Browse files
committed
cmd/protoc-gen-go: add test for "import option" directive
See https://protobuf.dev/editions/overview/#import-option for context. Change-Id: Iea73299fdd828301fe451ca3c6b9234e8dd9acf3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/699715 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Mike Kruskal <mkruskal@google.com> Reviewed-by: Lasse Folger <lassefolger@google.com>
1 parent d088586 commit 105a57b

File tree

8 files changed

+605
-0
lines changed

8 files changed

+605
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
importoptionpb "google.golang.org/protobuf/cmd/protoc-gen-go/testdata/import_option"
12+
"google.golang.org/protobuf/encoding/protowire"
13+
"google.golang.org/protobuf/proto"
14+
"google.golang.org/protobuf/types/descriptorpb"
15+
16+
// Ensure the custom option is linked into this test binary.
17+
// NB: import_option_unlinked is not linked into this test binary.
18+
importoptioncustompb "google.golang.org/protobuf/cmd/protoc-gen-go/testdata/import_option_custom"
19+
)
20+
21+
func TestImportOption(t *testing.T) {
22+
var nilMessage *importoptionpb.TestMessage
23+
md := nilMessage.ProtoReflect().Descriptor()
24+
25+
// Options from import option that are linked in should be available through
26+
// the extension API as usual.
27+
{
28+
fd := md.Fields().ByName("hello")
29+
fopts := fd.Options().(*descriptorpb.FieldOptions)
30+
if !proto.HasExtension(fopts, importoptioncustompb.E_FieldOption) {
31+
t.Errorf("FieldDescriptor(hello) does not have FieldOption extension set")
32+
}
33+
}
34+
35+
// Options from import option that are not linked in should be in unknown bytes.
36+
{
37+
fd := md.Fields().ByName("world")
38+
fopts := fd.Options().(*descriptorpb.FieldOptions)
39+
unknown := fopts.ProtoReflect().GetUnknown()
40+
var fields []protowire.Number
41+
b := unknown
42+
for len(b) > 0 {
43+
num, _, n := protowire.ConsumeField(b)
44+
if n < 0 {
45+
t.Errorf("FieldDescriptor(world) contains invalid wire format: ConsumeField = %d", n)
46+
}
47+
fields = append(fields, num)
48+
b = b[n:]
49+
}
50+
want := []protowire.Number{504589222}
51+
if diff := cmp.Diff(want, fields); diff != "" {
52+
t.Errorf("FieldDescriptor(world) unknown bytes contain unexpected fields: diff (-want +got):\n%s", diff)
53+
}
54+
}
55+
56+
}

cmd/protoc-gen-go/testdata/gen_test.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/protoc-gen-go/testdata/import_option/import_option.pb.go

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
edition = "2024";
6+
7+
package testimportoption;
8+
9+
import "google/protobuf/descriptor.proto";
10+
import option "cmd/protoc-gen-go/testdata/import_option_custom/import_option_custom.proto";
11+
import option "cmd/protoc-gen-go/testdata/import_option_unlinked/import_option_unlinked.proto";
12+
13+
option go_package = "google.golang.org/protobuf/cmd/protoc-gen-go/testdata/import_option";
14+
15+
message TestMessage {
16+
string hello = 1 [(testimportoption_custom.field_option) = { plain_field: 23 }];
17+
string world = 2 [(testimportoption_unlinked.field_option) = { plain_field: 23 }];
18+
}

0 commit comments

Comments
 (0)