From 6f384926cf0a5c4db328ccd6e06d526facbf905c Mon Sep 17 00:00:00 2001 From: golangci Date: Tue, 8 May 2018 13:33:00 +0300 Subject: [PATCH] fixes --- Makefile | 2 ++ internal/commands/run.go | 6 ++++-- pkg/config/config.go | 1 + pkg/enabled_linters_test.go | 9 ++++++++- pkg/fsutils/fsutils.go | 3 ++- pkg/golinters/golint.go | 9 +++++---- pkg/printers/text.go | 7 ++++++- pkg/testdata/with_issues/golint.go | 6 ++++++ 8 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..006a8ab8 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +test: + go test -v ./... diff --git a/internal/commands/run.go b/internal/commands/run.go index 6d9c6bf8..da5b6a8f 100644 --- a/internal/commands/run.go +++ b/internal/commands/run.go @@ -37,6 +37,7 @@ func (e *Executor) initRun() { config.OutFormatColoredLineNumber, fmt.Sprintf("Format of output: %s", strings.Join(config.OutFormats, "|"))) runCmd.Flags().BoolVar(&rc.PrintIssuedLine, "print-issued-lines", true, "Print lines of code with issue") + runCmd.Flags().BoolVar(&rc.PrintLinterName, "print-linter-name", true, "Print linter name in issue line") runCmd.Flags().IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code", 1, "Exit code when issues were found") @@ -53,7 +54,7 @@ func (e *Executor) initRun() { runCmd.Flags().BoolVar(&rc.Gofmt.Simplify, "gofmt.simplify", true, "Gofmt: simplify code") runCmd.Flags().IntVar(&rc.Gocyclo.MinComplexity, "gocyclo.min-complexity", - 50, "Minimal complexity of function to report it") + 30, "Minimal complexity of function to report it") runCmd.Flags().BoolVar(&rc.Structcheck.CheckExportedFields, "structcheck.exported-fields", false, "Structcheck: report about unused exported struct fields") runCmd.Flags().BoolVar(&rc.Varcheck.CheckExportedFields, "varcheck.exported-fields", false, "Varcheck: report about unused exported variables") @@ -240,7 +241,8 @@ func (e *Executor) executeRun(cmd *cobra.Command, args []string) { if e.cfg.Run.OutFormat == config.OutFormatJSON { p = printers.NewJSON() } else { - p = printers.NewText(e.cfg.Run.PrintIssuedLine, e.cfg.Run.OutFormat == config.OutFormatColoredLineNumber) + p = printers.NewText(e.cfg.Run.PrintIssuedLine, + e.cfg.Run.OutFormat == config.OutFormatColoredLineNumber, e.cfg.Run.PrintLinterName) } gotAnyIssues, err := p.Print(issues) if err != nil { diff --git a/pkg/config/config.go b/pkg/config/config.go index 5e9cd512..f4e87e8e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -35,6 +35,7 @@ type Run struct { // nolint:maligned OutFormat string PrintIssuedLine bool + PrintLinterName bool ExitCodeIfIssuesFound int diff --git a/pkg/enabled_linters_test.go b/pkg/enabled_linters_test.go index c72d1e89..27edf6d9 100644 --- a/pkg/enabled_linters_test.go +++ b/pkg/enabled_linters_test.go @@ -49,7 +49,14 @@ func installBinary(t *testing.T) { func testOneSource(t *testing.T, sourcePath string) { goErrchkBin := filepath.Join(runtime.GOROOT(), "test", "errchk") - cmd := exec.Command(goErrchkBin, binName, "run", "--enable-all", "--gocyclo.min-complexity", "20", sourcePath) + cmd := exec.Command(goErrchkBin, binName, "run", + "--enable-all", + "--dupl.threshold=20", + "--gocyclo.min-complexity=20", + "--print-issued-lines=false", + "--print-linter-name=false", + "--out-format=line-number", + sourcePath) runGoErrchk(cmd, t) } diff --git a/pkg/fsutils/fsutils.go b/pkg/fsutils/fsutils.go index 11db33c3..0a3baa17 100644 --- a/pkg/fsutils/fsutils.go +++ b/pkg/fsutils/fsutils.go @@ -34,7 +34,8 @@ func (p ProjectPaths) MixedPaths() []string { func (p ProjectPaths) FilesGrouppedByDirs() [][]string { dirToFiles := map[string][]string{} for _, f := range p.Files { - dirToFiles[filepath.Dir(f)] = append(dirToFiles[f], f) + dir := filepath.Dir(f) + dirToFiles[dir] = append(dirToFiles[dir], f) } ret := [][]string{} diff --git a/pkg/golinters/golint.go b/pkg/golinters/golint.go index dbf099fe..f79daf25 100644 --- a/pkg/golinters/golint.go +++ b/pkg/golinters/golint.go @@ -18,7 +18,7 @@ func (Golint) Name() string { func (g Golint) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) { var issues []result.Issue for _, pkgFiles := range lintCtx.Paths.FilesGrouppedByDirs() { - i, err := lintFiles(lintCtx.RunCfg().Golint.MinConfidence, pkgFiles...) + i, err := g.lintFiles(lintCtx.RunCfg().Golint.MinConfidence, pkgFiles...) if err != nil { // TODO: skip and warn return nil, fmt.Errorf("can't lint files %s: %s", lintCtx.Paths.Files, err) @@ -29,7 +29,7 @@ func (g Golint) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, erro return issues, nil } -func lintFiles(minConfidence float64, filenames ...string) ([]result.Issue, error) { +func (g Golint) lintFiles(minConfidence float64, filenames ...string) ([]result.Issue, error) { files := make(map[string][]byte) for _, filename := range filenames { src, err := ioutil.ReadFile(filename) @@ -49,8 +49,9 @@ func lintFiles(minConfidence float64, filenames ...string) ([]result.Issue, erro for _, p := range ps { if p.Confidence >= minConfidence { issues = append(issues, result.Issue{ - Pos: p.Position, - Text: p.Text, + Pos: p.Position, + Text: p.Text, + FromLinter: g.Name(), }) // TODO: use p.Link and p.Category } diff --git a/pkg/printers/text.go b/pkg/printers/text.go index 167d85df..d6cac7ce 100644 --- a/pkg/printers/text.go +++ b/pkg/printers/text.go @@ -14,12 +14,14 @@ import ( type Text struct { printIssuedLine bool useColors bool + printLinterName bool } -func NewText(printIssuedLine bool, useColors bool) *Text { +func NewText(printIssuedLine, useColors, printLinterName bool) *Text { return &Text{ printIssuedLine: printIssuedLine, useColors: useColors, + printLinterName: printLinterName, } } @@ -47,6 +49,9 @@ func (p Text) Print(issues chan result.Issue) (bool, error) { for i := range issues { gotAnyIssue = true text := p.SprintfColored(color.FgRed, "%s", i.Text) + if p.printLinterName { + text += fmt.Sprintf(" (%s)", i.FromLinter) + } pos := p.SprintfColored(color.Bold, "%s:%d", i.FilePath(), i.Line()) fmt.Fprintf(out, "%s: %s\n", pos, text) diff --git a/pkg/testdata/with_issues/golint.go b/pkg/testdata/with_issues/golint.go index 7ab20e53..0a99f978 100644 --- a/pkg/testdata/with_issues/golint.go +++ b/pkg/testdata/with_issues/golint.go @@ -13,3 +13,9 @@ type ExportedInterfaceWithNoComment interface{} // Bad comment // ERROR "comment on exported function ExportedFuncWithBadComment should be of the form .ExportedFuncWithBadComment \.\.\.." func ExportedFuncWithBadComment() {} + +type GolintTest struct{} + +func (receiver1 GolintTest) A() {} + +func (receiver2 GolintTest) B() {} // ERROR "receiver name receiver2 should be consistent with previous receiver name receiver1 for GolintTest"