From 117b718958bdfd27fb3dca00d420204414b66542 Mon Sep 17 00:00:00 2001 From: Gert Goet Date: Wed, 29 Dec 2021 21:23:21 +0100 Subject: [PATCH] Test whether version.txt contains semantic version (#871) semver.Compare considers all non-semantic versions as older; the root cause of #867. --- internal/cmd/cmd_version.go | 14 +++++++++----- internal/cmd/cmd_version_test.go | 15 +++++++++++++++ version.txt | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 internal/cmd/cmd_version_test.go diff --git a/internal/cmd/cmd_version.go b/internal/cmd/cmd_version.go index d59bea72a..a7c6d4633 100644 --- a/internal/cmd/cmd_version.go +++ b/internal/cmd/cmd_version.go @@ -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) } @@ -33,3 +30,10 @@ var CmdVersion = &Cmd{ return nil }), } + +func ensureVPrefixed(version string) string { + if !strings.HasPrefix(version, "v") { + return "v" + version + } + return version +} diff --git a/internal/cmd/cmd_version_test.go b/internal/cmd/cmd_version_test.go new file mode 100644 index 000000000..131e0679e --- /dev/null +++ b/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) + } +} diff --git a/version.txt b/version.txt index 0958964f6..50bcd8944 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -2.30.2 +2.30.2 \ No newline at end of file