From 4716d79a7efed5cc90cb9f622a535041703dcc9f Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Wed, 5 May 2021 15:08:14 +0800 Subject: [PATCH] fix(global): version compare #877 --- .github/workflows/golint.yml | 9 +++++++++ .github/workflows/suggester.yml | 9 +++++++++ global/all_test.go | 27 +++++++++++++++++++++++++++ global/param.go | 4 ++-- go.mod | 1 + go.sum | 1 + 6 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 global/all_test.go diff --git a/.github/workflows/golint.yml b/.github/workflows/golint.yml index 7f44693..7a5abd3 100644 --- a/.github/workflows/golint.yml +++ b/.github/workflows/golint.yml @@ -9,6 +9,15 @@ jobs: steps: - uses: actions/checkout@v2 + - name: Setup Go environment + uses: actions/setup-go@v2.1.3 + with: + go-version: 1.16 + + - name: Tests + run: | + run: go test $(go list ./...) + - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: diff --git a/.github/workflows/suggester.yml b/.github/workflows/suggester.yml index 2292082..331952b 100644 --- a/.github/workflows/suggester.yml +++ b/.github/workflows/suggester.yml @@ -9,6 +9,15 @@ jobs: steps: - uses: actions/checkout@v2 + - name: Setup Go environment + uses: actions/setup-go@v2.1.3 + with: + go-version: 1.16 + + - name: Tests + run: | + run: go test $(go list ./...) + - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: diff --git a/global/all_test.go b/global/all_test.go new file mode 100644 index 0000000..57b918a --- /dev/null +++ b/global/all_test.go @@ -0,0 +1,27 @@ +package global + +import ( + "strconv" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestVersionNameCompare(t *testing.T) { + var tests = [...]struct { + current string + remote string + expected bool + }{ + {"v0.9.29-fix2", "v0.9.29-fix2", false}, + {"v0.9.29-fix1", "v0.9.29-fix2", true}, + {"v0.9.29-fix2", "v0.9.29-fix1", false}, + {"v0.9.29-fix2", "v0.9.30", true}, + {"v1.0.0-beta1", "v0.9.40-fix5", false}, // issue #877 + } + for i := 0; i < len(tests); i++ { + t.Run("test case "+strconv.Itoa(i), func(t *testing.T) { + assert.Equal(t, tests[i].expected, VersionNameCompare(tests[i].current, tests[i].remote)) + }) + } +} diff --git a/global/param.go b/global/param.go index 893d5cd..f263727 100644 --- a/global/param.go +++ b/global/param.go @@ -79,8 +79,8 @@ func VersionNameCompare(current, remote string) bool { for i := 0; i < int(math.Min(float64(len(cur)), float64(len(re)))); i++ { curSub, _ := strconv.Atoi(cur[i][0]) reSub, _ := strconv.Atoi(re[i][0]) - if curSub < reSub { - return true + if curSub != reSub { + return curSub < reSub } } return len(cur) < len(re) diff --git a/go.mod b/go.mod index 831271d..ca9aff4 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.8.1 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect + github.com/stretchr/testify v1.7.0 // indirect github.com/syndtr/goleveldb v1.0.0 github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 github.com/tidwall/gjson v1.7.5 diff --git a/go.sum b/go.sum index 470a230..4d6f74e 100644 --- a/go.sum +++ b/go.sum @@ -110,6 +110,7 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=