Skip to content

Commit

Permalink
remove MaxVersion function
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy committed Jul 7, 2024
1 parent 1132771 commit d9085a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 42 deletions.
21 changes: 4 additions & 17 deletions internal/modproxy/modproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func (m *Module) MaxVersion(prefix string, pre bool) string {
if !pre && semver.Prerelease(v) != "" {
continue
}
max = MaxVersion(v, max)
if CompareVersion(max, v) < 0 {
max = v
}
}
return max
}
Expand All @@ -77,22 +79,7 @@ func IsNewerVersion(oldversion, newversion string, major bool) bool {
return semver.Compare(oldversion, newversion) < 0
}

// MaxVersion returns the larger of two versions according to semantic version precedence.
// Incompatible versions are considered lower than non-incompatible ones.
// Invalid versions are considered lower than valid ones.
// If both versions are invalid, the empty string is returned.
func MaxVersion(v, w string) string {
// sort by validity
if !semver.IsValid(v) && !semver.IsValid(w) {
return ""
}
if CompareVersion(v, w) == 1 {
return v
}
return w
}

// CompareVersion returns -1, 0, or 1 if v is less than, equal to, or greater than w.
// CompareVersion returns -1 if v < w, 1 if v > w, and 0 if v == w
// Incompatible versions are considered lower than non-incompatible ones.
// Invalid versions are considered lower than valid ones.
// If both versions are invalid, the empty string is returned.
Expand Down
31 changes: 6 additions & 25 deletions internal/modproxy/modproxy_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package modproxy

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -190,30 +189,6 @@ func TestModule(t *testing.T) {
}
}

func TestMaxVersion(t *testing.T) {
tests := []struct {
lo, hi string
}{
{"v0.0.0", "v0.0.1"},
{"v0.2.0", "v1.0.0"},
{"v3.0.0+incompatible", "v0.0.1"},
{"v3.0.0+incompatible", "v5.0.1+incompatible"},
{"", "v6.14.1+incompatible"},
{"invalid", ""},
}
for _, tt := range tests {
name := fmt.Sprintf("%s < %s", tt.lo, tt.hi)
t.Run(name, func(t *testing.T) {
if got := MaxVersion(tt.lo, tt.hi); got != tt.hi {
t.Fatalf("MaxVersion(%q, %q) = %q", tt.lo, tt.hi, got)
}
if got := MaxVersion(tt.hi, tt.lo); got != tt.hi {
t.Fatalf("MaxVersion(%q, %q) = %q", tt.hi, tt.lo, got)
}
})
}
}

func TestIsNewerVersion(t *testing.T) {
tests := []struct {
old, new string
Expand Down Expand Up @@ -263,6 +238,12 @@ func TestCompareVersion(t *testing.T) {
{v: "", w: "", want: 0},
{v: "v0.1.0", w: "bad", want: 1},
{v: "v0.0.0+incompatible", w: "v0.0.0", want: -1},
{v: "v0.0.0", w: "v0.0.1", want: -1},
{v: "v0.2.0", w: "v1.0.0", want: -1},
{v: "v3.0.0+incompatible", w: "v0.0.1", want: -1},
{v: "v3.0.0+incompatible", w: "v5.0.1+incompatible", want: -1},
{v: "", w: "v6.14.1+incompatible", want: -1},
{v: "invalid", w: ""},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
Expand Down

0 comments on commit d9085a8

Please sign in to comment.