Skip to content

Commit

Permalink
Clear is better than clever. https://go-proverbs.github.io/
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 17, 2024
1 parent 1eeea0b commit 74f0879
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 121 deletions.
6 changes: 3 additions & 3 deletions test/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func TestCgoOk(t *testing.T) {
"-D",
"nosnakecase",
).
WithArgs("--go=1.22"). // TODO(ldez) remove this line when we will run go1.23 on the CI. (related to intrange, copyloopvar)
WithTargetPath(testdataDir, "cgo").
ForceDisableUnsupportedLinters().
Runner().
Install().
Run().
Expand Down Expand Up @@ -356,8 +356,8 @@ func TestUnsafeOk(t *testing.T) {
testshared.NewRunnerBuilder(t).
WithNoConfig().
WithArgs("--enable-all").
WithArgs("--go=1.22"). // TODO(ldez) remove this line when we will run go1.23 on the CI. (related to intrange, copyloopvar)
WithTargetPath(testdataDir, "unsafe").
ForceDisableUnsupportedLinters().
Runner().
Install().
Run().
Expand Down Expand Up @@ -515,8 +515,8 @@ func TestEnableAllFastAndEnableCanCoexist(t *testing.T) {
testshared.NewRunnerBuilder(t).
WithNoConfig().
WithArgs(test.args...).
WithArgs("--go=1.22"). // TODO(ldez) remove this line when we will run go1.23 on the CI. (related to intrange, copyloopvar)
WithTargetPath(testdataDir, minimalPkg).
ForceDisableUnsupportedLinters().
Runner().
Run().
ExpectExitCode(test.expected...)
Expand Down
71 changes: 0 additions & 71 deletions test/testshared/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"
"sync"
"syscall"
"testing"
"time"

hcversion "github.com/hashicorp/go-version"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -153,14 +150,6 @@ func (b *RunnerBuilder) WithArgs(args ...string) *RunnerBuilder {
return b
}

// ForceDisableUnsupportedLinters temporary method to disable some linters.
// TODO(ldez) remove when we will run go1.23 on the CI.
func (b *RunnerBuilder) ForceDisableUnsupportedLinters() *RunnerBuilder {
b.args = forceDisableUnsupportedLinters(b.args)

return b
}

func (b *RunnerBuilder) WithTargetPath(targets ...string) *RunnerBuilder {
b.target = filepath.Join(targets...)

Expand Down Expand Up @@ -369,63 +358,3 @@ func InstallGolangciLint(tb testing.TB) string {

return abs
}

// TODO(ldez) remove when we will run go1.23 on the CI.
func forceDisableUnsupportedLinters(args []string) []string {
result := slices.Clone(args)

if !isGoLessThan("1.22") {
return append(result, "--go=1.22")
}

if len(result) == 0 {
return append(result, "-D", "intrange,copyloopvar")
}

if slices.ContainsFunc(args, func(arg string) bool { return strings.HasSuffix(arg, "-disable-all") }) {
return args
}

var appended bool

for i, arg := range args {
if !strings.HasSuffix(arg, "-D") && !strings.HasSuffix(arg, "-disable") {
continue
}

if len(args) <= i+1 || strings.HasPrefix(args[i+1], "-") {
continue
}

d := strings.Split(args[i+1], ",")
d = append(d, "intrange", "copyloopvar")

result[i+1] = strings.Join(slices.Compact(d), ",")

appended = true
break
}

if !appended {
result = append(result, "-D", "intrange,copyloopvar")
}

return result
}

// TODO(ldez) remove when we will run go1.23 on the CI.
func isGoLessThan(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
}

println(vRuntime.LessThanOrEqual(vTag))

return vRuntime.LessThan(vTag)
}
47 changes: 0 additions & 47 deletions test/testshared/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,50 +217,3 @@ func TestRunnerResult_ExpectOutputRegexp(t *testing.T) {
r.ExpectOutputRegexp(`an.+`)
r.ExpectOutputRegexp("an")
}

// TODO(ldez) remove when we will run go1.23 on the CI.
func Test_forceDisableUnsupportedLinters(t *testing.T) {
t.Skip("only for illustration purpose because works only with go1.21")

testCases := []struct {
desc string
args []string
expected []string
}{
{
desc: "no args",
expected: []string{"-D", "intrange,copyloopvar"},
},
{
desc: "simple",
args: []string{"-A", "B", "-E"},
expected: []string{"-A", "B", "-E", "-D", "intrange,copyloopvar"},
},
{
desc: "with existing disable linters",
args: []string{"-D", "a,b"},
expected: []string{"-D", "a,b,intrange,copyloopvar"},
},
{
desc: "complex",
args: []string{"-A", "B", "-D", "a,b", "C", "-E", "F"},
expected: []string{"-A", "B", "-D", "a,b,intrange,copyloopvar", "C", "-E", "F"},
},
{
desc: "disable-all",
args: []string{"-disable-all", "-D", "a,b"},
expected: []string{"-disable-all", "-D", "a,b"},
},
}

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

result := forceDisableUnsupportedLinters(test.args)

assert.Equal(t, test.expected, result)
})
}
}

0 comments on commit 74f0879

Please sign in to comment.