Skip to content

Commit

Permalink
Auto add headless true only using a feature
Browse files Browse the repository at this point in the history
Pitest automatically adds -Djava.awt.headless=true to the jvm args to
prevent keyboard focu being stolen on macs.

The change introduces a new extention point ConfigurationUpdater
to allow changes to be made to the supplied config.

This interface uses the feature system, so implementations can be
enabled and disabled at runtime.

The previously hard coded adding of headless=true is moved into a
feature. It is enabled by default, but can now be dissabled by passing

features="-macos_focus"

Consideration was given to only adding the param when running on OS X,
but this was decided against and it would make the build inconsistent.
  • Loading branch information
Henry Coles committed Jul 14, 2022
1 parent 206d89d commit cf684f0
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Read all about it at http://pitest.org
### 1.9.3 (unreleased)

* #1052 - Support maven argLine property and single string argLines
* #1054 - Provide control over auto addition of -Djava.awt.headless=true

1054 Moves support of auto adding headless=true (to prevent keyboard focus being stolen on Macs) into a feature.
It is enabled by default, but can be disabled by adding `-MACOS_FOCUS` to the features string.

### 1.9.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.pitest.mutationtest.config.ReportOptions.DEFAULT_CHILD_JVM_ARGS;

import java.io.File;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -108,7 +107,6 @@ public void shouldParseCommaSeparatedListOfJVMArgs() {
final ReportOptions actual = parseAddingRequiredArgs("--jvmArgs", "foo,bar");

List<String> expected = new ArrayList<>();
expected.addAll(DEFAULT_CHILD_JVM_ARGS);
expected.add("foo");
expected.add("bar");
assertEquals(expected, actual.getJvmArgs());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.pitest.mutationtest.autoconfig;

import org.pitest.mutationtest.config.ConfigurationUpdater;
import org.pitest.mutationtest.config.ReportOptions;
import org.pitest.plugin.Feature;

import static java.util.Collections.singletonList;

/**
* Pitest steals keyboard focus in OSX unless java is launched in
* headless mode.
*/
public class KeepMacOsFocus implements ConfigurationUpdater {

@Override
public void updateConfig(ReportOptions toModify) {
toModify.addChildJVMArgs(singletonList("-Djava.awt.headless=true"));
}

@Override
public Feature provides() {
return Feature.named("MACOS_FOCUS")
.withOnByDefault(true)
.withDescription(description());
}

@Override
public String description() {
return "Auto add java.awt.headless=true to keep keyboard focus on Mac OS";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.pitest.mutationtest.config;

import org.pitest.plugin.Feature;
import org.pitest.plugin.FeatureSelector;
import org.pitest.plugin.FeatureSetting;

import java.util.Collection;
import java.util.List;

public class CompoundConfigurationUpdater implements ConfigurationUpdater {
private final FeatureSelector<ConfigurationUpdater> features;

public CompoundConfigurationUpdater(List<FeatureSetting> features,
Collection<ConfigurationUpdater> children) {
this.features = new FeatureSelector<>(features, children);
}

@Override
public void updateConfig(ReportOptions toModify) {
for (ConfigurationUpdater each : features.getActiveFeatures() ) {
each.updateConfig(toModify);
}
}

@Override
public Feature provides() {
return Feature.named("n/a");
}

@Override
public String description() {
return "n/a";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.pitest.mutationtest.config;

import org.pitest.plugin.ProvidesFeature;
import org.pitest.plugin.ToolClasspathPlugin;

public interface ConfigurationUpdater extends ToolClasspathPlugin, ProvidesFeature {
void updateConfig(ReportOptions toModify);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public Collection<? extends ToolClasspathPlugin> findToolClasspathPlugins() {
l.addAll(findGroupers());
l.addAll(findTestPrioritisers());
l.addAll(findInterceptors());
l.addAll(findConfigurationUpdaters());
return l;
}

Expand All @@ -64,6 +65,10 @@ public List<? extends ClientClasspathPlugin> findClientClasspathPlugins() {
return l;
}

public Collection<? extends ConfigurationUpdater> findConfigurationUpdaters() {
return load(ConfigurationUpdater.class);
}

public Collection<? extends MethodMutatorFactory> findMutationOperators() {
return load(MethodMutatorFactory.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@
*/
public class ReportOptions {

public static final List<String> DEFAULT_CHILD_JVM_ARGS = Collections.singletonList("-Djava.awt.headless=true");

public static final Collection<String> LOGGING_CLASSES = Arrays
.asList(
"java.util.logging",
Expand Down Expand Up @@ -96,9 +94,9 @@ public class ReportOptions {
private Collection<String> mutators;
private Collection<String> features;

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

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

private int numberOfThreads = 0;
private float timeoutFactor = PercentAndConstantTimeoutStrategy.DEFAULT_FACTOR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public MutationResultListenerFactory createListener() {
return new CompoundListenerFactory(parser.parseFeatures(this.options.getFeatures()), findListeners());
}

public ConfigurationUpdater createUpdater() {
final FeatureParser parser = new FeatureParser();
return new CompoundConfigurationUpdater(parser.parseFeatures(this.options.getFeatures()), new ArrayList<>(plugins.findConfigurationUpdaters()));
}

public JavaExecutableLocator getJavaExecutable() {
if (this.options.getJavaExecutable() != null) {
return new KnownLocationJavaExecutableLocator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public AnalysisResult execute(File baseDir, ReportOptions data,
public AnalysisResult execute(File baseDir, ReportOptions data,
SettingsFactory settings, Map<String, String> environmentVariables) {

updateData(data, settings);

if (data.getVerbosity() == VERBOSE) {
Log.getLogger().info("Project base directory is " + data.getProjectBase());
Log.getLogger().info("---------------------------------------------------------------------------");
Expand Down Expand Up @@ -141,6 +143,11 @@ private List<String> createJvmArgs(ReportOptions data) {
return args;
}

private void updateData(ReportOptions data, SettingsFactory settings) {
settings.createUpdater().updateConfig(data);

}

private HistoryStore makeHistoryStore(ReportOptions data, Optional<WriterFactory> historyWriter) {
final Optional<Reader> reader = data.createHistoryReader();
if (!reader.isPresent() && !historyWriter.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.pitest.mutationtest.autoconfig.KeepMacOsFocus
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.pitest.mutationtest.autoconfig;

import org.junit.Test;
import org.pitest.mutationtest.config.ReportOptions;

import static org.assertj.core.api.Assertions.assertThat;

public class KeepMacOsFocusTest {
KeepMacOsFocus underTest = new KeepMacOsFocus();

@Test
public void addsHeadlessTrueToJvmArgs() {
ReportOptions data = new ReportOptions();

underTest.updateConfig(data);
assertThat(data.getJvmArgs()).contains("-Djava.awt.headless=true");
}

@Test
public void featureIsNamedMacOsFocus() {
KeepMacOsFocus underTest = new KeepMacOsFocus();

assertThat(underTest.provides().name()).isEqualTo("macos_focus");
}

@Test
public void featureIsOnByDefault() {
KeepMacOsFocus underTest = new KeepMacOsFocus();

assertThat(underTest.provides().isOnByDefault()).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,6 @@ public void shouldWorkWithGWTMockito() throws Exception {
assertThat(actual).doesNotContain("status='RUN_ERROR'");
}

private void skipIfJavaVersionNotSupportByThirdParty() {
String javaVersion = System.getProperty("java.version");
assumeFalse(javaVersion.startsWith("9") || javaVersion.startsWith("10") || javaVersion.startsWith("11"));
}

@Test
@Ignore("yatspec is not available on maven central. Repo currently down")
public void shouldWorkWithYatspec() throws Exception {
Expand Down Expand Up @@ -451,6 +446,12 @@ public void shouldFailCleanlyWhenTestPluginMissing() throws IOException {
}


private void skipIfJavaVersionNotSupportByThirdParty() {
String javaVersion = System.getProperty("java.version");
assumeFalse(javaVersion.startsWith("9") || javaVersion.startsWith("10") || javaVersion.startsWith("11"));
}


private static String readResults(File testDir) throws IOException {
File mutationReport = new File(testDir.getAbsoluteFile() + File.separator
+ "target" + File.separator + "pit-reports" + File.separator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public void testParsesListOfJVMArgs() {
final ReportOptions actual = parseConfig(xml);

List<String> expectedArgs = new ArrayList<>();
expectedArgs.addAll(ReportOptions.DEFAULT_CHILD_JVM_ARGS);
expectedArgs.add("foo");
expectedArgs.add("bar");

Expand Down

0 comments on commit cf684f0

Please sign in to comment.