Fix , fix : use go/packages

Use go/packages instead of x/tools/loader: it allows to work
with go modules and speedups loading of packages with the help
of build cache.

A lot of linters became "fast": they are enabled by --fast now and
work in 1-2 seconds. Only unparam, interfacer and megacheck
are "slow" linters now.

Average project is analyzed 20-40% faster than before if all linters are
enabled! If we enable all linters except unparam, interfacer and
megacheck analysis is 10-20x faster!
This commit is contained in:
Denis Isaev 2018-10-21 17:06:51 +03:00 committed by Isaev Denis
parent a84578d603
commit 0421bac259
94 changed files with 5918 additions and 1748 deletions

View file

@ -15,7 +15,7 @@ import (
"github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/stretchr/testify/assert"
assert "github.com/stretchr/testify/require"
)
var root = filepath.Join("..", "...")
@ -36,8 +36,8 @@ func checkNoIssuesRun(t *testing.T, out string, exitCode int) {
}
func TestNoCongratsMessage(t *testing.T) {
out, exitCode := runGolangciLint(t, "../...")
assert.Equal(t, exitcodes.Success, exitCode)
out, exitCode := runGolangciLint(t, filepath.Join("..", "..."))
assert.Equal(t, exitcodes.Success, exitCode, out)
assert.Equal(t, "", out)
}
@ -51,26 +51,27 @@ func TestAutogeneratedNoIssues(t *testing.T) {
checkNoIssuesRun(t, out, exitCode)
}
func TestSymlinkLoop(t *testing.T) {
out, exitCode := runGolangciLint(t, filepath.Join(testdataDir, "symlink_loop", "..."))
checkNoIssuesRun(t, out, exitCode)
func TestEmptyDirRun(t *testing.T) {
out, exitCode := runGolangciLint(t, filepath.Join(testdataDir, "nogofiles"))
assert.Equal(t, exitcodes.NoGoFiles, exitCode)
assert.Contains(t, out, ": no go files to analyze")
}
func TestRunOnAbsPath(t *testing.T) {
absPath, err := filepath.Abs(filepath.Join(testdataDir, ".."))
assert.NoError(t, err)
func TestNotExistingDirRun(t *testing.T) {
out, exitCode := runGolangciLint(t, filepath.Join(testdataDir, "no_such_dir"))
assert.True(t, exitCode == exitcodes.WarningInTest || exitCode == exitcodes.IssuesFound)
assert.Contains(t, out, `cannot find package \"./testdata/no_such_dir\"`)
}
out, exitCode := runGolangciLint(t, "--no-config", "--fast", absPath)
checkNoIssuesRun(t, out, exitCode)
out, exitCode = runGolangciLint(t, "--no-config", absPath)
func TestSymlinkLoop(t *testing.T) {
out, exitCode := runGolangciLint(t, filepath.Join(testdataDir, "symlink_loop", "..."))
checkNoIssuesRun(t, out, exitCode)
}
func TestDeadline(t *testing.T) {
out, exitCode := runGolangciLint(t, "--deadline=1ms", root)
assert.Equal(t, exitcodes.Timeout, exitCode)
assert.Contains(t, out, "deadline exceeded: try increase it by passing --deadline option")
assert.Contains(t, strings.ToLower(out), "deadline exceeded: try increase it by passing --deadline option")
}
func runGolangciLint(t *testing.T, args ...string) (string, int) {
@ -125,8 +126,9 @@ func runGolangciLintWithYamlConfigWithCode(t *testing.T, cfg string, args ...str
}
func TestTestsAreLintedByDefault(t *testing.T) {
out, exitCode := runGolangciLint(t, "./testdata/withtests")
assert.Equal(t, exitcodes.Success, exitCode, out)
out, exitCode := runGolangciLint(t, filepath.Join(testdataDir, "withtests"))
assert.Equal(t, exitcodes.IssuesFound, exitCode)
assert.Contains(t, out, "if block ends with a return")
}
func TestCgoOk(t *testing.T) {
@ -134,11 +136,26 @@ func TestCgoOk(t *testing.T) {
checkNoIssuesRun(t, out, exitCode)
}
func TestCgoWithIssues(t *testing.T) {
out, exitCode := runGolangciLint(t, "--enable-all", filepath.Join(testdataDir, "cgo_with_issues"))
assert.Equal(t, exitcodes.IssuesFound, exitCode)
assert.Contains(t, out, "Printf format %t has arg cs of wrong type")
}
func TestUnsafeOk(t *testing.T) {
out, exitCode := runGolangciLint(t, "--enable-all", filepath.Join(testdataDir, "unsafe"))
checkNoIssuesRun(t, out, exitCode)
}
func TestSkippedDirs(t *testing.T) {
out, exitCode := runGolangciLint(t, "--print-issued-lines=false", "--no-config", "--skip-dirs", "skip_me", "-Egolint",
filepath.Join(testdataDir, "skipdirs", "..."))
assert.Equal(t, exitcodes.IssuesFound, exitCode)
assert.Equal(t, out,
"testdata/skipdirs/examples_no_skip/with_issue.go:8:9: if block ends with "+
"a return statement, so drop this else and outdent its block (golint)\n")
}
func TestDeadcodeNoFalsePositivesInMainPkg(t *testing.T) {
out, exitCode := runGolangciLint(t, "--no-config", "--disable-all", "-Edeadcode",
filepath.Join(testdataDir, "deadcode_main_pkg"))
@ -173,7 +190,7 @@ func getEnabledByDefaultFastLintersExcept(except ...string) []string {
ebdl := m.GetAllEnabledByDefaultLinters()
ret := []string{}
for _, lc := range ebdl {
if lc.DoesFullImport {
if lc.NeedsSSARepr {
continue
}
@ -189,7 +206,7 @@ func getAllFastLintersWith(with ...string) []string {
linters := lintersdb.NewManager().GetAllSupportedLinterConfigs()
ret := append([]string{}, with...)
for _, lc := range linters {
if lc.DoesFullImport {
if lc.NeedsSSARepr {
continue
}
ret = append(ret, lc.Name())
@ -212,7 +229,7 @@ func getEnabledByDefaultFastLintersWith(with ...string) []string {
ebdl := lintersdb.NewManager().GetAllEnabledByDefaultLinters()
ret := append([]string{}, with...)
for _, lc := range ebdl {
if lc.DoesFullImport {
if lc.NeedsSSARepr {
continue
}
@ -337,8 +354,8 @@ func TestEnabledLinters(t *testing.T) {
},
{
name: "fast option combined with enable and enable-all",
args: "--enable-all --fast --enable=typecheck",
el: getAllFastLintersWith("typecheck"),
args: "--enable-all --fast --enable=megacheck",
el: getAllFastLintersWith("megacheck"),
noImplicitFast: true,
},
}
@ -361,15 +378,6 @@ func TestEnabledLinters(t *testing.T) {
}
}
func TestGovetInFastMode(t *testing.T) {
cfg := `
linters-settings:
use-installed-packages: true
`
out := runGolangciLintWithYamlConfig(t, cfg, "--fast", "-Egovet", root)
assert.Equal(t, noIssuesOut, out)
}
func TestEnabledPresetsAreNotDuplicated(t *testing.T) {
out, _ := runGolangciLint(t, "--no-config", "-v", "-p", "style,bugs")
assert.Contains(t, out, "Active presets: [bugs style]")