Write debug logs for autogen excluding for #86
This commit is contained in:
parent
b4bf038095
commit
f9027f7dbe
@ -8,6 +8,7 @@ import (
|
||||
"runtime/pprof"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/config"
|
||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||
"github.com/golangci/golangci-lint/pkg/printers"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
@ -16,7 +17,9 @@ import (
|
||||
|
||||
func setupLog(isVerbose bool) {
|
||||
log.SetFlags(0) // don't print time
|
||||
if isVerbose {
|
||||
if logutils.IsDebugEnabled() {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
} else if isVerbose {
|
||||
logrus.SetLevel(logrus.InfoLevel)
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package logutils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@ -15,3 +16,38 @@ func HiddenWarnf(format string, args ...interface{}) {
|
||||
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
|
||||
}
|
||||
|
@ -7,9 +7,12 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/lint/astcache"
|
||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||
"github.com/golangci/golangci-lint/pkg/result"
|
||||
)
|
||||
|
||||
var autogenDebugf = logutils.Debug("autogen_exclude")
|
||||
|
||||
type ageFileSummary struct {
|
||||
isGenerated bool
|
||||
}
|
||||
@ -62,10 +65,12 @@ func isGeneratedFileByComment(doc string) bool {
|
||||
doc = strings.ToLower(doc)
|
||||
for _, marker := range markers {
|
||||
if strings.Contains(doc, marker) {
|
||||
autogenDebugf("doc contains marker %q: file is generated", marker)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
autogenDebugf("doc of len %d doesn't contain any of markers: %s", len(doc), markers)
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
autogenDebugf("file %q is generated: %t", i.FilePath(), fs.isGenerated)
|
||||
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
|
||||
|
||||
importPos := f.End()
|
||||
var importPos token.Pos
|
||||
if len(f.Imports) != 0 {
|
||||
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
|
||||
for _, g := range f.Comments {
|
||||
if g.Pos() < importPos && fset.Position(g.Pos()).Column == 1 {
|
||||
neededComments = append(neededComments, g.Text())
|
||||
pos := g.Pos()
|
||||
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 {
|
||||
return ""
|
||||
}
|
||||
|
@ -1,13 +1,9 @@
|
||||
package processors
|
||||
|
||||
import (
|
||||
"go/token"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/lint/astcache"
|
||||
"github.com/golangci/golangci-lint/pkg/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -66,23 +62,3 @@ func TestIsAutogeneratedDetection(t *testing.T) {
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Code generated by ... DO NOT EDIT.
|
||||
package testdata
|
||||
package p
|
||||
|
||||
var vvv int
|
@ -3,6 +3,6 @@
|
||||
// bar.baz
|
||||
// x/y.z
|
||||
// DO NOT EDIT!
|
||||
package testdata
|
||||
package p
|
||||
|
||||
var vv int
|
2
test/testdata/autogenerated/mockgen.go
vendored
2
test/testdata/autogenerated/mockgen.go
vendored
@ -3,4 +3,4 @@
|
||||
|
||||
package p
|
||||
|
||||
func unusedFunc2() {}
|
||||
func UnusedFunc2() {}
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Code generated by protoc-gen-foo
|
||||
// source: bar.proto
|
||||
// DO NOT EDIT!!!
|
||||
package testdata
|
||||
package p
|
||||
|
||||
var v int
|
Loading…
x
Reference in New Issue
Block a user