Skip to content

Commit

Permalink
Merge pull request #162 from nao1215/nchika/fix-issue160
Browse files Browse the repository at this point in the history
Change go module version compare logic
  • Loading branch information
nao1215 committed Jun 24, 2024
2 parents 8f1e3ec + de44f49 commit 09a63e3
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/fatih/color v1.17.0
github.com/gen2brain/beeep v0.0.0-20220909211152-5a9ec94374f6
github.com/google/go-cmp v0.6.0
github.com/hashicorp/go-version v1.7.0
github.com/mattn/go-colorable v0.1.13
github.com/nao1215/gorky v0.2.1
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down
21 changes: 20 additions & 1 deletion internal/goutil/goutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/fatih/color"
"github.com/hashicorp/go-version"
"github.com/nao1215/gorky/file"
"github.com/nao1215/gup/internal/print"
"github.com/pkg/errors"
Expand Down Expand Up @@ -138,8 +139,25 @@ func (p *Package) IsAlreadyUpToDate() bool {
)
}

// versionUpToDate return whether current version is up to date or not.
func versionUpToDate(current, available string) bool {
return current >= available
if current == "unknown" || available == "unknown" {
return false // unknown version is not up to date
}

currentVer, err := version.NewVersion(current)
if err != nil {
return false // invalid version is not up to date
}
availableVer, err := version.NewVersion(available)
if err != nil {
return false // invalid version is not up to date
}

if currentVer.GreaterThanOrEqual(availableVer) {
return true
}
return false
}

// NewGoPaths return GoPaths instance.
Expand Down Expand Up @@ -371,6 +389,7 @@ func GetPackageVersion(cmdName string) string {

var goVersionRegex = regexp.MustCompile(`(^|\s)(go[1-9]\S+)`)

// GetInstalledGoVersion return installed go version.
func GetInstalledGoVersion() (string, error) {
var stdout, stderr bytes.Buffer
cmd := exec.Command(goExe, "version")
Expand Down
125 changes: 125 additions & 0 deletions internal/goutil/goutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,131 @@ func TestIsAlreadyUpToDate_golden(t *testing.T) {
}
}

func TestVersionUpToDate_golden(t *testing.T) {
type args struct {
current string
available string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "basic test",
args: args{
current: "1.0.0",
available: "1.0.1",
},
want: false,
},
{
name: "unknown treated as newer",
args: args{
current: "1.0.0",
available: "unknown",
},
want: false,
},
{
name: "differing digits, single older",
args: args{
current: "1.2.0",
available: "1.11.5",
},
want: false,
},
{
name: "same version",
args: args{
current: "1.0.0",
available: "1.0.0",
},
want: true,
},
{
name: "current newer",
args: args{
current: "2.0.0",
available: "1.0.0",
},
want: true,
},
{
name: "current patch newer",
args: args{
current: "1.0.1",
available: "1.0.0",
},
want: true,
},
{
name: "current minor newer",
args: args{
current: "1.1.0",
available: "1.0.1",
},
want: true,
},
{
name: "different lengths, current newer",
args: args{
current: "1.0",
available: "0.9.9",
},
want: true,
},
{
name: "additional test, current older major version",
args: args{
current: "0.9.9",
available: "1.0.0",
},
want: false,
},
{
name: "additional test, current older minor version",
args: args{
current: "1.0.0",
available: "1.1.0",
},
want: false,
},
{
name: "additional test, current older patch version",
args: args{
current: "1.0.0",
available: "1.0.1",
},
want: false,
},
{
name: "additional test, current much older version",
args: args{
current: "1.0.0",
available: "2.0.0",
},
want: false,
},
{
name: "additional test, current much newer version",
args: args{
current: "2.0.0",
available: "1.0.0",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := versionUpToDate(tt.args.current, tt.args.available)
if got != tt.want {
t.Errorf("versionUpToDate() test_name=%s, got = %v, want %v", tt.name, got, tt.want)
}
})
}
}

// ============================================================================
// Methods
// ============================================================================
Expand Down

0 comments on commit 09a63e3

Please sign in to comment.