-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_visitor.go
60 lines (51 loc) · 1.27 KB
/
node_visitor.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
package gomutate
import (
"go/ast"
"go/token"
"path/filepath"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/zabawaba99/gomutate/mutants"
)
type nodeVistor struct {
count int64
mutation mutants.Mutator
file *token.File
ast *AST
}
func newNodeVisitor(a *AST, file *token.File, m mutants.Mutator) *nodeVistor {
return &nodeVistor{ast: a, file: file, mutation: m}
}
func (v *nodeVistor) Visit(n ast.Node) ast.Visitor {
v.ast.mtx.Lock()
mutation, ok := v.mutation.Mutate(n)
if !ok {
v.ast.mtx.Unlock()
return v
}
v.count++
count := strconv.FormatInt(v.count, 10)
filename := trimWD(v.file.Name())
basedir := filepath.Join(mutationDir, v.mutation.Name(), filename+"."+count)
if err := v.ast.write(basedir); err != nil {
log.Fatalf("Could not create mutation file %s", err)
}
log.Debugf("Created mutation for %s", basedir)
mutation.Reset()
v.ast.mtx.Unlock()
md := mutants.Data{
Original: mutation.OrgStmt,
Mutation: mutation.NewStmt,
Type: v.mutation.Name(),
Filename: v.file.Name(),
LineNumber: v.file.Line(n.Pos()),
}
if err := md.Save(basedir); err != nil {
fields := log.Fields{
"error": err,
"mutation": basedir,
}
log.WithFields(fields).Warning("Could not save results for mutation")
}
return v
}