Repository navigation
Expand file tree
/
Copy pathcodegen_test.go
More file actions
206 lines (169 loc) · 5.04 KB
/
Copy pathcodegen_test.go
File metadata and controls
206 lines (169 loc) · 5.04 KB
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
196
197
198
199
200
201
202
203
204
205
206
package codegen_test
import (
"bytes"
"embed"
"encoding/json/jsontext"
"encoding/json/v2"
"errors"
"fmt"
"io/fs"
"path/filepath"
"strings"
"testing"
"github.com/MarkRosemaker/cassette"
"github.com/MarkRosemaker/openapi"
codegen "github.com/MarkRosemaker/openapi-codegen"
"github.com/MarkRosemaker/openapi-codegen/config"
"github.com/MarkRosemaker/openapi-codegen/ir"
"github.com/spf13/afero"
)
// Whether to test in debug mode.
const (
debugMode = false
production = true
)
//go:embed testdata
var testdata embed.FS
var genAll = config.Generate{
Types: true,
Client: true,
ClientTest: true,
Server: true,
JS: true,
}
// TestCodegen_TestData generates each package in testdata from its specification and interactions, three times over,
// and must get the files there each time. golden holds every case the generator handles, once; auth and debug are
// what golden cannot be as well: a document whose every request sends credentials, and the one generated in debug
// mode. The files are edited by hand, never regenerated, so a change in what comes out is read before it is accepted.
func TestCodegen_TestData(t *testing.T) {
entries, err := testdata.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
for _, tc := range entries {
t.Run(tc.Name(), func(t *testing.T) {
name := tc.Name()
f, err := testdata.Open(filepath.Join("testdata", name, "api", "openapi.json"))
if err != nil {
t.Fatal(err)
}
defer f.Close() //nolint
doc, err := openapi.LoadFromReader(f)
if err != nil {
t.Fatal(err)
}
debug := debugMode || name == "debug"
irDoc, err := ir.FromDocument(doc, name, "", production, debug)
if err != nil {
t.Fatalf("build IR: %v", err)
}
iasPath := filepath.Join("testdata", name, "api", "interactions.json")
ias, err := cassette.InteractionsReadFile(iasPath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
t.Fatal(err)
}
for it := range 3 {
t.Run(fmt.Sprintf("iteration %d", it+1), func(t *testing.T) {
memFs := afero.NewMemMapFs()
writeJSON(t, memFs, "ir.json", irDoc)
if err := codegen.Generate(codegen.Config{
Debug: debug,
Spec: doc,
PackageName: strings.ReplaceAll(name, "-", ""),
OutputFs: memFs,
Interactions: ias,
Generate: genAll,
Production: production,
}); err != nil {
t.Fatal(err)
}
wantFs := afero.NewBasePathFs(afero.NewOsFs(), filepath.Join("testdata", name, "pkg", strings.ReplaceAll(name, "-", "")))
compareFs(t, wantFs, memFs)
})
}
})
}
}
// compareBytes fails with the first line where the generated file differs from the one in testdata, which is to be
// edited by hand if the change is wanted.
func compareBytes(t *testing.T, expected, actual []byte, path string) {
t.Helper()
if bytes.Equal(expected, actual) {
return
}
want, got := strings.Split(string(expected), "\n"), strings.Split(string(actual), "\n")
i := 0
for i < len(want) && i < len(got) && want[i] == got[i] {
i++
}
line := func(lines []string) string {
if i < len(lines) {
return lines[i]
}
return "(end of file)"
}
t.Fatalf("%s:%d differs\nwant: %s\n got: %s", path, i+1, line(want), line(got))
}
// handWritten reports whether path is a file a golden directory carries but
// the generator does not produce. An error response type is passed to
// api.NewErrCustom, which takes an error, so the package author writes its
// Error method; what a good message reads like depends on the API, so the
// generator does not guess. A test the generator did not write is one too.
func handWritten(path string) bool {
switch base := filepath.Base(path); base {
case "error.go", "errors.go":
return true
default:
return strings.HasSuffix(base, "_test.go") && !strings.HasSuffix(base, ".gen_test.go")
}
}
func compareFs(t *testing.T, expected, actual afero.Fs) {
t.Helper()
if err := afero.Walk(expected, "", func(path string, info fs.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
if handWritten(path) {
return nil
}
want, err := afero.ReadFile(expected, path)
if err != nil {
return err
}
got, err := afero.ReadFile(actual, path)
if err != nil {
return fmt.Errorf("in actual FS: %w", err)
}
compareBytes(t, want, got, path)
return nil
}); err != nil {
t.Fatal(err)
}
if err := afero.Walk(actual, "", func(path string, info fs.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
if ok, err := afero.Exists(expected, path); err != nil {
return err
} else if !ok {
return fmt.Errorf("generated additional file %q", path)
}
return nil
}); err != nil {
t.Fatal(err)
}
}
func writeJSON(t *testing.T, fsys afero.Fs, path string, in any) {
f, err := fsys.Create(path)
if err != nil {
t.Fatal(err)
}
defer f.Close() //nolint:errcheck
if ias, ok := in.(cassette.Interactions); ok {
if err := ias.MarshalWrite(f); err != nil {
t.Fatal(err)
}
} else if err := json.MarshalWrite(f, in, jsontext.Multiline(true)); err != nil {
t.Fatal(err)
}
}