Skip to content

Commit

Permalink
Merge pull request #43 from fatih/add-only-tests
Browse files Browse the repository at this point in the history
Add '--only-tests' support
  • Loading branch information
fatih authored Apr 16, 2024
2 parents a9254ba + 42a2b64 commit 6cffdde
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
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`
}

0 comments on commit 6cffdde

Please sign in to comment.