fix small bugs

This commit is contained in:
golangci 2018-05-07 17:14:24 +03:00
parent 100efea259
commit c9bb265140
24 changed files with 48 additions and 17 deletions

2
Gopkg.lock generated
View File

@ -82,7 +82,7 @@
"unused",
"version"
]
revision = "f557f368b7f3d00a54ed44eb57b9eca59e85cee1"
revision = "b5abc2a37fd3a93a3716b0bc4da42d47b90ecaab"
[[projects]]
branch = "master"

View File

@ -153,7 +153,7 @@ func buildLintCtx(ctx context.Context, linters []pkg.Linter, cfg *config.Config)
args = []string{"./..."}
}
paths, err := fsutils.GetPathsForAnalysis(args)
paths, err := fsutils.GetPathsForAnalysis(ctx, args)
if err != nil {
return nil, err
}

View File

@ -1,4 +1,4 @@
package golinters
package pkg
import (
"bytes"
@ -19,11 +19,15 @@ func runGoErrchk(c *exec.Cmd, t *testing.T) {
}
const testdataDir = "testdata"
var testdataWithIssuesDir = filepath.Join(testdataDir, "with_issues")
var testdataNotCompilingDir = filepath.Join(testdataDir, "not_compiles")
const binName = "golangci-lint"
func TestSourcesFromTestdataDir(t *testing.T) {
t.Log(filepath.Join(testdataDir, "*.go"))
sources, err := filepath.Glob(filepath.Join(testdataDir, "*.go"))
func TestSourcesFromTestdataWithIssuesDir(t *testing.T) {
t.Log(filepath.Join(testdataWithIssuesDir, "*.go"))
sources, err := filepath.Glob(filepath.Join(testdataWithIssuesDir, "*.go"))
assert.NoError(t, err)
assert.NotEmpty(t, sources)
@ -39,7 +43,7 @@ func TestSourcesFromTestdataDir(t *testing.T) {
}
func installBinary(t *testing.T) {
cmd := exec.Command("go", "install", filepath.Join("..", "..", "cmd", binName))
cmd := exec.Command("go", "install", filepath.Join("..", "cmd", binName))
assert.NoError(t, cmd.Run(), "Can't go install %s", binName)
}
@ -48,3 +52,9 @@ func testOneSource(t *testing.T, sourcePath string) {
cmd := exec.Command(goErrchkBin, binName, "run", "--enable-all", "--gocyclo.min-complexity", "20", sourcePath)
runGoErrchk(cmd, t)
}
func TestNotCompilingProgram(t *testing.T) {
installBinary(t)
err := exec.Command(binName, "run", "--enable-all", testdataNotCompilingDir).Run()
assert.NoError(t, err)
}

View File

@ -8,6 +8,7 @@ import (
"path"
"path/filepath"
"strings"
"time"
"github.com/golangci/golangci-shared/pkg/analytics"
)
@ -54,14 +55,20 @@ func processPaths(root string, paths []string, maxPaths int) ([]string, error) {
return ret, nil
}
func GetPathsForAnalysis(inputPaths []string) (*ProjectPaths, error) {
func GetPathsForAnalysis(ctx context.Context, inputPaths []string) (ret *ProjectPaths, err error) {
defer func(startedAt time.Time) {
if ret != nil {
analytics.Log(ctx).Infof("Found paths for analysis for %s: %s", time.Since(startedAt), ret.MixedPaths())
}
}(time.Now())
for _, path := range inputPaths {
if strings.HasSuffix(path, ".go") && len(inputPaths) != 1 {
return nil, fmt.Errorf("Specific files for analysis are allowed only if one file is set")
}
}
excludeDirs := []string{"vendor", "testdata", "examples", "Godeps"}
excludeDirs := []string{"vendor", "testdata", "examples", "Godeps", "builtin"}
pr := NewPathResolver(excludeDirs, []string{".go"})
paths, err := pr.Resolve(inputPaths...)
if err != nil {

View File

@ -37,8 +37,8 @@ func runLinter(ctx context.Context, linter Linter, lintCtx *golinters.Context, i
}()
startedAt := time.Now()
res, err = linter.Run(ctx, lintCtx)
analytics.Log(ctx).Infof("worker #%d: linter %s took %s for paths %s", i, linter.Name(),
time.Since(startedAt), lintCtx.Paths.MixedPaths())
analytics.Log(ctx).Infof("worker #%d: linter %s took %s", i, linter.Name(),
time.Since(startedAt))
return
}

3
pkg/testdata/not_compiles/main.go vendored Normal file
View File

@ -0,0 +1,3 @@
package p
func F {

View File

@ -9,7 +9,7 @@ func (DuplLogger) level() int {
func (DuplLogger) Debug(args ...interface{}) {}
func (DuplLogger) Info(args ...interface{}) {}
func (logger *DuplLogger) First(args ...interface{}) { // ERROR "12-21 lines are duplicate of `testdata/dupl.go:23-32`"
func (logger *DuplLogger) First(args ...interface{}) { // ERROR "12-21 lines are duplicate of `testdata/with_issues/dupl.go:23-32`"
if logger.level() >= 0 {
logger.Debug(args...)
logger.Debug(args...)
@ -20,7 +20,7 @@ func (logger *DuplLogger) First(args ...interface{}) { // ERROR "12-21 lines are
}
}
func (logger *DuplLogger) Second(args ...interface{}) { // ERROR "23-32 lines are duplicate of `testdata/dupl.go:12-21`"
func (logger *DuplLogger) Second(args ...interface{}) { // ERROR "23-32 lines are duplicate of `testdata/with_issues/dupl.go:12-21`"
if logger.level() >= 1 {
logger.Info(args...)
logger.Info(args...)

View File

@ -11,7 +11,7 @@ func Govet() error {
func GovetShadow(f io.Reader, buf []byte) (err error) {
if f != nil {
_, err := f.Read(buf) // ERROR "declaration of .err. shadows declaration at testdata/govet.go:\d+"
_, err := f.Read(buf) // ERROR "declaration of .err. shadows declaration at testdata/with_issues/govet.go:\d+"
if err != nil {
return err
}

View File

@ -23,10 +23,10 @@ import (
"sync"
"unicode"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/loader"
"github.com/golangci/go-tools/ssa"
"github.com/golangci/go-tools/ssa/ssautil"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/loader"
)
type Job struct {
@ -482,6 +482,14 @@ type Pkg struct {
BuildPkg *build.Package
}
func (p Pkg) GetPackage() *types.Package {
if p.Package == nil {
return nil
}
return p.Pkg
}
type packager interface {
Package() *ssa.Package
}
@ -556,7 +564,7 @@ func (prog *Program) DisplayPosition(p token.Pos) token.Position {
func (j *Job) Errorf(n Positioner, format string, args ...interface{}) *Problem {
tf := j.Program.SSA.Fset.File(n.Pos())
f := j.Program.tokenFileMap[tf]
pkg := j.Program.astFileMap[f].Pkg
pkg := j.Program.astFileMap[f].GetPackage()
pos := j.Program.DisplayPosition(n.Pos())
problem := Problem{
@ -779,6 +787,9 @@ func NodeFns(pkgs []*Pkg) map[ast.Node]*ssa.Function {
wg := &sync.WaitGroup{}
chNodeFns := make(chan map[ast.Node]*ssa.Function, runtime.NumCPU()*2)
for _, pkg := range pkgs {
if pkg.Package == nil { // package wasn't loaded, probably it doesn't compile
continue
}
pkg := pkg
wg.Add(1)
go func() {