-
Notifications
You must be signed in to change notification settings - Fork 101
/
cache.py
509 lines (389 loc) · 17.2 KB
/
cache.py
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# -*- coding: utf-8 -*-
import hashlib
import os
from collections import defaultdict
from difflib import SequenceMatcher, unified_diff
from functools import wraps
from io import open
from itertools import groupby, zip_longest
from os.path import join, dirname
from typing import Tuple
from junit_xml import TestSuite, TestCase
from pony.orm import Database, Required, db_session, Set, Optional, select, \
PrimaryKey, RowNotFound, ERDiagramError, OperationalError
from mutmut import BAD_TIMEOUT, OK_SUSPICIOUS, BAD_SURVIVED, UNTESTED, \
OK_KILLED, RelativeMutationID, Context, mutate
db = Database()
current_db_version = 4
NO_TESTS_FOUND = 'NO TESTS FOUND'
class MiscData(db.Entity):
key = PrimaryKey(str, auto=True)
value = Optional(str, autostrip=False)
class SourceFile(db.Entity):
filename = Required(str, autostrip=False)
hash = Optional(str)
lines = Set('Line')
class Line(db.Entity):
sourcefile = Required(SourceFile)
line = Optional(str, autostrip=False)
line_number = Required(int)
mutants = Set('Mutant')
class Mutant(db.Entity):
line = Required(Line)
index = Required(int)
tested_against_hash = Optional(str, autostrip=False)
status = Required(str, autostrip=False) # really an enum of mutant_statuses
def init_db(f):
@wraps(f)
def wrapper(*args, **kwargs):
if db.provider is None:
cache_filename = os.path.join(os.getcwd(), '.mutmut-cache')
db.bind(provider='sqlite', filename=cache_filename, create_db=True)
try:
db.generate_mapping(create_tables=True)
except OperationalError:
pass
if os.path.exists(cache_filename):
# If the existing cache file is out of data, delete it and start over
with db_session:
try:
v = MiscData.get(key='version')
if v is None:
existing_db_version = 1
else:
existing_db_version = int(v.value)
except (RowNotFound, ERDiagramError, OperationalError):
existing_db_version = 1
if existing_db_version != current_db_version:
print('mutmut cache is out of date, clearing it...')
db.drop_all_tables(with_all_data=True)
db.schema = None # Pony otherwise thinks we've already created the tables
db.generate_mapping(create_tables=True)
with db_session:
v = get_or_create(MiscData, key='version')
v.value = str(current_db_version)
return f(*args, **kwargs)
return wrapper
def hash_of(filename):
with open(filename, 'rb') as f:
m = hashlib.sha256()
m.update(f.read())
return m.hexdigest()
def hash_of_tests(tests_dirs):
m = hashlib.sha256()
found_something = False
for tests_dir in tests_dirs:
for root, dirs, files in os.walk(tests_dir):
for filename in files:
with open(os.path.join(root, filename), 'rb') as f:
m.update(f.read())
found_something = True
if not found_something:
return NO_TESTS_FOUND
return m.hexdigest()
def get_apply_line(mutant):
apply_line = 'mutmut apply {}'.format(mutant.id)
return apply_line
def ranges(numbers):
if not numbers:
return []
result = []
start_range = numbers[0]
end_range = numbers[0]
def add_result():
if start_range == end_range:
result.append(str(start_range))
else:
result.append('{}-{}'.format(start_range, end_range))
for x in numbers[1:]:
if end_range + 1 == x:
end_range = x
else:
add_result()
start_range = x
end_range = x
add_result()
return ', '.join(result)
@init_db
@db_session
def print_result_cache(show_diffs=False, dict_synonyms=None, print_only_filename=None, only_this_file=None):
print('To apply a mutant on disk:')
print(' mutmut apply <id>')
print('')
print('To show a mutant:')
print(' mutmut show <id>')
print('')
def print_stuff(title, mutant_query):
mutant_list = list(sorted(mutant_query, key=lambda x: x.line.sourcefile.filename))
if mutant_list:
print('')
print("{} ({})".format(title, len(mutant_list)))
for filename, mutants in groupby(mutant_list, key=lambda x: x.line.sourcefile.filename):
if print_only_filename is not None and print_only_filename != filename:
continue
if only_this_file and filename != only_this_file:
continue
mutants = list(mutants)
print('')
print("---- {} ({}) ----".format(filename, len(mutants)))
print('')
if show_diffs:
with open(filename) as f:
source = f.read()
for x in mutants:
print('# mutant {}'.format(x.id))
print(get_unified_diff(x.id, dict_synonyms, update_cache=False, source=source))
else:
print(ranges([x.id for x in mutants]))
print_stuff('Timed out ⏰', select(x for x in Mutant if x.status == BAD_TIMEOUT))
print_stuff('Suspicious 🤔', select(x for x in Mutant if x.status == OK_SUSPICIOUS))
print_stuff('Survived 🙁', select(x for x in Mutant if x.status == BAD_SURVIVED))
print_stuff('Untested/skipped', select(x for x in Mutant if x.status == UNTESTED))
def get_unified_diff(argument, dict_synonyms, update_cache=True, source=None):
filename, mutation_id = filename_and_mutation_id_from_pk(argument)
if source is None:
with open(filename) as f:
source = f.read()
return _get_unified_diff(source, filename, mutation_id, dict_synonyms, update_cache)
def _get_unified_diff(source, filename, mutation_id, dict_synonyms, update_cache):
if update_cache:
update_line_numbers(filename)
if source is None:
with open(filename) as f:
source = f.read()
context = Context(
source=source,
filename=filename,
mutation_id=mutation_id,
dict_synonyms=dict_synonyms,
)
mutated_source, number_of_mutations_performed = mutate(context)
if not number_of_mutations_performed:
return ""
output = ""
for line in unified_diff(source.split('\n'), mutated_source.split('\n'), fromfile=filename, tofile=filename, lineterm=''):
output += line + "\n"
return output
@init_db
@db_session
def print_result_cache_junitxml(dict_synonyms, suspicious_policy, untested_policy):
test_cases = []
mutant_list = list(select(x for x in Mutant))
for filename, mutants in groupby(mutant_list, key=lambda x: x.line.sourcefile.filename):
for mutant in mutants:
tc = TestCase("Mutant #{}".format(mutant.id), file=filename, line=mutant.line.line_number, stdout=mutant.line.line)
if mutant.status == BAD_SURVIVED:
tc.add_failure_info(message=mutant.status, output=get_unified_diff(mutant.id, dict_synonyms))
if mutant.status == BAD_TIMEOUT:
tc.add_error_info(message=mutant.status, error_type="timeout", output=get_unified_diff(mutant.id, dict_synonyms))
if mutant.status == OK_SUSPICIOUS:
if suspicious_policy != 'ignore':
func = getattr(tc, 'add_{}_info'.format(suspicious_policy))
func(message=mutant.status, output=get_unified_diff(mutant.id, dict_synonyms))
if mutant.status == UNTESTED:
if untested_policy != 'ignore':
func = getattr(tc, 'add_{}_info'.format(untested_policy))
func(message=mutant.status, output=get_unified_diff(mutant.id, dict_synonyms))
test_cases.append(tc)
ts = TestSuite("mutmut", test_cases)
print(TestSuite.to_xml_string([ts]))
@init_db
@db_session
def create_html_report(dict_synonyms):
mutants = list(select(x for x in Mutant))
os.makedirs('html', exist_ok=True)
with open('html/index.html', 'w') as index_file:
index_file.write('<h1>Mutation testing report</h1>')
index_file.write('Killed %s out of %s mutants' % (len([x for x in mutants if x.status == OK_KILLED]), len(mutants)))
index_file.write('<table><thead><tr><th>File</th><th>Total</th><th>Killed</th><th>% killed</th><th>Survived</th></thead>')
for filename, mutants in groupby(mutants, key=lambda x: x.line.sourcefile.filename):
report_filename = join('html', filename)
mutants = list(mutants)
with open(filename) as f:
source = f.read()
os.makedirs(dirname(report_filename), exist_ok=True)
with open(join(report_filename + '.html'), 'w') as f:
mutants_by_status = defaultdict(list)
for mutant in mutants:
mutants_by_status[mutant.status].append(mutant)
f.write('<html><body>')
f.write('<h1>%s</h1>' % filename)
killed = len(mutants_by_status[OK_KILLED])
f.write('Killed %s out of %s mutants' % (killed, len(mutants)))
index_file.write('<tr><td><a href="%s.html">%s</a></td><td>%s</td><td>%s</td><td>%.2f</td><td>%s</td>' % (
filename,
filename,
killed,
len(mutants),
(killed / len(mutants) * 100),
len(mutants_by_status[BAD_SURVIVED]),
))
def print_diffs(status):
mutants = mutants_by_status[status]
for mutant in sorted(mutants, key=lambda m: m.id):
diff = _get_unified_diff(source, filename, RelativeMutationID(mutant.line.line, mutant.index, mutant.line.line_number), dict_synonyms, update_cache=False)
f.write('<h3>Mutant %s</h3>' % mutant.id)
f.write('<pre>%s</pre>' % diff)
if mutants_by_status[BAD_TIMEOUT]:
f.write('<h2>Timeouts</h2>')
f.write('Mutants that made the test suite take a lot longer so the tests were killed.')
print_diffs(BAD_TIMEOUT)
if mutants_by_status[BAD_SURVIVED]:
f.write('<h2>Survived</h2>')
f.write('Survived mutation testing. These mutants show holes in your test suite.')
print_diffs(BAD_SURVIVED)
if mutants_by_status[OK_SUSPICIOUS]:
f.write('<h2>Suspicious</h2>')
f.write('Mutants that made the test suite take longer, but otherwise seemed ok')
print_diffs(OK_SUSPICIOUS)
f.write('</body></html>')
index_file.write('</table></body></html>')
def get_or_create(model, defaults=None, **params):
if defaults is None:
defaults = {}
obj = model.get(**params)
if obj is None:
params = params.copy()
for k, v in defaults.items():
if k not in params:
params[k] = v
return model(**params)
else:
return obj
def sequence_ops(a, b):
sequence_matcher = SequenceMatcher(a=a, b=b)
for tag, i1, i2, j1, j2 in sequence_matcher.get_opcodes():
a_sub_sequence = a[i1:i2]
b_sub_sequence = b[j1:j2]
for x in zip_longest(a_sub_sequence, range(i1, i2), b_sub_sequence, range(j1, j2)):
yield (tag,) + x
@init_db
@db_session
def update_line_numbers(filename):
hash = hash_of(filename)
sourcefile = get_or_create(SourceFile, filename=filename)
if hash == sourcefile.hash:
return
cached_line_objects = list(sourcefile.lines.order_by(Line.line_number))
cached_lines = [x.line for x in cached_line_objects]
with open(filename) as f:
existing_lines = [x.strip('\n') for x in f.readlines()]
if not cached_lines:
for i, line in enumerate(existing_lines):
Line(sourcefile=sourcefile, line=line, line_number=i)
return
for command, a, a_index, b, b_index in sequence_ops(cached_lines, existing_lines):
if command == 'equal':
if a_index != b_index:
cached_obj = cached_line_objects[a_index]
assert cached_obj.line == existing_lines[b_index]
cached_obj.line_number = b_index
elif command == 'delete':
cached_line_objects[a_index].delete()
elif command == 'insert':
if b is not None:
Line(sourcefile=sourcefile, line=b, line_number=b_index)
elif command == 'replace':
if a_index is not None:
cached_line_objects[a_index].delete()
if b is not None:
Line(sourcefile=sourcefile, line=b, line_number=b_index)
else:
raise ValueError('Unknown opcode from SequenceMatcher: {}'.format(command))
sourcefile.hash = hash
@init_db
@db_session
def register_mutants(mutations_by_file):
for filename, mutation_ids in mutations_by_file.items():
hash = hash_of(filename)
sourcefile = get_or_create(SourceFile, filename=filename)
if hash == sourcefile.hash:
continue
for mutation_id in mutation_ids:
line = Line.get(sourcefile=sourcefile, line=mutation_id.line, line_number=mutation_id.line_number)
if line is None:
raise ValueError("Obtained null line for mutation_id: {}".format(mutation_id))
get_or_create(Mutant, line=line, index=mutation_id.index, defaults=dict(status=UNTESTED))
sourcefile.hash = hash
@init_db
@db_session
def update_mutant_status(file_to_mutate, mutation_id, status, tests_hash):
sourcefile = SourceFile.get(filename=file_to_mutate)
line = Line.get(sourcefile=sourcefile, line=mutation_id.line, line_number=mutation_id.line_number)
mutant = Mutant.get(line=line, index=mutation_id.index)
mutant.status = status
mutant.tested_against_hash = tests_hash
@init_db
@db_session
def get_cached_mutation_statuses(filename, mutations, hash_of_tests):
sourcefile = SourceFile.get(filename=filename)
assert sourcefile
line_obj_by_line = {}
result = {}
for mutation_id in mutations:
if mutation_id.line not in line_obj_by_line:
line_obj_by_line[mutation_id.line] = Line.get(sourcefile=sourcefile, line=mutation_id.line, line_number=mutation_id.line_number)
line = line_obj_by_line[mutation_id.line]
assert line
mutant = Mutant.get(line=line, index=mutation_id.index)
if mutant is None:
mutant = get_or_create(Mutant, line=line, index=mutation_id.index, defaults=dict(status=UNTESTED))
result[mutation_id] = mutant.status
if mutant.status == OK_KILLED:
# We assume that if a mutant was killed, a change to the test
# suite will mean it's still killed
result[mutation_id] = mutant.status
else:
if mutant.tested_against_hash != hash_of_tests or \
mutant.tested_against_hash == NO_TESTS_FOUND or \
hash_of_tests == NO_TESTS_FOUND:
result[mutation_id] = UNTESTED
else:
result[mutation_id] = mutant.status
return result
@init_db
@db_session
def cached_mutation_status(filename, mutation_id, hash_of_tests):
sourcefile = SourceFile.get(filename=filename)
assert sourcefile
line = Line.get(sourcefile=sourcefile, line=mutation_id.line, line_number=mutation_id.line_number)
assert line
mutant = Mutant.get(line=line, index=mutation_id.index)
if mutant is None:
mutant = get_or_create(Mutant, line=line, index=mutation_id.index, defaults=dict(status=UNTESTED))
if mutant.status == OK_KILLED:
# We assume that if a mutant was killed, a change to the test
# suite will mean it's still killed
return OK_KILLED
if mutant.tested_against_hash != hash_of_tests or \
mutant.tested_against_hash == NO_TESTS_FOUND or \
hash_of_tests == NO_TESTS_FOUND:
return UNTESTED
return mutant.status
@init_db
@db_session
def mutation_id_from_pk(pk):
mutant = Mutant.get(id=pk)
return RelativeMutationID(line=mutant.line.line, index=mutant.index, line_number=mutant.line.line_number)
@init_db
@db_session
def filename_and_mutation_id_from_pk(pk) -> Tuple[str, RelativeMutationID]:
mutant = Mutant.get(id=pk)
if mutant is None:
raise ValueError("Obtained null mutant for pk: {}".format(pk))
return mutant.line.sourcefile.filename, mutation_id_from_pk(pk)
@init_db
@db_session
def cached_test_time():
d = MiscData.get(key='baseline_time_elapsed')
return float(d.value) if d else None
@init_db
@db_session
def set_cached_test_time(baseline_time_elapsed, current_hash_of_tests):
get_or_create(MiscData, key='baseline_time_elapsed').value = str(baseline_time_elapsed)
get_or_create(MiscData, key='hash_of_tests').value = current_hash_of_tests
@init_db
@db_session
def cached_hash_of_tests():
d = MiscData.get(key='hash_of_tests')
return d.value if d else None