Skip to content

Commit

Permalink
support code and coverage modifications when aggregating
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoles committed May 22, 2023
1 parent 06e3892 commit ea7c39f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
package org.pitest.aggregate;

import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.function.Function;
import java.util.function.Predicate;

import org.pitest.classinfo.ClassName;
import org.pitest.classpath.ClassFilter;
import org.pitest.classpath.ClassPath;
import org.pitest.classpath.ClassPathRoot;
import org.pitest.classpath.CodeSource;
import org.pitest.classpath.DefaultCodeSource;
import org.pitest.classpath.DirectoryClassPathRoot;
import org.pitest.classpath.PathFilter;
import org.pitest.classpath.ProjectClassPaths;
import org.pitest.functional.FCollection;
import org.pitest.functional.prelude.Prelude;
import org.pitest.mutationtest.config.DefaultCodePathPredicate;
import org.pitest.mutationtest.config.DefaultDependencyPathPredicate;
import org.pitest.mutationtest.config.SettingsFactory;
import org.pitest.util.Glob;

import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.function.Function;
import java.util.function.Predicate;

class CodeSourceAggregator {

private final Collection<File> compiledCodeDirectories;
private final SettingsFactory settings;

CodeSourceAggregator(final Collection<File> compiledCodeDirectories) {
CodeSourceAggregator(SettingsFactory settings, Collection<File> compiledCodeDirectories) {
this.settings = settings;
this.compiledCodeDirectories = Collections.unmodifiableCollection(compiledCodeDirectories);
}

public CodeSource createCodeSource() {
return new DefaultCodeSource(createProjectClassPaths());
return settings.createCodeSource(createProjectClassPaths());
}

private ProjectClassPaths createProjectClassPaths() {
public ProjectClassPaths createProjectClassPaths() {
final ClassPath classPath = new ClassPath(this.compiledCodeDirectories);
final Predicate<String> classPredicate = createClassPredicate();
final Predicate<ClassPathRoot> pathPredicate = new DefaultCodePathPredicate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import org.pitest.mutationtest.MutationResult;
import org.pitest.mutationtest.MutationResultListener;
import org.pitest.mutationtest.SourceLocator;
import org.pitest.mutationtest.build.CoverageTransformer;
import org.pitest.mutationtest.config.PluginServices;
import org.pitest.mutationtest.config.ReportOptions;
import org.pitest.mutationtest.config.SettingsFactory;
import org.pitest.mutationtest.report.html.MutationHtmlReportListener;
import org.pitest.mutationtest.tooling.SmartSourceLocator;
import org.pitest.util.Log;
Expand All @@ -32,6 +36,8 @@
import static java.util.Arrays.asList;

public final class ReportAggregator {

private final SettingsFactory settings;
private final ResultOutputStrategy resultOutputStrategy;
private final DataLoader<BlockCoverage> blockCoverageLoader;
private final Set<File> mutationFiles;
Expand All @@ -41,13 +47,20 @@ public final class ReportAggregator {
private final Charset inputCharset;
private final Charset outputCharset;

private ReportAggregator(final ResultOutputStrategy resultOutputStrategy, final Set<File> lineCoverageFiles, final Set<File> mutationFiles,
final Set<File> sourceCodeDirs, final Set<File> compiledCodeDirs, Charset inputCharset, Charset outputCharset) {
private ReportAggregator(SettingsFactory settings,
ResultOutputStrategy resultOutputStrategy,
Set<File> lineCoverageFiles,
Set<File> mutationFiles,
Set<File> sourceCodeDirs,
Set<File> compiledCodeDirs,
Charset inputCharset,
Charset outputCharset) {
this.settings = settings;
this.resultOutputStrategy = resultOutputStrategy;
this.blockCoverageLoader = new BlockCoverageDataLoader(lineCoverageFiles);
this.mutationFiles = mutationFiles;
this.sourceCodeDirectories = Collections.unmodifiableCollection(new HashSet<>(sourceCodeDirs));
this.codeSourceAggregator = new CodeSourceAggregator(new HashSet<>(compiledCodeDirs));
this.codeSourceAggregator = new CodeSourceAggregator(settings, new HashSet<>(compiledCodeDirs));
this.inputCharset = inputCharset;
this.outputCharset = outputCharset;
}
Expand Down Expand Up @@ -83,7 +96,6 @@ public AggregationResult aggregateReport() throws ReportAggregationException {
private MutationResultListener createResultListener(SourceLocator sourceLocator, Collection<String> mutatorNames) throws ReportAggregationException {
final CodeSource codeSource = this.codeSourceAggregator.createCodeSource();
final ReportCoverage coverageDatabase = calculateCoverage(codeSource);
//final Collection<String> mutatorNames = new HashSet<>(FCollection.flatMap(mutationMetaData.getMutations(), resultToMutatorName()));

return new MutationHtmlReportListener(outputCharset, coverageDatabase, this.resultOutputStrategy, mutatorNames, sourceLocator);
}
Expand Down Expand Up @@ -112,12 +124,19 @@ private ReportCoverage calculateCoverage(final CodeSource codeSource) throws Rep
.collect(Collectors.toList());
CoverageData cd = new CoverageData(codeSource, new LineMapper(codeSource));
cd.loadBlockDataOnly(coverageData);
return cd;

return transformCoverage(cd);

} catch (final Exception e) {
throw new ReportAggregationException(e.getMessage(), e);
}
}

private ReportCoverage transformCoverage(CoverageData cd) {
CoverageTransformer transformer = settings.createCoverageTransformer(codeSourceAggregator.createCodeSource());
return transformer.transform(cd);
}

public static Builder builder() {
return new Builder();
}
Expand Down Expand Up @@ -247,7 +266,10 @@ public Charset getOutputCharSet() {

public ReportAggregator build() {
validateState();
return new ReportAggregator(this.resultOutputStrategy,
final SettingsFactory settings = new SettingsFactory(new ReportOptions(), PluginServices.makeForContextLoader());
return new ReportAggregator(
settings,
this.resultOutputStrategy,
this.lineCoverageFiles,
this.mutationResultsFiles,
this.sourceCodeDirectories,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.junit.Test;
import org.pitest.classinfo.ClassName;
import org.pitest.classpath.CodeSource;
import org.pitest.mutationtest.config.PluginServices;
import org.pitest.mutationtest.config.ReportOptions;
import org.pitest.mutationtest.config.SettingsFactory;

public class CodeSourceAggregatorTest {

Expand All @@ -23,7 +26,8 @@ public void setup() throws Exception {
.getParentFile() // pitest
.getParentFile() // org
.getParentFile(); // classes
this.underTest = new CodeSourceAggregator(Arrays.asList(testDir, mainDir));
SettingsFactory f = new SettingsFactory(new ReportOptions(), PluginServices.makeForContextLoader());
this.underTest = new CodeSourceAggregator(f, Arrays.asList(testDir, mainDir));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package org.pitest.aggregate;

import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -11,20 +20,12 @@
import static org.pitest.aggregate.TestInvocationHelper.getTestCompiledDirectory;
import static org.pitest.aggregate.TestInvocationHelper.getTestSourceDirectory;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Arrays;

import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ReportAggregatorBuilderTest {

private static final String NOT_A_FILE = "is not a file";
private static final String NOT_A_DIR = "is not a directory";
private static final String IS_NULL = "is null";

@Rule
public ExpectedException expected = ExpectedException.none();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.stream.Collectors;

import static java.util.function.Predicate.isEqual;

import static org.pitest.bytecode.analysis.InstructionMatchers.anyInstruction;
import static org.pitest.bytecode.analysis.InstructionMatchers.isInstruction;
import static org.pitest.bytecode.analysis.InstructionMatchers.notAnInstruction;
Expand Down

0 comments on commit ea7c39f

Please sign in to comment.