Skip to content

Commit

Permalink
Merge pull request #1288 from hcoles/feature/pluggable_coverage
Browse files Browse the repository at this point in the history
Pluggable coverage
  • Loading branch information
hcoles authored Jan 19, 2024
2 parents 960f2c3 + 1ea5e1e commit 3cd8d17
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.pitest.coverage;

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

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class CompoundCoverageExporterFactory implements CoverageExporterFactory {

private final FeatureSelector<CoverageExporterFactory> features;

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

@Override
public CoverageExporter create(ResultOutputStrategy source) {
List<CoverageExporter> exporters = this.features.getActiveFeatures().stream()
.map(f -> f.create(source))
.collect(Collectors.toList());
return c -> exporters.stream().forEach(exporter -> exporter.recordCoverage(c));
}

@Override
public String description() {
throw new UnsupportedOperationException();
}

@Override
public Feature provides() {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.pitest.coverage;

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

public interface CoverageExporterFactory extends ToolClasspathPlugin, ProvidesFeature {
CoverageExporter create(ResultOutputStrategy source);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.pitest.coverage;

import org.pitest.coverage.export.DefaultCoverageExporter;
import org.pitest.plugin.Feature;
import org.pitest.util.ResultOutputStrategy;

public class DefaultCoverageExporterFactory implements CoverageExporterFactory {
@Override
public CoverageExporter create(ResultOutputStrategy source) {
return new DefaultCoverageExporter(source);
}

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

@Override
public String description() {
return "Default coverage exporter";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.pitest.mutationtest.config;

import org.pitest.classpath.CodeSourceFactory;
import org.pitest.coverage.CoverageExporterFactory;
import org.pitest.mutationtest.HistoryFactory;
import org.pitest.mutationtest.build.CoverageTransformerFactory;
import org.pitest.mutationtest.MutationEngineFactory;
Expand Down Expand Up @@ -59,6 +60,7 @@ public Collection<? extends ToolClasspathPlugin> findToolClasspathPlugins() {
l.addAll(findVerifiers());
l.addAll(findCodeSources());
l.addAll(findHistory());
l.addAll(findCoverageExport());
return l;
}

Expand Down Expand Up @@ -133,6 +135,10 @@ public List<HistoryFactory> findHistory() {
return new ArrayList<>(load(HistoryFactory.class));
}


public List<CoverageExporterFactory> findCoverageExport() {
return new ArrayList<>(load(CoverageExporterFactory.class));
}
public Collection<ProvidesFeature> findFeatures() {
return findToolClasspathPlugins().stream()
.filter(p -> p instanceof ProvidesFeature)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import org.pitest.classpath.CodeSourceFactory;
import org.pitest.classpath.DefaultCodeSource;
import org.pitest.classpath.ProjectClassPaths;
import org.pitest.coverage.CompoundCoverageExporterFactory;
import org.pitest.coverage.CoverageExporter;
import org.pitest.mutationtest.HistoryFactory;
import org.pitest.mutationtest.build.CoverageTransformer;
import org.pitest.mutationtest.build.CoverageTransformerFactory;
import org.pitest.coverage.execute.CoverageOptions;
import org.pitest.coverage.export.DefaultCoverageExporter;
import org.pitest.coverage.export.NullCoverageExporter;
import org.pitest.functional.FCollection;
import org.pitest.mutationtest.CompoundMutationResultInterceptor;
Expand Down Expand Up @@ -63,7 +63,9 @@ public ResultOutputStrategy getOutputStrategy() {

public CoverageExporter createCoverageExporter() {
if (this.options.shouldExportLineCoverage()) {
return new DefaultCoverageExporter(getOutputStrategy());
final FeatureParser parser = new FeatureParser();
return new CompoundCoverageExporterFactory(parser.parseFeatures(this.options.getFeatures()), this.plugins.findCoverageExport())
.create(this.options.getReportDirectoryStrategy());
} else {
return new NullCoverageExporter();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.pitest.coverage.DefaultCoverageExporterFactory
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.pitest.mutationtest.config;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
import org.pitest.coverage.CoverageExporter;
import org.pitest.coverage.execute.CoverageOptions;
import org.pitest.coverage.export.DefaultCoverageExporter;
import org.pitest.coverage.export.NullCoverageExporter;
import org.pitest.mutationtest.engine.gregor.config.GregorEngineFactory;
import org.pitest.mutationtest.incremental.DefaultHistoryFactory;
Expand All @@ -12,6 +16,8 @@
import org.pitest.util.PitError;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.Consumer;
Expand All @@ -33,6 +39,9 @@ public class SettingsFactoryTest {

private SettingsFactory testee;

@Rule
public TemporaryFolder reportDir = new TemporaryFolder();

@Before
public void setUp() {
this.testee = new SettingsFactory(this.options, this.plugins);
Expand All @@ -45,6 +54,31 @@ public void shouldReturnANullCoverageExporterWhenOptionSetToFalse() {
assertTrue(this.testee.createCoverageExporter() instanceof NullCoverageExporter);
}

@Test
public void usesDefaultCoverageExporterWhenOptionSetToTrueAndNoOtherExportersPresent() throws IOException {
this.options.setExportLineCoverage(true);
this.options.setShouldCreateTimestampedReports(false);
this.options.setReportDir(reportDir.getRoot().getAbsolutePath());
CoverageExporter actual = this.testee.createCoverageExporter();
actual.recordCoverage(Collections.emptyList());

assertThat(Files.list(reportDir.getRoot().toPath()))
.anyMatch(path -> path.getFileName().toString().equals("linecoverage.xml"));
}

@Test
public void generatesNoCoverageWhenAllExportersDisabled() throws IOException {
this.options.setExportLineCoverage(true);
this.options.setShouldCreateTimestampedReports(false);
this.options.setReportDir(reportDir.getRoot().getAbsolutePath());
this.options.setFeatures(Arrays.asList("-defaultCoverage"));
CoverageExporter actual = this.testee.createCoverageExporter();
actual.recordCoverage(Collections.emptyList());

assertThat(Files.list(reportDir.getRoot().toPath()))
.noneMatch(path -> path.getFileName().toString().equals("linecoverage.xml"));
}

@Test
public void shouldReturnEngineWhenRequestedEngineIsKnown() {
assertTrue(this.testee.createEngine() instanceof GregorEngineFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,5 @@ public void handlesRealExampleArgLine() {
"--add-opens=java.base/java.lang=ALL-UNNAMED",
"--add-opens=java.base/java.math=ALL-UNNAMED");
}

}

0 comments on commit 3cd8d17

Please sign in to comment.