-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package golinters | ||
|
||
import ( | ||
"github.com/Antonboom/testifylint/analyzer" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
) | ||
|
||
func NewTestifylint(settings *config.TestifylintSettings) *goanalysis.Linter { | ||
a := analyzer.New() | ||
|
||
cfg := make(map[string]map[string]any) | ||
if settings != nil { | ||
cfg[a.Name] = map[string]any{ | ||
"enable-all": settings.EnableAll, | ||
} | ||
if len(settings.EnabledCheckers) > 0 { | ||
cfg[a.Name]["enable"] = settings.EnabledCheckers | ||
} | ||
if p := settings.ExpectedActual.ExpVarPattern; p != "" { | ||
cfg[a.Name]["expected-actual.pattern"] = p | ||
} | ||
if m := settings.SuiteExtraAssertCall.Mode; m != "" { | ||
cfg[a.Name]["suite-extra-assert-call.mode"] = m | ||
} | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
a.Name, | ||
a.Doc, | ||
[]*analysis.Analyzer{a}, | ||
cfg, | ||
).WithLoadMode(goanalysis.LoadModeTypesInfo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//golangcitest:args -Etestifylint | ||
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) // want "bool-compare: use assert\\.True" | ||
assert.True(t, resultInt == 1) // want "compares: use assert\\.Equal" | ||
assert.Equal(t, len(arr), 0) // want "empty: use assert\\.Empty" | ||
assert.Error(t, err, io.EOF) // want "error-is-as: invalid usage of assert\\.Error, use assert\\.ErrorIs instead" | ||
assert.Nil(t, err) // want "error-nil: use assert\\.NoError" | ||
assert.Equal(t, resultInt, 42) // want "expected-actual: need to reverse actual and expected values" | ||
assert.Equal(t, resultFloat, 42.42) // want "float-compare: use assert\\.InEpsilon \\(or InDelta\\)" | ||
assert.Equal(t, len(arr), 10) // want "len: use assert\\.Len" | ||
|
||
assert.True(t, predicate) | ||
assert.Equal(t, resultInt, 1) // want "expected-actual: need to reverse actual and expected values" | ||
assert.Empty(t, arr) | ||
assert.ErrorIs(t, err, io.EOF) // want "require-error: for error assertions use require" | ||
assert.NoError(t, err) // want "require-error: for error assertions use require" | ||
assert.Equal(t, 42, resultInt) | ||
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") // want "bool-compare: use assert\\.True" | ||
assert.Equal(t, predicate, true, "message %d", 42) // want "bool-compare: use assert\\.True" | ||
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" | ||
}) | ||
} | ||
|
||
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) // want "suite-extra-assert-call: need to simplify the assertion to s\\.True" | ||
} |