Skip to content
This repository has been archived by the owner on Apr 10, 2019. It is now read-only.

Commit

Permalink
Set errcheck -ignoretests when tests are not enabled (#377)
Browse files Browse the repository at this point in the history
* Add -ignoretest to errcheck when checking tests is not enabled

Add a test for var substitution
Remove unused vars from linterState

Signed-off-by: Daniel Nephin <dnephin@gmail.com>

* Use not_tests for unparam as well
  • Loading branch information
dnephin authored and alecthomas committed Oct 19, 2017
1 parent 7f9672e commit 9165748
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 11 deletions.
20 changes: 11 additions & 9 deletions execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,29 @@ func (v Vars) Replace(s string) string {

type linterState struct {
*Linter
id int
paths []string
issues chan *Issue
vars Vars
exclude *regexp.Regexp
include *regexp.Regexp
deadline <-chan time.Time
}

func (l *linterState) Partitions() ([][]string, error) {
command := l.vars.Replace(l.Command)
cmdArgs, err := parseCommand(command)
func (l *linterState) Partitions(paths []string) ([][]string, error) {
cmdArgs, err := parseCommand(l.command())
if err != nil {
return nil, err
}
parts, err := l.Linter.PartitionStrategy(cmdArgs, l.paths)
parts, err := l.Linter.PartitionStrategy(cmdArgs, paths)
if err != nil {
return nil, err
}
return parts, nil
}

func (l *linterState) command() string {
return l.vars.Replace(l.Command)
}

func runLinters(linters map[string]*Linter, paths []string, concurrency int, exclude, include *regexp.Regexp) (chan *Issue, chan error) {
errch := make(chan error, len(linters))
concurrencych := make(chan bool, concurrency)
Expand All @@ -85,9 +86,11 @@ func runLinters(linters map[string]*Linter, paths []string, concurrency int, exc
"min_occurrences": fmt.Sprintf("%d", config.MinOccurrences),
"min_const_length": fmt.Sprintf("%d", config.MinConstLength),
"tests": "",
"not_tests": "true",
}
if config.Test {
vars["tests"] = "-t"
vars["tests"] = "true"
vars["not_tests"] = ""
}

wg := &sync.WaitGroup{}
Expand All @@ -97,14 +100,13 @@ func runLinters(linters map[string]*Linter, paths []string, concurrency int, exc
state := &linterState{
Linter: linter,
issues: incomingIssues,
paths: paths,
vars: vars,
exclude: exclude,
include: include,
deadline: deadline,
}

partitions, err := state.Partitions()
partitions, err := state.Partitions(paths)
if err != nil {
errch <- err
continue
Expand Down
67 changes: 67 additions & 0 deletions execute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestLinterStateCommand(t *testing.T) {
varsDefault := Vars{"tests": "", "not_tests": "true"}
varsWithTest := Vars{"tests": "true", "not_tests": ""}

var testcases = []struct {
linter string
vars Vars
expected string
}{
{
linter: "errcheck",
vars: varsWithTest,
expected: `errcheck -abspath `,
},
{
linter: "errcheck",
vars: varsDefault,
expected: `errcheck -abspath -ignoretests`,
},
{
linter: "gotype",
vars: varsDefault,
expected: `gotype -e `,
},
{
linter: "gotype",
vars: varsWithTest,
expected: `gotype -e -t`,
},
{
linter: "structcheck",
vars: varsDefault,
expected: `structcheck `,
},
{
linter: "structcheck",
vars: varsWithTest,
expected: `structcheck -t`,
},
{
linter: "unparam",
vars: varsDefault,
expected: `unparam -tests=false`,
},
{
linter: "unparam",
vars: varsWithTest,
expected: `unparam `,
},
}

for _, testcase := range testcases {
ls := linterState{
Linter: getLinterByName(testcase.linter, LinterConfig{}),
vars: testcase.vars,
}
assert.Equal(t, testcase.expected, ls.command())
}
}
4 changes: 2 additions & 2 deletions linters.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ var defaultLinters = map[string]LinterConfig{
IsFast: true,
},
"errcheck": {
Command: `errcheck -abspath`,
Command: `errcheck -abspath {not_tests=-ignoretests}`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/kisielk/errcheck",
PartitionStrategy: partitionPathsAsPackages,
Expand Down Expand Up @@ -368,7 +368,7 @@ var defaultLinters = map[string]LinterConfig{
defaultEnabled: true,
},
"unparam": {
Command: `unparam`,
Command: `unparam {not_tests=-tests=false}`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "mvdan.cc/unparam",
PartitionStrategy: partitionPathsAsPackages,
Expand Down

0 comments on commit 9165748

Please sign in to comment.