Skip to content

Commit

Permalink
more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
shollyman committed Nov 5, 2024
1 parent f9cfe91 commit d0afdac
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 16 deletions.
22 changes: 13 additions & 9 deletions internal/gengapic/gengapic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
}
}
}
}
Expand All @@ -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 (")
Expand Down Expand Up @@ -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
}
Expand Down
98 changes: 91 additions & 7 deletions internal/gengapic/gengapic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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()
})
}
}

Expand All @@ -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
}
26 changes: 26 additions & 0 deletions internal/gengapic/testdata/shared_helpers_default_scope.want
Original file line number Diff line number Diff line change
@@ -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",
}
}
28 changes: 28 additions & 0 deletions internal/gengapic/testdata/shared_helpers_multiple_scopes.want
Original file line number Diff line number Diff line change
@@ -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",
}
}
20 changes: 20 additions & 0 deletions internal/gengapic/testdata/shared_helpers_no_scopes.want
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit d0afdac

Please sign in to comment.