Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #12132: Fix ArrayIndexOutOfBoundsException in pitest-survival-check-xml.groovy #12217

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/StarterRuleSet-AllRulesByCategory.groovy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ ruleset {
PropertyName
VariableName {
finalRegex = null
ignoreVariableNames = 'SEPARATOR,PROFILES,USAGE_STRING'
ignoreVariableNames = 'PROFILES,USAGE_STRING'
}

// rulesets/security.xml
Expand Down
37 changes: 19 additions & 18 deletions .ci/pitest-survival-check-xml.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import groovy.util.slurpersupport.GPathResult
import groovy.util.slurpersupport.NodeChildren
import groovy.xml.XmlUtil

@Field final static String SEPARATOR = System.getProperty("file.separator")
@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"

final int exitCode
int exitCode = 1
if (args.length == 2) {
exitCode = parseArgumentAndExecute(args[0], args[1])
}
else {
else if (args.length == 1) {
exitCode = parseArgumentAndExecute(args[0], null)
}
else {
throw new IllegalArgumentException(USAGE_STRING)
}
System.exit(exitCode)

/**
Expand All @@ -25,17 +31,10 @@ System.exit(exitCode)
*/
private int parseArgumentAndExecute(String argument, String flag) {
final Set<String> profiles = getPitestProfiles()
final String scriptPath = ".${SEPARATOR}.ci${SEPARATOR}pitest-survival-check-xml.groovy"
final String usageString = """
Usage groovy ${scriptPath} [profile] [-g | --generate-suppression]
To see the full list of supported profiles run
'groovy ${scriptPath} --list'
""".stripIndent()

final int exitCode
if (profiles.contains(argument)) {
if (flag != null && flag != "-g" && flag != "--generate-suppression") {
final String exceptionMessage = "\nUnexpected flag: ${flag}" + usageString
final String exceptionMessage = "\nUnexpected flag: '${flag}' " + USAGE_STRING
throw new IllegalArgumentException(exceptionMessage)
}
exitCode = checkPitestReport(argument, flag)
Expand All @@ -47,7 +46,7 @@ private int parseArgumentAndExecute(String argument, String flag) {
exitCode = 0
}
else {
final String exceptionMessage = "\nUnexpected argument: ${argument}" + usageString
final String exceptionMessage = "\nUnexpected argument: '${argument}' " + USAGE_STRING
throw new IllegalArgumentException(exceptionMessage)
}
return exitCode
Expand All @@ -59,7 +58,7 @@ private int parseArgumentAndExecute(String argument, String flag) {
* @return A set of all available pitest profiles
*/
private static Set<String> getPitestProfiles() {
final GPathResult mainNode = new XmlSlurper().parse(".${SEPARATOR}pom.xml")
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 ->
Expand All @@ -83,10 +82,11 @@ private static Set<String> getPitestProfiles() {
private static int checkPitestReport(String profile, String flag) {
final XmlParser xmlParser = new XmlParser()
File mutationReportFile = null
final String suppressedMutationFileUri =
".${SEPARATOR}.ci${SEPARATOR}pitest-suppressions${SEPARATOR}${profile}-suppressions.xml"
final String suppressedMutationFileUri = ".${File.separator}.ci${File.separator}" +
"pitest-suppressions${File.separator}${profile}-suppressions.xml"

final File pitReports = new File(".${SEPARATOR}target${SEPARATOR}pit-reports")
final File pitReports =
new File(".${File.separator}target${File.separator}pit-reports")

if (!pitReports.exists()) {
throw new IllegalStateException(
Expand Down Expand Up @@ -202,11 +202,12 @@ private static Mutation getMutation(Node mutationNode) {
}
if (lineContent == null) {
final String mutationFileName = mutationClassPackage + sourceFile
final String startingPath = ".${SEPARATOR}src${SEPARATOR}main${SEPARATOR}java${SEPARATOR}"
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(".", SEPARATOR) + javaExtension
.replace(".", File.separator) + javaExtension

final File file = new File(mutationFilePath)
lineContent = XmlUtil.escapeXml(file.readLines().get(lineNumber - 1).trim())
Expand Down