forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pitest-survival-check-xml.groovy
407 lines (367 loc) · 13.4 KB
/
pitest-survival-check-xml.groovy
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
import groovy.io.FileType
import groovy.transform.EqualsAndHashCode
import groovy.transform.Field
import groovy.transform.Immutable
import groovy.util.slurpersupport.GPathResult
import groovy.util.slurpersupport.NodeChildren
import groovy.xml.XmlUtil
@Field static final String USAGE_STRING = "Usage groovy .${File.separator}.ci${File.separator}" +
"pitest-survival-check-xml.groovy [profile] [-g | --generate-suppression]\n" +
"To see the full list of supported profiles run\ngroovy .${File.separator}" +
".ci${File.separator} pitest-survival-check-xml.groovy --list\n"
int exitCode = 1
if (args.length == 2) {
exitCode = parseArgumentAndExecute(args[0], args[1])
}
else if (args.length == 1) {
exitCode = parseArgumentAndExecute(args[0], null)
}
else {
throw new IllegalArgumentException(USAGE_STRING)
}
System.exit(exitCode)
/**
* Parse the command line arguments passed in and execute the branch based on the arguments.
*
* @param argument command line argument
* @return {@code 0} if command executes successfully, {@code 1} otherwise
*/
private int parseArgumentAndExecute(String argument, String flag) {
final Set<String> profiles = getPitestProfiles()
final int exitCode
if (profiles.contains(argument)) {
if (flag != null && flag != "-g" && flag != "--generate-suppression") {
final String exceptionMessage = "\nUnexpected flag: '${flag}' " + USAGE_STRING
throw new IllegalArgumentException(exceptionMessage)
}
exitCode = checkPitestReport(argument, flag)
}
else if (argument == "--list") {
println "Supported profiles:"
profiles.each { println it }
exitCode = 0
}
else {
final String exceptionMessage = "\nUnexpected argument: '${argument}' " + USAGE_STRING
throw new IllegalArgumentException(exceptionMessage)
}
return exitCode
}
/**
* Parse the pom.xml file to get all the available pitest profiles.
*
* @return A set of all available pitest profiles
*/
private static Set<String> getPitestProfiles() {
final GPathResult mainNode = new XmlSlurper().parse(".${File.separator}pom.xml")
final NodeChildren ids = mainNode.profiles.profile.id as NodeChildren
final Set<String> profiles = new HashSet<>()
ids.each { node ->
final GPathResult id = node as GPathResult
final String idText = id.text()
if (idText.startsWith("pitest-")) {
profiles.add(idText)
}
}
return profiles
}
/**
* Check the generated pitest report. Parse the surviving and suppressed mutations and compare
* them.
*
* @param profile the pitest profile to execute
* @param flag command line argument flag to determine output format
* @return {@code 0} if pitest report is as expected, {@code 1} otherwise
*/
private static int checkPitestReport(String profile, String flag) {
final XmlParser xmlParser = new XmlParser()
File mutationReportFile = null
final String suppressedMutationFileUri = ".${File.separator}.ci${File.separator}" +
"pitest-suppressions${File.separator}${profile}-suppressions.xml"
final File pitReports =
new File(".${File.separator}target${File.separator}pit-reports")
if (!pitReports.exists()) {
throw new IllegalStateException(
"Pitest report directory does not exist, generate pitest report first")
}
pitReports.eachFileRecurse(FileType.FILES) {
if (it.name == 'mutations.xml') {
mutationReportFile = it
}
}
final Node mutationReportNode = xmlParser.parse(mutationReportFile)
final Set<Mutation> survivingMutations = getSurvivingMutations(mutationReportNode)
final File suppressionFile = new File(suppressedMutationFileUri)
Set<Mutation> suppressedMutations = Collections.emptySet()
if (suppressionFile.exists()) {
final Node suppressedMutationNode = xmlParser.parse(suppressedMutationFileUri)
suppressedMutations = getSuppressedMutations(suppressedMutationNode)
}
return compareMutations(survivingMutations, suppressedMutations, flag)
}
/**
* Get the surviving mutations. All child nodes of the main {@code mutations} node
* are parsed.
*
* @param mainNode the main {@code mutations} node
* @return A set of surviving mutations
*/
private static Set<Mutation> getSurvivingMutations(Node mainNode) {
final List<Node> children = mainNode.children()
final Set<Mutation> survivingMutations = new HashSet<>()
children.each { node ->
final Node mutationNode = node as Node
final String mutationStatus = mutationNode.attribute("status")
if (mutationStatus == "SURVIVED" || mutationStatus == "NO_COVERAGE") {
survivingMutations.add(getMutation(mutationNode))
}
}
return survivingMutations
}
/**
* Get the suppressed mutations. All child nodes of the main {@code suppressedMutations} node
* are parsed.
*
* @param mainNode the main {@code suppressedMutations} node
* @return A set of suppressed mutations
*/
private static Set<Mutation> getSuppressedMutations(Node mainNode) {
final List<Node> children = mainNode.children()
final Set<Mutation> suppressedMutations = new HashSet<>()
children.each { node ->
final Node mutationNode = node as Node
suppressedMutations.add(getMutation(mutationNode))
}
return suppressedMutations
}
/**
* Construct the {@link Mutation} object from the {@code mutation} XML node.
* The {@code mutations.xml} file is parsed to get the {@code mutationNode}.
*
* @param mutationNode the {@code mutation} XML node
* @return {@link Mutation} object represented by the {@code mutation} XML node
*/
private static Mutation getMutation(Node mutationNode) {
final List childNodes = mutationNode.children()
String sourceFile = null
String mutatedClass = null
String mutatedMethod = null
String mutator = null
String lineContent = null
String description = null
String mutationClassPackage = null
int lineNumber = 0
childNodes.each {
final Node childNode = it as Node
final String text = childNode.name()
final String childNodeText = XmlUtil.escapeXml(childNode.text())
switch (text) {
case "sourceFile":
sourceFile = childNodeText
break
case "mutatedClass":
mutatedClass = childNodeText
mutationClassPackage = mutatedClass.split("[A-Z]")[0]
break
case "mutatedMethod":
mutatedMethod = childNodeText
break
case "mutator":
mutator = childNodeText
break
case "description":
description = childNodeText
break
case "lineNumber":
lineNumber = Integer.parseInt(childNodeText)
break
case "lineContent":
lineContent = childNodeText
break
}
}
if (lineContent == null) {
final String mutationFileName = mutationClassPackage + sourceFile
final String startingPath =
".${File.separator}src${File.separator}main${File.separator}java${File.separator}"
final String javaExtension = ".java"
final String mutationFilePath = startingPath + mutationFileName
.substring(0, mutationFileName.length() - javaExtension.length())
.replace(".", File.separator) + javaExtension
final File file = new File(mutationFilePath)
lineContent = XmlUtil.escapeXml(file.readLines().get(lineNumber - 1).trim())
}
if (lineNumber == 0) {
lineNumber = -1
}
final String unstableAttributeValue = mutationNode.attribute("unstable")
final boolean isUnstable = Boolean.parseBoolean(unstableAttributeValue)
return new Mutation(sourceFile, mutatedClass, mutatedMethod, mutator, description,
lineContent, lineNumber, isUnstable)
}
/**
* Compare surviving and suppressed mutations. The comparison passes successfully (i.e. returns 0)
* when:
* <ol>
* <li>Surviving and suppressed mutations are equal.</li>
* <li>There are extra suppressed mutations but they are unstable
* i.e. {@code unstable="true"}.</li>
* </ol>
* The comparison fails (i.e. returns 1) when:
* <ol>
* <li>Surviving mutations are not present in the suppressed list.</li>
* <li>There are mutations in the suppression list that are not there is surviving list.</li>
* </ol>
*
* @param survivingMutations A set of surviving mutations
* @param suppressedMutations A set of suppressed mutations
* @param flag command line argument flag to determine output format
* @return {@code 0} if comparison passes successfully
*/
private static int compareMutations(Set<Mutation> survivingMutations,
Set<Mutation> suppressedMutations,
String flag) {
final Set<Mutation> survivingUnsuppressedMutations =
setDifference(survivingMutations, suppressedMutations)
final Set<Mutation> extraSuppressions =
setDifference(suppressedMutations, survivingMutations)
final int exitCode
if (survivingMutations == suppressedMutations) {
exitCode = 0
}
else if (survivingUnsuppressedMutations.isEmpty()
&& !hasOnlyStableMutations(extraSuppressions)) {
exitCode = 0
}
else {
if (!survivingUnsuppressedMutations.isEmpty()) {
println "New surviving mutation(s) found:"
survivingUnsuppressedMutations.each {
printMutation(flag, it)
}
}
if (!extraSuppressions.isEmpty()
&& extraSuppressions.any { !it.isUnstable() }) {
println "\nUnnecessary suppressed mutation(s) found and should be removed:"
extraSuppressions.each {
if (!it.isUnstable()) {
printMutation(flag, it)
}
}
}
exitCode = 1
}
return exitCode
}
/**
* Whether a set has only stable mutations.
*
* @param mutations A set of mutations
* @return {@code true} if a set has only stable mutations
*/
private static boolean hasOnlyStableMutations(Set<Mutation> mutations) {
return mutations.every { !it.isUnstable() }
}
/**
* Prints the mutation according to the nature of the flag.
*
* @param flag command line argument flag to determine output format
* @param mutation mutation to print
*/
private static void printMutation(String flag, Mutation mutation) {
if (flag != null) {
println mutation.toXmlString()
}
else {
println mutation
}
}
/**
* Determine the difference between 2 sets. The result is {@code setOne - setTwo}.
*
* @param setOne The first set in the difference
* @param setTwo The second set in the difference
* @return {@code setOne - setTwo}
*/
private static Set<Mutation> setDifference(final Set<Mutation> setOne,
final Set<Mutation> setTwo) {
final Set<Mutation> result = new TreeSet<>(setOne)
result.removeIf { mutation -> setTwo.contains(mutation) }
return result
}
/**
* A class to represent the XML {@code mutation} node.
*/
@EqualsAndHashCode(excludes = ["lineNumber", "unstable"])
@Immutable
class Mutation implements Comparable<Mutation> {
/**
* Mutation nodes present in suppressions file do not have a {@code lineNumber}.
* The {@code lineNumber} is set to {@code -1} for such mutations.
*/
private static final int LINE_NUMBER_NOT_PRESENT_VALUE = -1
String sourceFile
String mutatedClass
String mutatedMethod
String mutator
String description
String lineContent
int lineNumber
boolean unstable
@Override
String toString() {
String toString = """
Source File: "${getSourceFile()}"
Class: "${getMutatedClass()}"
Method: "${getMutatedMethod()}"
Line Contents: "${getLineContent()}"
Mutator: "${getMutator()}"
Description: "${getDescription()}"
""".stripIndent()
if (getLineNumber() != LINE_NUMBER_NOT_PRESENT_VALUE) {
toString += 'Line Number: ' + getLineNumber()
}
return toString
}
@Override
int compareTo(Mutation other) {
int i = getSourceFile() <=> other.getSourceFile()
if (i != 0) {
return i
}
i = getMutatedClass() <=> other.getMutatedClass()
if (i != 0) {
return i
}
i = getMutatedMethod() <=> other.getMutatedMethod()
if (i != 0) {
return i
}
i = getLineContent() <=> other.getLineContent()
if (i != 0) {
return i
}
i = getMutator() <=> other.getMutator()
if (i != 0) {
return i
}
return getDescription() <=> other.getDescription()
}
/**
* XML format of the mutation.
*
* @return XML format of the mutation
*/
String toXmlString() {
return """
<mutation unstable="${isUnstable()}">
<sourceFile>${getSourceFile()}</sourceFile>
<mutatedClass>${getMutatedClass()}</mutatedClass>
<mutatedMethod>${getMutatedMethod()}</mutatedMethod>
<mutator>${getMutator()}</mutator>
<description>${getDescription()}</description>
<lineContent>${getLineContent()}</lineContent>
</mutation>
""".stripIndent(10)
}
}