Skip to content

Commit

Permalink
Support for standard argLine
Browse files Browse the repository at this point in the history
Surefire and other plugins support the `argLine` property for passing
parameters to child jvms in the form of a single string.

Pitest currently supports passing a list of properties using the jvmArgs
param. This requires duplicating values already specified in the
argLine.

The list format also causes problems when the arguments contain
delimiters, for which some unsatisfactory workarounds have been applied.

This change adds a new argLine property to pitest, which is read from
the standard maven property by default. It can be set explicitly for Ant
and the command line.

The existing jvmArgs parameter is retained, and can be used to set
additional arguments for pitest.
  • Loading branch information
Henry Coles committed Jul 14, 2022
1 parent 34153e5 commit 04b672c
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 12 deletions.
5 changes: 5 additions & 0 deletions pitest-ant/src/main/java/org/pitest/ant/PitestTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ public void setOutputEncoding(String value) {
this.setOption(ConfigOption.OUTPUT_ENCODING, value);
}

public void setArgLine(String value) {
this.setOption(ConfigOption.ARG_LINE, value);
}

private void setOption(final ConfigOption option, final String value) {
if (!"".equals(value)) {
this.options.put(option.getParamName(), value);
Expand All @@ -271,4 +275,5 @@ public void setUseClasspathJar(String value) {
}



}
6 changes: 6 additions & 0 deletions pitest-ant/src/test/java/org/pitest/ant/PitestTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@ public void passesOutputEncodingToJavaTask() {
verify(this.arg).setValue("--outputEncoding=US-ASCII");
}

@Test
public void passesArgLineToJavaTask() {
this.pitestTask.setArgLine("-Dfoo=\"bar\"");
this.pitestTask.execute(this.java);
verify(this.arg).setValue("--argLine=-Dfoo=\"bar\"");
}

private static class PathMatcher implements ArgumentMatcher<Path> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.function.Predicate;
import java.util.logging.Logger;

import static org.pitest.mutationtest.config.ConfigOption.ARG_LINE;
import static org.pitest.mutationtest.config.ConfigOption.AVOID_CALLS;
import static org.pitest.mutationtest.config.ConfigOption.CHILD_JVM;
import static org.pitest.mutationtest.config.ConfigOption.CLASSPATH;
Expand Down Expand Up @@ -107,6 +108,7 @@ public class OptionsParser {
private final OptionSpec<File> historyInputSpec;
private final OptionSpec<String> mutators;
private final OptionSpec<String> features;
private final OptionSpec<String> argLine;
private final OptionSpec<String> jvmArgs;
private final CommaAwareArgsProcessor jvmArgsProcessor;
private final OptionSpec<Float> timeoutFactorSpec;
Expand Down Expand Up @@ -203,6 +205,9 @@ public OptionsParser(Predicate<String> dependencyFilter) {
.ofType(String.class).withValuesSeparatedBy(',')
.describedAs("comma separated list of features to enable/disable.");

this.argLine = parserAccepts(ARG_LINE).withRequiredArg()
.describedAs("argline for child JVMs");

this.jvmArgs = parserAccepts(CHILD_JVM).withRequiredArg()
.describedAs("comma separated list of child JVM args");

Expand Down Expand Up @@ -416,6 +421,8 @@ private ParseResult parseCommandLine(final ReportOptions data,
data.setMutators(this.mutators.values(userArgs));
data.setFeatures(this.features.values(userArgs));

data.setArgLine(this.argLine.value(userArgs));

data.addChildJVMArgs(this.jvmArgsProcessor.values(userArgs));

data.setFullMutationMatrix(this.fullMutationMatrixSpec.value(userArgs));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public void shouldParseCommaSeparatedListOfSourceDirectories() {
assertEquals(Arrays.asList(new File("foo/bar"), new File("bar/far")), actual.getSourceDirs());
}

@Test
public void shouldSetArgLine() {
final ReportOptions actual = parseAddingRequiredArgs("--argLine", "-Dfoo=\"bar\"");

assertThat(actual.getArgLine()).isEqualTo("-Dfoo=\"bar\"");
}

@Test
public void shouldParseCommaSeparatedListOfJVMArgs() {
final ReportOptions actual = parseAddingRequiredArgs("--jvmArgs", "foo,bar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public enum ConfigOption {
*/
CHILD_JVM("jvmArgs"),

/**
* Arguments to launch child processes with expressed as single line
*/
ARG_LINE("argLine"),

/**
* Do/don't create timestamped folders for reports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public class ReportOptions {
private Collection<String> features;

private final List<String> jvmArgs = new ArrayList<>(DEFAULT_CHILD_JVM_ARGS);

private String argLine;

private int numberOfThreads = 0;
private float timeoutFactor = PercentAndConstantTimeoutStrategy.DEFAULT_FACTOR;
private long timeoutConstant = PercentAndConstantTimeoutStrategy.DEFAULT_CONSTANT;
Expand Down Expand Up @@ -219,6 +222,14 @@ public void addChildJVMArgs(final List<String> args) {
this.jvmArgs.addAll(args);
}

public String getArgLine() {
return argLine;
}

public void setArgLine(String argLine) {
this.argLine = argLine;
}

public ClassPath getClassPath() {
if (this.classPathElements != null) {
return createClassPathFromElements();
Expand Down Expand Up @@ -628,6 +639,7 @@ public void setOutputEncoding(Charset outputEncoding) {
this.outputEncoding = outputEncoding;
}


@Override
public String toString() {
return new StringJoiner(", ", ReportOptions.class.getSimpleName() + "[", "]")
Expand All @@ -644,6 +656,7 @@ public String toString() {
.add("mutators=" + mutators)
.add("features=" + features)
.add("jvmArgs=" + jvmArgs)
.add("argLine=" + argLine)
.add("numberOfThreads=" + numberOfThreads)
.add("timeoutFactor=" + timeoutFactor)
.add("timeoutConstant=" + timeoutConstant)
Expand Down Expand Up @@ -676,4 +689,6 @@ public String toString() {
.add("outputEncoding=" + outputEncoding)
.toString();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.pitest.mutationtest.incremental.WriterFactory;
import org.pitest.plugin.Feature;
import org.pitest.plugin.FeatureParameter;
import org.pitest.process.ArgLineParser;
import org.pitest.process.JavaAgent;
import org.pitest.process.LaunchOptions;
import org.pitest.util.Log;
Expand All @@ -28,6 +29,8 @@
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
Expand Down Expand Up @@ -97,7 +100,7 @@ public AnalysisResult execute(File baseDir, ReportOptions data,

final CoverageOptions coverageOptions = settings.createCoverageOptions();
final LaunchOptions launchOptions = new LaunchOptions(ja,
settings.getJavaExecutable(), data.getJvmArgs(), environmentVariables)
settings.getJavaExecutable(), createJvmArgs(data), environmentVariables)
.usingClassPathJar(data.useClasspathJar());
final ProjectClassPaths cps = data.getMutationClassPaths();

Expand Down Expand Up @@ -132,6 +135,12 @@ public AnalysisResult execute(File baseDir, ReportOptions data,

}

private List<String> createJvmArgs(ReportOptions data) {
List<String> args = new ArrayList<>(data.getJvmArgs());
args.addAll(ArgLineParser.split(data.getArgLine()));
return args;
}

private HistoryStore makeHistoryStore(ReportOptions data, Optional<WriterFactory> historyWriter) {
final Optional<Reader> reader = data.createHistoryReader();
if (!reader.isPresent() && !historyWriter.isPresent()) {
Expand Down
107 changes: 107 additions & 0 deletions pitest-entry/src/main/java/org/pitest/process/ArgLineParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.pitest.process;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.StringTokenizer;

import static org.pitest.process.ArgLineParser.State.START;

/**
* Simple state machine to split arglines into sections. Arglines may
* contain single or double quotes, which might be escaped.
*/
public class ArgLineParser {

private static final String ESCAPE_CHAR = "\\";
private static final String SINGLE_QUOTE = "\'";
public static final String DOUBLE_QUOTE = "\"";

public static List<String> split(String in) {
return process(stripWhiteSpace(in));
}

private static List<String> process(String in) {
if (in.isEmpty()) {
return Collections.emptyList();
}

final StringTokenizer tokenizer = new StringTokenizer(in, "\"\' \\", true);
List<String> tokens = new ArrayList<>();

Deque<State> state = new ArrayDeque<>();
state.push(START);
StringBuilder current = new StringBuilder();
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
switch (state.peek()) {
case START:
if (token.equals(SINGLE_QUOTE)) {
state.push(State.IN_QUOTE);
} else if (token.equals(DOUBLE_QUOTE)) {
state.push(State.IN_DOUBLE_QUOTE);
} else if (token.equals(" ")) {
if (current.length() != 0) {
tokens.add(current.toString());
current = new StringBuilder();
}
} else {
current.append(token);
if (token.equals(ESCAPE_CHAR)) {
state.push(State.IN_ESCAPE);
}
}
break;
case IN_QUOTE:
if (token.equals(SINGLE_QUOTE)) {
state.pop();
} else {
current.append(token);
if (token.equals(ESCAPE_CHAR)) {
state.push(State.IN_ESCAPE);
}
}
break;
case IN_DOUBLE_QUOTE:
if (token.equals(DOUBLE_QUOTE)) {
state.pop();
} else {
current.append(token);
if (token.equals(ESCAPE_CHAR)) {
state.push(State.IN_ESCAPE);
}
}
break;
case IN_ESCAPE:
current.append(token);
if (!token.equals(ESCAPE_CHAR)) {
state.pop();
}
break;
}
}

if (current.length() != 0) {
tokens.add(current.toString());
}

if (state.size() != 1) {
throw new RuntimeException("Unclosed quote in " + in);
}

return tokens;
}

private static String stripWhiteSpace(String in) {
if (in == null) {
return "";
}
return in.replaceAll("\\s", " ").trim();
}

enum State {
START, IN_ESCAPE, IN_QUOTE, IN_DOUBLE_QUOTE
}
}
11 changes: 7 additions & 4 deletions pitest-entry/src/main/java/org/pitest/process/LaunchOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ public LaunchOptions(JavaAgent javaAgentFinder) {
}

public LaunchOptions(JavaAgent javaAgentFinder,
JavaExecutableLocator javaExecutable, List<String> childJVMArgs,
Map<String, String> environmentVariables) {
JavaExecutableLocator javaExecutable,
List<String> childJVMArgs,
Map<String, String> environmentVariables) {
this(javaAgentFinder, javaExecutable, childJVMArgs, environmentVariables, false);
}

public LaunchOptions(JavaAgent javaAgentFinder,
JavaExecutableLocator javaExecutable, List<String> childJVMArgs,
Map<String, String> environmentVariables, boolean usingClassPathJar) {
JavaExecutableLocator javaExecutable,
List<String> childJVMArgs,
Map<String, String> environmentVariables,
boolean usingClassPathJar) {
this.javaAgentFinder = javaAgentFinder;
this.childJVMArgs = childJVMArgs;
this.javaExecutable = javaExecutable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.pitest.process;

import static org.pitest.functional.prelude.Prelude.or;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
Expand Down Expand Up @@ -34,7 +33,8 @@ public void start() throws IOException {
final String[] args = { "" + this.port };

final ProcessBuilder processBuilder = createProcessBuilder(
this.processArgs.getJavaExecutable(), this.processArgs.getJvmArgs(),
this.processArgs.getJavaExecutable(),
this.processArgs.getJvmArgs(),
this.minionClass, Arrays.asList(args),
this.processArgs.getJavaAgentFinder(),
this.processArgs.getLaunchClassPath());
Expand Down Expand Up @@ -109,6 +109,7 @@ private List<String> createLaunchArgs(String javaProcess,
cmd.addAll(args);

addPITJavaAgent(agentJarLocator, cmd);

addLaunchJavaAgents(cmd);

cmd.add(mainClass.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected void createAndRun(SettingsFactory settings) {
final CoverageOptions coverageOptions = createCoverageOptions(settings.createCoverageOptions().getPitConfig());
final LaunchOptions launchOptions = new LaunchOptions(agent,
new DefaultJavaExecutableLocator(), this.data.getJvmArgs(),
new HashMap<String, String>());
new HashMap<>());

final PathFilter pf = new PathFilter(p -> true, p -> true);
final ProjectClassPaths cps = new ProjectClassPaths(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,11 @@ private void createEngineAndRun(final ReportOptions data,
final JavaAgent agent,
final Collection<String> mutators) {

// data.setConfiguration(this.config);
final CoverageOptions coverageOptions = createCoverageOptions(data);

final LaunchOptions launchOptions = new LaunchOptions(agent,
new DefaultJavaExecutableLocator(), data.getJvmArgs(),
new HashMap<String, String>());
new HashMap<>());

final PathFilter pf = new PathFilter(
Prelude.not(new DefaultDependencyPathPredicate()),
Expand Down
Loading

0 comments on commit 04b672c

Please sign in to comment.