Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add line comments for missed coverage in pit results #334

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
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