Skip to content

Commit b747025

Browse files
build(deps): bump github.com/ldez/grignotin from 0.7.0 to 0.8.0 (#5295)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent d516da9 commit b747025

File tree

4 files changed

+76
-17
lines changed

4 files changed

+76
-17
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ require (
6767
github.com/lasiar/canonicalheader v1.1.2
6868
github.com/ldez/exptostd v0.3.1
6969
github.com/ldez/gomoddirectives v0.6.0
70-
github.com/ldez/grignotin v0.7.0
70+
github.com/ldez/grignotin v0.8.0
7171
github.com/ldez/tagliatelle v0.7.1
7272
github.com/ldez/usetesting v0.4.2
7373
github.com/leonklingele/grouper v1.1.2

go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

+29-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
hcversion "github.com/hashicorp/go-version"
12+
"github.com/ldez/grignotin/goenv"
1213
"github.com/ldez/grignotin/gomod"
1314
"golang.org/x/mod/modfile"
1415
)
@@ -80,43 +81,57 @@ func IsGoGreaterThanOrEqual(current, limit string) bool {
8081
}
8182

8283
func detectGoVersion() string {
83-
goVersion := detectGoVersionFromGoMod()
84-
if goVersion != "" {
85-
return goVersion
86-
}
87-
88-
return cmp.Or(os.Getenv("GOVERSION"), "1.17")
84+
return cmp.Or(detectGoVersionFromGoMod(), "1.17")
8985
}
9086

9187
// detectGoVersionFromGoMod tries to get Go version from go.mod.
9288
// It returns `toolchain` version if present,
9389
// else it returns `go` version if present,
90+
// else it returns `GOVERSION` version if present,
9491
// else it returns empty.
9592
func detectGoVersionFromGoMod() string {
96-
modPath, err := gomod.GetGoModPath()
93+
values, err := goenv.Get(goenv.GOMOD, goenv.GOVERSION)
9794
if err != nil {
98-
modPath = detectGoModFallback()
99-
if modPath == "" {
100-
return ""
95+
values = map[string]string{
96+
goenv.GOMOD: detectGoModFallback(),
10197
}
10298
}
10399

104-
file, err := parseGoMod(modPath)
100+
if values[goenv.GOMOD] == "" {
101+
return parseGoVersion(values[goenv.GOVERSION])
102+
}
103+
104+
file, err := parseGoMod(values[goenv.GOMOD])
105105
if err != nil {
106-
return ""
106+
return parseGoVersion(values[goenv.GOVERSION])
107107
}
108108

109109
// The toolchain exists only if 'toolchain' version > 'go' version.
110110
// If 'toolchain' version <= 'go' version, `go mod tidy` will remove 'toolchain' version from go.mod.
111111
if file.Toolchain != nil && file.Toolchain.Name != "" {
112-
return strings.TrimPrefix(file.Toolchain.Name, "go")
112+
return parseGoVersion(file.Toolchain.Name)
113113
}
114114

115115
if file.Go != nil && file.Go.Version != "" {
116116
return file.Go.Version
117117
}
118118

119-
return ""
119+
return parseGoVersion(values[goenv.GOVERSION])
120+
}
121+
122+
func parseGoVersion(v string) string {
123+
raw := strings.TrimPrefix(v, "go")
124+
125+
// prerelease version (ex: go1.24rc1)
126+
idx := strings.IndexFunc(raw, func(r rune) bool {
127+
return (r < '0' || r > '9') && r != '.'
128+
})
129+
130+
if idx != -1 {
131+
raw = raw[:idx]
132+
}
133+
134+
return raw
120135
}
121136

122137
func parseGoMod(goMod string) (*modfile.File, error) {

pkg/config/config_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,47 @@ func TestIsGoGreaterThanOrEqual(t *testing.T) {
8383
})
8484
}
8585
}
86+
87+
func Test_parseGoVersion(t *testing.T) {
88+
testCases := []struct {
89+
desc string
90+
version string
91+
expected string
92+
}{
93+
{
94+
desc: "empty version",
95+
version: "",
96+
expected: "",
97+
},
98+
{
99+
desc: "no prefixed version",
100+
version: "1.23.0",
101+
expected: "1.23.0",
102+
},
103+
{
104+
desc: "semver version",
105+
version: "go1.23.0",
106+
expected: "1.23.0",
107+
},
108+
{
109+
desc: "family version",
110+
version: "go1.23",
111+
expected: "1.23",
112+
},
113+
{
114+
desc: "prerelease version",
115+
version: "go1.24rc1",
116+
expected: "1.24",
117+
},
118+
}
119+
120+
for _, test := range testCases {
121+
t.Run(test.desc, func(t *testing.T) {
122+
t.Parallel()
123+
124+
v := parseGoVersion(test.version)
125+
126+
assert.Equal(t, test.expected, v)
127+
})
128+
}
129+
}

0 commit comments

Comments
 (0)