-
Notifications
You must be signed in to change notification settings - Fork 207
/
version_test.go
304 lines (278 loc) · 6.82 KB
/
version_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
299
300
301
302
303
304
package version
import (
"crypto/sha1"
"crypto/sha256"
"fmt"
"io"
"os"
"reflect"
"testing"
"time"
"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
"github.com/voidint/g/pkg/errs"
)
func TestSemantify(t *testing.T) {
type args struct {
vname string
}
tests := []struct {
name string
args args
wantString string
wantErr bool
}{
{
name: "普通三位版本号",
args: args{vname: "1.21.4"},
wantString: "1.21.4",
wantErr: false,
},
{
name: "普通两位版本号",
args: args{vname: "1.18"},
wantString: "1.18.0",
wantErr: false,
},
{
name: "1.21.0版本号",
args: args{vname: "1.21.0"},
wantString: "1.21.0",
wantErr: false,
},
{
name: "alpha",
args: args{vname: "1.19alpha1"},
wantString: "1.19.0-alpha1",
wantErr: false,
},
{
name: "beta",
args: args{vname: "1.19beta1"},
wantString: "1.19.0-beta1",
wantErr: false,
},
{
name: "rc",
args: args{vname: "1.21rc4"},
wantString: "1.21.0-rc4",
wantErr: false,
},
{
name: "无效版本号",
args: args{vname: "abcdef"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Semantify(tt.args.vname)
if (err != nil) != tt.wantErr {
t.Errorf("Semantify() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil {
if !reflect.DeepEqual(got.String(), tt.wantString) {
t.Errorf("Semantify().String() = %v, want %v", got.String(), tt.wantString)
}
}
})
}
}
func TestNew(t *testing.T) {
t.Run("合法的版本号", func(t *testing.T) {
vname := "1.21.4"
opts := []func(v *Version){nil, nil, nil}
v, err := New(vname, opts...)
assert.Nil(t, err)
assert.True(t, reflect.DeepEqual(v, &Version{name: "1.21.4", sv: semver.MustParse("1.21.4")}))
})
t.Run("非法的版本号", func(t *testing.T) {
vname := "1.2.3.4"
v, err := New(vname)
assert.NotNil(t, err)
assert.True(t, errs.IsMalformedVersion(err))
assert.Nil(t, v)
})
}
func TestMustNew(t *testing.T) {
t.Run("合法的版本号", func(t *testing.T) {
vname := "1.21.4"
v := MustNew(vname)
assert.True(t, reflect.DeepEqual(v, &Version{name: "1.21.4", sv: semver.MustParse("1.21.4")}))
})
t.Run("非法的版本号", func(t *testing.T) {
vname := "1.2.3.4"
assert.Panics(t, func() {
MustNew(vname)
})
})
}
func TestVersion_FindPackages(t *testing.T) {
vs, err := genVersions()
if err != nil {
assert.Nil(t, err)
}
t.Run("查找到唯一的软件包", func(t *testing.T) {
v1214 := vs[len(vs)-1] // 1.21.4
pkgs, err := v1214.FindPackages(ArchiveKind, "darwin", "arm64")
assert.Nil(t, err)
assert.Equal(t, 1, len(pkgs))
assert.Equal(t, pkgs[0].FileName, "go1.21.4.darwin-arm64.tar.gz")
assert.Equal(t, pkgs[0].URL, "https://golang.google.cn/dl/go1.21.4.darwin-arm64.tar.gz")
assert.Equal(t, pkgs[0].Kind, ArchiveKind)
assert.Equal(t, pkgs[0].OS, "macOS")
assert.Equal(t, pkgs[0].Arch, "ARM64")
assert.Equal(t, pkgs[0].Size, "62MB")
assert.Equal(t, pkgs[0].Checksum, "8b7caf2ac60bdff457dba7d4ff2a01def889592b834453431ae3caecf884f6a5")
assert.Equal(t, pkgs[0].Algorithm, "SHA256")
})
t.Run("查找到多个软件包", func(t *testing.T) {
v1214 := vs[len(vs)-1] // 1.21.4
pkgs, err := v1214.FindPackages(ArchiveKind, "linux", "arm")
assert.Nil(t, err)
assert.Equal(t, 2, len(pkgs))
})
t.Run("未查找到软件包", func(t *testing.T) {
v1214 := vs[len(vs)-1] // 1.21.4
pkgs, err := v1214.FindPackages(ArchiveKind, "darwin", "ppc64")
assert.True(t, errs.IsPackageNotFound(err))
assert.Equal(t, 0, len(pkgs))
})
}
func TestVersion_Packages(t *testing.T) {
filename := "go1.21.4.darwin-arm64.tar.gz"
url := "https://golang.google.cn/dl/go1.21.4.darwin-arm64.tar.gz"
kind := ArchiveKind
os := "macOS"
arch := "ARM64"
size := "62MB"
checksum := "8b7caf2ac60bdff457dba7d4ff2a01def889592b834453431ae3caecf884f6a5"
algorithm := "SHA256"
tests := []struct {
name string
v *Version
want []Package
}{
{
name: "软件包列表为空",
v: MustNew("1"),
want: make([]Package, 0),
},
{
name: "软件包列表非空",
v: MustNew("1.21.4", WithPackages([]*Package{
{
FileName: filename,
URL: url,
Kind: kind,
OS: os,
Arch: arch,
Size: size,
Checksum: checksum,
Algorithm: algorithm,
},
})),
want: []Package{
{
FileName: filename,
URL: url,
Kind: kind,
OS: os,
Arch: arch,
Size: size,
Checksum: checksum,
Algorithm: algorithm,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.v.Packages(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Version.Packages() = %v, want %v", got, tt.want)
}
})
}
}
func TestVersion_MatchConstraint(t *testing.T) {
cs, err := semver.NewConstraint("~1.18")
assert.Nil(t, err)
assert.NotNil(t, cs)
tests := []struct {
name string
v *Version
cs *semver.Constraints
want bool
}{
{
name: "不匹配约束",
v: MustNew("1.21.4"),
cs: cs,
want: false,
},
{
name: "匹配约束",
v: MustNew("1.18.4"),
cs: cs,
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.v.MatchConstraint(tt.cs); got != tt.want {
t.Errorf("Version.MatchConstraint() = %v, want %v", got, tt.want)
}
})
}
}
func TestVerifyChecksum(t *testing.T) {
t.Run("检查安装包校验和", func(t *testing.T) {
filename := fmt.Sprintf("%d.txt", time.Now().Unix())
f, err := os.Create(filename)
assert.Nil(t, err)
defer os.Remove(filename)
defer f.Close()
_, err = f.WriteString("hello 世界!")
assert.Nil(t, err)
t.Run("SHA256", func(t *testing.T) {
_, _ = f.Seek(0, 0)
h := sha256.New()
_, err = io.Copy(h, f)
assert.Nil(t, err)
pkg := &Package{
Algorithm: "SHA256",
Checksum: fmt.Sprintf("%x", h.Sum(nil)),
}
assert.Nil(t, pkg.VerifyChecksum(filename))
})
t.Run("校验和不匹配", func(t *testing.T) {
_, _ = f.Seek(0, 0)
h := sha1.New()
_, err = io.Copy(h, f)
assert.Nil(t, err)
pkg := &Package{
Algorithm: "SHA1",
Checksum: fmt.Sprintf("%x", h.Sum(nil)),
}
assert.Nil(t, pkg.VerifyChecksum(filename))
})
t.Run("SHA1", func(t *testing.T) {
_, _ = f.Seek(0, 0)
h := sha1.New()
_, err = io.Copy(h, f)
assert.Nil(t, err)
pkg := &Package{
Algorithm: "SHA1",
Checksum: "hello",
}
assert.Equal(t, errs.ErrChecksumNotMatched, pkg.VerifyChecksum(filename))
})
t.Run("SHA1024", func(t *testing.T) {
pkg := &Package{
Algorithm: "SHA1024",
}
assert.Equal(t, errs.ErrUnsupportedChecksumAlgorithm, pkg.VerifyChecksum(filename))
})
})
}