Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check that Go version use to build is greater or equals to the Go version of the project #4938

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package config

import (
"fmt"
"go/version"
"os"
"regexp"
"runtime"
"strings"

hcversion "github.com/hashicorp/go-version"
Expand Down Expand Up @@ -108,3 +111,37 @@ func trimGoVersion(v string) string {

return v
}

func getRuntimeGoVersion() string {
goVersion := runtime.Version()

parts := strings.Fields(goVersion)

if len(parts) == 0 {
return goVersion
}

// When using GOEXPERIMENT, the version returned might look something like "go1.23.0 X:boringcrypto".
return parts[0]
}

func checkGoVersion(goVersion string) error {
langVersion := version.Lang(getRuntimeGoVersion())

runtimeVersion, err := hcversion.NewVersion(strings.TrimPrefix(langVersion, "go"))
if err != nil {
return err
}

targetedVersion, err := hcversion.NewVersion(trimGoVersion(goVersion))
if err != nil {
return err
}

if runtimeVersion.LessThan(targetedVersion) {
return fmt.Errorf("the Go language version (%s) used to build golangci-lint is lower than the targeted Go version (%s)",
langVersion, goVersion)
}

return nil
}
54 changes: 54 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestIsGoGreaterThanOrEqual(t *testing.T) {
Expand Down Expand Up @@ -131,3 +132,56 @@ func Test_trimGoVersion(t *testing.T) {
})
}
}

func Test_checkGoVersion(t *testing.T) {
testCases := []struct {
desc string
version string
require require.ErrorAssertionFunc
}{
{
desc: "version greater than runtime version (patch)",
version: "1.30.1",
require: require.Error,
},
{
desc: "version greater than runtime version (family)",
version: "1.30",
require: require.Error,
},
{
desc: "version greater than runtime version (RC)",
version: "1.30.0-rc1",
require: require.Error,
},
{
desc: "version equals to runtime version",
version: getRuntimeGoVersion(),
require: require.NoError,
},
{
desc: "version lower than runtime version (patch)",
version: "1.19.1",
require: require.NoError,
},
{
desc: "version lower than runtime version (family)",
version: "1.19",
require: require.NoError,
},
{
desc: "version lower than runtime version (RC)",
version: "1.19.0-rc1",
require: require.NoError,
},
}

for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

err := checkGoVersion(test.version)
test.require(t, err)
})
}
}
5 changes: 5 additions & 0 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func (l *Loader) Load(opts LoadOptions) error {

l.handleGoVersion()

err = checkGoVersion(l.cfg.Run.Go)
if err != nil {
return err
}

err = l.handleEnableOnlyOption()
if err != nil {
return err
Expand Down
Loading