From 5a56c16882b68e0aaedf3a2a29737f412bbd66be Mon Sep 17 00:00:00 2001 From: Vyom-Yadav Date: Mon, 19 Sep 2022 18:51:32 +0530 Subject: [PATCH] Issue #12132: Fix ArrayIndexOutOfBoundsException in pitest-survival-check-xml.groovy --- ...arterRuleSet-AllRulesByCategory.groovy.txt | 2 +- .ci/pitest-survival-check-xml.groovy | 37 ++++++++++--------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/.ci/StarterRuleSet-AllRulesByCategory.groovy.txt b/.ci/StarterRuleSet-AllRulesByCategory.groovy.txt index 7176621c19a..c066aab0b60 100644 --- a/.ci/StarterRuleSet-AllRulesByCategory.groovy.txt +++ b/.ci/StarterRuleSet-AllRulesByCategory.groovy.txt @@ -350,7 +350,7 @@ ruleset { PropertyName VariableName { finalRegex = null - ignoreVariableNames = 'SEPARATOR,PROFILES,USAGE_STRING' + ignoreVariableNames = 'PROFILES,USAGE_STRING' } // rulesets/security.xml diff --git a/.ci/pitest-survival-check-xml.groovy b/.ci/pitest-survival-check-xml.groovy index 3f8504e7836..b721719eb3d 100644 --- a/.ci/pitest-survival-check-xml.groovy +++ b/.ci/pitest-survival-check-xml.groovy @@ -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) /** @@ -25,17 +31,10 @@ System.exit(exitCode) */ private int parseArgumentAndExecute(String argument, String flag) { final Set 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) @@ -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 @@ -59,7 +58,7 @@ private int parseArgumentAndExecute(String argument, String flag) { * @return A set of all available pitest profiles */ private static Set 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 profiles = new HashSet<>() ids.each { node -> @@ -83,10 +82,11 @@ private static Set 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( @@ -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())