Skip to content

Commit

Permalink
Fix generated file name error in some cases (#351)
Browse files Browse the repository at this point in the history
Closes #350

Co-authored-by: Veniamin Albaev <v.albaev@city-mobil.ru>
  • Loading branch information
albenik and Veniamin Albaev authored Mar 8, 2022
1 parent 2e6083c commit 68a69dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion protoc-gen-twirp/go_naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (t *twirp) goFileName(f *descriptor.FileDescriptorProto) string {
// otherwise, the directory is taken from the option go_package
if impPath, _, ok := goPackageOption(f); ok && impPath != "" {
if t.modulePrefix != "" {
impPath = strings.TrimPrefix(impPath, t.modulePrefix)
impPath = strings.TrimPrefix(strings.TrimPrefix(impPath, t.modulePrefix), "/")
}

// Replace the existing dirname with the import path from go_package
Expand Down
37 changes: 37 additions & 0 deletions protoc-gen-twirp/go_naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,40 @@ func TestGoPackageOption(t *testing.T) {
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))
}

func TestGoFileName(t *testing.T) {
testcase := func(srcrelpaths bool, modprefix, fname, gopkg, wantName string) func(t2 *testing.T) {
return func(t *testing.T) {
f := &descriptor.FileDescriptorProto{
Name: &fname,
Options: &descriptor.FileOptions{
GoPackage: &gopkg,
},
}

tw := &twirp{
sourceRelativePaths: srcrelpaths,
modulePrefix: modprefix,
}

if name := tw.goFileName(f); name != wantName {
t.Errorf("wrong goFileName, have=%q want=%q", name, wantName)
}
}
}

t.Run("paths=source_relative",
testcase(true, "",
"rpc/v1/service.proto", "example.com/module/package/rpc/v1",
"rpc/v1/service.twirp.go"))

t.Run("paths=import,module=example.com/module/package",
testcase(false, "example.com/module/package",
"rpc/v1/service.proto", "example.com/module/package/rpc/v1",
"rpc/v1/service.twirp.go"))

t.Run("paths=import,module=example.com/module/package/",
testcase(false, "example.com/module/package/",
"rpc/v1/service.proto", "example.com/module/package/rpc/v1",
"rpc/v1/service.twirp.go"))
}

0 comments on commit 68a69dc

Please sign in to comment.