From d90e955de73fe43de7411470ed4c1e4e28192bd6 Mon Sep 17 00:00:00 2001 From: Mario Carrion Date: Tue, 11 Jun 2019 22:20:49 -0400 Subject: [PATCH] nit: include `_test.go` files --- cmd/nit/main.go | 29 ++++++++++++++++++++--------- file_sections_test.go | 2 ++ imports_test.go | 6 ++++++ nitpicking.go | 1 - 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/cmd/nit/main.go b/cmd/nit/main.go index 6a0fff3..badf56d 100644 --- a/cmd/nit/main.go +++ b/cmd/nit/main.go @@ -6,6 +6,7 @@ import ( "go/build" "os" "path/filepath" + "strings" "github.com/MarioCarrion/nit" ) @@ -28,6 +29,7 @@ func main() { localPkg := flag.String("pkg", "", "local package") skipGenerated := flag.Bool("skip-generated", false, "skip generated files") + includeTests := flag.Bool("include-tests", false, "include test files") showVersion := flag.Bool("version", false, "prints current version information") flag.Parse() @@ -45,26 +47,35 @@ func main() { var failed bool - for _, pkg := range flag.Args() { - p, err := build.Import(pkg, ".", 0) - if err != nil { - fmt.Printf("error importing %s\n", pkg) - os.Exit(1) - } + nitpick := func(files []string) { + for _, f := range files { + if strings.HasSuffix(f, "_test.go") && !*includeTests { + continue + } - for _, f := range p.GoFiles { - fullpath := filepath.Join(p.Dir, f) v := nit.Nitpicker{ LocalPath: *localPkg, SkipGeneratedFile: *skipGenerated, } - if err := v.Validate(fullpath); err != nil { + + if err := v.Validate(f); err != nil { failed = true fmt.Println(err) } } } + for _, pkg := range flag.Args() { + p, err := build.Import(pkg, ".", 0) + if err != nil { + fmt.Printf("error importing %s\n", pkg) + os.Exit(1) + } + + gofiles, _ := filepath.Glob(filepath.Join(p.Dir, "*.go")) + nitpick(gofiles) + } + if failed { os.Exit(1) } diff --git a/file_sections_test.go b/file_sections_test.go index 0da5028..d5e5c76 100644 --- a/file_sections_test.go +++ b/file_sections_test.go @@ -20,6 +20,8 @@ func TestNewFileSectionTransition(t *testing.T) { } } +//- + func TestFileSectionMachine(t *testing.T) { s := nit.FileSectionMachine{} diff --git a/imports_test.go b/imports_test.go index 6d12c2b..5ce399f 100644 --- a/imports_test.go +++ b/imports_test.go @@ -44,6 +44,8 @@ func TestNewImportsSection(t *testing.T) { } } +//- + //nolint:dupl func TestImportsValidator(t *testing.T) { tests := [...]struct { @@ -98,6 +100,8 @@ func TestImportsValidator(t *testing.T) { } } +//- + func TestNewImportsTransition(t *testing.T) { tests := [...]struct { name string @@ -136,6 +140,8 @@ func TestNewImportsTransition(t *testing.T) { } } +//- + func TestImportsTransitionExternal(t *testing.T) { i, _ := nit.NewImportsTransition(nit.ImportsSectionExternal) diff --git a/nitpicking.go b/nitpicking.go index 6a90726..6b03eee 100644 --- a/nitpicking.go +++ b/nitpicking.go @@ -37,7 +37,6 @@ func (v *Nitpicker) Validate(filename string) error { } for _, s := range f.Decls { - // fmt.Printf("%d == %T - %+v -- %t\n", v.fset.PositionFor(s.Pos(), false).Line, s, s, s.End().IsValid()) if err := v.validateToken(s); err != nil { return err }