Skip to content

Commit

Permalink
bump testifylint to stable version
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonboom committed Nov 5, 2023
1 parent 61f2ef3 commit 1046297
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 15 deletions.
40 changes: 36 additions & 4 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1916,29 +1916,61 @@ 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
- 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

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
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
10 changes: 9 additions & 1 deletion pkg/golinters/testifylint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions test/testdata/configs/testifylint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
linters-settings:
testifylint:
disable-all: true
enable: require-error
require-error:
fn-pattern: ^NoError$
9 changes: 9 additions & 0 deletions test/testdata/testifylint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
73 changes: 73 additions & 0 deletions test/testdata/testifylint_config.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 1046297

Please sign in to comment.