From d0afdac3c5aac90e25cc700950832f43f26ee3cb Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Tue, 5 Nov 2024 20:53:58 +0000 Subject: [PATCH] more testing --- internal/gengapic/gengapic.go | 22 +++-- internal/gengapic/gengapic_test.go | 98 +++++++++++++++++-- .../shared_helpers_default_scope.want | 26 +++++ .../shared_helpers_multiple_scopes.want | 28 ++++++ .../testdata/shared_helpers_no_scopes.want | 20 ++++ 5 files changed, 178 insertions(+), 16 deletions(-) create mode 100644 internal/gengapic/testdata/shared_helpers_default_scope.want create mode 100644 internal/gengapic/testdata/shared_helpers_multiple_scopes.want create mode 100644 internal/gengapic/testdata/shared_helpers_no_scopes.want diff --git a/internal/gengapic/gengapic.go b/internal/gengapic/gengapic.go index 52b3afbe5..3c5110957 100644 --- a/internal/gengapic/gengapic.go +++ b/internal/gengapic/gengapic.go @@ -102,7 +102,7 @@ func gen(genReq *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorResponse g.snippetMetadata = g.newSnippetsMetadata(protoPkg) // generate shared code such as client hooks and scopes. - if err := g.genAndCommitSharedCode(scopes); err != nil { + if err := g.genAndCommitSharedHelpers(scopes); err != nil { return &g.resp, fmt.Errorf("error generating shared code file: %v", err) } @@ -205,9 +205,13 @@ func (g *generator) collectServicesAndScopes(genReq *pluginpb.CodeGeneratorReque servs := f.GetService() for _, s := range servs { eOauthScopes := proto.GetExtension(s.Options, annotations.E_OauthScopes) - scopes := strings.Split(eOauthScopes.(string), ",") - for _, sc := range scopes { - scopeSet[sc] = true + if scopeStr, ok := eOauthScopes.(string); ok { + if len(scopeStr) > 0 { + scopes := strings.Split(scopeStr, ",") + for _, sc := range scopes { + scopeSet[sc] = true + } + } } } } @@ -220,10 +224,10 @@ func (g *generator) collectServicesAndScopes(genReq *pluginpb.CodeGeneratorReque return } -// getAndCommitSharedCode commits shared generated code that should be defined only once. -// Currently, this includes functionality for reporting default scopes, and client constructor -// hooks. -func (g *generator) genAndCommitSharedCode(scopes []string) error { +// getAndCommitSharedHelpers commits shared generated code that should be defined only once. +// Currently, this includes functionality for reporting default scopes, version information, +// and client constructors hooks. +func (g *generator) genAndCommitSharedHelpers(scopes []string) error { p := g.printf g.reset() p("import (") @@ -260,7 +264,7 @@ func (g *generator) genAndCommitSharedCode(scopes []string) error { p("}") } - outFile := filepath.Join(g.opts.outDir, "shared_common_code.go") + outFile := filepath.Join(g.opts.outDir, "shared_helpers.go") g.commit(outFile, g.opts.pkgName) return nil } diff --git a/internal/gengapic/gengapic_test.go b/internal/gengapic/gengapic_test.go index 33a66897b..f45c1aaa9 100644 --- a/internal/gengapic/gengapic_test.go +++ b/internal/gengapic/gengapic_test.go @@ -26,6 +26,7 @@ import ( conf "github.com/googleapis/gapic-generator-go/internal/grpc_service_config" "github.com/googleapis/gapic-generator-go/internal/pbinfo" "github.com/googleapis/gapic-generator-go/internal/snippets" + "github.com/googleapis/gapic-generator-go/internal/testing/sample" "github.com/googleapis/gapic-generator-go/internal/txtdiff" "google.golang.org/genproto/googleapis/api/annotations" "google.golang.org/genproto/googleapis/api/serviceconfig" @@ -1256,6 +1257,7 @@ func TestCollectServicesAndScopes(t *testing.T) { libraryServ := &descriptorpb.ServiceDescriptorProto{ Name: proto.String("Library"), } + library := &descriptorpb.FileDescriptorProto{ Name: proto.String("google/cloud/library/v1/library.proto"), Options: &descriptorpb.FileOptions{ @@ -1269,13 +1271,15 @@ func TestCollectServicesAndScopes(t *testing.T) { toGen []string fileSet []*descriptorpb.FileDescriptorProto want []*descriptorpb.ServiceDescriptorProto + wantScopes []string }{ { - name: "simple", - goPkgPath: "cloud.google.com/go/library/apiv1", - toGen: []string{library.GetName()}, - fileSet: []*descriptorpb.FileDescriptorProto{library}, - want: []*descriptorpb.ServiceDescriptorProto{libraryServ}, + name: "simple", + goPkgPath: "cloud.google.com/go/library/apiv1", + toGen: []string{library.GetName()}, + fileSet: []*descriptorpb.FileDescriptorProto{library}, + want: []*descriptorpb.ServiceDescriptorProto{libraryServ}, + wantScopes: []string{"https://www.googleapis.com/auth/cloud-platform"}, }, { name: "ignore-mixins", @@ -1287,7 +1291,8 @@ func TestCollectServicesAndScopes(t *testing.T) { mixinFiles["google.iam.v1.IAMPolicy"][0], mixinFiles["google.cloud.location.Locations"][0], }, - want: []*descriptorpb.ServiceDescriptorProto{libraryServ}, + want: []*descriptorpb.ServiceDescriptorProto{libraryServ}, + wantScopes: []string{"scope-a", "scope-b"}, }, { name: "include-iam-mixin", @@ -1302,13 +1307,80 @@ func TestCollectServicesAndScopes(t *testing.T) { }, } { g := &generator{opts: &options{pkgPath: tst.goPkgPath}} - gotServices, _ := g.collectServicesAndScopes(&pluginpb.CodeGeneratorRequest{ + setOAuthScopes(libraryServ, tst.wantScopes) + gotServices, gotScopes := g.collectServicesAndScopes(&pluginpb.CodeGeneratorRequest{ FileToGenerate: tst.toGen, ProtoFile: tst.fileSet, }) if diff := cmp.Diff(gotServices, tst.want, cmp.Comparer(proto.Equal)); diff != "" { t.Errorf("%s: got(-),want(+):\n%s", tst.name, diff) } + if diff := cmp.Diff(gotScopes, tst.wantScopes); diff != "" { + t.Errorf("%s scopes got(-),want(+):\n%s", tst.name, diff) + } + } +} + +func TestGenAndCommentSharedHelpers(t *testing.T) { + g := generator{ + apiName: sample.ServiceTitle, + imports: map[pbinfo.ImportSpec]bool{}, + serviceConfig: sample.ServiceConfig(), + opts: &options{ + pkgPath: sample.GoPackagePath, + pkgName: sample.GoPackageName, + transports: []transport{grpc, rest}, + }, + } + inputType := sample.InputType(sample.CreateRequest) + outputType := sample.OutputType(sample.Resource) + file := sample.File() + + commonTypes(&g) + for _, typ := range []*descriptorpb.DescriptorProto{ + inputType, outputType, + } { + typName := sample.DescriptorInfoTypeName(typ.GetName()) + g.descInfo.Type[typName] = typ + g.descInfo.ParentFile[typ] = file + } + + serv := sample.Service() + serv.Method = nil + for _, tst := range []struct { + description string + scopes []string + want string + }{ + { + description: "nil", + scopes: nil, + want: filepath.Join("testdata", "shared_helpers_no_scopes.want"), + }, + { + description: "empty", + scopes: []string{}, + want: filepath.Join("testdata", "shared_helpers_no_scopes.want"), + }, + { + description: "default", + scopes: []string{"https://www.googleapis.com/auth/cloud-platform"}, + want: filepath.Join("testdata", "shared_helpers_default_scope.want"), + }, + { + description: "multiple", + scopes: []string{"scope-a", "scope-b", "scope-c"}, + want: filepath.Join("testdata", "shared_helpers_multiple_scopes.want"), + }, + } { + t.Run(tst.description, func(t *testing.T) { + if err := g.genAndCommitSharedHelpers(tst.scopes); err != nil { + t.Errorf("genAndCommitSharedHelpers: %v", err) + return + } + txtdiff.Diff(t, g.pt.String(), tst.want) + g.reset() + }) } } @@ -1319,3 +1391,15 @@ func setHTTPOption(o *descriptorpb.MethodOptions, pattern string) { }, }) } + +func setOAuthScopes(o *descriptorpb.ServiceDescriptorProto, scopes []string) { + sopts := o.Options + if sopts == nil { + sopts = &descriptorpb.ServiceOptions{} + } + proto.ClearExtension(sopts, annotations.E_OauthScopes) + if len(scopes) > 0 { + proto.SetExtension(sopts, annotations.E_OauthScopes, strings.Join(scopes, ",")) + } + o.Options = sopts +} diff --git a/internal/gengapic/testdata/shared_helpers_default_scope.want b/internal/gengapic/testdata/shared_helpers_default_scope.want new file mode 100644 index 000000000..475b61b51 --- /dev/null +++ b/internal/gengapic/testdata/shared_helpers_default_scope.want @@ -0,0 +1,26 @@ +import ( + "context" + + "google.golang.org/api/option" +) + +// For more information on implementing a client constructor hook, see +// https://github.com/googleapis/google-cloud-go/wiki/Customizing-constructors. +type clientHookParams struct{} +type clientHook func(context.Context, clientHookParams) ([]option.ClientOption, error) + +var versionClient string + +func getVersionClient() string { + if versionClient == "" { + return "UNKNOWN" + } + return versionClient +} + +// DefaultAuthScopes reports the default set of authentication scopes to use with this package. +func DefaultAuthScopes() []string { + return []string{ + "https://www.googleapis.com/auth/cloud-platform", + } +} diff --git a/internal/gengapic/testdata/shared_helpers_multiple_scopes.want b/internal/gengapic/testdata/shared_helpers_multiple_scopes.want new file mode 100644 index 000000000..4ee47353c --- /dev/null +++ b/internal/gengapic/testdata/shared_helpers_multiple_scopes.want @@ -0,0 +1,28 @@ +import ( + "context" + + "google.golang.org/api/option" +) + +// For more information on implementing a client constructor hook, see +// https://github.com/googleapis/google-cloud-go/wiki/Customizing-constructors. +type clientHookParams struct{} +type clientHook func(context.Context, clientHookParams) ([]option.ClientOption, error) + +var versionClient string + +func getVersionClient() string { + if versionClient == "" { + return "UNKNOWN" + } + return versionClient +} + +// DefaultAuthScopes reports the default set of authentication scopes to use with this package. +func DefaultAuthScopes() []string { + return []string{ + "scope-a", + "scope-b", + "scope-c", + } +} diff --git a/internal/gengapic/testdata/shared_helpers_no_scopes.want b/internal/gengapic/testdata/shared_helpers_no_scopes.want new file mode 100644 index 000000000..d6745936d --- /dev/null +++ b/internal/gengapic/testdata/shared_helpers_no_scopes.want @@ -0,0 +1,20 @@ +import ( + "context" + + "google.golang.org/api/option" +) + +// For more information on implementing a client constructor hook, see +// https://github.com/googleapis/google-cloud-go/wiki/Customizing-constructors. +type clientHookParams struct{} +type clientHook func(context.Context, clientHookParams) ([]option.ClientOption, error) + +var versionClient string + +func getVersionClient() string { + if versionClient == "" { + return "UNKNOWN" + } + return versionClient +} +