forked from go-gremlins/gremlins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
380 lines (322 loc) · 8.87 KB
/
engine.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright 2022 The Gremlins Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package engine
import (
"context"
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/singhnishant94/gremlins/internal/coverage"
"github.com/singhnishant94/gremlins/internal/diff"
"github.com/singhnishant94/gremlins/internal/engine/workerpool"
"github.com/singhnishant94/gremlins/internal/exclusion"
"github.com/singhnishant94/gremlins/internal/mutator"
"github.com/singhnishant94/gremlins/internal/report"
"github.com/singhnishant94/gremlins/internal/configuration"
"github.com/singhnishant94/gremlins/internal/gomodule"
)
// Engine is the "engine" that performs the mutation testing.
//
// It traverses the AST of the project, finds which TokenMutator can be applied and
// performs the actual mutation testing.
type Engine struct {
fs fs.FS
jDealer ExecutorDealer
codeData CodeData
mutantStream chan mutator.Mutator
mutants []mutator.Mutator
module gomodule.GoModule
logger report.MutantLogger
}
// CodeData is used to check if the mutant should be executed.
type CodeData struct {
Cov coverage.Profile
Diff diff.Diff
Exclusion exclusion.Rules
}
type Comment struct {
Body string `json:"body"`
Path string `json:"path"`
Line int `json:"line"`
Side string `json:"side"`
}
// Option for the Engine initialization.
type Option func(m Engine) Engine
// New instantiates an Engine.
//
// It gets a fs.FS on which to perform the analysis, a CodeData to
// check if the mutants are executable and a sets of Option.
func New(mod gomodule.GoModule, codeData CodeData, jDealer ExecutorDealer, opts ...Option) Engine {
dirFS := os.DirFS(filepath.Join(mod.Root, mod.CallingDir))
mut := Engine{
module: mod,
jDealer: jDealer,
codeData: codeData,
fs: dirFS,
logger: report.NewLogger(),
}
for _, opt := range opts {
mut = opt(mut)
}
return mut
}
// WithDirFs overrides the fs.FS of the module (mainly used for testing purposes).
func WithDirFs(dirFS fs.FS) Option {
return func(m Engine) Engine {
m.fs = dirFS
return m
}
}
// Run executes the mutation testing.
//
// It walks the fs.FS provided and checks every .go file which is not a test.
// For each file it will scan for tokenMutations and gather all the mutants found.
func (mu *Engine) Run(ctx context.Context) report.Results {
// mu.mutantStream = make(chan mutator.Mutator)
// go func() {
// defer close(mu.mutantStream)
start := time.Now()
fmt.Printf("Start parsing files\n")
_ = fs.WalkDir(mu.fs, ".", func(path string, _ fs.DirEntry, _ error) error {
isGoCode := filepath.Ext(path) == ".go" && !strings.HasSuffix(path, "_test.go")
if isGoCode && !mu.codeData.Exclusion.IsFileExcluded(path) {
mu.runOnFile(path)
}
return nil
})
// }()
runnable := 0
for _, m := range mu.mutants {
if m.Status() == mutator.Runnable {
runnable++
}
}
fmt.Printf("Found %d mutations in %f seconds out of which %d is runnable\n",
len(mu.mutants), time.Since(start).Seconds(), runnable)
start = time.Now()
res := mu.executeTests(ctx)
res.Elapsed = time.Since(start)
res.Module = mu.module.Name
return res
}
func (mu *Engine) runOnFile(fileName string) {
src, _ := mu.fs.Open(fileName)
set := token.NewFileSet()
file, err := parser.ParseFile(set, fileName, src, parser.ParseComments)
fmt.Printf("Parsing %s\n", fileName)
// file, _, , err := mu.parseAndTypeCheckFile(fileName)
if err != nil {
_ = src.Close()
fmt.Printf("Error parsing file %s\n err: %s", fileName, err)
return
}
_ = src.Close()
ast.Inspect(file, func(node ast.Node) bool {
n, ok := NewTokenNode(node)
if !ok {
return true
}
mu.findTokenMutations(fileName, set, file, n)
return true
})
ast.Inspect(file, func(node ast.Node) bool {
n, ok := NewNode(node)
if !ok {
return true
}
mu.findNodeMutations(fileName, set, file, n)
return true
})
}
func (mu *Engine) findTokenMutations(fileName string, set *token.FileSet, file *ast.File, node *NodeToken) {
mutantTypes, ok := TokenMutantType[node.Tok()]
if !ok {
return
}
pkg := mu.pkgName(fileName, file.Name.Name)
for _, mt := range mutantTypes {
if !configuration.Get[bool](configuration.MutantTypeEnabledKey(mt)) {
continue
}
mutantType := mt
tm := NewTokenMutant(pkg, set, file, node)
tm.SetType(mutantType)
tm.SetStatus(mu.mutationStatus(set.Position(node.TokPos)))
mu.mutants = append(mu.mutants, tm)
// mu.mutantStream <- tm
}
}
func (mu *Engine) pkgName(fileName, fPkg string) string {
var pkg string
fn := fmt.Sprintf("%s/%s", mu.module.CallingDir, fileName)
p := filepath.Dir(fn)
for {
if strings.HasSuffix(p, fPkg) {
pkg = fmt.Sprintf("%s/%s", mu.module.Name, p)
break
}
d := filepath.Dir(p)
if d == p {
pkg = mu.module.Name
break
}
p = d
}
return normalisePkgPath(pkg)
}
func normalisePkgPath(pkg string) string {
sep := fmt.Sprintf("%c", os.PathSeparator)
return strings.ReplaceAll(pkg, sep, "/")
}
func (mu *Engine) mutationStatus(pos token.Position) mutator.Status {
var status mutator.Status
if mu.codeData.Cov.IsCovered(pos) {
status = mutator.Runnable
}
if !mu.codeData.Diff.IsChanged(pos) {
status = mutator.Skipped
}
return status
}
func (mu *Engine) findNodeMutations(fileName string, set *token.FileSet, file *ast.File, node *Node) {
// Statement block removal
var l []ast.Stmt
switch n := (*node.node).(type) {
case *ast.BlockStmt:
l = n.List
case *ast.CaseClause:
l = n.Body
}
for i, ni := range l {
if checkRemoveStatement(ni) {
tm := NewStmtRemover(mu.pkgName(fileName, file.Name.Name), set, file, node, i, ni.Pos())
tm.SetType(mutator.RemoveStatement)
tm.SetStatus(mu.mutationStatus(set.Position(tm.Pos())))
mu.mutants = append(mu.mutants, tm)
}
}
}
func checkRemoveStatement(node ast.Stmt) bool {
switch n := node.(type) {
case *ast.AssignStmt:
if n.Tok != token.DEFINE {
return true
}
case *ast.ExprStmt, *ast.IncDecStmt:
return true
}
return false
}
func (mu *Engine) executeTests(ctx context.Context) report.Results {
pool := workerpool.Initialize("mutator")
pool.Start()
var mutants []mutator.Mutator
outCh := make(chan mutator.Mutator)
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for _, mut := range mu.mutants {
ok := checkDone(ctx)
if !ok {
pool.Stop()
break
}
wg.Add(1)
pool.AppendExecutor(mu.jDealer.NewExecutor(mut, outCh, wg))
}
}()
go func() {
wg.Wait()
close(outCh)
}()
surfacedMutants := map[string]map[int]bool{}
comments := []Comment{}
for m := range outCh {
mu.logger.Mutant(m)
mutants = append(mutants, m)
if m.Status() == mutator.Lived {
shouldComment := false
fileMutants, ok := surfacedMutants[m.Position().Filename]
if !ok {
fileMutants = map[int]bool{m.Position().Line: true}
surfacedMutants[m.Position().Filename] = fileMutants
shouldComment = true
} else {
_, ok := fileMutants[m.Position().Line]
if !ok {
fileMutants[m.Position().Line] = true
surfacedMutants[m.Position().Filename] = fileMutants
shouldComment = true
}
}
if shouldComment {
comment := Comment{
Body: getPRComment(m),
Path: m.Position().Filename,
Line: m.Position().Line,
Side: "RIGHT",
}
comments = append(comments, comment)
}
}
}
// Marshal the data into JSON
jsonData, err := json.MarshalIndent(comments, "", " ")
if err != nil {
log.Fatalf("Error occurred during marshalling. %v", err)
}
// Writing the JSON data to a file
fileName := "comments.json"
file, err := os.Create(fileName)
if err != nil {
log.Fatalf("Error occurred creating file: %v", err)
}
defer file.Close()
// Write the JSON data to file
_, err = file.Write(jsonData)
if err != nil {
log.Fatalf("Error occurred writing to file: %v", err)
}
log.Printf("Data successfully written to %s", fileName)
return results(mutants)
}
func getPRComment(m mutator.Mutator) string {
return fmt.Sprintf(
"[gremlins] Changing the code like shown below does not cause any tests exercising them to fail.\n"+
"Consider adding tests that fail when the code is mutated.\n\n"+
"```diff\n%s\n```", m.Diff())
}
func checkDone(ctx context.Context) bool {
select {
case <-ctx.Done():
return false
default:
return true
}
}
func results(m []mutator.Mutator) report.Results {
return report.Results{Mutants: m}
}