-
Notifications
You must be signed in to change notification settings - Fork 1
/
gomutate.go
129 lines (105 loc) · 2.85 KB
/
gomutate.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
package gomutate
import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/zabawaba99/gomutate/mutants"
)
const mutationDir = "_gomutate"
type Gomutate struct {
wd string
}
func New(wd string) *Gomutate {
if err := os.RemoveAll(mutationDir); err != nil {
log.Fatalf("Could not delete '_gomutate' directory %s", err)
}
if err := os.Mkdir(mutationDir, 0777); err != nil {
log.Fatalf("Could not recreate '_gomutate' directory %s", err)
}
return &Gomutate{wd: wd}
}
func (g *Gomutate) Run(pkg string, mutations []mutants.Mutator) {
// parse files
a, err := newAST(filepath.Join(g.wd, pkg))
if err != nil {
log.Fatalf("Could not read dir %s", err)
}
var wg sync.WaitGroup
for _, m := range mutations {
wg.Add(1)
go func(m mutants.Mutator) {
log.WithField("mutation", m.Name()).Info("Generating mutation...")
// generate mutations
a.ApplyMutation(m)
log.WithField("mutation", m.Name()).Info("Testing mutations...")
// run tests
g.runTests(pkg, m)
wg.Done()
}(m)
}
wg.Wait()
}
func (g *Gomutate) runTests(pkg string, m mutants.Mutator) {
mtpath := filepath.Join(mutationDir, m.Name(), pkg)
deviants, err := ioutil.ReadDir(mtpath)
if err != nil {
log.WithField("pkg", mtpath).Warning("Could not find mutant directories")
return
}
reg := regexp.MustCompile(`^.+\.go\.\d+$`)
for _, mt := range deviants {
if !mt.IsDir() {
continue
}
if !reg.MatchString(mt.Name()) {
// a subpackage
continue
}
mutant := filepath.Join(mtpath, mt.Name())
log.Debugf("Running tests for %s", mutant)
cmd := exec.Command("go", "test", "."+separator+mutant+separator+"...")
cmd.Run()
var md mutants.Data
if err := md.Load(mutant); err != nil {
fields := log.Fields{"error": err, "mutation": mutant}
log.WithFields(fields).Warning("Could not save results for mutation")
continue
}
md.Killed = !cmd.ProcessState.Success()
if err := md.Save(mutant); err != nil {
fields := log.Fields{"error": err, "mutation": mutant}
log.WithFields(fields).Warning("Could not save results for mutation")
}
fields := log.Fields{"killed": md.Killed, "mutant": mutant}
if md.Killed {
log.WithFields(fields).Info("Result")
} else {
log.WithFields(fields).Warn("Result")
}
}
}
func (g *Gomutate) GatherResults() {
results := []mutants.Data{}
filepath.Walk(mutationDir, func(path string, info os.FileInfo, err error) error {
if info.Name() != mutants.DataFileName {
return nil
}
pkg := strings.TrimSuffix(path, info.Name())
var result mutants.Data
result.Load(pkg)
results = append(results, result)
return nil
})
f, err := os.Create(filepath.Join(mutationDir, "results.json"))
if err != nil {
log.WithError(err).Fatal("Could not create gomutate.json")
}
defer f.Close()
json.NewEncoder(f).Encode(results)
}