feat: update semver matching

This commit is contained in:
deviantony
2024-10-04 07:57:31 +00:00
parent b40d22dc74
commit ababd63d97
8 changed files with 63 additions and 12 deletions

View File

@@ -11,7 +11,7 @@ import (
httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/response"
"github.com/coreos/go-semver/semver"
"github.com/Masterminds/semver/v3"
"github.com/rs/zerolog/log"
"github.com/segmentio/encoding/json"
)
@@ -119,7 +119,7 @@ func HasNewerVersion(currentVersion, latestVersion string) bool {
return false
}
return currentVersionSemver.LessThan(*latestVersionSemver)
return currentVersionSemver.LessThan(latestVersionSemver)
}
// @id Version

View File

@@ -18,6 +18,60 @@ import (
"github.com/stretchr/testify/assert"
)
func TestHasNewerVersion(t *testing.T) {
// Test cases
tests := []struct {
name string
currentVersion string
latestVersion string
expected bool
}{
{
name: "current version is less than latest version",
currentVersion: "2.22.0",
latestVersion: "v2.22.1",
expected: true,
},
{
name: "current version is equal to latest version",
currentVersion: "2.22.0",
latestVersion: "v2.22.0",
expected: false,
},
{
name: "current version is greater than latest version",
currentVersion: "v2.22.2",
latestVersion: "v2.22.1",
expected: false,
},
{
name: "invalid current version",
currentVersion: "invalid",
latestVersion: "v2.22.0",
expected: false,
},
{
name: "invalid latest version",
currentVersion: "2.22.0",
latestVersion: "invalid",
expected: false,
},
{
name: "both versions are invalid",
currentVersion: "invalid",
latestVersion: "invalid",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := HasNewerVersion(tt.currentVersion, tt.latestVersion)
assert.Equal(t, tt.expected, result)
})
}
}
func Test_getSystemVersion(t *testing.T) {
is := assert.New(t)