Skip to content

Commit

Permalink
Feat: keep descartes intermediate reports (#912)
Browse files Browse the repository at this point in the history
* feat: keeping test criterion report

* feat: implement a way to have all the reports from descartes / gregor

* refactor: update API to match the new one
  • Loading branch information
danglotb authored Oct 20, 2019
1 parent c2fa903 commit b8a43ea
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import spoon.reflect.declaration.CtType;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* Created by Benjamin DANGLOT
Expand Down Expand Up @@ -161,7 +158,7 @@ public TestSelectorElementReport report() {
);
final TestClassJSON testClassJSON = this.reportJson();
this.reset();
return new TestSelectorElementReportImpl(output.toString(), testClassJSON);
return new TestSelectorElementReportImpl(output.toString(), testClassJSON, Collections.emptyList(), "");
}

private TestClassJSON reportJson() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -210,7 +211,7 @@ public TestSelectorElementReport report() {
(double) coverageResults.getInstructionsTotal())))
.append("%")
.append(AmplificationHelper.LINE_SEPARATOR);
lastReport = new TestSelectorElementReportImpl(report.toString(), jsonReport(coverageResults));
lastReport = new TestSelectorElementReportImpl(report.toString(), jsonReport(coverageResults), Collections.emptyList(), "");

return lastReport;
} catch (TimeoutException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ public enum OutputFormat {XML, CSV}

private String absolutePathToProjectRoot;

private List<String> mutationsScoreResults;

public PitMutantScoreSelector(AutomaticBuilder automaticBuilder,
InputConfiguration configuration) {
super(automaticBuilder, configuration);
this.mutationsScoreResults = new ArrayList<>();
this.absolutePathToProjectRoot = configuration.getAbsolutePathToProjectRoot();
this.shouldTargetOneTestClass = configuration.shouldTargetOneTestClass();
this.testClassTargetOne =
configuration.getTestClasses() == null || configuration.getTestClasses().isEmpty() ? null :
configuration.getFactory().Class().get(configuration.getTestClasses().get(0));
configuration.getFactory().Class().get(configuration.getTestClasses().get(0));
this.testThatKilledMutants = new HashMap<>();
this.parser = new PitXMLResultParser();
final String pathPitResult = configuration.getPathPitResult();
Expand All @@ -92,6 +95,7 @@ public PitMutantScoreSelector(AutomaticBuilder automaticBuilder,
parser = originalResultParser = new PitXMLResultParser();
break;
}
this.mutationsScoreResults.add(this.readFileContentAsString(pathPitResult));
initOriginalPitResult(originalResultParser.parse(new File(pathPitResult)));
}

Expand All @@ -109,11 +113,16 @@ public boolean init() {
return false;
}
}
initOriginalPitResult(parser.parseAndDelete( this.absolutePathToProjectRoot + this.automaticBuilder.getOutputDirectoryPit()));
this.mutationsScoreResults.add(
this.readFileContentAsString(
this.parser.getPathOfMutationsFile(this.absolutePathToProjectRoot + this.automaticBuilder.getOutputDirectoryPit()).getAbsolutePath()
)
);
initOriginalPitResult(parser.parseAndDelete(this.absolutePathToProjectRoot + this.automaticBuilder.getOutputDirectoryPit()));
} else {
baselineKilledMutants = new ArrayList<>();
for(AbstractPitResult r : originalKilledMutants) {
baselineKilledMutants.add(r.clone());
this.baselineKilledMutants = new ArrayList<>();
for (AbstractPitResult r : this.originalKilledMutants) {
this.baselineKilledMutants.add(r.clone());
}
}
return true;
Expand All @@ -131,7 +140,7 @@ private void initOriginalPitResult(List<AbstractPitResult> results) {
.collect(Collectors.toList());
LOGGER.info("The original test suite kill {} / {}", this.originalKilledMutants.size(), results.size());
this.baselineKilledMutants = new ArrayList<>();
for(AbstractPitResult r : this.originalKilledMutants) {
for (AbstractPitResult r : this.originalKilledMutants) {
this.baselineKilledMutants.add(r.clone());
}
}
Expand Down Expand Up @@ -176,6 +185,9 @@ public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) {
new File(this.absolutePathToProjectRoot + "/" + this.pathToTestClasses)
);
this.automaticBuilder.runPit(clone);
this.mutationsScoreResults.add(this.readFileContentAsString(
parser.getPathOfMutationsFile(this.absolutePathToProjectRoot + automaticBuilder.getOutputDirectoryPit()).getAbsolutePath())
);
final List<AbstractPitResult> results = parser.parseAndDelete(this.absolutePathToProjectRoot + automaticBuilder.getOutputDirectoryPit());
Set<CtMethod<?>> selectedTests = new HashSet<>();
if (results != null) {
Expand Down Expand Up @@ -219,6 +231,14 @@ public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) {
return new ArrayList<>(selectedTests);
}

private String readFileContentAsString(final String pathname) {
try (BufferedReader buffer = new BufferedReader(new FileReader(pathname))) {
return buffer.lines().collect(Collectors.joining(AmplificationHelper.LINE_SEPARATOR));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private boolean killsNewMutant(AbstractPitResult result) {
if (baselineKilledMutants.contains(result)) {
return false;
Expand All @@ -231,7 +251,7 @@ private boolean killsNewMutant(AbstractPitResult result) {

@Override
public TestSelectorElementReport report() {
if(currentClassTestToBeAmplified == null) {
if (currentClassTestToBeAmplified == null) {
return lastReport;
}
final String reportStdout = reportStdout();
Expand All @@ -241,7 +261,13 @@ public TestSelectorElementReport report() {
this.currentClassTestToBeAmplified = null;
this.testThatKilledMutants.clear();
this.selectedAmplifiedTest.clear();
lastReport = new TestSelectorElementReportImpl(reportStdout, testClassJSON);

this.lastReport = new TestSelectorElementReportImpl(
reportStdout,
testClassJSON,
this.mutationsScoreResults,
this.parser instanceof PitXMLResultParser ? ".xml" : ".csv"
);
return lastReport;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import spoon.reflect.declaration.CtType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -50,7 +51,7 @@ public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) {
public TestSelectorElementReport report() {
final String report = "Amplification results with " + this.selectedAmplifiedTest.size() + " new tests.";
reset();
return new TestSelectorElementReportImpl(report, null);
return new TestSelectorElementReportImpl(report, null, Collections.emptyList(), "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract public class AbstractParser<T extends AbstractPitResult> {
this.PATH_TO_MUTATIONS_RESULT = PATH_TO_MUTATIONS_RESULT;
}

protected File getPathOfMutationsFile(String pathToDirectoryResults) {
public File getPathOfMutationsFile(String pathToDirectoryResults) {
if (!new File(pathToDirectoryResults).exists()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
* created by Benjamin DANGLOT
Expand All @@ -22,9 +23,18 @@ public class TestSelectorElementReportImpl implements TestSelectorElementReport

private TestClassJSON testClassJSON;

public TestSelectorElementReportImpl(String textualReport, TestClassJSON testClassJSON) {
private List<String> testCriterionReports;

private String extension;

public TestSelectorElementReportImpl(String textualReport,
TestClassJSON testClassJSON,
List<String> testCriterionReports,
String extension) {
this.textualReport = textualReport;
this.testClassJSON = testClassJSON;
this.testCriterionReports = testCriterionReports;
this.extension = extension;
}

@Override
Expand All @@ -44,7 +54,20 @@ public String output(CtType<?> testClass, String outputDirectory) {
} catch (IOException e) {
throw new RuntimeException(e);
}
// 2 return the textual report for this test classTestClassJSON
// 2 output the baseline, intermediate and final test criterion reports
final String reportPathName = DSpotUtils.shouldAddSeparator
.apply(outputDirectory) + testClass.getQualifiedName().replaceAll("\\.", "_")
+ "_test_criterion_report_";
testCriterionReports.forEach(testCriterionReportContent -> {
try (FileWriter writer =
new FileWriter(reportPathName + testCriterionReports.indexOf(testCriterionReportContent) + this.extension, false)) {
writer.write(testCriterionReportContent);
} catch (Exception e) {
//ignored
}
}
);
// 3 return the textual report for this test classTestClassJSON
return this.textualReport;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand All @@ -21,7 +20,7 @@ public class TestSelectorReportImpl implements TestSelectorReport {

private static final Logger LOGGER = LoggerFactory.getLogger(TestSelectorReport.class);

private Map<CtType<?>, TestSelectorElementReportImpl> testSelectorElementReportPerTestClass;
private Map<CtType<?>, TestSelectorElementReport> testSelectorElementReportPerTestClass;

public TestSelectorReportImpl() {
this.testSelectorElementReportPerTestClass = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package eu.stamp_project.utils.pit;

import eu.stamp_project.utils.AmplificationHelper;
import eu.stamp_project.utils.report.output.selector.TestSelectorElementReportImpl;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

import static org.junit.Assert.*;

Expand All @@ -27,4 +33,5 @@ public void test() throws Exception {
assertEquals(3343, pitCSVResults.stream().filter(pitResult -> pitResult.getStateOfMutant() == PitCSVResult.State.KILLED).count(), nbErrors);
assertEquals(1014, pitCSVResults.stream().filter(pitResult -> pitResult.getStateOfMutant() == PitCSVResult.State.NO_COVERAGE).count(), nbErrors);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package eu.stamp_project.utils.pit;

import eu.stamp_project.utils.AmplificationHelper;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;

/**
Expand All @@ -11,6 +18,7 @@
* on 14/11/18
*/
public class PitXMLParserAndResultTest {

@Test
public void test() throws Exception {
final List<? extends AbstractPitResult> pitXMLResults = (new PitXMLResultParser()).parse(new File("src/test/resources/mutations_test-projects.xml"));
Expand Down

0 comments on commit b8a43ea

Please sign in to comment.