Skip to content

Commit 7941dde

Browse files
committed
add tests and validation
Signed-off-by: Danny Brito <[email protected]>
1 parent ee3e8e3 commit 7941dde

File tree

4 files changed

+824
-0
lines changed

4 files changed

+824
-0
lines changed

generator_gomod_test.go

Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
package dalec
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/goccy/go-yaml"
8+
)
9+
10+
func TestGomodRequireUnmarshal(t *testing.T) {
11+
t.Parallel()
12+
13+
tests := []struct {
14+
name string
15+
input string
16+
unmarshal func([]byte, interface{}) error
17+
expectErr bool
18+
expectedModule string
19+
expectedVersion string
20+
}{
21+
{
22+
name: "YAML string format",
23+
input: `"github.com/stretchr/testify:github.com/stretchr/[email protected]"`,
24+
unmarshal: yaml.Unmarshal,
25+
expectErr: false,
26+
expectedModule: "github.com/stretchr/testify",
27+
expectedVersion: "github.com/stretchr/[email protected]",
28+
},
29+
{
30+
name: "YAML struct format",
31+
input: `
32+
module: github.com/cpuguy83/tar2go
33+
version: github.com/cpuguy83/[email protected]
34+
`,
35+
unmarshal: yaml.Unmarshal,
36+
expectErr: false,
37+
expectedModule: "github.com/cpuguy83/tar2go",
38+
expectedVersion: "github.com/cpuguy83/[email protected]",
39+
},
40+
{
41+
name: "JSON string format",
42+
input: `"github.com/stretchr/testify:github.com/stretchr/[email protected]"`,
43+
unmarshal: json.Unmarshal,
44+
expectErr: false,
45+
expectedModule: "github.com/stretchr/testify",
46+
expectedVersion: "github.com/stretchr/[email protected]",
47+
},
48+
{
49+
name: "JSON struct format",
50+
input: `{"module":"github.com/cpuguy83/tar2go","version":"github.com/cpuguy83/[email protected]"}`,
51+
unmarshal: json.Unmarshal,
52+
expectErr: false,
53+
expectedModule: "github.com/cpuguy83/tar2go",
54+
expectedVersion: "github.com/cpuguy83/[email protected]",
55+
},
56+
{
57+
name: "invalid string format - no colon",
58+
input: `"github.com/stretchr/testify"`,
59+
unmarshal: yaml.Unmarshal,
60+
expectErr: true,
61+
},
62+
{
63+
name: "invalid string format - no @version",
64+
input: `"github.com/stretchr/testify:v1.8.0"`,
65+
unmarshal: yaml.Unmarshal,
66+
expectErr: true,
67+
},
68+
{
69+
name: "invalid struct format - missing version",
70+
input: `{"module":"github.com/cpuguy83/tar2go"}`,
71+
unmarshal: json.Unmarshal,
72+
expectErr: true,
73+
},
74+
{
75+
name: "invalid struct format - version without @",
76+
input: `{"module":"github.com/cpuguy83/tar2go","version":"v0.3.1"}`,
77+
unmarshal: json.Unmarshal,
78+
expectErr: true,
79+
},
80+
}
81+
82+
for _, tt := range tests {
83+
t.Run(tt.name, func(t *testing.T) {
84+
var req GomodRequire
85+
err := tt.unmarshal([]byte(tt.input), &req)
86+
87+
if tt.expectErr {
88+
if err == nil {
89+
t.Fatal("expected error, got nil")
90+
}
91+
return
92+
}
93+
94+
if err != nil {
95+
t.Fatalf("failed to unmarshal: %v", err)
96+
}
97+
98+
if req.Module != tt.expectedModule {
99+
t.Errorf("expected Module=%q, got %q", tt.expectedModule, req.Module)
100+
}
101+
if req.Version != tt.expectedVersion {
102+
t.Errorf("expected Version=%q, got %q", tt.expectedVersion, req.Version)
103+
}
104+
})
105+
}
106+
}
107+
108+
func TestGomodReplaceUnmarshal(t *testing.T) {
109+
t.Parallel()
110+
111+
tests := []struct {
112+
name string
113+
input string
114+
unmarshal func([]byte, interface{}) error
115+
expectErr bool
116+
expectedOld string
117+
expectedNew string
118+
}{
119+
{
120+
name: "YAML string format",
121+
input: `"github.com/stretchr/testify:github.com/stretchr/[email protected]"`,
122+
unmarshal: yaml.Unmarshal,
123+
expectErr: false,
124+
expectedOld: "github.com/stretchr/testify",
125+
expectedNew: "github.com/stretchr/[email protected]",
126+
},
127+
{
128+
name: "YAML struct format",
129+
input: `
130+
old: github.com/cpuguy83/tar2go
131+
new: github.com/cpuguy83/[email protected]
132+
`,
133+
unmarshal: yaml.Unmarshal,
134+
expectErr: false,
135+
expectedOld: "github.com/cpuguy83/tar2go",
136+
expectedNew: "github.com/cpuguy83/[email protected]",
137+
},
138+
{
139+
name: "JSON string format",
140+
input: `"github.com/stretchr/testify:github.com/stretchr/[email protected]"`,
141+
unmarshal: json.Unmarshal,
142+
expectErr: false,
143+
expectedOld: "github.com/stretchr/testify",
144+
expectedNew: "github.com/stretchr/[email protected]",
145+
},
146+
{
147+
name: "JSON struct format",
148+
input: `{"old":"github.com/cpuguy83/tar2go","new":"github.com/cpuguy83/[email protected]"}`,
149+
unmarshal: json.Unmarshal,
150+
expectErr: false,
151+
expectedOld: "github.com/cpuguy83/tar2go",
152+
expectedNew: "github.com/cpuguy83/[email protected]",
153+
},
154+
{
155+
name: "invalid string format - no colon",
156+
input: `"github.com/stretchr/testify"`,
157+
unmarshal: yaml.Unmarshal,
158+
expectErr: true,
159+
},
160+
{
161+
name: "invalid struct format - missing new",
162+
input: `{"old":"github.com/cpuguy83/tar2go"}`,
163+
unmarshal: json.Unmarshal,
164+
expectErr: true,
165+
},
166+
{
167+
name: "invalid struct format - empty old",
168+
input: `{"old":"","new":"github.com/cpuguy83/[email protected]"}`,
169+
unmarshal: json.Unmarshal,
170+
expectErr: true,
171+
},
172+
}
173+
174+
for _, tt := range tests {
175+
t.Run(tt.name, func(t *testing.T) {
176+
var repl GomodReplace
177+
err := tt.unmarshal([]byte(tt.input), &repl)
178+
179+
if tt.expectErr {
180+
if err == nil {
181+
t.Fatal("expected error, got nil")
182+
}
183+
return
184+
}
185+
186+
if err != nil {
187+
t.Fatalf("failed to unmarshal: %v", err)
188+
}
189+
190+
if repl.Old != tt.expectedOld {
191+
t.Errorf("expected Old=%q, got %q", tt.expectedOld, repl.Old)
192+
}
193+
if repl.New != tt.expectedNew {
194+
t.Errorf("expected New=%q, got %q", tt.expectedNew, repl.New)
195+
}
196+
})
197+
}
198+
}
199+
200+
func TestGomodRequireGoModEditArg(t *testing.T) {
201+
t.Parallel()
202+
203+
tests := []struct {
204+
name string
205+
req GomodRequire
206+
expectErr bool
207+
expectedArg string
208+
}{
209+
{
210+
name: "valid require",
211+
req: GomodRequire{
212+
Module: "github.com/stretchr/testify",
213+
Version: "github.com/stretchr/[email protected]",
214+
},
215+
expectErr: false,
216+
expectedArg: "github.com/stretchr/[email protected]",
217+
},
218+
{
219+
name: "missing @version",
220+
req: GomodRequire{
221+
Module: "github.com/stretchr/testify",
222+
Version: "v1.8.0",
223+
},
224+
expectErr: true,
225+
},
226+
{
227+
name: "empty module",
228+
req: GomodRequire{
229+
Module: "",
230+
Version: "github.com/stretchr/[email protected]",
231+
},
232+
expectErr: true,
233+
},
234+
}
235+
236+
for _, tt := range tests {
237+
t.Run(tt.name, func(t *testing.T) {
238+
arg, err := tt.req.goModEditArg()
239+
240+
if tt.expectErr {
241+
if err == nil {
242+
t.Fatal("expected error, got nil")
243+
}
244+
return
245+
}
246+
247+
if err != nil {
248+
t.Fatalf("unexpected error: %v", err)
249+
}
250+
251+
if arg != tt.expectedArg {
252+
t.Errorf("expected %q, got %q", tt.expectedArg, arg)
253+
}
254+
})
255+
}
256+
}
257+
258+
func TestGomodReplaceGoModEditArg(t *testing.T) {
259+
t.Parallel()
260+
261+
tests := []struct {
262+
name string
263+
repl GomodReplace
264+
expectErr bool
265+
expectedArg string
266+
}{
267+
{
268+
name: "valid replace",
269+
repl: GomodReplace{
270+
Old: "github.com/stretchr/testify",
271+
New: "github.com/stretchr/[email protected]",
272+
},
273+
expectErr: false,
274+
expectedArg: "github.com/stretchr/testify=github.com/stretchr/[email protected]",
275+
},
276+
{
277+
name: "empty old",
278+
repl: GomodReplace{
279+
Old: "",
280+
New: "github.com/stretchr/[email protected]",
281+
},
282+
expectErr: true,
283+
},
284+
{
285+
name: "empty new",
286+
repl: GomodReplace{
287+
Old: "github.com/stretchr/testify",
288+
New: "",
289+
},
290+
expectErr: true,
291+
},
292+
}
293+
294+
for _, tt := range tests {
295+
t.Run(tt.name, func(t *testing.T) {
296+
arg, err := tt.repl.goModEditArg()
297+
298+
if tt.expectErr {
299+
if err == nil {
300+
t.Fatal("expected error, got nil")
301+
}
302+
return
303+
}
304+
305+
if err != nil {
306+
t.Fatalf("unexpected error: %v", err)
307+
}
308+
309+
if arg != tt.expectedArg {
310+
t.Errorf("expected %q, got %q", tt.expectedArg, arg)
311+
}
312+
})
313+
}
314+
}

load.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,31 @@ func (g *SourceGenerator) Validate() error {
602602
// An empty generator is invalid
603603
return fmt.Errorf("no generator type specified")
604604
}
605+
if err := g.Gomod.validate(); err != nil {
606+
return errors.Wrap(err, "gomod generator")
607+
}
608+
609+
return nil
610+
}
611+
612+
func (g *GeneratorGomod) validate() error {
613+
// Validate gomod edits if present
614+
if g != nil && g.Edits != nil {
615+
var errs []error
616+
for i, req := range g.Edits.Require {
617+
if _, err := req.goModEditArg(); err != nil {
618+
errs = append(errs, errors.Wrapf(err, "require[%d]", i))
619+
}
620+
}
621+
for i, rep := range g.Edits.Replace {
622+
if _, err := rep.goModEditArg(); err != nil {
623+
errs = append(errs, errors.Wrapf(err, "replace[%d]", i))
624+
}
625+
}
626+
if err := goerrors.Join(errs...); err != nil {
627+
return errors.Wrap(err, "gomod edits")
628+
}
629+
}
605630
return nil
606631
}
607632

0 commit comments

Comments
 (0)