diff --git a/update/update.go b/update/update.go index bf89a12e8..a44f6da29 100644 --- a/update/update.go +++ b/update/update.go @@ -24,22 +24,26 @@ type StateEntry struct { // CheckForUpdate checks whether this software has had a newer release on GitHub func CheckForUpdate(client *api.Client, stateFilePath, repo, currentVersion string) (*ReleaseInfo, error) { - latestRelease, err := getLatestReleaseInfo(client, stateFilePath, repo, currentVersion) + releaseInfo, err := getLatestReleaseInfo(client, stateFilePath, repo) if err != nil { return nil, err } - if versionGreaterThan(latestRelease.Version, currentVersion) { - return latestRelease, nil + if releaseInfo == nil { + return nil, nil + } + + if versionGreaterThan(releaseInfo.LatestRelease.Version, currentVersion) { + return &releaseInfo.LatestRelease, nil } return nil, nil } -func getLatestReleaseInfo(client *api.Client, stateFilePath, repo, currentVersion string) (*ReleaseInfo, error) { +func getLatestReleaseInfo(client *api.Client, stateFilePath, repo string) (*StateEntry, error) { stateEntry, err := getStateEntry(stateFilePath) if err == nil && time.Since(stateEntry.CheckedForUpdateAt).Hours() < 24 { - return &stateEntry.LatestRelease, nil + return nil, nil } var latestRelease ReleaseInfo @@ -53,7 +57,12 @@ func getLatestReleaseInfo(client *api.Client, stateFilePath, repo, currentVersio return nil, err } - return &latestRelease, nil + stateEntry, err = getStateEntry(stateFilePath) + if err != nil { + return nil, err + } + + return stateEntry, nil } func getStateEntry(stateFilePath string) (*StateEntry, error) { diff --git a/update/update_test.go b/update/update_test.go index 2fcb2d6ab..3ea6cb533 100644 --- a/update/update_test.go +++ b/update/update_test.go @@ -52,6 +52,9 @@ func TestCheckForUpdate(t *testing.T) { for _, s := range scenarios { t.Run(s.Name, func(t *testing.T) { + stateFilePath := tempFilePath() + defer os.Remove(stateFilePath) + http := &httpmock.Registry{} client := api.NewClient(api.ReplaceTripper(http)) http.StubResponse(200, bytes.NewBufferString(fmt.Sprintf(`{ @@ -59,7 +62,7 @@ func TestCheckForUpdate(t *testing.T) { "html_url": "%s" }`, s.LatestVersion, s.LatestURL))) - rel, err := CheckForUpdate(client, tempFilePath(), "OWNER/REPO", s.CurrentVersion) + rel, err := CheckForUpdate(client, stateFilePath, "OWNER/REPO", s.CurrentVersion) if err != nil { t.Fatal(err) } @@ -93,10 +96,18 @@ func TestCheckForUpdate(t *testing.T) { } func tempFilePath() string { - file, err := ioutil.TempFile("", "") + content := []byte( + `checked_for_update_at: 2020-11-22T09:53:32.609610344-03:00 +latest_release: + version: v1.1.0 + url: https://github.com/cli/cli/releases/tag/v1.1.0`) + file, err := ioutil.TempFile("", "state*.yml") if err != nil { log.Fatal(err) } - os.Remove(file.Name()) + + if _, err := file.Write(content); err != nil { + log.Fatal(err) + } return file.Name() }