Skip to content

Commit

Permalink
fix: use go/version package for validation (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane authored Apr 24, 2024
1 parent 98cee6f commit ade4f8d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ on:
jobs:
test:
uses: go-simpler/.github/.github/workflows/test.yml@main
with:
go-versions: '[ "stable" ]' # go/version package available since 1.22
lint:
uses: go-simpler/.github/.github/workflows/lint.yml@main
vuln:
Expand Down
21 changes: 11 additions & 10 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"errors"
"flag"
"fmt"
goversion "go/version"
"io"
"io/fs"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
Expand All @@ -39,8 +39,9 @@ var (
}
)

//nolint:gocritic // regexpSimplify: [0-9] reads better here than \d
var versionRE = regexp.MustCompile(`^(1(\.[1-9][0-9]*)?(\.[1-9][0-9]*)?((rc|beta)[1-9]+)?|tip)$`)
func isValid(version string) bool {
return goversion.IsValid("go"+version) || version == "tip"
}

// use switches the current Go version to the one specified.
// If it's not installed, use will install it and download its SDK first.
Expand All @@ -59,7 +60,7 @@ func use(ctx context.Context, args []string) error {
version = local.main
}

if !versionRE.MatchString(version) {
if !isValid(version) {
return fmt.Errorf("malformed version %q", version)
}

Expand Down Expand Up @@ -152,19 +153,19 @@ func list(ctx context.Context, args []string) error {
var extra string
switch {
case version == local.main:
extra = " (main)"
extra = "\t(main)"
case !local.contains(version):
extra = " (not installed)"
extra = "\t(not installed)"
case !downloaded(version):
extra = " (missing SDK)"
extra = "\t(missing SDK)"
}

prefix := " "
if version == local.current {
prefix = "*"
}

fmt.Fprintf(output, "%s %-10s%s\n", prefix, version, extra)
fmt.Fprintf(output, "%s %s%s\n", prefix, version, extra)
}

return nil
Expand All @@ -187,7 +188,7 @@ func remove(ctx context.Context, args []string) error {
version = local.main
}

if !versionRE.MatchString(version) {
if !isValid(version) {
return fmt.Errorf("malformed version %q", version)
}

Expand Down Expand Up @@ -289,7 +290,7 @@ func localVersions(ctx context.Context) (*local, error) {
continue
}
version := strings.TrimPrefix(entry.Name(), "go")
if versionRE.MatchString(version) {
if isValid(version) {
list = append(list, version)
}
}
Expand Down
37 changes: 8 additions & 29 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,6 @@ import (
. "go-simpler.org/assert/EF"
)

func Test_versionRE(t *testing.T) {
test := func(s string, match bool) {
t.Helper()
got := versionRE.MatchString(s)
assert.Equal[E](t, got, match)
}

test("tip", true)
test("top", false)
test("1", true)
test("1.", false)
test("1.18", true)
test("1.18rc", false)
test("1.18rc1", true)
test("1.18alpha1", false)
test("1.18beta1", true)
test("1.18.", false)
test("1.18.10", true)
test("1.18.10.", false)
}

const mainVersion = "1.19"

var ctx = context.Background()
Expand Down Expand Up @@ -145,9 +124,9 @@ func Test_list(t *testing.T) {
err := list(ctx, nil)
assert.NoErr[F](t, err)
assert.Equal[E](t, "\n"+buf.String(), `
1.19 (main)
* 1.18
1.17 (missing SDK)
1.19 (main)
* 1.18
1.17 (missing SDK)
`)
assert.Equal[E](t, steps, []string{
"exec: go version", // 1. read main version
Expand Down Expand Up @@ -185,10 +164,10 @@ func Test_list(t *testing.T) {
err := list(ctx, []string{"-all"})
assert.NoErr[F](t, err)
assert.Equal[E](t, "\n"+buf.String(), `
tip (not installed)
1.19 (main)
* 1.18
1.17 (not installed)
tip (not installed)
1.19 (main)
* 1.18
1.17 (not installed)
`)
assert.Equal[E](t, steps, []string{
"exec: go version", // 1. read main version
Expand Down Expand Up @@ -258,7 +237,7 @@ func Test_remove(t *testing.T) {
}

func recordCommands(commands *[]string) {
command = func(ctx context.Context, name string, args ...string) error {
command = func(_ context.Context, name string, args ...string) error {
c := strings.Join(append([]string{name}, args...), " ")
*commands = append(*commands, "exec: "+c)
return nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module go-simpler.org/goversion

go 1.21
go 1.22

require go-simpler.org/assert v0.9.0

0 comments on commit ade4f8d

Please sign in to comment.