-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper_test.go
63 lines (52 loc) · 1.23 KB
/
helper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package exclude_test
import (
"go/token"
"testing"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/analysistest"
"github.com/gostaticanalysis/exclude"
)
type reportRecoder struct {
reports []analysis.Diagnostic
}
func (r *reportRecoder) new(f exclude.Func, line int) *analysis.Analyzer {
a := exclude.By(&analysis.Analyzer{
Name: "TestAnalyzer",
Doc: "document",
Run: func(pass *analysis.Pass) (interface{}, error) {
if pass.Pkg.Name() == "main" ||
len(pass.Files) == 0 {
return nil, nil
}
var pos token.Pos
if line >= 0 {
file := pass.Fset.File(pass.Files[0].Pos())
pos = file.LineStart(line)
} else {
pos = pass.Files[0].Pos()
}
pass.Reportf(pos, "hello")
return nil, nil
},
}, f)
orgRun := a.Run
a.Run = func(pass *analysis.Pass) (interface{}, error) {
pass.Report = func(d analysis.Diagnostic) {
r.reports = append(r.reports, d)
}
return orgRun(pass)
}
return a
}
func (r *reportRecoder) isReported() bool {
return len(r.reports) != 0
}
func writeFiles(t *testing.T, filemap map[string]string) string {
t.Helper()
dir, clean, err := analysistest.WriteFiles(filemap)
if err != nil {
t.Fatal("unexpected error:", err)
}
t.Cleanup(clean)
return dir
}