forked from chrusty/protoc-gen-jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotoc_gen_jsonschema_test.go
195 lines (163 loc) · 6.78 KB
/
protoc_gen_jsonschema_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bytes"
"fmt"
"os/exec"
"testing"
log "github.com/Sirupsen/logrus"
testdata "github.com/chrusty/protoc-gen-jsonschema/testdata"
proto "github.com/golang/protobuf/proto"
descriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
assert "github.com/stretchr/testify/assert"
)
var (
protocBinary = "/bin/protoc"
sampleProtoDirectory = "testdata/proto"
sampleProtos = make(map[string]SampleProto)
)
type SampleProto struct {
AllowNullValues bool
ExpectedJsonSchema []string
FilesToGenerate []string
ProtoFileName string
}
func TestGenerateJsonSchema(t *testing.T) {
// We only want to see "Info" level logs and above (there's a LOT of debug otherwise):
log.SetLevel(log.InfoLevel)
// Make sure we have "protoc" installed and available:
testForProtocBinary(t)
// Configure the list of sample protos to test, and their expected JSON-Schemas:
configureSampleProtos()
// Convert the protos, compare the results against the expected JSON-Schemas:
testConvertSampleProtos(t, sampleProtos["ArrayOfMessages"])
testConvertSampleProtos(t, sampleProtos["ArrayOfObjects"])
testConvertSampleProtos(t, sampleProtos["ArrayOfPrimitives"])
testConvertSampleProtos(t, sampleProtos["EnumCeption"])
testConvertSampleProtos(t, sampleProtos["ImportedEnum"])
testConvertSampleProtos(t, sampleProtos["NestedMessage"])
testConvertSampleProtos(t, sampleProtos["NestedObject"])
testConvertSampleProtos(t, sampleProtos["PayloadMessage"])
testConvertSampleProtos(t, sampleProtos["SeveralEnums"])
testConvertSampleProtos(t, sampleProtos["SeveralMessages"])
}
func testForProtocBinary(t *testing.T) {
path, err := exec.LookPath("protoc")
if err != nil {
assert.NoError(t, err, "Can't find 'protoc' binary in $PATH")
} else {
protocBinary = path
log.Infof("Found 'protoc' binary (%v)", protocBinary)
}
}
func testConvertSampleProto(t *testing.T) {
// Go through the sample protos:
for _, sampleProto := range sampleProtos {
log.Infof("SampleProto: %v", sampleProto.ProtoFileName)
}
}
func testConvertSampleProtos(t *testing.T, sampleProto SampleProto) {
// Set allowNullValues accordingly:
allowNullValues = sampleProto.AllowNullValues
// Open the sample proto file:
sampleProtoFileName := fmt.Sprintf("%v/%v", sampleProtoDirectory, sampleProto.ProtoFileName)
// Prepare to run the "protoc" command (generates a CodeGeneratorRequest):
protocCommand := exec.Command(protocBinary, "--descriptor_set_out=/dev/stdout", "--include_imports", fmt.Sprintf("--proto_path=%v", sampleProtoDirectory), sampleProtoFileName)
var protocCommandOutput bytes.Buffer
protocCommand.Stdout = &protocCommandOutput
// Run the command:
err := protocCommand.Run()
assert.NoError(t, err, "Unable to prepare a codeGeneratorRequest using protoc (%v) for sample proto file (%v)", protocBinary, sampleProtoFileName)
// Unmarshal the output from the protoc command (should be a "FileDescriptorSet"):
fileDescriptorSet := new(descriptor.FileDescriptorSet)
err = proto.Unmarshal(protocCommandOutput.Bytes(), fileDescriptorSet)
assert.NoError(t, err, "Unable to unmarshal proto FileDescriptorSet for sample proto file (%v)", sampleProtoFileName)
// Prepare a request:
codeGeneratorRequest := plugin.CodeGeneratorRequest{
FileToGenerate: sampleProto.FilesToGenerate,
ProtoFile: fileDescriptorSet.GetFile(),
}
// Perform the conversion:
response, err := convert(&codeGeneratorRequest)
assert.NoError(t, err, "Unable to convert sample proto file (%v)", sampleProtoFileName)
assert.Equal(t, len(sampleProto.ExpectedJsonSchema), len(response.File), "Incorrect number of JSON-Schema files returned for sample proto file (%v)", sampleProtoFileName)
if len(sampleProto.ExpectedJsonSchema) != len(response.File) {
t.Fail()
} else {
for responseFileIndex, responseFile := range response.File {
assert.Equal(t, sampleProto.ExpectedJsonSchema[responseFileIndex], *responseFile.Content, "Incorrect JSON-Schema returned for sample proto file (%v)", sampleProtoFileName)
}
}
}
func configureSampleProtos() {
// ArrayOfMessages:
sampleProtos["ArrayOfMessages"] = SampleProto{
AllowNullValues: false,
ExpectedJsonSchema: []string{testdata.PayloadMessage, testdata.ArrayOfMessages},
FilesToGenerate: []string{"ArrayOfMessages.proto", "PayloadMessage.proto"},
ProtoFileName: "ArrayOfMessages.proto",
}
// ArrayOfObjects:
sampleProtos["ArrayOfObjects"] = SampleProto{
AllowNullValues: true,
ExpectedJsonSchema: []string{testdata.ArrayOfObjects},
FilesToGenerate: []string{"ArrayOfObjects.proto"},
ProtoFileName: "ArrayOfObjects.proto",
}
// ArrayOfPrimitives:
sampleProtos["ArrayOfPrimitives"] = SampleProto{
AllowNullValues: true,
ExpectedJsonSchema: []string{testdata.ArrayOfPrimitives},
FilesToGenerate: []string{"ArrayOfPrimitives.proto"},
ProtoFileName: "ArrayOfPrimitives.proto",
}
// EnumCeption:
sampleProtos["EnumCeption"] = SampleProto{
AllowNullValues: false,
ExpectedJsonSchema: []string{testdata.PayloadMessage, testdata.ImportedEnum, testdata.EnumCeption},
FilesToGenerate: []string{"EnumCeption.proto", "PayloadMessage.proto", "ImportedEnum.proto"},
ProtoFileName: "EnumCeption.proto",
}
// ImportedEnum:
sampleProtos["ImportedEnum"] = SampleProto{
AllowNullValues: false,
ExpectedJsonSchema: []string{testdata.ImportedEnum},
FilesToGenerate: []string{"ImportedEnum.proto"},
ProtoFileName: "ImportedEnum.proto",
}
// NestedMessage:
sampleProtos["NestedMessage"] = SampleProto{
AllowNullValues: false,
ExpectedJsonSchema: []string{testdata.PayloadMessage, testdata.NestedMessage},
FilesToGenerate: []string{"NestedMessage.proto", "PayloadMessage.proto"},
ProtoFileName: "NestedMessage.proto",
}
// NestedObject:
sampleProtos["NestedObject"] = SampleProto{
AllowNullValues: false,
ExpectedJsonSchema: []string{testdata.NestedObject},
FilesToGenerate: []string{"NestedObject.proto"},
ProtoFileName: "NestedObject.proto",
}
// PayloadMessage:
sampleProtos["PayloadMessage"] = SampleProto{
AllowNullValues: false,
ExpectedJsonSchema: []string{testdata.PayloadMessage},
FilesToGenerate: []string{"PayloadMessage.proto"},
ProtoFileName: "PayloadMessage.proto",
}
// SeveralEnums:
sampleProtos["SeveralEnums"] = SampleProto{
AllowNullValues: false,
ExpectedJsonSchema: []string{testdata.FirstEnum, testdata.SecondEnum},
FilesToGenerate: []string{"SeveralEnums.proto"},
ProtoFileName: "SeveralEnums.proto",
}
// SeveralMessages:
sampleProtos["SeveralMessages"] = SampleProto{
AllowNullValues: false,
ExpectedJsonSchema: []string{testdata.FirstMessage, testdata.SecondMessage},
FilesToGenerate: []string{"SeveralMessages.proto"},
ProtoFileName: "SeveralMessages.proto",
}
}