/
scanner.go
335 lines (306 loc) · 7.23 KB
/
scanner.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package scanner
import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"encoding/json"
"github.com/dave/gopackages"
"github.com/dave/kerr"
"github.com/dave/kerr/ksrc"
)
// ke: {"package": {"notest": true}}
var baseDir string
type Source struct {
Wraps []ErrDef
Panics []PosDef
ExcludedBlocks []PosDef
ExcludedPackages map[string]bool
ExcludedFiles map[string]bool
ExcludedFuncs []FuncDef
CompletePackages map[string]bool
Skipped map[string]bool
All map[string]ErrDef
JsTestPackages map[string]bool
}
type ErrDef struct {
Id string
File string
Line int
}
type PosDef struct {
File string
Line int
}
type FuncDef struct {
File string
LineStart int
LineEnd int
}
var source = &Source{
Skipped: map[string]bool{},
ExcludedPackages: map[string]bool{},
ExcludedFiles: map[string]bool{},
CompletePackages: map[string]bool{},
All: map[string]ErrDef{},
JsTestPackages: map[string]bool{},
}
func Get(dir string) (*Source, error) {
baseDir = dir
// walk each file in the working directory
walker := func(filename string, file os.FileInfo, err error) error {
if strings.HasPrefix(getParentDir(filename), ".") {
return filepath.SkipDir
}
if strings.HasPrefix(file.Name(), ".") {
return nil
}
if file.IsDir() {
return nil
}
if strings.HasSuffix(filename, ".go") {
return scanFile(filename)
}
return nil
}
if err := filepath.Walk(baseDir, walker); err != nil {
return nil, kerr.Wrap("UIBJTFCKHQ", err)
}
return source, nil
}
func getParentDir(filename string) string {
dir, _ := filepath.Split(filename)
dirs := strings.Split(dir, string(os.PathSeparator))
return dirs[len(dirs)-2]
}
func scanFile(filename string) error {
b, err := ioutil.ReadFile(filename)
if err != nil {
return kerr.Wrap("DNMNGWPEYO", err)
}
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return kerr.Wrap("WTIOTIMQDV", err)
}
gopathdir := gopackages.GetCurrentGopath(os.Getenv("GOPATH"), filepath.Dir(filename))
pkg, err := gopackages.GetPackageFromDir(os.Getenv("GOPATH"), filepath.Dir(filename))
if err != nil {
return kerr.Wrap("PJFJLKNVBO", err)
}
kerrName := ""
assertName := ""
for _, is := range file.Imports {
importPath, _ := strconv.Unquote(is.Path.Value)
switch importPath {
case "github.com/dave/kerr":
if is.Name != nil {
kerrName, _ = strconv.Unquote(is.Name.Name)
} else {
kerrName = "kerr"
}
case "github.com/dave/ktest/assert":
if is.Name != nil {
assertName, _ = strconv.Unquote(is.Name.Name)
} else {
assertName = "assert"
}
}
}
relfilename, err := filepath.Rel(filepath.Join(gopathdir, "src"), filename)
if err != nil {
return kerr.Wrap("WSDPGDGMDH", err)
}
for _, cg := range file.Comments {
for _, c := range cg.List {
if strings.HasPrefix(c.Text, "// ke: ") {
val := struct {
Block struct {
Notest bool
}
Package struct {
Jstest bool
Notest bool
Complete bool
}
File struct {
Notest bool
}
}{}
err := json.Unmarshal([]byte(c.Text[7:]), &val)
if err != nil {
return kerr.Wrap("LAWVOJLLRJ", err)
}
if val.Block.Notest {
source.ExcludedBlocks = append(source.ExcludedBlocks, PosDef{
File: relfilename,
Line: fset.Position(c.Pos()).Line,
})
}
if val.Package.Complete {
source.CompletePackages[pkg] = true
}
if val.Package.Notest {
source.ExcludedPackages[pkg] = true
}
if val.Package.Jstest {
source.JsTestPackages[pkg] = true
}
if val.File.Notest {
source.ExcludedFiles[relfilename] = true
}
}
}
}
// visitor implements ast.Visitor
v := &visitor{
Bytes: b,
kerrName: kerrName,
assertName: assertName,
file: relfilename,
pkg: pkg,
fset: fset,
}
ast.Walk(v, file)
return v.err
}
type visitor struct {
Bytes []byte
t *testing.T
kerrName string
assertName string
file string
pkg string
fset *token.FileSet
err error
}
func (v *visitor) Visit(node ast.Node) (w ast.Visitor) {
if node == nil {
return v
}
switch ob := node.(type) {
/*
case *ast.File:
for _, cg := range ty.Comments {
for _, c := range cg.List {
if strings.HasPrefix(c.Text, "// ke: ") {
val := struct{ Notest bool }{}
err := json.UnmarshalPlain([]byte(c.Text[7:]), &val)
require.NoError(v.t, err)
if val.Notest {
def := getPkgDef(v.pkg)
def.notest = true
}
}
}
}
*/
case *ast.FuncDecl:
if ob.Doc == nil {
return v
}
for _, c := range ob.Doc.List {
if strings.HasPrefix(c.Text, "// ke: ") {
val := struct {
Func struct {
Notest bool
}
}{}
err := json.Unmarshal([]byte(c.Text[7:]), &val)
if err != nil {
v.err = err
return nil
}
if val.Func.Notest {
source.ExcludedFuncs = append(source.ExcludedFuncs, FuncDef{
File: v.file,
LineStart: v.fset.Position(ob.Pos()).Line,
LineEnd: v.fset.Position(ob.End()).Line,
})
}
}
}
case *ast.CallExpr:
name := ""
pkg := ""
if i, ok := ob.Fun.(*ast.Ident); ok {
if i.Name == "panic" {
source.Panics = append(source.Panics, PosDef{
File: v.file,
Line: v.fset.Position(ob.Pos()).Line,
})
}
}
if f, ok := ob.Fun.(*ast.SelectorExpr); ok {
name = f.Sel.Name
if i, ok := f.X.(*ast.Ident); ok {
pkg = i.Name
}
if pkg == "" {
return v
}
if pkg == v.kerrName && name == "Wrap" {
id, err := getErrorId(v.t, ob.Args, 0, v.file)
if err != nil {
v.err = err
return nil
}
source.Wraps = append(source.Wraps, ErrDef{
Id: id,
File: v.file,
Line: v.fset.Position(ob.Pos()).Line,
})
} else if pkg == v.kerrName && name == "New" {
id, err := getErrorId(v.t, ob.Args, 0, v.file)
if err != nil {
v.err = err
return nil
}
source.All[id] = ErrDef{
Id: id,
File: v.file,
Line: v.fset.Position(ob.Pos()).Line,
}
} else if pkg == v.assertName && (name == "SkipError") {
id, err := getErrorId(v.t, ob.Args, 0, v.file)
if err != nil {
v.err = err
return nil
}
source.Skipped[id] = true
} /*else if pkg == v.assertName && (name == "IsError" || name == "HasError") {
def := getErrData(v.t, ty.Args, 2, v.file)
def.tested = true
} else if pkg == v.assertName && (name == "SkipError") {
def := getErrData(v.t, ty.Args, 0, v.file)
def.skipped = true
}*/
}
}
return v
}
func getErrorId(t *testing.T, args []ast.Expr, arg int, file string) (string, error) {
if len(args) <= arg {
return "", kerr.New("FLKWAYFFYL", "Not enough args (%s)", file)
}
b, ok := args[arg].(*ast.BasicLit)
if !ok {
return "", kerr.New("YFKEYRIUWJ", "Arg should be *ast.BasicLit (%s)", file)
}
if b.Kind != token.STRING {
return "", kerr.New("YWPXGGRLEL", "kind should be token.STRING (%s)", file)
}
id, err := strconv.Unquote(b.Value)
if err != nil {
return "", kerr.Wrap("NTUDBTYOQY", err)
}
if !ksrc.IsId(id) {
return "", kerr.New("YLTSLGODKR", "Invalid kerr ID %s (%s)", id, file)
}
return id, nil
}