diff --git a/internal/gengapic/example_test.go b/internal/gengapic/example_test.go index 2be61d906..d24f3cf04 100644 --- a/internal/gengapic/example_test.go +++ b/internal/gengapic/example_test.go @@ -283,29 +283,19 @@ func TestExample(t *testing.T) { func TestGenSnippetFile(t *testing.T) { var g generator g.imports = map[pbinfo.ImportSpec]bool{} - g.serviceConfig = &serviceconfig.Service{ - Apis: []*apipb.Api{ - {Name: sample.ProtoServiceName}, - }, - } - g.snippetMetadata = snippets.NewMetadata(sample.ProtoPackagePath, sample.GoPackagePath, sample.GoPackageName) + g.serviceConfig = sample.ServiceConfig() - inputType := &descriptorpb.DescriptorProto{ - Name: proto.String(sample.CreateRequest), - } - outputType := &descriptorpb.DescriptorProto{ - Name: proto.String(sample.Resource), - } + serv := sample.Service() + g.snippetMetadata = snippets.NewMetadata(sample.ProtoPackagePath, sample.GoPackagePath, sample.GoPackageName) + g.snippetMetadata.AddService(serv.GetName(), sample.ServiceURL) - file := &descriptorpb.FileDescriptorProto{ - Options: &descriptorpb.FileOptions{ - GoPackage: proto.String(sample.GoProtoPackagePath), - }, - Package: proto.String(sample.ProtoPackagePath), - } + inputType := sample.InputType(sample.CreateRequest) + outputType := sample.OutputType(sample.Resource) files := []*descriptorpb.FileDescriptorProto{} g.descInfo = pbinfo.Of(files) + + file := sample.File() for _, typ := range []*descriptorpb.DescriptorProto{ inputType, outputType, } { @@ -313,24 +303,13 @@ func TestGenSnippetFile(t *testing.T) { g.descInfo.ParentFile[typ] = file } - serv := &descriptorpb.ServiceDescriptorProto{ - Name: proto.String(sample.ServiceName), - Method: []*descriptorpb.MethodDescriptorProto{ - { - Name: proto.String(sample.CreateMethod), - InputType: proto.String(sample.DescriptorInfoTypeName(sample.CreateRequest)), - OutputType: proto.String(sample.DescriptorInfoTypeName(sample.Resource)), - }, - }, - } - - for _, tst := range []struct { - tstName string + for _, test := range []struct { + name string options options imports map[pbinfo.ImportSpec]bool }{ { - tstName: "snippet", + name: "snippet", options: options{ pkgName: "migration", transports: []transport{grpc, rest}, @@ -341,20 +320,19 @@ func TestGenSnippetFile(t *testing.T) { }, }, } { - t.Run(tst.tstName, func(t *testing.T) { + t.Run(test.name, func(t *testing.T) { g.reset() - g.opts = &tst.options - g.snippetMetadata.AddService(serv.GetName(), sample.ServiceURL) + g.opts = &test.options err := g.genSnippetFile(serv, serv.Method[0]) if err != nil { t.Fatal(err) } g.commit(filepath.Join("cloud.google.com/go", "internal", "generated", "snippets", "bigquery", "main.go"), "main") - if diff := cmp.Diff(g.imports, tst.imports); diff != "" { - t.Errorf("TestExample(%s): imports got(-),want(+):\n%s", tst.tstName, diff) + if diff := cmp.Diff(test.imports, g.imports); diff != "" { + t.Errorf("TestExample(%s) imports mismatch: (-want +got):\n%s", test.name, diff) } got := *g.resp.File[0].Content + *g.resp.File[1].Content - txtdiff.Diff(t, got, filepath.Join("testdata", tst.tstName+".want")) + txtdiff.Diff(t, got, filepath.Join("testdata", test.name+".want")) }) } } diff --git a/internal/testing/sample/BUILD.bazel b/internal/testing/sample/BUILD.bazel index 527ac977d..fa196d9d9 100644 --- a/internal/testing/sample/BUILD.bazel +++ b/internal/testing/sample/BUILD.bazel @@ -5,4 +5,10 @@ go_library( srcs = ["sample.go"], importpath = "github.com/googleapis/gapic-generator-go/internal/testing/sample", visibility = ["//:__subpackages__"], + deps = [ + "@org_golang_google_genproto_googleapis_api//serviceconfig", + "@org_golang_google_protobuf//proto", + "@org_golang_google_protobuf//types/descriptorpb", + "@org_golang_google_protobuf//types/known/apipb", + ], ) diff --git a/internal/testing/sample/sample.go b/internal/testing/sample/sample.go index f14514361..5078c82c0 100644 --- a/internal/testing/sample/sample.go +++ b/internal/testing/sample/sample.go @@ -18,6 +18,11 @@ package sample import ( "fmt" + + "google.golang.org/genproto/googleapis/api/serviceconfig" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/descriptorpb" + "google.golang.org/protobuf/types/known/apipb" ) const ( @@ -139,3 +144,55 @@ const ( func DescriptorInfoTypeName(typ string) string { return fmt.Sprintf(".%s.%s", ProtoPackagePath, typ) } + +// ServiceConfig returns service config information. +func ServiceConfig() *serviceconfig.Service { + return &serviceconfig.Service{ + Apis: []*apipb.Api{ + {Name: ProtoServiceName}, + }, + } +} + +// Service returns a service descriptor using the sample values. +func Service() *descriptorpb.ServiceDescriptorProto { + return &descriptorpb.ServiceDescriptorProto{ + Name: proto.String(ServiceName), + Method: []*descriptorpb.MethodDescriptorProto{ + { + Name: proto.String(CreateMethod), + InputType: proto.String(DescriptorInfoTypeName(CreateRequest)), + OutputType: proto.String(DescriptorInfoTypeName(Resource)), + }, + { + Name: proto.String(GetMethod), + InputType: proto.String(DescriptorInfoTypeName(GetRequest)), + OutputType: proto.String(DescriptorInfoTypeName(Resource)), + }, + }, + } +} + +// InputType returns an input type for a method. +func InputType(input string) *descriptorpb.DescriptorProto { + return &descriptorpb.DescriptorProto{ + Name: proto.String(input), + } +} + +// OutputType returns an output type for a method. +func OutputType(output string) *descriptorpb.DescriptorProto { + return &descriptorpb.DescriptorProto{ + Name: proto.String(output), + } +} + +// File returns a proto file. +func File() *descriptorpb.FileDescriptorProto { + return &descriptorpb.FileDescriptorProto{ + Options: &descriptorpb.FileOptions{ + GoPackage: proto.String(GoProtoPackagePath), + }, + Package: proto.String(ProtoPackagePath), + } +}