Write debug logs for autogen excluding for #86

This commit is contained in:
Denis Isaev 2018-06-12 15:32:34 +03:00
parent b4bf038095
commit f9027f7dbe
No known key found for this signature in database
GPG Key ID: A36A0EC8E27A1A01
9 changed files with 70 additions and 34 deletions

View File

@ -8,6 +8,7 @@ import (
"runtime/pprof" "runtime/pprof"
"github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/printers" "github.com/golangci/golangci-lint/pkg/printers"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -16,7 +17,9 @@ import (
func setupLog(isVerbose bool) { func setupLog(isVerbose bool) {
log.SetFlags(0) // don't print time log.SetFlags(0) // don't print time
if isVerbose { if logutils.IsDebugEnabled() {
logrus.SetLevel(logrus.DebugLevel)
} else if isVerbose {
logrus.SetLevel(logrus.InfoLevel) logrus.SetLevel(logrus.InfoLevel)
} }
} }

View File

@ -2,6 +2,7 @@ package logutils
import ( import (
"os" "os"
"strings"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -15,3 +16,38 @@ func HiddenWarnf(format string, args ...interface{}) {
logrus.Infof(format, args...) logrus.Infof(format, args...)
} }
} }
func getEnabledDebugs() map[string]bool {
ret := map[string]bool{}
debugVar := os.Getenv("GL_DEBUG")
if debugVar == "" {
return ret
}
for _, tag := range strings.Split(debugVar, ",") {
ret[tag] = true
}
return ret
}
var enabledDebugs = getEnabledDebugs()
type DebugFunc func(format string, args ...interface{})
func nopDebugf(format string, args ...interface{}) {}
func Debug(tag string) DebugFunc {
if !enabledDebugs[tag] {
return nopDebugf
}
return func(format string, args ...interface{}) {
newArgs := append([]interface{}{tag}, args...)
logrus.Debugf("%s: "+format, newArgs...)
}
}
func IsDebugEnabled() bool {
return len(enabledDebugs) != 0
}

View File

@ -7,9 +7,12 @@ import (
"strings" "strings"
"github.com/golangci/golangci-lint/pkg/lint/astcache" "github.com/golangci/golangci-lint/pkg/lint/astcache"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result" "github.com/golangci/golangci-lint/pkg/result"
) )
var autogenDebugf = logutils.Debug("autogen_exclude")
type ageFileSummary struct { type ageFileSummary struct {
isGenerated bool isGenerated bool
} }
@ -62,10 +65,12 @@ func isGeneratedFileByComment(doc string) bool {
doc = strings.ToLower(doc) doc = strings.ToLower(doc)
for _, marker := range markers { for _, marker := range markers {
if strings.Contains(doc, marker) { if strings.Contains(doc, marker) {
autogenDebugf("doc contains marker %q: file is generated", marker)
return true return true
} }
} }
autogenDebugf("doc of len %d doesn't contain any of markers: %s", len(doc), markers)
return false return false
} }
@ -83,27 +88,43 @@ func (p *AutogeneratedExclude) getOrCreateFileSummary(i *result.Issue) (*ageFile
return nil, fmt.Errorf("can't parse file %s: %s", i.FilePath(), f.Err) return nil, fmt.Errorf("can't parse file %s: %s", i.FilePath(), f.Err)
} }
doc := getDoc(f.F, f.Fset) autogenDebugf("file %q: astcache file is %+v", i.FilePath(), *f)
doc := getDoc(f.F, f.Fset, i.FilePath())
fs.isGenerated = isGeneratedFileByComment(doc) fs.isGenerated = isGeneratedFileByComment(doc)
autogenDebugf("file %q is generated: %t", i.FilePath(), fs.isGenerated)
return fs, nil return fs, nil
} }
func getDoc(f *ast.File, fset *token.FileSet) string { func getDoc(f *ast.File, fset *token.FileSet, filePath string) string {
// don't use just f.Doc: e.g. mockgen leaves extra line between comment and package name // don't use just f.Doc: e.g. mockgen leaves extra line between comment and package name
importPos := f.End() var importPos token.Pos
if len(f.Imports) != 0 { if len(f.Imports) != 0 {
importPos = f.Imports[0].Pos() importPos = f.Imports[0].Pos()
autogenDebugf("file %q: search comments until first import pos %d (%s)", filePath, importPos, fset.Position(importPos))
} else {
importPos = f.End()
autogenDebugf("file %q: search comments until EOF pos %d (%s)", filePath, importPos, fset.Position(importPos))
} }
var neededComments []string var neededComments []string
for _, g := range f.Comments { for _, g := range f.Comments {
if g.Pos() < importPos && fset.Position(g.Pos()).Column == 1 { pos := g.Pos()
neededComments = append(neededComments, g.Text()) filePos := fset.Position(pos)
text := g.Text()
isAllowed := pos < importPos && filePos.Column == 1
if isAllowed {
autogenDebugf("file %q: pos=%d, filePos=%s: comment %q: it's allowed", filePath, pos, filePos, text)
neededComments = append(neededComments, text)
} else {
autogenDebugf("file %q: pos=%d, filePos=%s: comment %q: it's NOT allowed", filePath, pos, filePos, text)
} }
} }
autogenDebugf("file %q: got %d allowed comments", filePath, len(neededComments))
if len(neededComments) == 0 { if len(neededComments) == 0 {
return "" return ""
} }

View File

@ -1,13 +1,9 @@
package processors package processors
import ( import (
"go/token"
"path/filepath"
"strings" "strings"
"testing" "testing"
"github.com/golangci/golangci-lint/pkg/lint/astcache"
"github.com/golangci/golangci-lint/pkg/result"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -66,23 +62,3 @@ func TestIsAutogeneratedDetection(t *testing.T) {
assert.False(t, isGenerated) assert.False(t, isGenerated)
} }
} }
func TestNoIssuesInAutogeneratedFiles(t *testing.T) {
files := []string{
"autogenerated.go",
"autogenerated_alt_hdr.go",
"autogenerated_alt_hdr2.go",
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
i := result.Issue{
Pos: token.Position{
Filename: filepath.Join("testdata", file),
Line: 4,
},
}
p := NewAutogeneratedExclude(astcache.NewCache())
processAssertEmpty(t, p, i)
})
}
}

View File

@ -1,4 +1,4 @@
// Code generated by ... DO NOT EDIT. // Code generated by ... DO NOT EDIT.
package testdata package p
var vvv int var vvv int

View File

@ -3,6 +3,6 @@
// bar.baz // bar.baz
// x/y.z // x/y.z
// DO NOT EDIT! // DO NOT EDIT!
package testdata package p
var vv int var vv int

View File

@ -3,4 +3,4 @@
package p package p
func unusedFunc2() {} func UnusedFunc2() {}

View File

@ -1,6 +1,6 @@
// Code generated by protoc-gen-foo // Code generated by protoc-gen-foo
// source: bar.proto // source: bar.proto
// DO NOT EDIT!!! // DO NOT EDIT!!!
package testdata package p
var v int var v int