Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package updater
import (
"encoding/json"
"net/http"
"strings"
"time"

"github.com/achannarasappa/ticker/v5/internal/cache"
Expand Down Expand Up @@ -45,9 +46,11 @@ func Check(currentVersion, releasesURL, cacheFilePath string, fs afero.Fs) strin
}

// newerVersion returns latest when it differs from currentVersion, otherwise an
// empty string.
// empty string. The leading "v" is ignored on both sides: release binaries are
// built with a version stripped of that prefix while the GitHub tag keeps it,
// so comparing the raw strings would report an update on every release.
func newerVersion(latest, currentVersion string) string {
if latest != currentVersion {
if strings.TrimPrefix(latest, "v") != strings.TrimPrefix(currentVersion, "v") {
return latest
}

Expand Down
20 changes: 20 additions & 0 deletions internal/updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package updater_test

import (
"net/http"
"strings"
"time"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -116,4 +117,23 @@ var _ = Describe("Check", func() {
Expect(output).To(BeEmpty())
})
})

When("the current version is a release build without the v prefix", func() {
// Release binaries are built with the version stripped of its leading
// "v" while the GitHub tag keeps it, so the two must still compare equal.
It("should return empty when the latest version only differs by that prefix", func() {
server.SetHandler(0,
ghttp.CombineHandlers(
ghttp.RespondWithJSONEncoded(http.StatusOK, map[string]string{"tag_name": currentVersion}),
),
)
output := updater.Check(strings.TrimPrefix(currentVersion, "v"), server.URL()+"/releases/latest", cacheFilePath, fs)
Expect(output).To(BeEmpty())
})

It("should still return the latest version when a newer one is available", func() {
output := updater.Check(strings.TrimPrefix(currentVersion, "v"), server.URL()+"/releases/latest", cacheFilePath, fs)
Expect(output).To(Equal(latestVersion))
})
})
})