-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblocked_test.go
258 lines (242 loc) · 6.26 KB
/
blocked_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package gomodguard_test
import (
"reflect"
"strings"
"testing"
"github.com/ryancurrah/gomodguard"
)
func TestBlockedModuleIsAllowed(t *testing.T) {
var tests = []struct {
testName string
blockedModule gomodguard.BlockedModule
currentModuleName string
wantIsAllowed bool
}{
{
"blocked",
gomodguard.BlockedModule{
Recommendations: []string{
"github.com/somerecommended/module",
},
},
"github.com/ryancurrah/gomodguard",
false,
},
{
"allowed",
gomodguard.BlockedModule{
Recommendations: []string{
"github.com/ryancurrah/gomodguard",
},
},
"github.com/ryancurrah/gomodguard",
true,
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
isAllowed := tt.blockedModule.IsCurrentModuleARecommendation(tt.currentModuleName)
if isAllowed != tt.wantIsAllowed {
t.Errorf("got '%v' want '%v'", isAllowed, tt.wantIsAllowed)
}
})
}
}
func TestBlockedModuleMessage(t *testing.T) {
blockedWithNoRecommendation := "Some reason."
blockedWithRecommendation := "`github.com/somerecommended/module` is a recommended module. Some reason."
blockedWithRecommendations := "`github.com/somerecommended/module`, `github.com/someotherrecommended/module` " +
"and `github.com/someotherotherrecommended/module` are recommended modules. Some reason."
var tests = []struct {
testName string
blockedModule gomodguard.BlockedModule
currentModuleName string
wantMessage string
}{
{
"blocked with no recommendation",
gomodguard.BlockedModule{
Recommendations: []string{},
Reason: "Some reason.",
},
"github.com/ryancurrah/gomodguard",
blockedWithNoRecommendation,
},
{
"blocked with recommendation",
gomodguard.BlockedModule{
Recommendations: []string{
"github.com/somerecommended/module",
},
Reason: "Some reason.",
},
"github.com/ryancurrah/gomodguard",
blockedWithRecommendation,
},
{
"blocked with multiple recommendations",
gomodguard.BlockedModule{
Recommendations: []string{
"github.com/somerecommended/module",
"github.com/someotherrecommended/module",
"github.com/someotherotherrecommended/module",
},
Reason: "Some reason.",
},
"github.com/ryancurrah/gomodguard",
blockedWithRecommendations,
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
message := tt.blockedModule.Message()
if !strings.EqualFold(message, tt.wantMessage) {
t.Errorf("got '%s' want '%s'", message, tt.wantMessage)
}
})
}
}
func TestBlockedModuleHasRecommendations(t *testing.T) {
var tests = []struct {
testName string
blockedModule gomodguard.BlockedModule
wantIsAllowed bool
}{
{
"does not have recommendations",
gomodguard.BlockedModule{Recommendations: []string{}},
false,
},
{
"does have recommendations",
gomodguard.BlockedModule{
Recommendations: []string{
"github.com/ryancurrah/gomodguard",
},
},
true,
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
hasRecommendations := tt.blockedModule.HasRecommendations()
if hasRecommendations != tt.wantIsAllowed {
t.Errorf("got '%v' want '%v'", hasRecommendations, tt.wantIsAllowed)
}
})
}
}
func TestBlockedModulesGet(t *testing.T) {
var tests = []struct {
testName string
blockedModules gomodguard.BlockedModules
wantBlockedModules []string
}{
{
"get all blocked module names",
gomodguard.BlockedModules{
{
"github.com/someblocked/module": gomodguard.BlockedModule{
Recommendations: []string{
"github.com/ryancurrah/gomodguard",
},
},
},
},
[]string{"github.com/someblocked/module"},
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
blockedModules := tt.blockedModules.Get()
if !reflect.DeepEqual(blockedModules, tt.wantBlockedModules) {
t.Errorf("got '%v' want '%v'", blockedModules, tt.wantBlockedModules)
}
})
}
}
func TestBlockedVersionMessage(t *testing.T) {
blockedWithVersionConstraint := "version `1.0.0` is blocked because it does not meet the version constraint " +
"`1.0.0`. Some reason."
blockedWithVersionConstraintNoReason := "version `1.0.0` is blocked because it does not meet the version " +
"constraint `<= 1.0.0`."
var tests = []struct {
testName string
blockedVersion gomodguard.BlockedVersion
lintedModuleVersion string
wantMessage string
}{
{
"blocked with version constraint",
gomodguard.BlockedVersion{
Version: "1.0.0",
Reason: "Some reason.",
},
"1.0.0",
blockedWithVersionConstraint,
},
{
"blocked with version constraint and no reason",
gomodguard.BlockedVersion{Version: "<= 1.0.0"},
"1.0.0",
blockedWithVersionConstraintNoReason,
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
message := tt.blockedVersion.Message(tt.lintedModuleVersion)
if !strings.EqualFold(message, tt.wantMessage) {
t.Errorf("got '%s' want '%s'", message, tt.wantMessage)
}
})
}
}
func TestBlockedModulesGetBlockedModule(t *testing.T) {
var tests = []struct {
testName string
blockedModules gomodguard.BlockedModules
currentModuleName string
lintedModuleName string
wantIsAllowed bool
}{
{
"blocked",
gomodguard.BlockedModules{
{
"github.com/someblocked/module": gomodguard.BlockedModule{
Recommendations: []string{
"github.com/someother/module",
},
},
},
},
"github.com/ryancurrah/gomodguard",
"github.com/someblocked/module",
false,
},
{
"allowed",
gomodguard.BlockedModules{
{
"github.com/someblocked/module": gomodguard.BlockedModule{
Recommendations: []string{
"github.com/ryancurrah/gomodguard",
},
},
},
},
"github.com/ryancurrah/gomodguard",
"github.com/someblocked/module",
true,
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
blockedModule := tt.blockedModules.GetBlockReason(tt.lintedModuleName)
if blockedModule.IsCurrentModuleARecommendation(tt.currentModuleName) != tt.wantIsAllowed {
t.Errorf("got '%+v' want '%+v'", blockedModule.IsCurrentModuleARecommendation(tt.currentModuleName),
tt.wantIsAllowed)
}
})
}
}