From 0049f93d3efd0bf92d93693b35706fe256bbe3f1 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Mon, 3 Jun 2019 10:58:45 +0200 Subject: [PATCH] protoc-gen-ruby: add test data --- protoc-gen-twirp_ruby/main.go | 48 +++++++++--------- protoc-gen-twirp_ruby/main_test.go | 45 ++++++++++++---- protoc-gen-twirp_ruby/testdata/fileset.pb | Bin 0 -> 2818 bytes protoc-gen-twirp_ruby/testdata/gen.go | 16 ++++++ .../testdata/rubytypes.proto | 18 +++++++ 5 files changed, 93 insertions(+), 34 deletions(-) create mode 100644 protoc-gen-twirp_ruby/testdata/fileset.pb create mode 100644 protoc-gen-twirp_ruby/testdata/gen.go create mode 100644 protoc-gen-twirp_ruby/testdata/rubytypes.proto diff --git a/protoc-gen-twirp_ruby/main.go b/protoc-gen-twirp_ruby/main.go index b9c7f70..ae3e39d 100644 --- a/protoc-gen-twirp_ruby/main.go +++ b/protoc-gen-twirp_ruby/main.go @@ -33,21 +33,25 @@ import ( func main() { genReq := readGenRequest(os.Stdin) - g := &generator{ - version: Version, - genReq: genReq, - fileToGoPackageName: make(map[*descriptor.FileDescriptorProto]string), - reg: typemap.New(genReq.ProtoFile), - } - genResp := g.Generate() + genResp := newGenerator(genReq).Generate() writeGenResponse(os.Stdout, genResp) } +func newGenerator(genReq *plugin.CodeGeneratorRequest) *generator { + return &generator{ + version: Version, + genReq: genReq, + fileToGoPackageName: make(map[*descriptor.FileDescriptorProto]string), + reg: typemap.New(genReq.ProtoFile), + } +} + type generator struct { version string genReq *plugin.CodeGeneratorRequest reg *typemap.Registry + genFiles []*descriptor.FileDescriptorProto fileToGoPackageName map[*descriptor.FileDescriptorProto]string } @@ -62,17 +66,9 @@ func fileDescSliceContains(slice []*descriptor.FileDescriptorProto, f *descripto func (g *generator) Generate() *plugin.CodeGeneratorResponse { resp := new(plugin.CodeGeneratorResponse) - genFiles := g.protoFilesToGenerate() + g.findProtoFilesToGenerate() - for _, f := range g.genReq.ProtoFile { - if fileDescSliceContains(genFiles, f) { - g.fileToGoPackageName[f] = "" - } else { - g.fileToGoPackageName[f] = f.GetPackage() - } - } - - for _, f := range genFiles { + for _, f := range g.genFiles { twirpFileName := noExtension(filePath(f)) + "_twirp.rb" // e.g. "hello_world/service_twirp.rb" pbFileRelativePath := noExtension(onlyBase(filePath(f))) + "_pb.rb" // e.g. "service_pb.rb" @@ -98,7 +94,7 @@ func (g *generator) generateRubyCode(file *descriptor.FileDescriptorProto, pbFil pkgName := file.GetPackage() var modules []string - if file.Options.RubyPackage != nil { + if file.Options != nil && file.Options.RubyPackage != nil { modules = strings.Split(*file.Options.RubyPackage, "::") } else { modules = splitRubyConstants(pkgName) @@ -144,17 +140,23 @@ func (g *generator) generateRubyCode(file *descriptor.FileDescriptorProto, pbFil } // protoFilesToGenerate selects descriptor proto files that were explicitly listed on the command-line. -func (g *generator) protoFilesToGenerate() []*descriptor.FileDescriptorProto { - files := []*descriptor.FileDescriptorProto{} +func (g *generator) findProtoFilesToGenerate() { for _, name := range g.genReq.FileToGenerate { // explicitly listed on the command-line for _, f := range g.genReq.ProtoFile { // all files and everything they import if f.GetName() == name { // match - files = append(files, f) + g.genFiles = append(g.genFiles, f) continue } } } - return files + + for _, f := range g.genReq.ProtoFile { + if fileDescSliceContains(g.genFiles, f) { + g.fileToGoPackageName[f] = "" + } else { + g.fileToGoPackageName[f] = f.GetPackage() + } + } } // indentation represents the level of Ruby indentation for a block of code. It @@ -232,7 +234,7 @@ func (g *generator) toRubyType(protoType string) string { var prefix string if pkg := g.fileToGoPackageName[def.File]; pkg != "" { - prefix = pkg + "::" + prefix = strings.Join(splitRubyConstants(pkg), "::") + "::" } var name string diff --git a/protoc-gen-twirp_ruby/main_test.go b/protoc-gen-twirp_ruby/main_test.go index 594d2f8..66d8126 100644 --- a/protoc-gen-twirp_ruby/main_test.go +++ b/protoc-gen-twirp_ruby/main_test.go @@ -2,10 +2,36 @@ package main import ( "bytes" + "io/ioutil" + "path/filepath" "reflect" "testing" + + "github.com/golang/protobuf/proto" + "github.com/golang/protobuf/protoc-gen-go/descriptor" + plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin" + "github.com/stretchr/testify/require" ) +func loadTestPb(t *testing.T) []*descriptor.FileDescriptorProto { + f, err := ioutil.ReadFile(filepath.Join("testdata", "fileset.pb")) + require.NoError(t, err, "unable to read testdata protobuf file") + + set := new(descriptor.FileDescriptorSet) + err = proto.Unmarshal(f, set) + require.NoError(t, err, "unable to unmarshal testdata protobuf file") + + return set.File +} + +func testGenerator(t *testing.T) *generator { + genReq := &plugin_go.CodeGeneratorRequest{ + FileToGenerate: []string{"rubytypes.proto"}, + ProtoFile: loadTestPb(t), + } + return newGenerator(genReq) +} + func TestPrint(t *testing.T) { b := new(bytes.Buffer) print(b, "Hello World") @@ -38,21 +64,18 @@ func TestFilePathOnlyBaseNoExtension(t *testing.T) { func TestToRubyType(t *testing.T) { tests := []struct { protoType string - modules []string expected string }{ - {"", []string{}, ""}, - {"", []string{"Foo", "Bar"}, ""}, - {".foo.my_message", []string{}, "Foo::MyMessage"}, - {".foo.my_message", []string{"Foo"}, "MyMessage"}, - {"m.v.p99.hello_world", []string{}, "M::V::P99::HelloWorld"}, - {"m.v.p99.hello_world", []string{"M", "V"}, "P99::HelloWorld"}, - {"m.v.p99.hello_world", []string{"M", "V", "P99"}, "HelloWorld"}, - {"m.v.p99.hello_world", []string{"P99"}, "M::V::P99::HelloWorld"}, - {"google.protobuf.Empty", []string{"Foo"}, "Google::Protobuf::Empty"}, + {".twirp.rubytypes.foo.my_message", "Foo::MyMessage"}, + {".twirp.rubytypes.m.v.p99.hello_world", "M::V::P99::HelloWorld"}, + {".google.protobuf.Empty", "Google::Protobuf::Empty"}, } + + g := testGenerator(t) + g.findProtoFilesToGenerate() + for _, tt := range tests { - actual := toRubyType(tt.protoType, tt.modules) + actual := g.toRubyType(tt.protoType) if !reflect.DeepEqual(actual, tt.expected) { t.Errorf("expected %v; actual %v", tt.expected, actual) } diff --git a/protoc-gen-twirp_ruby/testdata/fileset.pb b/protoc-gen-twirp_ruby/testdata/fileset.pb new file mode 100644 index 0000000000000000000000000000000000000000..122ddbfaacdf61fda3ccb8b29347f904f8252bf1 GIT binary patch literal 2818 zcmbtW&u`nv6{aM{mdA49Y8TsG7f4>U*(PZfd68VY8=wFs(K1t+R7onXd)ZLr$YvpN zRFbl5EcWl{y~q9qy%y-P|4Xk03Kad`(2AY(*P%Yx!}n(1``-6H&l~Z_w)nxhQs)bK zxXM+jX6yN(T&~LNp8jkKwV?ibK9_kxOaq@W}!BTBPU=J5qUNSdpB8I^(t zYIU8b=NBct`0~r2-W(t&i+e=YVnNzhP%aCZU&^E>=%OrF#n*?2iM*5xwL+o|2VA6P z4IrKaIfFP9f_#~zMVY6wb(yM+qAa0xApwU9wa#OyEweO>@@tK&=+YHvBbDpFY7LxA zm8A1DjyOV>qFfR(FVnJ=2_ffYnqX00L?wQ4b&G|%O0#o{RhFcDsNh40Bg?M=iGP0l z0kDG9e1j-fiKOMaC{cMC0Vzj~X6jNR;tgYhGF7Ir?82EAv_Op<;ciA$`aT>G5ig>2 zDRVIS??E8h9WFK?P-C(NRqt?!TOj`z4#9oksELZ#OPQ6LVs6PH_*d}c1f|G4jTXf% z)w)4&3cB0dcSX4p4a7+foJC8?#8hIew3Q+D#;18t%YrM)Dw!y#cNtyNOtMlyq9U0k zup&^-AeO3x0yHNj)D3hHC(Ypyh>LC%YF=KkOc*E>M8!(RtRS39Sy(x1C#zIc6c9)c zvIvizfCBF*JhgnAu%7td8)s+_secBiO#^Ro<~yU~kdD3a(Dnne+##%P=sW#s==p)5 zjuqf&M|&*yjO<@dd^-rp^T|1xj2%Qnc;9kE#}2yWxP$R@=(wXUAp*HxC}`}QI3f0h zURS66*K_0@(TVL3juFP{J7Xt2(+Q89(B))Do-fFviRFjRU^=#ZnoRwP7udv=4V_>x zwwx1t*h7BgB>Ro+h7=rI)V~o|vZ@PlaT?TJSKD ziFp5f!M7of=VVQsqK7mX(%88m`P*o@T3~7w98b@$k|KQIhRfw|*0R8qy1CUbEmxR0 z$c%~2S0At-9uJ=jmGtNc`_il^qb!!2X~gpt!vsD>-&V7MC|||YkMgGljw`w#wCRhaMiVgt(80i>JD70>d`HIHWiz17j)a%B~ldZU{CTE->4{r0=JkcJQq z(?EA$Huq**xOYqSQvA6g9^~uUwSLBIo=fFbny-5A%m@GNan-RzW3E)YEAHvUpVp}M zboa$4oA-py!!IBW(62Tfw1ik*e{<9C&O=dK9(*Qhmj@q<#_FrD4)(bqF2uZa`G#_G6UD`ZBkSo!Yx{x0<_RSD2>3o3_nQ zMN8b{4=}Oa`lNEz;cA+X#U8urhS@fo=Jw6by0OL1&#H5EKG!^WwE1e_tMj=|Tx*-V zjFEY+Vb^YR2fIwOhQwye+`p&K;0wN1Yfs!4TkK=+Uh}~YzPGDy`1kfd5)VYP`exww TTOF}4b~XlVx&QH_O&k9SO9KWb literal 0 HcmV?d00001 diff --git a/protoc-gen-twirp_ruby/testdata/gen.go b/protoc-gen-twirp_ruby/testdata/gen.go new file mode 100644 index 0000000..62b03dc --- /dev/null +++ b/protoc-gen-twirp_ruby/testdata/gen.go @@ -0,0 +1,16 @@ +// Copyright 2018 Twitch Interactive, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may not +// use this file except in compliance with the License. A copy of the License is +// located at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// or in the "license" file accompanying this file. This file is distributed on +// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +package testdata + +//go:generate protoc --descriptor_set_out=fileset.pb --include_imports --include_source_info ./rubytypes.proto diff --git a/protoc-gen-twirp_ruby/testdata/rubytypes.proto b/protoc-gen-twirp_ruby/testdata/rubytypes.proto new file mode 100644 index 0000000..9524c71 --- /dev/null +++ b/protoc-gen-twirp_ruby/testdata/rubytypes.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; + +package twirp.rubytypes; + +import "google/protobuf/empty.proto"; + +message foo { + message my_message{} + google.protobuf.Empty empty = 1; +} + +message m { + message v { + message p99 { + message hello_world {} + } + } +}