Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add '--only-tests' support #43

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion faillint/faillint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package faillint

import (
"errors"
"fmt"
"go/ast"
"go/token"
Expand Down Expand Up @@ -62,13 +63,15 @@ type path struct {
type faillint struct {
paths string // -paths flag
ignoretests bool // -ignore-tests flag
onlyTests bool // -only-tests flag
}

// NewAnalyzer create a faillint analyzer.
func NewAnalyzer() *analysis.Analyzer {
f := faillint{
paths: "",
ignoretests: false,
onlyTests: false,
}
a := &analysis.Analyzer{
Name: "faillint",
Expand All @@ -90,7 +93,9 @@ Fail on the usage of prometheus.DefaultGatherer and prometheus.MustRegister

Fail on the usage of errors, golang.org/x/net and all sub packages under golang.org/x/net
-paths errors,golang.org/x/net/...`)

a.Flags.BoolVar(&f.ignoretests, "ignore-tests", false, "ignore all _test.go files")
a.Flags.BoolVar(&f.onlyTests, "only-tests", false, "include only _test.go files")
return a
}

Expand All @@ -111,6 +116,10 @@ func (f *faillint) run(pass *analysis.Pass) (interface{}, error) {
return nil, nil
}

if f.ignoretests && f.onlyTests {
return nil, errors.New("--ignore-tests and --only-tests flags cannot be used together")
}

for _, file := range pass.Files {
filename := pass.Fset.File(file.Package).Name()
isGenerated, err := generated.ParseFile(filename)
Expand All @@ -122,9 +131,16 @@ func (f *faillint) run(pass *analysis.Pass) (interface{}, error) {
continue
}

if f.ignoretests && strings.Contains(pass.Fset.File(file.Package).Name(), "_test.go") {
isTestFile := strings.Contains(pass.Fset.File(file.Package).Name(), "_test.go")

if f.ignoretests && isTestFile {
continue
}

if f.onlyTests && !isTestFile {
continue
}

if anyHasDirective(pass, file.Comments, fileIgnoreKey) {
continue
}
Expand Down
18 changes: 17 additions & 1 deletion faillint/faillint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,21 @@ func TestRun(t *testing.T) {
dir string
paths string

ignoreTestFiles bool
ignoreTestFiles bool
onlyTestFunctions bool
}{
{
name: "sleep in a function which is not a test",
dir: "sleepintest",
paths: "time.{Sleep}",
onlyTestFunctions: true,
},
{
name: "sleep in a function which is a test",
dir: "sleepintest_err",
paths: "time.{Sleep}",
onlyTestFunctions: true,
},
{
name: "unwanted errors package present",
dir: "a",
Expand Down Expand Up @@ -320,6 +333,9 @@ func TestRun(t *testing.T) {
if tcase.ignoreTestFiles {
f.Flags.Set("ignore-tests", "true")
}
if tcase.onlyTestFunctions {
f.Flags.Set("only-tests", "true")
}

// No assertion on result is required as 'analysistest' is for that.
// All expected diagnosis should be specified by comment in affected file starting with `// want`.
Expand Down
9 changes: 9 additions & 0 deletions faillint/testdata/src/sleepintest/sleepintest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package sleepintest

import (
"time"
)

func fooTest() {
time.Sleep(1 * time.Second)
}
10 changes: 10 additions & 0 deletions faillint/testdata/src/sleepintest_err/sleepintest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sleepintest

import (
"testing"
"time"
)

func TestFoo(t *testing.T) {
time.Sleep(1 * time.Second) // want `declaration "Sleep" from package "time" shouldn't be used`
}
Loading