Skip to content

Commit

Permalink
expand feature mechanism to history plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Coles committed Sep 19, 2023
1 parent 21450d9 commit 209b9d1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.pitest.mutationtest;

import org.pitest.classpath.CodeSource;
import org.pitest.mutationtest.incremental.WriterFactory;
import org.pitest.plugin.ProvidesFeature;
import org.pitest.plugin.ToolClasspathPlugin;

import java.io.Reader;
import java.util.Optional;

public interface HistoryFactory extends ToolClasspathPlugin {
History makeHistory(CodeSource code, WriterFactory output, Optional<Reader> input);
public interface HistoryFactory extends ToolClasspathPlugin, ProvidesFeature {
History makeHistory(HistoryParams params, WriterFactory output, Optional<Reader> input);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.pitest.mutationtest;

import org.pitest.classpath.CodeSource;
import org.pitest.plugin.FeatureSelector;
public class HistoryParams {
private final FeatureSelector conf;
private final CodeSource code;

public HistoryParams(FeatureSelector conf, CodeSource code) {
this.conf = conf;
this.code = code;
}

public FeatureSelector featureSettings() {
return conf;
}

public CodeSource code() {
return code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,19 @@ public CodeSource createCodeSource(ProjectClassPaths classPath) {
}

public HistoryFactory createHistory() {
List<HistoryFactory> sources = this.plugins.findHistory();
if (sources.isEmpty()) {
List<HistoryFactory> available = this.plugins.findHistory();

final FeatureParser parser = new FeatureParser();
FeatureSelector<HistoryFactory> historyFeatures = new FeatureSelector<>(parser.parseFeatures(this.options.getFeatures()), available);
List<HistoryFactory> enabledHistory = historyFeatures.getActiveFeatures();

if (enabledHistory.isEmpty()) {
return new DefaultHistoryFactory();
}
if (sources.size() > 1) {
throw new RuntimeException("More than one HistoryFactory found on classpath.");
if (enabledHistory.size() > 1) {
throw new RuntimeException("More than one HistoryFactory enabled.");
}
return sources.get(0);
return enabledHistory.get(0);
}

public void describeFeatures(Consumer<Feature> enabled, Consumer<Feature> disabled) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package org.pitest.mutationtest.incremental;

import org.pitest.classpath.CodeSource;
import org.pitest.mutationtest.History;
import org.pitest.mutationtest.HistoryFactory;
import org.pitest.mutationtest.HistoryParams;
import org.pitest.plugin.Feature;

import java.io.Reader;
import java.util.Optional;

public class DefaultHistoryFactory implements HistoryFactory {
@Override
public History makeHistory(CodeSource code, WriterFactory output, Optional<Reader> input) {
return new ObjectOutputStreamHistory(code, output, input);
public History makeHistory(HistoryParams params, WriterFactory output, Optional<Reader> input) {
return new ObjectOutputStreamHistory(params.code(), output, input);
}

@Override
public String description() {
return "Default history";
}

@Override
public Feature provides() {
return Feature.named("default_history")
.withOnByDefault(true)
.asInternalFeature()
.withDescription(description());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.pitest.coverage.execute.DefaultCoverageGenerator;
import org.pitest.mutationtest.History;
import org.pitest.mutationtest.HistoryFactory;
import org.pitest.mutationtest.HistoryParams;
import org.pitest.mutationtest.incremental.HistoryResultInterceptor;
import org.pitest.mutationtest.MutationResultListenerFactory;
import org.pitest.mutationtest.config.PluginServices;
Expand All @@ -19,6 +20,8 @@
import org.pitest.mutationtest.incremental.WriterFactory;
import org.pitest.plugin.Feature;
import org.pitest.plugin.FeatureParameter;
import org.pitest.plugin.FeatureParser;
import org.pitest.plugin.FeatureSelector;
import org.pitest.process.ArgLineParser;
import org.pitest.process.JavaAgent;
import org.pitest.process.LaunchOptions;
Expand All @@ -36,6 +39,7 @@
import java.util.Optional;
import java.util.function.Consumer;

import static java.util.Collections.singletonList;
import static org.pitest.util.Verbosity.VERBOSE;
import static org.pitest.util.Verbosity.VERBOSE_NO_SPINNER;

Expand Down Expand Up @@ -155,7 +159,9 @@ private History pickHistoryStore(CodeSource code, ReportOptions data, Optional<W
if (!reader.isPresent() && !historyWriter.isPresent()) {
return new NullHistory();
}
return factory.makeHistory(code, historyWriter.orElse(new NullWriterFactory()), reader);
FeatureParser parser = new FeatureParser();
FeatureSelector select = new FeatureSelector(parser.parseFeatures(data.getFeatures()), singletonList(factory));
return factory.makeHistory(new HistoryParams(select, code), historyWriter.orElse(new NullWriterFactory()), reader);
}

private void checkMatrixMode(ReportOptions data) {
Expand Down
5 changes: 5 additions & 0 deletions pitest/src/main/java/org/pitest/plugin/FeatureSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public Optional<Integer> getInteger(String key) {
return val.map(Integer::parseInt);
}

public Optional<Boolean> getBoolean(String key) {
Optional<String> val = this.getString(key);
return val.map(Boolean::parseBoolean);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public void shouldParseSingleIntegerConfigValues() {
assertThat(actual.getInteger("size")).contains(42);
}

@Test
public void shouldParseSingleBooleanConfigValues() {
final FeatureSetting actual = parse("+BAR(on[true])");
assertThat(actual.getBoolean("on")).contains(true);
}

@Test
public void shouldParseMultipleConfigValues() {
final FeatureSetting actual = parse("+BAR(name[hello]size[42])");
Expand All @@ -90,7 +96,6 @@ public void failsCleanlyWhenBracketsWrongWayRound() {
.hasMessageContaining("Could not parse feature. Parameters should be configured with +feature(param[value], param2[value2])");
}


private FeatureSetting parse(String dsl) {
final List<FeatureSetting> actual = this.testee.parseFeatures(Collections.singletonList(dsl));
return actual.get(0);
Expand Down

0 comments on commit 209b9d1

Please sign in to comment.