From e1ce62b20ea013a0518ab01bc356b07acea6ac52 Mon Sep 17 00:00:00 2001 From: Henry Coles Date: Thu, 28 Mar 2024 18:23:22 +0000 Subject: [PATCH] read surefire environment variables --- .../mutationtest/config/ReportOptions.java | 9 +++++++ .../org/pitest/maven/AbstractPitMojo.java | 7 ++++- .../pitest/maven/SurefireConfigConverter.java | 26 ++++++++++++++++--- .../maven/SurefireConfigConverterTest.java | 14 +++++++++- 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/config/ReportOptions.java b/pitest-entry/src/main/java/org/pitest/mutationtest/config/ReportOptions.java index 55c06939b..c35927a3b 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/config/ReportOptions.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/config/ReportOptions.java @@ -44,8 +44,10 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Properties; import java.util.StringJoiner; @@ -149,6 +151,9 @@ public class ReportOptions { private Charset inputEncoding; private Charset outputEncoding; + // currently used only via maven + private Map environmentVariables = new HashMap<>(); + public Verbosity getVerbosity() { return this.verbosity; @@ -657,6 +662,10 @@ public void setReportCoverage(boolean reportCoverage) { this.reportCoverage = reportCoverage; } + public Map getEnvironmentVariables() { + return environmentVariables; + } + @Override public String toString() { return new StringJoiner(", ", ReportOptions.class.getSimpleName() + "[", "]") diff --git a/pitest-maven/src/main/java/org/pitest/maven/AbstractPitMojo.java b/pitest-maven/src/main/java/org/pitest/maven/AbstractPitMojo.java index 89fb20b5f..979e0eedd 100644 --- a/pitest-maven/src/main/java/org/pitest/maven/AbstractPitMojo.java +++ b/pitest-maven/src/main/java/org/pitest/maven/AbstractPitMojo.java @@ -523,8 +523,13 @@ private void throwErrorIfMoreThanMaximumSurvivors(final MutationStatistics resul protected Optional analyse() throws MojoExecutionException { final ReportOptions data = new MojoToReportOptionsConverter(this, new SurefireConfigConverter(this.isParseSurefireArgLine()), this.filter).convert(); + + // overwrite variable from surefire with any explicitly set + // for pitest / add additional values + data.getEnvironmentVariables().putAll(this.environmentVariables); + return Optional.ofNullable(this.goalStrategy.execute(detectBaseDir(), data, - this.plugins, this.environmentVariables)); + this.plugins, data.getEnvironmentVariables())); } protected File detectBaseDir() { diff --git a/pitest-maven/src/main/java/org/pitest/maven/SurefireConfigConverter.java b/pitest-maven/src/main/java/org/pitest/maven/SurefireConfigConverter.java index 6f0886c4e..4719dbce2 100644 --- a/pitest-maven/src/main/java/org/pitest/maven/SurefireConfigConverter.java +++ b/pitest-maven/src/main/java/org/pitest/maven/SurefireConfigConverter.java @@ -9,6 +9,7 @@ import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.Xpp3Dom; +import java.util.Map; import java.util.Objects; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -35,6 +36,8 @@ public ReportOptions update(ReportOptions option, Xpp3Dom configuration) { convertExcludes(option, configuration); convertGroups(option, configuration); convertTestFailureIgnore(option, configuration); + convertEnvironmentVariables(option, configuration); + if (parseArgLine) { convertArgLine(option, configuration); } @@ -69,6 +72,7 @@ private void convertExcludes(ReportOptions option, Xpp3Dom configuration) { List> excludes = new ArrayList<>(); List> surefireExcludes = extract("excludes", configuration).stream() + .map(p -> p.value) .filter(Objects::nonNull) .map(this::filenameToClassFilter) .collect(Collectors.toList()); @@ -86,17 +90,23 @@ private void convertArgLine(ReportOptions option, Xpp3Dom configuration) { } + private void convertEnvironmentVariables(ReportOptions option, Xpp3Dom configuration) { + Map environmentVariables = extract("environmentVariables", configuration).stream() + .collect(Collectors.toMap(p -> p.name, p -> p.value)); + option.getEnvironmentVariables().putAll(environmentVariables); + } + private Predicate filenameToClassFilter(String filename) { return new Glob(filename.replace(".java", "").replace("/", ".")); } - private List extract(String childname, Xpp3Dom config) { + private List extract(String childname, Xpp3Dom config) { final Xpp3Dom subelement = config.getChild(childname); if (subelement != null) { - List result = new LinkedList<>(); + List result = new LinkedList<>(); final Xpp3Dom[] children = subelement.getChildren(); for (Xpp3Dom child : children) { - result.add(child.getValue()); + result.add(new Pair(child.getName(), child.getValue())); } return result; } @@ -111,3 +121,13 @@ private void convertTestFailureIgnore(ReportOptions option, Xpp3Dom configuratio } } } + +class Pair { + final String name; + final String value; + + Pair(String name, String value) { + this.name = name; + this.value = value; + } +} \ No newline at end of file diff --git a/pitest-maven/src/test/java/org/pitest/maven/SurefireConfigConverterTest.java b/pitest-maven/src/test/java/org/pitest/maven/SurefireConfigConverterTest.java index 58ebeac20..6737710ba 100644 --- a/pitest-maven/src/test/java/org/pitest/maven/SurefireConfigConverterTest.java +++ b/pitest-maven/src/test/java/org/pitest/maven/SurefireConfigConverterTest.java @@ -4,6 +4,7 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; @@ -198,10 +199,21 @@ public void doesNotConvertArglineWhenFlagNotSet() throws Exception { assertThat(actual.getArgLine()).isEqualTo("-foo"); } + @Test + public void shouldConvertEnvironmentVariables() throws Exception { + this.surefireConfig = makeConfig("value" + + "42"); + + ReportOptions actual = this.testee + .update(this.options, this.surefireConfig); + + assertThat(actual.getEnvironmentVariables()).containsEntry("ONE_OF_MANY", "value"); + assertThat(actual.getEnvironmentVariables()).containsEntry("B", "42"); + } private Xpp3Dom makeConfig(String s) throws Exception { String xml = "" + s + ""; - InputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8")); + InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)); return Xpp3DomBuilder.build(stream, "UTF-8"); }