diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 8e8823758879..d0332249954e 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1916,12 +1916,37 @@ linters-settings: all: false testifylint: - # Enable all checkers. + # Enable all checkers (https://github.com/Antonboom/testifylint#checkers). # Default: false enable-all: true - # Enable specific checkers. - # https://github.com/Antonboom/testifylint#checkers - # Default: ["bool-compare", "compares", "empty", "error-is-as", "error-nil", "expected-actual", "float-compare", "len", "require-error", "suite-dont-use-pkg", "suite-extra-assert-call"] + # Disable checkers by name + # (in addition to default + # suite-thelper + # ). + disable: + - bool-compare + - compares + - empty + - error-is-as + - error-nil + - expected-actual + - go-require + - float-compare + - len + - nil-compare + - require-error + - suite-dont-use-pkg + - suite-extra-assert-call + - suite-thelper + + # Disable all checkers (https://github.com/Antonboom/testifylint#checkers). + # Default: false + disable-all: true + # Enable checkers by name + # (in addition to default + # bool-compare, compares, empty, error-is-as, error-nil, expected-actual, go-require, float-compare, len, + # nil-compare, require-error, suite-dont-use-pkg, suite-extra-assert-call + # ). enable: - bool-compare - compares @@ -1929,16 +1954,23 @@ linters-settings: - error-is-as - error-nil - expected-actual + - go-require - float-compare - len + - nil-compare - require-error - suite-dont-use-pkg - suite-extra-assert-call - suite-thelper + expected-actual: # Regexp for expected variable name. # Default: (^(exp(ected)?|want(ed)?)([A-Z]\w*)?$)|(^(\w*[a-z])?(Exp(ected)?|Want(ed)?)$) pattern: ^expected + require-error: + # Regexp for assertions to analyze. If defined then only matched assertions will be reported. + # Default: "" + fn-pattern: ^(Errorf?|NoErrorf?)$ suite-extra-assert-call: # To require or remove extra Assert() call? # Default: remove diff --git a/go.mod b/go.mod index a75ebc7346ba..b93f63e8439e 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/Abirdcfly/dupword v0.0.13 github.com/Antonboom/errname v0.1.12 github.com/Antonboom/nilnil v0.1.7 - github.com/Antonboom/testifylint v0.2.3 + github.com/Antonboom/testifylint v1.0.1 github.com/BurntSushi/toml v1.3.2 github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 github.com/GaijinEntertainment/go-exhaustruct/v3 v3.1.0 @@ -191,9 +191,9 @@ require ( go.uber.org/multierr v1.6.0 // indirect go.uber.org/zap v1.24.0 // indirect golang.org/x/exp/typeparams v0.0.0-20230307190834-24139beb5833 // indirect - golang.org/x/mod v0.13.0 // indirect + golang.org/x/mod v0.14.0 // indirect golang.org/x/sync v0.4.0 // indirect - golang.org/x/sys v0.13.0 // indirect + golang.org/x/sys v0.14.0 // indirect golang.org/x/text v0.13.0 // indirect google.golang.org/protobuf v1.28.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/go.sum b/go.sum index 5d70f209aad2..a1588c7fd0d1 100644 --- a/go.sum +++ b/go.sum @@ -48,8 +48,8 @@ github.com/Antonboom/errname v0.1.12 h1:oh9ak2zUtsLp5oaEd/erjB4GPu9w19NyoIskZClD github.com/Antonboom/errname v0.1.12/go.mod h1:bK7todrzvlaZoQagP1orKzWXv59X/x0W0Io2XT1Ssro= github.com/Antonboom/nilnil v0.1.7 h1:ofgL+BA7vlA1K2wNQOsHzLJ2Pw5B5DpWRLdDAVvvTow= github.com/Antonboom/nilnil v0.1.7/go.mod h1:TP+ScQWVEq0eSIxqU8CbdT5DFWoHp0MbP+KMUO1BKYQ= -github.com/Antonboom/testifylint v0.2.3 h1:MFq9zyL+rIVpsvLX4vDPLojgN7qODzWsrnftNX2Qh60= -github.com/Antonboom/testifylint v0.2.3/go.mod h1:IYaXaOX9NbfAyO+Y04nfjGI8wDemC1rUyM/cYolz018= +github.com/Antonboom/testifylint v1.0.1 h1:O+omzFmvgxAVYKUuMYQYwFxY6lpySeZ1nxQ62Xonw64= +github.com/Antonboom/testifylint v1.0.1/go.mod h1:tGEV9t6Th7DHXFVjd8oyLOBbIxXzs4CMEIAkbQ2RuC8= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= @@ -664,8 +664,8 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91 golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= -golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= +golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -797,8 +797,9 @@ golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 0fee9f81ef53..6a701baa1a7e 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -764,13 +764,19 @@ type TagliatelleSettings struct { } type TestifylintSettings struct { - EnableAll bool `mapstructure:"enable-all"` - EnabledCheckers []string `mapstructure:"enable"` + EnableAll bool `mapstructure:"enable-all"` + DisabledCheckers []string `mapstructure:"disable"` + DisableAll bool `mapstructure:"disable-all"` + EnabledCheckers []string `mapstructure:"enable"` ExpectedActual struct { ExpVarPattern string `mapstructure:"pattern"` } `mapstructure:"expected-actual"` + RequireError struct { + FnPattern string `mapstructure:"fn-pattern"` + } `mapstructure:"require-error"` + SuiteExtraAssertCall struct { Mode string `mapstructure:"mode"` } `mapstructure:"suite-extra-assert-call"` diff --git a/pkg/golinters/testifylint.go b/pkg/golinters/testifylint.go index 83bae2868a5b..d9cfc04f6e05 100644 --- a/pkg/golinters/testifylint.go +++ b/pkg/golinters/testifylint.go @@ -14,14 +14,22 @@ func NewTestifylint(settings *config.TestifylintSettings) *goanalysis.Linter { cfg := make(map[string]map[string]any) if settings != nil { cfg[a.Name] = map[string]any{ - "enable-all": settings.EnableAll, + "enable-all": settings.EnableAll, + "disable-all": settings.DisableAll, } if len(settings.EnabledCheckers) > 0 { cfg[a.Name]["enable"] = settings.EnabledCheckers } + if len(settings.DisabledCheckers) > 0 { + cfg[a.Name]["disable"] = settings.DisabledCheckers + } + if p := settings.ExpectedActual.ExpVarPattern; p != "" { cfg[a.Name]["expected-actual.pattern"] = p } + if p := settings.RequireError.FnPattern; p != "" { + cfg[a.Name]["require-error.fn-pattern"] = p + } if m := settings.SuiteExtraAssertCall.Mode; m != "" { cfg[a.Name]["suite-extra-assert-call.mode"] = m } diff --git a/test/testdata/configs/testifylint.yml b/test/testdata/configs/testifylint.yml new file mode 100644 index 000000000000..dde08e3a5491 --- /dev/null +++ b/test/testdata/configs/testifylint.yml @@ -0,0 +1,6 @@ +linters-settings: + testifylint: + disable-all: true + enable: require-error + require-error: + fn-pattern: ^NoError$ diff --git a/test/testdata/testifylint.go b/test/testdata/testifylint.go index dc6d96de735e..4ec89c61a69a 100644 --- a/test/testdata/testifylint.go +++ b/test/testdata/testifylint.go @@ -46,6 +46,15 @@ func TestTestifylint(t *testing.T) { assert.Equalf(t, predicate, true, "message") // want "bool-compare: use assert\\.Truef" assert.Equalf(t, predicate, true, "message %d", 42) // want "bool-compare: use assert\\.Truef" }) + + assert.Equal(t, arr, nil) // want "nil-compare: use assert\\.Nil" + assert.Nil(t, arr) + + go func() { + if assert.Error(t, err) { + require.ErrorIs(t, err, io.EOF) // want "go-require: require must only be used in the goroutine running the test function" + } + }() } type SuiteExample struct { diff --git a/test/testdata/testifylint_config.go b/test/testdata/testifylint_config.go new file mode 100644 index 000000000000..512acbfab304 --- /dev/null +++ b/test/testdata/testifylint_config.go @@ -0,0 +1,73 @@ +//golangcitest:args -Etestifylint +//golangcitest:config_path testdata/configs/testifylint.yml +package testdata + +import ( + "io" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +func TestTestifylint(t *testing.T) { + var ( + predicate bool + resultInt int + resultFloat float64 + arr []string + err error + ) + + assert.Equal(t, predicate, true) + assert.True(t, resultInt == 1) + assert.Equal(t, len(arr), 0) + assert.Error(t, err, io.EOF) + assert.Nil(t, err) + assert.Equal(t, resultInt, 42) + assert.Equal(t, resultFloat, 42.42) + assert.Equal(t, len(arr), 10) + + assert.True(t, predicate) + assert.Equal(t, resultInt, 1) + assert.Empty(t, arr) + assert.ErrorIs(t, err, io.EOF) + assert.NoError(t, err) // want "require-error: for error assertions use require" + assert.Equal(t, 42, resultInt) + assert.NoErrorf(t, err, "boom!") + assert.InEpsilon(t, 42.42, resultFloat, 0.0001) + assert.Len(t, arr, 10) + + require.ErrorIs(t, err, io.EOF) + require.NoError(t, err) + + t.Run("formatted", func(t *testing.T) { + assert.Equal(t, predicate, true, "message") + assert.Equal(t, predicate, true, "message %d", 42) + assert.Equalf(t, predicate, true, "message") + assert.Equalf(t, predicate, true, "message %d", 42) + }) + + assert.Equal(t, arr, nil) + assert.Nil(t, arr) + + go func() { + if assert.Error(t, err) { + require.ErrorIs(t, err, io.EOF) + } + }() +} + +type SuiteExample struct { + suite.Suite +} + +func TestSuiteExample(t *testing.T) { + suite.Run(t, new(SuiteExample)) +} + +func (s *SuiteExample) TestAll() { + var b bool + s.Assert().True(b) +}