Repository: lima-vm/lima
Pull request #2461 (2024-07-02 07:46:52)

pkg/store: split pkg/version/versionutil
Split from:
- #1953


Patch:
diff --git a/pkg/store/instance.go b/pkg/store/instance.go
index 79c2e809..dc2d8ab9 100644
--- a/pkg/store/instance.go
+++ b/pkg/store/instance.go
@@ -16,13 +16,13 @@ import (
 	"text/template"
 	"time"
 
-	"github.com/coreos/go-semver/semver"
 	"github.com/docker/go-units"
 	hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
 	"github.com/lima-vm/lima/pkg/limayaml"
 	"github.com/lima-vm/lima/pkg/store/dirnames"
 	"github.com/lima-vm/lima/pkg/store/filenames"
 	"github.com/lima-vm/lima/pkg/textutil"
+	"github.com/lima-vm/lima/pkg/version/versionutil"
 	"github.com/sirupsen/logrus"
 )
 
@@ -174,7 +174,7 @@ func Inspect(instName string) (*Instance, error) {
 	limaVersionFile := filepath.Join(instDir, filenames.LimaVersion)
 	if version, err := os.ReadFile(limaVersionFile); err == nil {
 		inst.LimaVersion = strings.TrimSpace(string(version))
-		if _, err = parseLimaVersion(inst.LimaVersion); err != nil {
+		if _, err = versionutil.Parse(inst.LimaVersion); err != nil {
 			logrus.Warnf("treating lima version %q from %q as very latest release", inst.LimaVersion, limaVersionFile)
 		}
 	} else if !errors.Is(err, os.ErrNotExist) {
@@ -436,36 +436,3 @@ func (inst *Instance) Unprotect() error {
 	inst.Protected = false
 	return nil
 }
-
-// parseLimaVersion parses a Lima version string by removing the leading "v" character and
-// stripping everything from the first "-" forward (which are `git describe` artifacts and
-// not semver pre-release markers). So "v0.19.1-16-gf3dc6ed.m" will be parsed as "0.19.1".
-func parseLimaVersion(version string) (*semver.Version, error) {
-	version = strings.TrimPrefix(version, "v")
-	version, _, _ = strings.Cut(version, "-")
-	return semver.NewVersion(version)
-}
-
-// LimaVersionGreaterThan returns true if the Lima version used to create an instance is greater
-// than a specific older version. Always returns false if the Lima version is the empty string.
-// Unparsable lima versions (like SHA1 commit ids) are treated as the latest version and return true.
-// limaVersion is a `github describe` string, not a semantic version. So "0.19.1-16-gf3dc6ed.m"
-// will be considered greater than "0.19.1".
-func LimaVersionGreaterThan(limaVersion, oldVersion string) bool {
-	if limaVersion == "" {
-		return false
-	}
-	version, err := parseLimaVersion(limaVersion)
-	if err != nil {
-		return true
-	}
-	switch version.Compare(*semver.New(oldVersion)) {
-	case -1:
-		return false
-	case +1:
-		return true
-	case 0:
-		return strings.Contains(limaVersion, "-")
-	}
-	panic("unreachable")
-}
diff --git a/pkg/version/versionutil/versionutil.go b/pkg/version/versionutil/versionutil.go
new file mode 100644
index 00000000..f7b81a58
--- /dev/null
+++ b/pkg/version/versionutil/versionutil.go
@@ -0,0 +1,40 @@
+package versionutil
+
+import (
+	"strings"
+
+	"github.com/coreos/go-semver/semver"
+)
+
+// Parse parses a Lima version string by removing the leading "v" character and
+// stripping everything from the first "-" forward (which are `git describe` artifacts and
+// not semver pre-release markers). So "v0.19.1-16-gf3dc6ed.m" will be parsed as "0.19.1".
+func Parse(version string) (*semver.Version, error) {
+	version = strings.TrimPrefix(version, "v")
+	version, _, _ = strings.Cut(version, "-")
+	return semver.NewVersion(version)
+}
+
+// GreaterThan returns true if the Lima version used to create an instance is greater
+// than a specific older version. Always returns false if the Lima version is the empty string.
+// Unparsable lima versions (like SHA1 commit ids) are treated as the latest version and return true.
+// limaVersion is a `github describe` string, not a semantic version. So "0.19.1-16-gf3dc6ed.m"
+// will be considered greater than "0.19.1".
+func GreaterThan(limaVersion, oldVersion string) bool {
+	if limaVersion == "" {
+		return false
+	}
+	version, err := Parse(limaVersion)
+	if err != nil {
+		return true
+	}
+	switch version.Compare(*semver.New(oldVersion)) {
+	case -1:
+		return false
+	case +1:
+		return true
+	case 0:
+		return strings.Contains(limaVersion, "-")
+	}
+	panic("unreachable")
+}


Test patch:
diff --git a/pkg/store/instance_test.go b/pkg/store/instance_test.go
index 0d532673..bdbbba7d 100644
--- a/pkg/store/instance_test.go
+++ b/pkg/store/instance_test.go
@@ -155,12 +155,3 @@ func TestPrintInstanceTableTwo(t *testing.T) {
 	assert.NilError(t, err)
 	assert.Equal(t, tableTwo, buf.String())
 }
-
-func TestLimaVersionGreaterThan(t *testing.T) {
-	assert.Equal(t, LimaVersionGreaterThan("", "0.1.0"), false)
-	assert.Equal(t, LimaVersionGreaterThan("0.0.1", "0.1.0"), false)
-	assert.Equal(t, LimaVersionGreaterThan("0.1.0", "0.1.0"), false)
-	assert.Equal(t, LimaVersionGreaterThan("0.1.0-2", "0.1.0"), true)
-	assert.Equal(t, LimaVersionGreaterThan("0.2.0", "0.1.0"), true)
-	assert.Equal(t, LimaVersionGreaterThan("abacab", "0.1.0"), true)
-}
diff --git a/pkg/version/versionutil/versionutil_test.go b/pkg/version/versionutil/versionutil_test.go
new file mode 100644
index 00000000..410a8848
--- /dev/null
+++ b/pkg/version/versionutil/versionutil_test.go
@@ -0,0 +1,16 @@
+package versionutil
+
+import (
+	"testing"
+
+	"gotest.tools/v3/assert"
+)
+
+func TestGreaterThan(t *testing.T) {
+	assert.Equal(t, GreaterThan("", "0.1.0"), false)
+	assert.Equal(t, GreaterThan("0.0.1", "0.1.0"), false)
+	assert.Equal(t, GreaterThan("0.1.0", "0.1.0"), false)
+	assert.Equal(t, GreaterThan("0.1.0-2", "0.1.0"), true)
+	assert.Equal(t, GreaterThan("0.2.0", "0.1.0"), true)
+	assert.Equal(t, GreaterThan("abacab", "0.1.0"), true)
+}