Skip to content

Commit

Permalink
Test whether version.txt contains semantic version
Browse files Browse the repository at this point in the history
semver.Compare considers all non-semantic versions as older; the root
cause of #867.
  • Loading branch information
eval committed Dec 29, 2021
1 parent af4363e commit 5982070
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
14 changes: 9 additions & 5 deletions internal/cmd/cmd_version.go
Expand Up @@ -14,12 +14,9 @@ var CmdVersion = &Cmd{
Args: []string{"[VERSION_AT_LEAST]"},
Aliases: []string{"--version"},
Action: actionSimple(func(env Env, args []string) error {
semVersion := "v" + version
semVersion := ensureVPrefixed(version)
if len(args) > 1 {
atLeast := args[1]
if !strings.HasPrefix(atLeast, "v") {
atLeast = "v" + atLeast
}
atLeast := ensureVPrefixed(args[1])
if !semver.IsValid(atLeast) {
return fmt.Errorf("%s is not a valid semver version", atLeast)
}
Expand All @@ -33,3 +30,10 @@ var CmdVersion = &Cmd{
return nil
}),
}

func ensureVPrefixed(version string) string {
if !strings.HasPrefix(version, "v") {
return "v" + version
}
return version
}
15 changes: 15 additions & 0 deletions internal/cmd/cmd_version_test.go
@@ -0,0 +1,15 @@
package cmd

import (
"golang.org/x/mod/semver"
"io/ioutil"
"testing"
)

func TestVersionDotTxt(t *testing.T) {
version, _ := ioutil.ReadFile("../../version.txt")

if !semver.IsValid(ensureVPrefixed(string(version))) {
t.Fatalf(`version.txt does not contain a valid semantic version: %q`, version)
}
}
2 changes: 1 addition & 1 deletion version.txt
@@ -1 +1 @@
2.30.2
2.30.2

0 comments on commit 5982070

Please sign in to comment.