Skip to content

Commit

Permalink
fix(updater): version comparison mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
metafates committed Sep 18, 2022
1 parent b9146f6 commit 0b1f5f2
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to
[Semantic Versioning](https://semver.org).

## 3.9.1

- Fix version comparison mechanism for `update` command.
Not it compares each fragment separately (major, minor, patch) instead of comparing two versions as strings lexicographically.

## 3.9.0

- New sources: [Mangakakalot](https://mangakakalot.com) & [Mangapill](https://mangapill.com)
Expand Down
2 changes: 0 additions & 2 deletions constant/build.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package constant

const Version = "3.9.0"

var (
BuiltAt = "Unknown"
BuiltBy = "Unknown"
Expand Down
6 changes: 3 additions & 3 deletions constant/constant.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package constant

const (
Mangal = "mangal"
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
InstallScriptURL = "https://raw.githubusercontent.com/metafates/mangal/main/scripts/install"
Mangal = "mangal"
Version = "3.9.1"
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
)

const (
Expand Down
13 changes: 10 additions & 3 deletions updater/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ func Update() (err error) {
}

erase()
if constant.Version >= version {

comp, err := util.CompareVersions(constant.Version, version)
if err != nil {
return err
}

if comp >= 0 {
fmt.Printf(
"%s %s %s\n",
style.Green("Congrats!"),
Expand All @@ -50,6 +56,8 @@ func Update() (err error) {
return
}

fmt.Printf("%s %s is out! You're on %s\n", style.Bold(constant.Mangal), style.Cyan(version), style.Blue(constant.Version))

method := DetectInstallationMethod()

switch method {
Expand All @@ -74,8 +82,7 @@ func Update() (err error) {
return
}

fmt.Printf(`Updated.
fmt.Printf(`
%s
Report any bugs:
Expand Down
38 changes: 38 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,41 @@ func PrintErasable(msg string) (eraser func()) {
func Capitalize(s string) string {
return strings.ToUpper(s[:1]) + s[1:]
}

func CompareVersions(a, b string) (int, error) {
type version struct {
major, minor, patch int
}

parse := func(s string) (version, error) {
var v version
_, err := fmt.Sscanf(strings.TrimPrefix(s, "v"), "%d.%d.%d", &v.major, &v.minor, &v.patch)
return v, err
}

av, err := parse(a)
if err != nil {
return 0, err
}

bv, err := parse(b)
if err != nil {
return 0, err
}

for _, pair := range []lo.Tuple2[int, int]{
{av.major, bv.major},
{av.minor, bv.minor},
{av.patch, bv.patch},
} {
if pair.A > pair.B {
return 1, nil
}

if pair.A < pair.B {
return -1, nil
}
}

return 0, nil
}
84 changes: 84 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,87 @@ func TestSanitizeFilename(t *testing.T) {
func TestTerminalSize(t *testing.T) {
t.Skipf("Cannot test terminal size")
}

func TestCompareVersions(t *testing.T) {
Convey("Given two versions with different patches", t, func() {
v1, v2 := "1.0.0", "1.0.1"
Convey("When comparing "+v1+" to "+v2, func() {
result, err := CompareVersions(v1, v2)
Convey("Error should be nil", func() {
So(err, ShouldBeNil)
Convey("Then the result should be -1", func() {
So(result, ShouldEqual, -1)
})
})
})

Convey("When comparing "+v2+" to "+v1, func() {
result, err := CompareVersions(v2, v1)
Convey("Error should be nil", func() {
So(err, ShouldBeNil)
Convey("Then the result should be 1", func() {
So(result, ShouldEqual, 1)
})
})
})
})

Convey("Given two versions with different minor versions", t, func() {
v1, v2 := "1.0.0", "1.1.0"
Convey("When comparing "+v1+" to "+v2, func() {
result, err := CompareVersions(v1, v2)
Convey("Error should be nil", func() {
So(err, ShouldBeNil)
Convey("Then the result should be -1", func() {
So(result, ShouldEqual, -1)
})
})
})

Convey("When comparing "+v2+" to "+v1, func() {
result, err := CompareVersions(v2, v1)
Convey("Error should be nil", func() {
So(err, ShouldBeNil)
Convey("Then the result should be 1", func() {
So(result, ShouldEqual, 1)
})
})
})
})

Convey("Given two versions with different major versions", t, func() {
v1, v2 := "1.0.0", "2.0.0"
Convey("When comparing "+v1+" to "+v2, func() {
result, err := CompareVersions(v1, v2)
Convey("Error should be nil", func() {
So(err, ShouldBeNil)
Convey("Then the result should be -1", func() {
So(result, ShouldEqual, -1)
})
})
})

Convey("When comparing "+v2+" to "+v1, func() {
result, err := CompareVersions(v2, v1)
Convey("Error should be nil", func() {
So(err, ShouldBeNil)
Convey("Then the result should be 1", func() {
So(result, ShouldEqual, 1)
})
})
})
})

Convey("Given two same versions", t, func() {
v1, v2 := "1.0.0", "1.0.0"
Convey("When comparing "+v1+" to "+v2, func() {
result, err := CompareVersions(v1, v2)
Convey("Error should be nil", func() {
So(err, ShouldBeNil)
Convey("Then the result should be 0", func() {
So(result, ShouldEqual, 0)
})
})
})
})
}

0 comments on commit 0b1f5f2

Please sign in to comment.