From c6a3ee40e6e46fdf3808d2d4795290bb5036541c Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 17 Feb 2024 20:55:18 +0100 Subject: [PATCH] fix: improve some tests for Go 1.21 --- test/run_test.go | 49 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/test/run_test.go b/test/run_test.go index 2ec4b64280a0..b13499bdc022 100644 --- a/test/run_test.go +++ b/test/run_test.go @@ -2,8 +2,11 @@ package test import ( "path/filepath" + "runtime" + "strings" "testing" + hcversion "github.com/hashicorp/go-version" "github.com/stretchr/testify/require" _ "github.com/valyala/quicktemplate" @@ -131,14 +134,20 @@ func TestTestsAreLintedByDefault(t *testing.T) { } func TestCgoOk(t *testing.T) { + args := []string{"--timeout=3m", + "--enable-all", + "-D", + "nosnakecase", // try to analyze the generated Go. + } + + // TODO(ldez) remove when we will run go1.23 on the CI. + if isGoVersion("1.21") { + args = append(args, "-D", "intrange,copyloopvar") + } + testshared.NewRunnerBuilder(t). WithNoConfig(). - WithArgs( - "--timeout=3m", - "--enable-all", - "-D", - "nosnakecase,gci", - ). + WithArgs(args...). WithTargetPath(testdataDir, "cgo"). Runner(). Install(). @@ -353,9 +362,16 @@ func TestLineDirectiveProcessedFiles(t *testing.T) { } func TestUnsafeOk(t *testing.T) { + args := []string{"--enable-all"} + + // TODO(ldez) remove when we will run go1.23 on the CI. + if isGoVersion("1.21") { + args = append(args, "-D", "intrange,copyloopvar") + } + testshared.NewRunnerBuilder(t). WithNoConfig(). - WithArgs("--enable-all"). + WithArgs(args...). WithTargetPath(testdataDir, "unsafe"). Runner(). Install(). @@ -511,6 +527,11 @@ func TestEnableAllFastAndEnableCanCoexist(t *testing.T) { t.Run(test.desc, func(t *testing.T) { t.Parallel() + // TODO(ldez) remove when we will run go1.23 on the CI. + if isGoVersion("1.21") { + test.args = append(test.args, "-D", "intrange,copyloopvar") + } + testshared.NewRunnerBuilder(t). WithNoConfig(). WithArgs(test.args...). @@ -681,3 +702,17 @@ func TestPathPrefix(t *testing.T) { }) } } + +func isGoVersion(tag string) bool { + vRuntime, err := hcversion.NewVersion(strings.TrimPrefix(runtime.Version(), "go")) + if err != nil { + return false + } + + vTag, err := hcversion.NewVersion(strings.TrimPrefix(tag, "go")) + if err != nil { + return false + } + + return vRuntime.GreaterThanOrEqual(vTag) +}