
Use build.Import instead of manual parser.ParseFile and paths traversal. It allows: 1. support build tags for all linters. 2. analyze files only for current GOOS/GOARCH: less false-positives. 3. analyze xtest packages (*_test) by golint: upstream golint and gometalinter can't do it! And don't break analysis on the first xtest package like it was before. 4. proper handling of xtest packages for linters like goconst where package boundary is important: less false-positives is expected. Also: 1. reuse AST parsing for golint and goconst: minor speedup. 2. allow to specify path (not only name) regexp for --skip-files and --skip-dirs 3. add more default exclude filters for golint about commits: `(comment on exported (method|function)|should have( a package)? comment|comment should be of the form)` 4. print skipped dir in verbose (-v) mode 5. refactor per-linter tests: declare arguments in comments, run only one linter and in combination with slow linter
104 lines
2.5 KiB
Go
104 lines
2.5 KiB
Go
package test
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func runGoErrchk(c *exec.Cmd, t *testing.T) {
|
|
output, err := c.CombinedOutput()
|
|
assert.NoError(t, err, "Output:\n%s", output)
|
|
|
|
// Can't check exit code: tool only prints to output
|
|
assert.False(t, bytes.Contains(output, []byte("BUG")), "Output:\n%s", output)
|
|
}
|
|
|
|
const testdataDir = "testdata"
|
|
const binName = "golangci-lint"
|
|
|
|
func testSourcesFromDir(t *testing.T, dir string) {
|
|
t.Log(filepath.Join(dir, "*.go"))
|
|
sources, err := filepath.Glob(filepath.Join(dir, "*.go"))
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, sources)
|
|
|
|
installBinary(t)
|
|
|
|
for _, s := range sources {
|
|
s := s
|
|
t.Run(s, func(t *testing.T) {
|
|
t.Parallel()
|
|
testOneSource(t, s)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSourcesFromTestdataWithIssuesDir(t *testing.T) {
|
|
testSourcesFromDir(t, testdataDir)
|
|
}
|
|
|
|
func TestTypecheck(t *testing.T) {
|
|
testSourcesFromDir(t, filepath.Join(testdataDir, "notcompiles"))
|
|
}
|
|
|
|
func testOneSource(t *testing.T, sourcePath string) {
|
|
goErrchkBin := filepath.Join(runtime.GOROOT(), "test", "errchk")
|
|
args := []string{
|
|
binName, "run",
|
|
"--no-config",
|
|
"--disable-all",
|
|
"--print-issued-lines=false",
|
|
"--print-linter-name=false",
|
|
"--out-format=line-number",
|
|
}
|
|
|
|
for _, addArg := range []string{"", "-Etypecheck"} {
|
|
caseArgs := append([]string{}, args...)
|
|
caseArgs = append(caseArgs, getAdditionalArgs(t, sourcePath)...)
|
|
if addArg != "" {
|
|
caseArgs = append(caseArgs, addArg)
|
|
}
|
|
|
|
caseArgs = append(caseArgs, sourcePath)
|
|
|
|
cmd := exec.Command(goErrchkBin, caseArgs...)
|
|
t.Log(caseArgs)
|
|
runGoErrchk(cmd, t)
|
|
}
|
|
}
|
|
|
|
func getAdditionalArgs(t *testing.T, sourcePath string) []string {
|
|
data, err := ioutil.ReadFile(sourcePath)
|
|
assert.NoError(t, err)
|
|
|
|
lines := strings.SplitN(string(data), "\n", 2)
|
|
firstLine := lines[0]
|
|
|
|
parts := strings.Split(firstLine, "args:")
|
|
if len(parts) == 1 {
|
|
return nil
|
|
}
|
|
|
|
return strings.Split(parts[len(parts)-1], " ")
|
|
}
|
|
|
|
func TestGolintConsumesXTestFiles(t *testing.T) {
|
|
dir := filepath.Join(testdataDir, "withxtest")
|
|
const expIssue = "if block ends with a return statement, so drop this else and outdent its block"
|
|
|
|
out, ec := runGolangciLint(t, "--no-config", "--disable-all", "-Egolint", dir)
|
|
assert.Equal(t, 1, ec)
|
|
assert.Contains(t, out, expIssue)
|
|
|
|
out, ec = runGolangciLint(t, "--no-config", "--disable-all", "-Egolint", filepath.Join(dir, "p_test.go"))
|
|
assert.Equal(t, 1, ec)
|
|
assert.Contains(t, out, expIssue)
|
|
}
|