-
Notifications
You must be signed in to change notification settings - Fork 30
/
plugin_test.go
96 lines (72 loc) · 2.44 KB
/
plugin_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package protokit_test
import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/stretchr/testify/suite"
"bytes"
"errors"
"testing"
"github.com/pseudomuto/protokit"
"github.com/pseudomuto/protokit/utils"
)
type PluginTest struct {
suite.Suite
}
func TestPlugin(t *testing.T) {
suite.Run(t, new(PluginTest))
}
func (assert *PluginTest) TestRunPlugin() {
fds, err := utils.LoadDescriptorSet("fixtures", "fileset.pb")
assert.NoError(err)
req := utils.CreateGenRequest(fds, "booking.proto", "todo.proto")
data, err := proto.Marshal(req)
assert.NoError(err)
in := bytes.NewBuffer(data)
out := new(bytes.Buffer)
assert.NoError(protokit.RunPluginWithIO(new(OkPlugin), in, out))
assert.NotEmpty(out)
}
func (assert *PluginTest) TestRunPluginInputError() {
in := bytes.NewBufferString("Not a codegen request")
out := new(bytes.Buffer)
err := protokit.RunPluginWithIO(nil, in, out)
assert.EqualError(err, "proto: can't skip unknown wire type 6 for plugin_go.CodeGeneratorRequest")
assert.Empty(out)
}
func (assert *PluginTest) TestRunPluginNoFilesToGenerate() {
fds, err := utils.LoadDescriptorSet("fixtures", "fileset.pb")
assert.NoError(err)
req := utils.CreateGenRequest(fds)
data, err := proto.Marshal(req)
assert.NoError(err)
in := bytes.NewBuffer(data)
out := new(bytes.Buffer)
err = protokit.RunPluginWithIO(new(ErrorPlugin), in, out)
assert.EqualError(err, "no files were supplied to the generator")
assert.Empty(out)
}
func (assert *PluginTest) TestRunPluginGeneratorError() {
fds, err := utils.LoadDescriptorSet("fixtures", "fileset.pb")
assert.NoError(err)
req := utils.CreateGenRequest(fds, "booking.proto", "todo.proto")
data, err := proto.Marshal(req)
assert.NoError(err)
in := bytes.NewBuffer(data)
out := new(bytes.Buffer)
err = protokit.RunPluginWithIO(new(ErrorPlugin), in, out)
assert.EqualError(err, "generator error")
assert.Empty(out)
}
type ErrorPlugin struct{}
func (ep *ErrorPlugin) Generate(r *plugin_go.CodeGeneratorRequest) (*plugin_go.CodeGeneratorResponse, error) {
return nil, errors.New("generator error")
}
type OkPlugin struct{}
func (op *OkPlugin) Generate(r *plugin_go.CodeGeneratorRequest) (*plugin_go.CodeGeneratorResponse, error) {
resp := new(plugin_go.CodeGeneratorResponse)
resp.File = append(resp.File, &plugin_go.CodeGeneratorResponse_File{
Name: proto.String("myfile.out"),
Content: proto.String("someoutput"),
})
return resp, nil
}