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 flag to ignore test files #5

Merged
merged 3 commits into from
Mar 6, 2020
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
40 changes: 27 additions & 13 deletions faillint/faillint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,39 @@ import (
"golang.org/x/tools/go/analysis"
)

// Analyzer of the linter
var Analyzer = &analysis.Analyzer{
Name: "faillint",
Doc: "report unwanted import path usages",
Run: run,
RunDespiteErrors: true,
type faillint struct {
paths string // -paths flag
ignoretests bool // -ignore-tests flag
}

var paths string // -paths flag
// Analyzer global instance of the linter (if possible use NewAnalyzer)
var Analyzer = NewAnalyzer()

func init() {
// seems like using init() is the only way to add our own flags
Analyzer.Flags.StringVar(&paths, "paths", paths, "import paths to fail")
// NewAnalyzer create a faillint analyzer
func NewAnalyzer() *analysis.Analyzer {
f := faillint{
paths: "",
ignoretests: false,
}
a := &analysis.Analyzer{
Name: "faillint",
Doc: "report unwanted import path usages",
Run: f.run,
RunDespiteErrors: true,
}

a.Flags.StringVar(&f.paths, "paths", "", "import paths to fail")
a.Flags.BoolVar(&f.ignoretests, "ignore-tests", false, "ignore all _test.go files and packages.")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be now only ignore all _test.go files

return a
}

// Run is the runner for an analysis pass
func run(pass *analysis.Pass) (interface{}, error) {
if paths == "" {
func (f *faillint) run(pass *analysis.Pass) (interface{}, error) {
if f.paths == "" {
return nil, nil
}

p := strings.Split(paths, ",")
p := strings.Split(f.paths, ",")

suggestions := make(map[string]string, len(p))
imports := make([]string, 0, len(p))
Expand All @@ -51,6 +62,9 @@ func run(pass *analysis.Pass) (interface{}, error) {
}

for _, file := range pass.Files {
if f.ignoretests && strings.Contains(pass.Fset.File(file.Package).Name(), "_test.go") {
continue
}
for _, path := range imports {
imp := usesImport(file, path)
if imp == nil {
Expand Down
53 changes: 39 additions & 14 deletions faillint/faillint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,60 @@ func Test(t *testing.T) {
testdata := analysistest.TestData()

var tests = []struct {
name string
paths string
name string
paths string
ignoretests bool
}{
{
name: "a",
paths: "errors",
name: "a",
paths: "errors",
ignoretests: false,
},
{
name: "b",
paths: "",
name: "b",
paths: "",
ignoretests: false,
},
{
name: "c",
paths: "errors=", // malformed suggestion
name: "c",
paths: "errors=", // malformed suggestion
ignoretests: false,
},
{
name: "d",
paths: "errors=github.com/pkg/errors",
name: "d",
paths: "errors=github.com/pkg/errors",
ignoretests: false,
},
{
name: "e",
paths: "errors=github.com/pkg/errors,golang.org/x/net/context=context",
name: "e",
paths: "errors=github.com/pkg/errors,golang.org/x/net/context=context",
ignoretests: false,
},
{
name: "f",
paths: "errors",
ignoretests: true,
},
{
name: "g",
paths: "errors",
ignoretests: true,
},
{
name: "h",
paths: "errors",
ignoretests: false,
},
}
for _, ts := range tests {
ts := ts
t.Run(ts.name, func(t *testing.T) {
faillint.Analyzer.Flags.Set("paths", ts.paths)
analysistest.Run(t, testdata, faillint.Analyzer, ts.name)
a := faillint.NewAnalyzer()
a.Flags.Set("paths", ts.paths)
if ts.ignoretests {
a.Flags.Set("ignore-tests", "true")
}
analysistest.Run(t, testdata, a, ts.name)
})
}
}
9 changes: 9 additions & 0 deletions faillint/testdata/src/f/f.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package f

import (
"errors" // want `package "errors" shouldn't be imported`
)

func foo() error {
return errors.New("bar!")
}
10 changes: 10 additions & 0 deletions faillint/testdata/src/f/f_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package f_test

import (
"errors"
"testing"
)

func TestFoo(t *testing.T) {
t.Errorf("Got bar error: %g", errors.New("bar!"))
}
9 changes: 9 additions & 0 deletions faillint/testdata/src/g/g.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package g

import (
"errors" // want `package "errors" shouldn't be imported`
)

func foo() error {
return errors.New("bar!")
}
10 changes: 10 additions & 0 deletions faillint/testdata/src/g/g_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package g

import (
"errors"
"testing"
)

func TestFoo(t *testing.T) {
t.Errorf("Got bar error: %g", errors.New("bar!"))
}
9 changes: 9 additions & 0 deletions faillint/testdata/src/h/h.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package h

import (
"errors" // want `package "errors" shouldn't be imported`
)

func foo() error {
return errors.New("bar!")
}
10 changes: 10 additions & 0 deletions faillint/testdata/src/h/h_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package h_test

import (
"errors" // want `package "errors" shouldn't be imported`
"testing"
)

func TestFoo(t *testing.T) {
t.Errorf("Got bar error: %g", errors.New("bar!"))
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

func main() {
singlechecker.Main(faillint.Analyzer)
singlechecker.Main(faillint.NewAnalyzer())
}