github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/cmd/protoc-gen-openapi/converter/converter_test.go (about) 1 package converter 2 3 import ( 4 "bytes" 5 "fmt" 6 "os/exec" 7 "strings" 8 "testing" 9 10 "github.com/golang/protobuf/proto" 11 "github.com/golang/protobuf/protoc-gen-go/descriptor" 12 plugin "github.com/golang/protobuf/protoc-gen-go/plugin" 13 "github.com/stretchr/testify/assert" 14 prot "google.golang.org/protobuf/compiler/protogen" 15 16 "github.com/tickoalcantara12/micro/v3/cmd/protoc-gen-openapi/converter/testdata" 17 ) 18 19 const ( 20 sampleProtoDirectory = "testdata/proto" 21 ) 22 23 type sampleProto struct { 24 ExpectedAPISpec []string 25 FilesToGenerate []string 26 ProtoFileName string 27 microServiceName string 28 } 29 30 func TestGenerateOpenAPI(t *testing.T) { 31 32 // Configure the list of sample protos to test, and their expected API spec: 33 sampleProtos := configureSampleProtos() 34 35 // Convert the protos, compare the results against the expected API spec: 36 for _, sampleProto := range sampleProtos { 37 testConvertSampleProto(t, sampleProto) 38 } 39 } 40 41 func testConvertSampleProto(t *testing.T, sampleProto sampleProto) { 42 43 protoConverter := New() 44 protoConverter.defaultSpec() 45 46 // Open the sample proto file: 47 sampleProtoFileName := fmt.Sprintf("%v/%v", sampleProtoDirectory, sampleProto.ProtoFileName) 48 fileDescriptorSet := mustReadProtoFiles(t, sampleProtoDirectory, sampleProto.ProtoFileName) 49 50 // Prepare a request: 51 codeGeneratorRequest := plugin.CodeGeneratorRequest{ 52 FileToGenerate: sampleProto.FilesToGenerate, 53 ProtoFile: fileDescriptorSet.GetFile(), 54 } 55 opts := &prot.Options{} 56 plug, err := opts.New(&codeGeneratorRequest) 57 if err != nil { 58 t.Fatal(err) 59 } 60 protoConverter.plug = plug 61 62 // Perform the conversion: 63 response, err := protoConverter.convert(&codeGeneratorRequest) 64 assert.NoError(t, err, "Unable to convert sample proto file (%v)", sampleProtoFileName) 65 assert.Equal(t, len(sampleProto.ExpectedAPISpec), len(response.File), "Incorrect number of JSON-Schema files returned for sample proto file (%v)", sampleProtoFileName) 66 if len(sampleProto.ExpectedAPISpec) != len(response.File) { 67 t.Fail() 68 } else { 69 for responseFileIndex, responseFile := range response.File { 70 assert.Equal(t, strings.TrimSpace(sampleProto.ExpectedAPISpec[responseFileIndex]), *responseFile.Content, "Incorrect JSON-Schema returned for sample proto file (%v)", sampleProtoFileName) 71 } 72 } 73 74 // Return now if we have no files: 75 if len(response.File) == 0 { 76 return 77 } 78 79 // Check for the correct prefix: 80 assert.NotContains(t, response.File[0].GetName(), "samples") 81 } 82 83 func configureSampleProtos() map[string]sampleProto { 84 return map[string]sampleProto{ 85 "Signup": { 86 ExpectedAPISpec: []string{testdata.Signup}, 87 FilesToGenerate: []string{"signup.proto"}, 88 ProtoFileName: "signup.proto", 89 }, 90 } 91 } 92 93 // Load the specified .proto files into a FileDescriptorSet. Any errors in loading/parsing will 94 // immediately fail the test. 95 func mustReadProtoFiles(t *testing.T, includePath string, filenames ...string) *descriptor.FileDescriptorSet { 96 protocBinary, err := exec.LookPath("protoc") 97 if err != nil { 98 t.Fatalf("Can't find 'protoc' binary in $PATH: %s", err.Error()) 99 } 100 101 // Use protoc to output descriptor info for the specified .proto files. 102 var args []string 103 args = append(args, "--descriptor_set_out=/dev/stdout") 104 args = append(args, "--include_source_info") 105 args = append(args, "--include_imports") 106 args = append(args, "--proto_path="+includePath) 107 args = append(args, filenames...) 108 cmd := exec.Command(protocBinary, args...) 109 stdoutBuf := bytes.Buffer{} 110 stderrBuf := bytes.Buffer{} 111 cmd.Stdout = &stdoutBuf 112 cmd.Stderr = &stderrBuf 113 err = cmd.Run() 114 if err != nil { 115 t.Fatalf("failed to load descriptor set (%s): %s: %s", 116 strings.Join(cmd.Args, " "), err.Error(), stderrBuf.String()) 117 } 118 fds := &descriptor.FileDescriptorSet{} 119 err = proto.Unmarshal(stdoutBuf.Bytes(), fds) 120 if err != nil { 121 t.Fatalf("failed to parse protoc output as FileDescriptorSet: %s", err.Error()) 122 } 123 return fds 124 }