Skip to content

Commit 94c8a50

Browse files
committed
feat: add ForceDisableUnsupportedLinters option
1 parent c6a3ee4 commit 94c8a50

File tree

3 files changed

+125
-42
lines changed

3 files changed

+125
-42
lines changed

test/run_test.go

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package test
22

33
import (
44
"path/filepath"
5-
"runtime"
6-
"strings"
75
"testing"
86

9-
hcversion "github.com/hashicorp/go-version"
107
"github.com/stretchr/testify/require"
118
_ "github.com/valyala/quicktemplate"
129

@@ -134,21 +131,15 @@ func TestTestsAreLintedByDefault(t *testing.T) {
134131
}
135132

136133
func TestCgoOk(t *testing.T) {
137-
args := []string{"--timeout=3m",
138-
"--enable-all",
139-
"-D",
140-
"nosnakecase", // try to analyze the generated Go.
141-
}
142-
143-
// TODO(ldez) remove when we will run go1.23 on the CI.
144-
if isGoVersion("1.21") {
145-
args = append(args, "-D", "intrange,copyloopvar")
146-
}
147-
148134
testshared.NewRunnerBuilder(t).
149135
WithNoConfig().
150-
WithArgs(args...).
136+
WithArgs("--timeout=3m",
137+
"--enable-all",
138+
"-D",
139+
"nosnakecase",
140+
).
151141
WithTargetPath(testdataDir, "cgo").
142+
ForceDisableUnsupportedLinters().
152143
Runner().
153144
Install().
154145
Run().
@@ -362,17 +353,11 @@ func TestLineDirectiveProcessedFiles(t *testing.T) {
362353
}
363354

364355
func TestUnsafeOk(t *testing.T) {
365-
args := []string{"--enable-all"}
366-
367-
// TODO(ldez) remove when we will run go1.23 on the CI.
368-
if isGoVersion("1.21") {
369-
args = append(args, "-D", "intrange,copyloopvar")
370-
}
371-
372356
testshared.NewRunnerBuilder(t).
373357
WithNoConfig().
374-
WithArgs(args...).
358+
WithArgs("--enable-all").
375359
WithTargetPath(testdataDir, "unsafe").
360+
ForceDisableUnsupportedLinters().
376361
Runner().
377362
Install().
378363
Run().
@@ -527,15 +512,11 @@ func TestEnableAllFastAndEnableCanCoexist(t *testing.T) {
527512
t.Run(test.desc, func(t *testing.T) {
528513
t.Parallel()
529514

530-
// TODO(ldez) remove when we will run go1.23 on the CI.
531-
if isGoVersion("1.21") {
532-
test.args = append(test.args, "-D", "intrange,copyloopvar")
533-
}
534-
535515
testshared.NewRunnerBuilder(t).
536516
WithNoConfig().
537517
WithArgs(test.args...).
538518
WithTargetPath(testdataDir, minimalPkg).
519+
ForceDisableUnsupportedLinters().
539520
Runner().
540521
Run().
541522
ExpectExitCode(test.expected...)
@@ -702,17 +683,3 @@ func TestPathPrefix(t *testing.T) {
702683
})
703684
}
704685
}
705-
706-
func isGoVersion(tag string) bool {
707-
vRuntime, err := hcversion.NewVersion(strings.TrimPrefix(runtime.Version(), "go"))
708-
if err != nil {
709-
return false
710-
}
711-
712-
vTag, err := hcversion.NewVersion(strings.TrimPrefix(tag, "go"))
713-
if err != nil {
714-
return false
715-
}
716-
717-
return vRuntime.GreaterThanOrEqual(vTag)
718-
}

test/testshared/runner.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import (
44
"os"
55
"os/exec"
66
"path/filepath"
7+
"runtime"
8+
"slices"
79
"strings"
810
"sync"
911
"syscall"
1012
"testing"
1113
"time"
1214

15+
hcversion "github.com/hashicorp/go-version"
1316
"github.com/stretchr/testify/assert"
1417
"github.com/stretchr/testify/require"
1518

@@ -150,6 +153,14 @@ func (b *RunnerBuilder) WithArgs(args ...string) *RunnerBuilder {
150153
return b
151154
}
152155

156+
// ForceDisableUnsupportedLinters temporary method to disable some linters.
157+
// TODO(ldez) remove when we will run go1.23 on the CI.
158+
func (b *RunnerBuilder) ForceDisableUnsupportedLinters() *RunnerBuilder {
159+
b.args = forceDisableUnsupportedLinters(b.args)
160+
161+
return b
162+
}
163+
153164
func (b *RunnerBuilder) WithTargetPath(targets ...string) *RunnerBuilder {
154165
b.target = filepath.Join(targets...)
155166

@@ -358,3 +369,61 @@ func InstallGolangciLint(tb testing.TB) string {
358369

359370
return abs
360371
}
372+
373+
// TODO(ldez) remove when we will run go1.23 on the CI.
374+
func forceDisableUnsupportedLinters(args []string) []string {
375+
if !isGoVersion("1.21") {
376+
return args
377+
}
378+
379+
result := slices.Clone(args)
380+
381+
if len(result) == 0 {
382+
return append(result, "-D", "intrange,copyloopvar")
383+
}
384+
385+
if slices.ContainsFunc(args, func(arg string) bool { return strings.HasSuffix(arg, "-disable-all") }) {
386+
return args
387+
}
388+
389+
var appended bool
390+
391+
for i, arg := range args {
392+
if !strings.HasSuffix(arg, "-D") && !strings.HasSuffix(arg, "-disable") {
393+
continue
394+
}
395+
396+
if len(args) <= i+1 || strings.HasPrefix(args[i+1], "-") {
397+
continue
398+
}
399+
400+
d := strings.Split(args[i+1], ",")
401+
d = append(d, "intrange", "copyloopvar")
402+
403+
result[i+1] = strings.Join(slices.Compact(d), ",")
404+
405+
appended = true
406+
break
407+
}
408+
409+
if !appended {
410+
result = append(result, "-D", "intrange,copyloopvar")
411+
}
412+
413+
return result
414+
}
415+
416+
// TODO(ldez) remove when we will run go1.23 on the CI.
417+
func isGoVersion(tag string) bool {
418+
vRuntime, err := hcversion.NewVersion(strings.TrimPrefix(runtime.Version(), "go"))
419+
if err != nil {
420+
return false
421+
}
422+
423+
vTag, err := hcversion.NewVersion(strings.TrimPrefix(tag, "go"))
424+
if err != nil {
425+
return false
426+
}
427+
428+
return vRuntime.GreaterThanOrEqual(vTag)
429+
}

test/testshared/runner_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,50 @@ func TestRunnerResult_ExpectOutputRegexp(t *testing.T) {
217217
r.ExpectOutputRegexp(`an.+`)
218218
r.ExpectOutputRegexp("an")
219219
}
220+
221+
// TODO(ldez) remove when we will run go1.23 on the CI.
222+
func Test_forceDisableUnsupportedLinters(t *testing.T) {
223+
t.Skip("only for illustration purpose because works only with go1.21")
224+
225+
testCases := []struct {
226+
desc string
227+
args []string
228+
expected []string
229+
}{
230+
{
231+
desc: "no args",
232+
expected: []string{"-D", "intrange,copyloopvar"},
233+
},
234+
{
235+
desc: "simple",
236+
args: []string{"-A", "B", "-E"},
237+
expected: []string{"-A", "B", "-E", "-D", "intrange,copyloopvar"},
238+
},
239+
{
240+
desc: "with existing disable linters",
241+
args: []string{"-D", "a,b"},
242+
expected: []string{"-D", "a,b,intrange,copyloopvar"},
243+
},
244+
{
245+
desc: "complex",
246+
args: []string{"-A", "B", "-D", "a,b", "C", "-E", "F"},
247+
expected: []string{"-A", "B", "-D", "a,b,intrange,copyloopvar", "C", "-E", "F"},
248+
},
249+
{
250+
desc: "disable-all",
251+
args: []string{"-disable-all", "-D", "a,b"},
252+
expected: []string{"-disable-all", "-D", "a,b"},
253+
},
254+
}
255+
256+
for _, test := range testCases {
257+
test := test
258+
t.Run(test.desc, func(t *testing.T) {
259+
t.Parallel()
260+
261+
result := forceDisableUnsupportedLinters(test.args)
262+
263+
assert.Equal(t, test.expected, result)
264+
})
265+
}
266+
}

0 commit comments

Comments
 (0)