Skip to content

Commit

Permalink
Merge pull request #334 from uhafner/pit-line-coverage
Browse files Browse the repository at this point in the history
Add line comments for missed coverage in pit results
  • Loading branch information
uhafner authored Apr 1, 2024
2 parents 6bae815 + 413fac7 commit f62f8f5
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 261 deletions.
217 changes: 0 additions & 217 deletions etc/configuration.json

This file was deleted.

27 changes: 0 additions & 27 deletions etc/example.json

This file was deleted.

5 changes: 5 additions & 0 deletions src/main/java/edu/hm/hafner/grading/CommentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public enum CommentType {

private final List<String> prefixes;

CommentBuilder() {
this(new String[0]);
}

protected CommentBuilder(final String... prefixesToRemove) {
prefixes = Arrays.asList(prefixesToRemove);
}
Expand Down Expand Up @@ -231,6 +235,7 @@ private void createAnnotationsForSurvivedMutations(final FileNode file,
final Set<String> sourcePaths) {
file.getSurvivedMutationsPerLine().entrySet()
.forEach(entry -> createAnnotationForSurvivedMutation(file, entry, sourcePaths));

Check warning on line 237 in src/main/java/edu/hm/hafner/grading/CommentBuilder.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 237 (VoidMethodCallMutator)
Raw output
Survived mutations:
- removed call to java/util/Set::forEach (org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator)
createAnnotationsForMissedLines(file, sourcePaths);
}

private void createAnnotationForSurvivedMutation(final FileNode file,
Expand Down
34 changes: 17 additions & 17 deletions src/test/java/edu/hm/hafner/grading/AggregatedScoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ protected void assertThatRestoredInstanceEqualsOriginalInstance(
void shouldGradeCoverageReport() {
var aggregation = new AggregatedScore(COVERAGE_CONFIGURATION, new FilteredLog("Test"));

aggregation.gradeCoverage((tool, log) -> readReport(tool));
aggregation.gradeCoverage((tool, log) -> readCoverageReport("jacoco.xml", tool, CoverageParserType.JACOCO));

var coveredFiles = new String[] {"ReportFactory.java",
"ReportFinder.java",
Expand All @@ -391,21 +391,6 @@ void shouldGradeCoverageReport() {
assertThat(aggregation.getIssues()).isEmpty();
}

private Node readReport(final ToolConfiguration tool) {
var parser = new ParserRegistry().get(CoverageParserType.JACOCO, ProcessingMode.FAIL_FAST);
var fileName = "jacoco.xml";
try (InputStream stream = createStream(fileName);
Reader reader = new InputStreamReader(Objects.requireNonNull(stream), StandardCharsets.UTF_8)) {
var root = parser.parse(reader, fileName, new FilteredLog("Test"));
var containerNode = new ModuleNode(tool.getDisplayName());
containerNode.addChild(root);
return containerNode;
}
catch (IOException e) {
throw new AssertionError(e);
}
}

@Test
void shouldGradeAnalysisReport() {
var aggregation = new AggregatedScore(ANALYSIS_CONFIGURATION, new FilteredLog("Test"));
Expand All @@ -431,6 +416,21 @@ void shouldGradeAnalysisReport() {
"edu/hm/hafner/analysis/IssuesTest.java");
}

static Node readCoverageReport(final String fileName, final ToolConfiguration tool,
final CoverageParserType type) {
var parser = new ParserRegistry().get(type, ProcessingMode.FAIL_FAST);
try (InputStream stream = createStream(fileName);
Reader reader = new InputStreamReader(Objects.requireNonNull(stream), StandardCharsets.UTF_8)) {
var root = parser.parse(reader, fileName, new FilteredLog("Test"));
var containerNode = new ModuleNode(tool.getDisplayName());
containerNode.addChild(root);
return containerNode;
}
catch (IOException e) {
throw new AssertionError(e);
}
}

private Report readAnalysisReport(final ToolConfiguration tool) {
try {
var registry = new edu.hm.hafner.analysis.registry.ParserRegistry();
Expand All @@ -450,7 +450,7 @@ private Path createPath(final String fileName) throws URISyntaxException {

@MustBeClosed
@SuppressFBWarnings("OBL")
private InputStream createStream(final String fileName) {
private static InputStream createStream(final String fileName) {
return Objects.requireNonNull(CoverageScoreTest.class.getResourceAsStream(fileName),
"File not found: " + fileName);
}
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/edu/hm/hafner/grading/CommentBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package edu.hm.hafner.grading;

import org.junit.jupiter.api.Test;

import edu.hm.hafner.coverage.registry.ParserRegistry.CoverageParserType;
import edu.hm.hafner.util.FilteredLog;

import static org.mockito.Mockito.*;

class CommentBuilderTest {
private static final String COVERAGE_CONFIGURATION = """
{
"coverage": [
{
"tools": [
{
"id": "pit",
"name": "Mutation Coverage",
"metric": "mutation",
"pattern": "**/src/**/mutations-dashboard.xml"
}
],
"name": "PIT",
"maxScore": 100,
"coveredPercentageImpact": 1,
"missedPercentageImpact": -1
}
]
}
""";

@Test
void shouldRunComments() {
var aggregation = new AggregatedScore(COVERAGE_CONFIGURATION, new FilteredLog("Test"));
aggregation.gradeCoverage((tool, log) -> AggregatedScoreTest.readCoverageReport(
"mutations-dashboard.xml", tool, CoverageParserType.PIT));

CommentBuilder builder = spy(CommentBuilder.class);

builder.createAnnotations(aggregation);

verify(builder, times(7))
.createComment(any(), anyString(), anyInt(), anyInt(), anyString(), anyString(),
anyInt(), anyInt(), anyString(), anyString());
}
}
Loading

0 comments on commit f62f8f5

Please sign in to comment.