cli/update/update_test.go
Mislav Marohnić 67f0cf3ce3 Improvements to update notifier
- Only report an update available if the version number of the release
  is greater than the current version

- Removes `command` dependency from `update` package; instead, pass
  current version as an argument

- Remove `brew upgrade` instructions since we can't be certain that gh
  was installed via Homebrew in the first place.

- Does not check for updates unless stderr is a tty

- Preserve stderr color output even if stdout is not a tty

- Fixes stderr color output on Windows
2019-12-04 14:47:27 +01:00

89 lines
2.1 KiB
Go

package update
import (
"bytes"
"fmt"
"testing"
"github.com/github/gh-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, "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)
}
})
}
}