go.uber.org/yarpc@v1.72.1/internal/protoplugin/multi_runner_test.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package protoplugin 22 23 import ( 24 "errors" 25 "testing" 26 27 "github.com/gogo/protobuf/proto" 28 "github.com/gogo/protobuf/protoc-gen-gogo/plugin" 29 "github.com/stretchr/testify/assert" 30 "go.uber.org/multierr" 31 ) 32 33 func TestMultiRunnerRun(t *testing.T) { 34 testMultiRunnerRun( 35 t, 36 []Runner{ 37 newSuccessTestRunner("foo"), 38 newSuccessTestRunner("bar", "baz"), 39 newSuccessTestRunner("bat"), 40 }, 41 []string{ 42 "bar", 43 "bat", 44 "baz", 45 "foo", 46 }, 47 nil, 48 ) 49 testMultiRunnerRun( 50 t, 51 []Runner{ 52 newSuccessTestRunner("foo"), 53 newSuccessTestRunner("bar", "baz"), 54 newSuccessTestRunner(""), 55 }, 56 nil, 57 errNoFileName, 58 ) 59 testMultiRunnerRun( 60 t, 61 []Runner{ 62 newSuccessTestRunner("foo"), 63 newSuccessTestRunner("bar", "baz"), 64 newSuccessTestRunner("foo"), 65 }, 66 nil, 67 newErrorDuplicateFileName("foo"), 68 ) 69 testMultiRunnerRun( 70 t, 71 []Runner{ 72 newSuccessTestRunner("foo"), 73 newErrorTestRunner(errors.New("value")), 74 newSuccessTestRunner("bar"), 75 }, 76 nil, 77 errors.New("value"), 78 ) 79 testMultiRunnerRun( 80 t, 81 []Runner{ 82 newSuccessTestRunner("foo"), 83 newErrorTestRunner(errors.New("value")), 84 newErrorTestRunner(errors.New("value2")), 85 }, 86 nil, 87 multierr.Append( 88 errors.New("value"), 89 errors.New("value2"), 90 ), 91 ) 92 } 93 94 func testMultiRunnerRun( 95 t *testing.T, 96 runners []Runner, 97 expectedFileNames []string, 98 expectedError error, 99 ) { 100 response := NewMultiRunner(runners...).Run(nil) 101 if expectedError != nil { 102 assert.Equal(t, newResponseError(expectedError), response) 103 } else { 104 fileNames := make([]string, 0, len(expectedFileNames)) 105 for _, file := range response.GetFile() { 106 fileNames = append(fileNames, file.GetName()) 107 } 108 assert.Equal(t, expectedFileNames, fileNames) 109 } 110 } 111 112 type testRunner struct { 113 fileNames []string 114 err error 115 } 116 117 func newSuccessTestRunner(fileNames ...string) *testRunner { 118 return &testRunner{ 119 fileNames: fileNames, 120 } 121 } 122 123 func newErrorTestRunner(err error) *testRunner { 124 return &testRunner{ 125 err: err, 126 } 127 } 128 129 func (r *testRunner) Run(*plugin_go.CodeGeneratorRequest) *plugin_go.CodeGeneratorResponse { 130 if r.err != nil { 131 return newResponseError(r.err) 132 } 133 files := make([]*plugin_go.CodeGeneratorResponse_File, 0, len(r.fileNames)) 134 for _, fileName := range r.fileNames { 135 files = append(files, &plugin_go.CodeGeneratorResponse_File{ 136 Name: proto.String(fileName), 137 }) 138 } 139 return newResponseFiles(files) 140 }