-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathgenerator_gomod_test.go
More file actions
158 lines (144 loc) · 3.57 KB
/
generator_gomod_test.go
File metadata and controls
158 lines (144 loc) · 3.57 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
package dalec
import (
"encoding/json"
"testing"
"github.com/goccy/go-yaml"
)
func TestGomodReplaceUnmarshal(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
unmarshal func([]byte, interface{}) error
expectErr bool
expectedOld string
expectedNew string
}{
{
name: "YAML string format",
input: `"github.com/stretchr/testify => github.com/stretchr/testify@v1.8.0"`,
unmarshal: yaml.Unmarshal,
expectErr: false,
expectedOld: "github.com/stretchr/testify",
expectedNew: "github.com/stretchr/testify@v1.8.0",
},
{
name: "YAML struct format",
input: `
old: github.com/cpuguy83/tar2go
new: github.com/cpuguy83/tar2go@v0.3.1
`,
unmarshal: yaml.Unmarshal,
expectErr: false,
expectedOld: "github.com/cpuguy83/tar2go",
expectedNew: "github.com/cpuguy83/tar2go@v0.3.1",
},
{
name: "JSON string format",
input: `"github.com/stretchr/testify => github.com/stretchr/testify@v1.8.0"`,
unmarshal: json.Unmarshal,
expectErr: false,
expectedOld: "github.com/stretchr/testify",
expectedNew: "github.com/stretchr/testify@v1.8.0",
},
{
name: "JSON struct format",
input: `{"old":"github.com/cpuguy83/tar2go","new":"github.com/cpuguy83/tar2go@v0.3.1"}`,
unmarshal: json.Unmarshal,
expectErr: false,
expectedOld: "github.com/cpuguy83/tar2go",
expectedNew: "github.com/cpuguy83/tar2go@v0.3.1",
},
{
name: "invalid string format - no arrow",
input: `"github.com/stretchr/testify"`,
unmarshal: yaml.Unmarshal,
expectErr: true,
},
{
name: "invalid struct format - missing new",
input: `{"old":"github.com/cpuguy83/tar2go"}`,
unmarshal: json.Unmarshal,
expectErr: true,
},
{
name: "invalid struct format - empty old",
input: `{"old":"","new":"github.com/cpuguy83/tar2go@v0.3.1"}`,
unmarshal: json.Unmarshal,
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var repl GomodReplace
err := tt.unmarshal([]byte(tt.input), &repl)
if tt.expectErr {
if err == nil {
t.Fatal("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
if repl.Original != tt.expectedOld {
t.Errorf("expected Old=%q, got %q", tt.expectedOld, repl.Original)
}
if repl.Update != tt.expectedNew {
t.Errorf("expected New=%q, got %q", tt.expectedNew, repl.Update)
}
})
}
}
func TestGomodReplaceGoModEditArg(t *testing.T) {
t.Parallel()
tests := []struct {
name string
repl GomodReplace
expectErr bool
expectedArg string
}{
{
name: "valid replace",
repl: GomodReplace{
Original: "github.com/stretchr/testify",
Update: "github.com/stretchr/testify@v1.8.0",
},
expectErr: false,
expectedArg: "github.com/stretchr/testify=github.com/stretchr/testify@v1.8.0",
},
{
name: "empty old",
repl: GomodReplace{
Original: "",
Update: "github.com/stretchr/testify@v1.8.0",
},
expectErr: true,
},
{
name: "empty new",
repl: GomodReplace{
Original: "github.com/stretchr/testify",
Update: "",
},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
arg, err := tt.repl.goModEditArg()
if tt.expectErr {
if err == nil {
t.Fatal("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if arg != tt.expectedArg {
t.Errorf("expected %q, got %q", tt.expectedArg, arg)
}
})
}
}