-
Notifications
You must be signed in to change notification settings - Fork 213
/
finder_test.go
298 lines (282 loc) · 6.68 KB
/
finder_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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package version
import (
"encoding/json"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/voidint/g/pkg/errs"
)
func genVersions() ([]*Version, error) {
data, err := os.ReadFile("./testdata/versions.json")
if err != nil {
return nil, err
}
var items []*struct {
Name string `json:"version"`
Packages []*Package `json:"packages"`
}
if err = json.Unmarshal(data, &items); err != nil {
return nil, err
}
vs := make([]*Version, 0, len(items))
for _, item := range items {
vs = append(vs, MustNew(item.Name, WithPackages(item.Packages)))
}
return vs, nil
}
func TestFinder_Find(t *testing.T) {
vs, err := genVersions()
if err != nil {
assert.Nil(t, err)
}
type fields struct {
goos string
goarch string
items []*Version
}
type args struct {
vname string
}
f := fields{
goos: "darwin",
goarch: "arm64",
items: vs,
}
fdr := NewFinder(f.items, WithFinderGoos(f.goos), WithFinderGoarch(f.goarch))
tests := []struct {
name string
fields fields
args args
want *Version
wantErr bool
}{
{
name: "1、具体版本号",
fields: f,
args: args{vname: "1.21.4"},
want: vs[len(vs)-1],
wantErr: false,
},
{
name: "2、最新版本",
fields: f,
args: args{vname: Latest},
want: vs[len(vs)-1],
wantErr: false,
},
{
name: "3.1、通配符x",
fields: f,
args: args{vname: "1.20.x"},
want: fdr.MustFind("1.20.11"),
wantErr: false,
},
{
name: "3.2、通配符X",
fields: f,
args: args{vname: "1.20.X"},
want: fdr.MustFind("1.20.11"),
wantErr: false,
},
{
name: "3.3、通配符*",
fields: f,
args: args{vname: "1.20.*"},
want: fdr.MustFind("1.20.11"),
wantErr: false,
},
{
name: "3.4、通配符*",
fields: f,
args: args{vname: "1.*"},
want: fdr.MustFind("1.21.4"),
wantErr: false,
},
{
name: "4.1、^匹配最新的次版本号",
fields: f,
args: args{vname: "^1"},
want: fdr.MustFind("1.21.4"),
wantErr: false,
},
{
name: "4.2、^匹配最新的次版本号",
fields: f,
args: args{vname: "^1.18"},
want: fdr.MustFind("1.21.4"),
wantErr: false,
},
{
name: "4.3、^匹配最新的次版本号",
fields: f,
args: args{vname: "^1.18.10"},
want: fdr.MustFind("1.21.4"),
wantErr: false,
},
{
name: "5.1、~匹配某个次版本号的最新修订号",
fields: f,
args: args{vname: "~1.18"},
want: fdr.MustFind("1.18.10"),
wantErr: false,
},
{
name: "5.2、~匹配某个次版本号的最新修订版",
fields: f,
args: args{vname: "~1.18.2"},
want: fdr.MustFind("1.18.10"),
wantErr: false,
},
{
name: "6、>大于某个版本",
fields: f,
args: args{vname: ">1.18.2"},
want: fdr.MustFind("1.21.4"),
wantErr: false,
},
{
name: "7.1、<小于某个版本",
fields: f,
args: args{vname: "<1.18.10"},
want: fdr.MustFind("1.18.9"),
wantErr: false,
},
{
name: "7.2、<小于某个版本",
fields: f,
args: args{vname: "<1.18"},
want: fdr.MustFind("1.17.13"),
wantErr: false,
},
{
name: "7.3、<小于某个版本但没有合适的软件包",
fields: f,
args: args{vname: "<1.16"},
want: nil,
wantErr: true,
},
{
name: "8.1、匹配目标版本区间内的最新版本",
fields: f,
args: args{vname: "1.16 - 1.21"},
want: fdr.MustFind("1.21.4"),
wantErr: false,
},
{
name: "8.2、匹配目标版本区间内的最新版本",
fields: f,
args: args{vname: "1.16 - 1.20.7"},
want: fdr.MustFind("1.20.7"),
wantErr: false,
},
{
name: "9、非法版本号",
fields: f,
args: args{vname: "voidint"},
want: nil,
wantErr: true,
},
{
name: "10、不存在的版本号",
fields: f,
args: args{vname: "1.11.111"},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := fdr.Find(tt.args.vname)
if (err != nil) != tt.wantErr {
t.Errorf("Finder.Find() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Finder.Find() = %v, want %v", got, tt.want)
}
})
}
}
func TestFinder_MustFind(t *testing.T) {
vs, err := genVersions()
if err != nil {
assert.Nil(t, err)
}
fdr := NewFinder(vs,
WithFinderPackageKind(ArchiveKind),
WithFinderGoos("darwin"),
WithFinderGoarch("arm64"),
)
t.Run("查找到版本", func(t *testing.T) {
v := fdr.MustFind("1.21.4")
assert.NotNil(t, v)
assert.Equal(t, v.Name(), "1.21.4")
})
t.Run("查找不到版本", func(t *testing.T) {
assert.Panics(t, func() {
fdr.MustFind("~1.15")
})
})
}
func TestFinder_findLatest(t *testing.T) {
vs := []*Version{
MustNew("1.16.1", WithPackages([]*Package{
{
FileName: "go1.16.1.darwin-amd64.tar.gz",
URL: "https://golang.google.cn/dl/go1.16.1.darwin-amd64.tar.gz",
Kind: ArchiveKind,
OS: "macOS",
Arch: "x86-64",
Size: "124MB",
Checksum: "a760929667253cdaa5b10117f536a912be2b0be1006215ff86e957f98f76fd58",
Algorithm: "SHA256",
},
{
FileName: "go1.16.1.darwin-arm64.tar.gz",
URL: "https://golang.google.cn/dl/go1.16.1.darwin-arm64.tar.gz",
Kind: ArchiveKind,
OS: "macOS",
Arch: "ARM64",
Size: "120MB",
Checksum: "de2847f49faac2d0608b4afc324cbb3029a496c946db616c294d26082e45f32d",
Algorithm: "SHA256",
},
})),
}
tests := []struct {
name string
fdr *Finder
wantV *Version
wantErr error
}{
{
name: "查找器中版本列表为空",
fdr: NewFinder(nil, WithFinderPackageKind(ArchiveKind), WithFinderGoos("darwin"), WithFinderGoarch("arm64")),
wantV: nil,
wantErr: errs.NewVersionNotFoundError(Latest, "darwin", "arm64"),
},
{
name: "查找器中版本列表非空且软件包亦匹配",
fdr: NewFinder(vs, WithFinderPackageKind(ArchiveKind), WithFinderGoos("darwin"), WithFinderGoarch("arm64")),
wantV: vs[0],
wantErr: nil,
},
{
name: "查找器中版本列表非空但未找到匹配的软件包",
fdr: NewFinder(vs, WithFinderPackageKind(InstallerKind), WithFinderGoos("windows"), WithFinderGoarch("arm64")),
wantV: nil,
wantErr: errs.NewPackageNotFoundError(string(InstallerKind), "windows", "arm64"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v, err := tt.fdr.findLatest()
if err != nil {
assert.Equal(t, tt.wantErr.Error(), err.Error())
} else {
assert.Equal(t, tt.wantV.Name(), v.Name())
}
})
}
}