forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_test.go
101 lines (92 loc) · 2.27 KB
/
update_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
package update
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"github.com/cli/cli/api"
)
func TestCheckForUpdate(t *testing.T) {
scenarios := []struct {
Name string
CurrentVersion string
LatestVersion string
LatestURL string
ExpectsResult bool
}{
{
Name: "latest is newer",
CurrentVersion: "v0.0.1",
LatestVersion: "v1.0.0",
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
ExpectsResult: true,
},
{
Name: "current is prerelease",
CurrentVersion: "v1.0.0-pre.1",
LatestVersion: "v1.0.0",
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
ExpectsResult: true,
},
{
Name: "latest is current",
CurrentVersion: "v1.0.0",
LatestVersion: "v1.0.0",
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
ExpectsResult: false,
},
{
Name: "latest is older",
CurrentVersion: "v0.10.0-pre.1",
LatestVersion: "v0.9.0",
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
ExpectsResult: false,
},
}
for _, s := range scenarios {
t.Run(s.Name, func(t *testing.T) {
http := &api.FakeHTTP{}
client := api.NewClient(api.ReplaceTripper(http))
http.StubResponse(200, bytes.NewBufferString(fmt.Sprintf(`{
"tag_name": "%s",
"html_url": "%s"
}`, s.LatestVersion, s.LatestURL)))
rel, err := CheckForUpdate(client, tempFilePath(), "OWNER/REPO", s.CurrentVersion)
if err != nil {
t.Fatal(err)
}
if len(http.Requests) != 1 {
t.Fatalf("expected 1 HTTP request, got %d", len(http.Requests))
}
requestPath := http.Requests[0].URL.Path
if requestPath != "/repos/OWNER/REPO/releases/latest" {
t.Errorf("HTTP path: %q", requestPath)
}
if !s.ExpectsResult {
if rel != nil {
t.Fatal("expected no new release")
}
return
}
if rel == nil {
t.Fatal("expected to report new release")
}
if rel.Version != s.LatestVersion {
t.Errorf("Version: %q", rel.Version)
}
if rel.URL != s.LatestURL {
t.Errorf("URL: %q", rel.URL)
}
})
}
}
func tempFilePath() string {
file, err := ioutil.TempFile("", "")
if err != nil {
log.Fatal(err)
}
os.Remove(file.Name())
return file.Name()
}