-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupdate_test.go
More file actions
98 lines (88 loc) · 2.55 KB
/
update_test.go
File metadata and controls
98 lines (88 loc) · 2.55 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
package main
import (
"runtime"
"testing"
)
func TestParseVersion(t *testing.T) {
tests := []struct {
input string
major, minor, patch int
ok bool
}{
{"0.1.0", 0, 1, 0, true},
{"v1.2.3", 1, 2, 3, true},
{"10.20.30", 10, 20, 30, true},
{"0.0.0", 0, 0, 0, true},
{"1.2", 0, 0, 0, false},
{"1.2.3.4", 0, 0, 0, false},
{"abc", 0, 0, 0, false},
{"1.2.x", 0, 0, 0, false},
{"", 0, 0, 0, false},
{"v", 0, 0, 0, false},
}
for _, tt := range tests {
major, minor, patch, ok := parseVersion(tt.input)
if major != tt.major || minor != tt.minor || patch != tt.patch || ok != tt.ok {
t.Errorf("parseVersion(%q) = (%d, %d, %d, %v), want (%d, %d, %d, %v)",
tt.input, major, minor, patch, ok, tt.major, tt.minor, tt.patch, tt.ok)
}
}
}
func TestIsNewer(t *testing.T) {
tests := []struct {
latest, current string
want bool
}{
{"0.2.0", "0.1.0", true},
{"1.0.0", "0.9.9", true},
{"0.1.1", "0.1.0", true},
{"0.1.0", "0.1.0", false},
{"0.1.0", "0.2.0", false},
{"0.0.9", "0.1.0", false},
{"v1.0.0", "v0.9.0", true},
{"invalid", "0.1.0", false},
{"0.1.0", "invalid", false},
{"2.0.0", "1.99.99", true},
}
for _, tt := range tests {
got := isNewer(tt.latest, tt.current)
if got != tt.want {
t.Errorf("isNewer(%q, %q) = %v, want %v", tt.latest, tt.current, got, tt.want)
}
}
}
func TestBinaryAssetName(t *testing.T) {
name := binaryAssetName()
expected := "ask-" + runtime.GOOS + "-" + runtime.GOARCH
if name != expected {
t.Errorf("binaryAssetName() = %q, want %q", name, expected)
}
}
func TestDownloadURL(t *testing.T) {
release := &githubRelease{
TagName: "v0.2.0",
Assets: []githubAsset{
{Name: "ask-linux-amd64", BrowserDownloadURL: "https://example.com/ask-linux-amd64"},
{Name: "ask-darwin-arm64", BrowserDownloadURL: "https://example.com/ask-darwin-arm64"},
{Name: "ask-darwin-amd64", BrowserDownloadURL: "https://example.com/ask-darwin-amd64"},
},
}
url, err := downloadURL(release)
if err != nil {
t.Fatalf("downloadURL() unexpected error: %v", err)
}
expectedName := binaryAssetName()
expectedURL := "https://example.com/" + expectedName
if url != expectedURL {
t.Errorf("downloadURL() = %q, want %q", url, expectedURL)
}
// Test missing platform
emptyRelease := &githubRelease{
TagName: "v0.2.0",
Assets: []githubAsset{{Name: "ask-windows-amd64", BrowserDownloadURL: "https://example.com/ask-windows-amd64"}},
}
_, err = downloadURL(emptyRelease)
if err == nil {
t.Error("downloadURL() expected error for missing platform, got nil")
}
}