-
Notifications
You must be signed in to change notification settings - Fork 1
/
printer_test.go
128 lines (111 loc) · 2.8 KB
/
printer_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
package main
import (
"bytes"
"flag"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/anz-bank/sysl/pkg/parse"
"github.com/anz-bank/sysl/pkg/syslutil"
"github.com/spf13/afero"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
"github.com/anz-bank/protoc-gen-sysl/proto2sysl"
"google.golang.org/protobuf/proto"
"github.com/stretchr/testify/assert"
)
var tests = []string{
"test/",
"multiplefiles/",
"date/",
"any/",
"hello/",
"externaltype/",
"disconnectedimport/",
"any/",
"simple/",
"empty/",
"repeated/",
"messageinmessage/",
"test",
"otheroption/",
"enum/",
"names/",
}
const testDir = "./tests"
func TestPrinting(t *testing.T) {
t.Parallel()
for _, test := range tests {
t.Run(test, func(t *testing.T) {
t.Parallel()
dir := filepath.Join(testDir, test)
_, fs := syslutil.WriteToMemOverlayFs(dir)
GeneratorResponse, err := ConvertSyslToProto(filepath.Join(dir, "code_generator_request.pb.bin"))
require.NoError(t, err)
file := GeneratorResponse.File[0]
golden, err := afero.ReadFile(fs, *file.Name)
require.NoError(t, err)
assert.Equal(t, *file.Content, string(golden), *file.Content)
_, err = parse.NewParser().ParseString(*file.Content)
require.NoError(t, err)
})
}
}
// ConvertSyslToProto opens a sysl filename and returns the CodeGeneratorResponse for the test cases.
func ConvertSyslToProto(filename string) (*pluginpb.CodeGeneratorResponse, error) {
req, err := os.Open(filename)
if err != nil {
return nil, err
}
var (
flags flag.FlagSet
_ = flags.String("tests/test", "", "prefix to prepend to import paths")
res bytes.Buffer
)
if err := run(protogen.Options{ParamFunc: flags.Set}, req, &res, proto2sysl.GenerateFiles); err != nil {
return nil, err
}
response := &pluginpb.CodeGeneratorResponse{}
err = proto.Unmarshal(res.Bytes(), response)
if err != nil {
return nil, err
}
return response, nil
}
func run(opts protogen.Options, input io.Reader, output io.Writer, f func(*protogen.Plugin) error) error {
in, err := ioutil.ReadAll(input)
if err != nil {
return err
}
req := &pluginpb.CodeGeneratorRequest{}
if err := proto.Unmarshal(in, req); err != nil {
return err
}
this := ""
req.Parameter = &this
gen, err := opts.New(req)
if err != nil {
return err
}
if err := f(gen); err != nil {
// Errors from the plugin function are reported by setting the
// error field in the CodeGeneratorResponse.
//
// In contrast, errors that indicate a problem in protoc
// itself (unparsable input, I/O errors, etc.) are reported
// to stderr.
gen.Error(err)
}
resp := gen.Response()
out, err := proto.Marshal(resp)
if err != nil {
return err
}
if _, err := output.Write(out); err != nil {
return err
}
return nil
}