Skip to content

Commit

Permalink
Merge pull request #288 from marwan-at-work/optfix
Browse files Browse the repository at this point in the history
Fix go_option that contains both path and pkg
  • Loading branch information
amcohen-twitch authored Jan 6, 2021
2 parents 12cd54f + 9065ae9 commit 0913c81
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions protoc-gen-twirp/go_naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func goPackageOption(f *descriptor.FileDescriptorProto) (impPath, pkg string, ok
return
}
ok = true
if bits := strings.Split(pkg, ";"); len(bits) == 2 {
return bits[0], bits[1], ok
}
// The presence of a slash implies there's an import path.
slash := strings.LastIndex(pkg, "/")
if slash < 0 {
Expand Down
37 changes: 34 additions & 3 deletions protoc-gen-twirp/go_naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

package main

import "testing"
import (
"testing"

"github.com/golang/protobuf/protoc-gen-go/descriptor"
)

func TestParseGoPackageOption(t *testing.T) {
testcase := func(in, wantImport, wantPkg string) func(*testing.T) {
return func(t *testing.T) {
in := ""
wantImport, wantPkg := "", ""
haveImport, havePkg := parseGoPackageOption(in)
if haveImport != wantImport {
t.Errorf("wrong importPath, have=%q want=%q", haveImport, wantImport)
Expand All @@ -35,4 +37,33 @@ func TestParseGoPackageOption(t *testing.T) {
t.Run("full import", testcase("github.com/example/foo", "github.com/example/foo", "foo"))
t.Run("full import with override",
testcase("github.com/example/foo;bar", "github.com/example/foo", "bar"))
t.Run("non dotted import with package", testcase("foo;bar", "foo", "bar"))
}

func TestGoPackageOption(t *testing.T) {
testcase := func(in, wantImport, wantPkg string, wantOK bool) func(*testing.T) {
return func(t *testing.T) {
haveImport, havePkg, haveOK := goPackageOption(&descriptor.FileDescriptorProto{
Options: &descriptor.FileOptions{
GoPackage: &in,
},
})
if wantOK != haveOK {
t.Errorf("wrong ok, have=%t want=%t", haveOK, wantOK)
}
if haveImport != wantImport {
t.Errorf("wrong importPath, have=%q want=%q", haveImport, wantImport)
}
if havePkg != wantPkg {
t.Errorf("wrong packageName, have=%q want=%q", havePkg, wantPkg)
}
}
}

t.Run("empty string", testcase("", "", "", false))
t.Run("bare package", testcase("foo", "", "foo", true))
t.Run("full import", testcase("github.com/example/foo", "github.com/example/foo", "foo", true))
t.Run("full import with override",
testcase("github.com/example/foo;bar", "github.com/example/foo", "bar", true))
t.Run("non dotted import with package", testcase("foo;bar", "foo", "bar", true))
}

0 comments on commit 0913c81

Please sign in to comment.