-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_test.go
More file actions
105 lines (100 loc) · 2.58 KB
/
update_test.go
File metadata and controls
105 lines (100 loc) · 2.58 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
package main
import "testing"
func TestCompareSemver(t *testing.T) {
tests := []struct {
a, b string
want int
}{
{"v1.0.0", "v1.0.0", 0},
{"1.0.0", "1.0.0", 0},
{"v1.0.0", "v1.0.1", -1},
{"v1.0.1", "v1.0.0", 1},
{"v1.0.0", "v1.1.0", -1},
{"v2.0.0", "v1.9.9", 1},
{"v0.1.0", "v0.2.0", -1},
{"v0.2.0", "v0.1.0", 1},
{"v10.0.0", "v9.0.0", 1}, // numeric compare, not lexicographic
{"v1.0.0-rc1", "v1.0.0", 0}, // pre-release stripped
{"dev", "v1.0.0", 0}, // unparseable -> 0
{"v1.0.0", "dev", 0},
{"garbage", "more garbage", 0},
}
for _, tt := range tests {
got := compareSemver(tt.a, tt.b)
if got != tt.want {
t.Errorf("compareSemver(%q, %q) = %d, want %d", tt.a, tt.b, got, tt.want)
}
}
}
func TestIsValidRepo(t *testing.T) {
tests := []struct {
repo string
want bool
}{
{"", false},
{"romkazor/IncottHIDApp", true},
{"user/repo", true},
{"user.name/repo.name", true},
{"user_1/repo-2", true},
{"no-slash", false},
{"/missing-owner", false},
{"missing-repo/", false},
{"too/many/slashes", false},
{"../../evil/path", false},
{"user/repo?query", false},
{"user/repo#frag", false},
{"user/repo@branch", false},
{"user /repo", false}, // space not allowed
}
for _, tt := range tests {
got := isValidRepo(tt.repo)
if got != tt.want {
t.Errorf("isValidRepo(%q) = %v, want %v", tt.repo, got, tt.want)
}
}
}
func TestBuildReleasesAPIURL(t *testing.T) {
tests := []struct {
repo string
want string
}{
{"romkazor/IncottHIDApp", "https://api.github.com/repos/romkazor/IncottHIDApp/releases/latest"},
{"user/MyFork", "https://api.github.com/repos/user/MyFork/releases/latest"},
{"", ""},
{"garbage", ""},
{"too/many/slashes", ""},
}
for _, tt := range tests {
got := buildReleasesAPIURL(tt.repo)
if got != tt.want {
t.Errorf("buildReleasesAPIURL(%q) = %q, want %q", tt.repo, got, tt.want)
}
}
}
func TestParseSemver(t *testing.T) {
tests := []struct {
v string
want [3]int
wantOK bool
}{
{"v1.2.3", [3]int{1, 2, 3}, true},
{"1.2.3", [3]int{1, 2, 3}, true},
{"v0.0.0", [3]int{0, 0, 0}, true},
{"v10.20.30", [3]int{10, 20, 30}, true},
{"v1.2.3-rc1", [3]int{1, 2, 3}, true},
{"v1.2.3+build", [3]int{1, 2, 3}, true},
{"v1.2", [3]int{}, false},
{"v1", [3]int{}, false},
{"dev", [3]int{}, false},
{"", [3]int{}, false},
}
for _, tt := range tests {
got, ok := parseSemver(tt.v)
if ok != tt.wantOK {
t.Errorf("parseSemver(%q) ok = %v, want %v", tt.v, ok, tt.wantOK)
}
if ok && got != tt.want {
t.Errorf("parseSemver(%q) = %v, want %v", tt.v, got, tt.want)
}
}
}