Skip to content

Commit

Permalink
Merge pull request #1319 from hcoles/feature/maven_environment
Browse files Browse the repository at this point in the history
read surefire environment variables
  • Loading branch information
hcoles authored Apr 2, 2024
2 parents 65d3364 + e1ce62b commit 1cf23ab
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -149,6 +151,9 @@ public class ReportOptions {
private Charset inputEncoding;
private Charset outputEncoding;

// currently used only via maven
private Map<String,String> environmentVariables = new HashMap<>();


public Verbosity getVerbosity() {
return this.verbosity;
Expand Down Expand Up @@ -657,6 +662,10 @@ public void setReportCoverage(boolean reportCoverage) {
this.reportCoverage = reportCoverage;
}

public Map<String,String> getEnvironmentVariables() {
return environmentVariables;
}

@Override
public String toString() {
return new StringJoiner(", ", ReportOptions.class.getSimpleName() + "[", "]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,13 @@ private void throwErrorIfMoreThanMaximumSurvivors(final MutationStatistics resul
protected Optional<CombinedStatistics> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -69,6 +72,7 @@ private void convertExcludes(ReportOptions option, Xpp3Dom configuration) {
List<Predicate<String>> excludes = new ArrayList<>();
List<Predicate<String>> surefireExcludes =
extract("excludes", configuration).stream()
.map(p -> p.value)
.filter(Objects::nonNull)
.map(this::filenameToClassFilter)
.collect(Collectors.toList());
Expand All @@ -86,17 +90,23 @@ private void convertArgLine(ReportOptions option, Xpp3Dom configuration) {
}


private void convertEnvironmentVariables(ReportOptions option, Xpp3Dom configuration) {
Map<String, String> environmentVariables = extract("environmentVariables", configuration).stream()
.collect(Collectors.toMap(p -> p.name, p -> p.value));
option.getEnvironmentVariables().putAll(environmentVariables);
}

private Predicate<String> filenameToClassFilter(String filename) {
return new Glob(filename.replace(".java", "").replace("/", "."));
}

private List<String> extract(String childname, Xpp3Dom config) {
private List<Pair> extract(String childname, Xpp3Dom config) {
final Xpp3Dom subelement = config.getChild(childname);
if (subelement != null) {
List<String> result = new LinkedList<>();
List<Pair> 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;
}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -198,10 +199,21 @@ public void doesNotConvertArglineWhenFlagNotSet() throws Exception {
assertThat(actual.getArgLine()).isEqualTo("-foo");
}

@Test
public void shouldConvertEnvironmentVariables() throws Exception {
this.surefireConfig = makeConfig("<environmentVariables><ONE_OF_MANY>value</ONE_OF_MANY>" +
"<B>42</B></environmentVariables>");

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 = "<configuration>" + s + "</configuration>";
InputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
return Xpp3DomBuilder.build(stream, "UTF-8");
}

Expand Down

0 comments on commit 1cf23ab

Please sign in to comment.