Skip to content

Commit

Permalink
Merge pull request #4 from Antonboom/feat/analyze-all-files
Browse files Browse the repository at this point in the history
Do not ignore not test files
  • Loading branch information
Antonboom authored Oct 4, 2023
2 parents 7caf9ef + 6be4d96 commit 724570e
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 91 deletions.
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ issues:
linters: [ "revive" ]
text: "exported"

- path: "internal/testgen"
linters: [ "forbidigo", "gochecknoinits" ] # Internal test generation tool.

- path: "internal"
linters: [ "revive" ]
text: "exported (method|const)"
Expand All @@ -33,6 +36,9 @@ linters-settings:

forbidigo:
forbid:
- p: panic
msg: Please, don't panic

- p: types\.ExprString
msg: Please, use analysisutil.NodeBytes/NodeString instead

Expand Down
3 changes: 0 additions & 3 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ type testifyLint struct {
func (tl *testifyLint) run(pass *analysis.Pass) (any, error) {
filesToAnalysis := make([]*ast.File, 0, len(pass.Files))
for _, f := range pass.Files {
if !analysisutil.IsTestFile(pass.Fset, f) {
continue
}
if !analysisutil.Imports(f, testify.AssertPkgPath, testify.RequirePkgPath, testify.SuitePkgPath) {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestTestifyLint(t *testing.T) {
},
},
{dir: "ginkgo"},
{dir: "not-test-file"}, // We ignores regular files.
{dir: "not-true-testify"}, // We ignores stretchr/testify's forks.
{dir: "not-test-file"}, // By default, linter checks regular files too.
{dir: "not-true-testify"}, // Linter ignores stretchr/testify's forks.
{dir: "pkg-alias"},
{
dir: "suite-require-extra-assert-call",
Expand Down
10 changes: 10 additions & 0 deletions analyzer/testdata/src/not-test-file/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package service

import (
"testing"
)

func TestService(t *testing.T) {
var predicate bool
assertTrue(t, predicate)
}
14 changes: 0 additions & 14 deletions analyzer/testdata/src/not-test-file/test_file.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main
package service

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNotFake(t *testing.T) {
var predicate bool
func assertTrue(t *testing.T, predicate bool) {
t.Helper()

assert.Equal(t, true, predicate) // want "bool-compare: use assert\\.True"
assert.Equalf(t, true, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.True"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main
package service

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNotFake(t *testing.T) {
var predicate bool
func assertTrue(t *testing.T, predicate bool) {
t.Helper()

assert.True(t, predicate) // want "bool-compare: use assert\\.True"
assert.Truef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.True"
Expand Down
9 changes: 0 additions & 9 deletions internal/analysisutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@ package analysisutil

import (
"go/ast"
"go/token"
"strconv"
"strings"
)

// IsTestFile returns true if the file from the file set has the `_test.go` suffix.
// If the file does not belong to the set, then the function will return false.
func IsTestFile(fset *token.FileSet, file *ast.File) bool {
fname := fset.Position(file.Pos()).Filename
return strings.HasSuffix(fname, "_test.go")
}

// Imports tells if the file imports at least one of the packages.
// If no packages provided then function returns false.
func Imports(file *ast.File, pkgs ...string) bool {
Expand Down
55 changes: 0 additions & 55 deletions internal/analysisutil/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,6 @@ import (
"github.com/Antonboom/testifylint/internal/analysisutil"
)

func TestIsTestFile(t *testing.T) {
cases := []struct {
filename string
fileSrc string
isTestFile bool
}{
{
filename: "service.go",
fileSrc: "package service",
isTestFile: false,
},
{
filename: "service_unix.go",
fileSrc: "package service",
isTestFile: false,
},
{
filename: "service_test.go",
fileSrc: "package service",
isTestFile: true,
},
{
filename: "service_test.go",
fileSrc: "package service_test",
isTestFile: true,
},
}

for _, tt := range cases {
t.Run("", func(t *testing.T) {
fset := token.NewFileSet()

file, err := parser.ParseFile(fset, tt.filename, tt.fileSrc, parser.PackageClauseOnly)
if err != nil {
t.Fatal(err)
}

if tt.isTestFile != analysisutil.IsTestFile(fset, file) {
t.FailNow()
}
})
}
}

func TestIsTestFile_FileIsNotInFileSet(t *testing.T) {
file, err := parser.ParseFile(token.NewFileSet(), "service_test.go", "package service_test", parser.PackageClauseOnly)
if err != nil {
t.Fatal(err)
}

if analysisutil.IsTestFile(token.NewFileSet(), file) {
t.FailNow()
}
}

func TestImports(t *testing.T) {
fset := token.NewFileSet()

Expand Down
2 changes: 1 addition & 1 deletion internal/checkers/bool_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func xor(a, b bool) bool {
// anyVal returns the first value[i] for which bools[i] is true.
func anyVal[T any](bools []bool, vals ...T) (T, bool) {
if len(bools) != len(vals) {
panic("inconsistent usage of valOr")
panic("inconsistent usage of valOr") //nolint:forbidigo // Does not depend on the code being analyzed.
}

for i, b := range bools {
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
// NewDefault builds default testifylint config.
func NewDefault() Config {
return Config{
EnableAll: false,
EnabledCheckers: checkers.EnabledByDefault(),
ExpectedActual: ExpectedActualConfig{
ExpVarPattern: RegexpValue{checkers.DefaultExpectedVarPattern},
Expand Down
7 changes: 7 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ import (

func TestNewDefault(t *testing.T) {
cfg := config.NewDefault()

if cfg.EnableAll {
t.Fatal()
}
if !slices.Equal(cfg.EnabledCheckers, checkers.EnabledByDefault()) {
t.Fatal()
}
if cfg.ExpectedActual.ExpVarPattern.String() != checkers.DefaultExpectedVarPattern.String() {
t.Fatal()
}
if cfg.SuiteExtraAssertCall.Mode != checkers.SuiteExtraAssertCallModeRemove {
t.Fatal()
}
}

func TestBindToFlags(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/testgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var checkerTestsGenerators = []CheckerTestsGenerator{
SuiteTHelperTestsGenerator{},
}

func init() { //nolint:gochecknoinits // Internal test generation tool.
func init() {
genForChecker := make(map[string]struct{}, len(checkerTestsGenerators))
for _, g := range checkerTestsGenerators {
name := g.Checker().Name()
Expand Down

0 comments on commit 724570e

Please sign in to comment.