Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Diff-Coverage Maven plugin

## 1.2.0

### Added
- `entityCountThreshold` parameter in `violations` configuration: if the number of changed entities is below the threshold, the coverage check for that entity is skipped

## 1.1.0

- Added Java 25 class files support
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,24 @@ The plugin does the next steps:
</diffSource>

<!-- Optional -->
<violations>
<violations>
<!-- Default 'false'. Fail build if violation rules weren't met -->
<failOnViolation>true</failOnViolation>

<!-- Sets min coverage rule for: instructions, lines, branches -->
<minCoverage>0.7</minCoverage>

<!-- Each rule could be configured separately -->
<!-- Default '0.0'. If value is '0.0' then the rule is disabled -->
<minLines>0.1</minLines>
<minBranches>0.7</minBranches>
<minInstructions>1.0</minInstructions>

<!-- Optional. Default '0' (disabled).
If the number of changed entities is below this threshold,
coverage violation checks are skipped.
Useful to avoid failures on small PRs with few changed lines. -->
<entityCountThreshold>20</entityCountThreshold>
</violations>

<!-- Optional. Exec files include pattern. By default 'build/jacoco.exec' file is used -->
Expand Down
14 changes: 14 additions & 0 deletions src/it/violation-rule-threshold-check/diffFile.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
===================================================================
--- a/src/main/java/com/test/Example.java (revision 9e78667391946d3461759f5589b67d3a676d8ce7)
+++ b/src/main/java/com/test/Example.java (date 1593433398791)
@@ -5,7 +5,10 @@
public void sayHello(boolean a) {
if (a) {
System.out.println("if");
+ } else {
+ System.out.println("else");
}
+ System.out.println("return");
}

}
71 changes: 71 additions & 0 deletions src/it/violation-rule-threshold-check/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>jacoco</groupId>
<artifactId>it-threshold-check-passes</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.14</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.github.surpsg</groupId>
<artifactId>diff-coverage-maven-plugin</artifactId>

<configuration>
<diffSource>
<file>diffFile.patch</file>
</diffSource>

<violations>
<failOnViolation>true</failOnViolation>
<minCoverage>1.0</minCoverage>
<entityCountThreshold>20</entityCountThreshold>
</violations>

<reports>
<report>
<type>console</type>
<enabled>false</enabled>
</report>
</reports>
</configuration>

<executions>
<execution>
<goals>
<goal>diffCoverage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.test;

public class Example {

public void sayHello(boolean a) {
if (a) {
System.out.println("if");
} else {
System.out.println("else");
}
System.out.println("return");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.test;
import org.junit.Test;

public class ExampleTest {

@Test
public void test() {
new Example().sayHello(true);
}

}
9 changes: 9 additions & 0 deletions src/it/violation-rule-threshold-check/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import java.io.*;
import org.codehaus.plexus.util.*;

String expectedIgnoredMsg = "was ignored because threshold=20";

String buildLog = FileUtils.fileRead( new File( basedir, "build.log" ) );
if ( !buildLog.contains( expectedIgnoredMsg ) ) {
throw new RuntimeException( "Expected coverage violations to be ignored due to entityCountThreshold, but message not found: " + expectedIgnoredMsg );
}
9 changes: 7 additions & 2 deletions src/main/kotlin/io/github/surpsg/DiffCoverageConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ class ViolationsConfiguration(
var minBranches: Double = 0.0,
var minInstructions: Double = 0.0,

var minCoverage: Double = MIN_COVERAGE_PROPERTY_DEFAULT
)
var minCoverage: Double = MIN_COVERAGE_PROPERTY_DEFAULT,

var entityCountThreshold: Int = 0
) {
val entityCountThresholdOrNull: Int?
get() = entityCountThreshold.takeIf { it > 0 }
}

class Report(
var type: String? = null,
Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/io/github/surpsg/DiffCoverageMojo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ class DiffCoverageMojo : AbstractMojo() {
failOnViolation = violations.failOnViolation
violationRules += if (isMinCoverageSet) {
CoverageEntity.entries.map { entity ->
ViolationRule { coverageEntity = entity; minCoverageRatio = violations.minCoverage }
ViolationRule {
coverageEntity = entity
minCoverageRatio = violations.minCoverage
entityCountThreshold = violations.entityCountThresholdOrNull
}
}
} else {
buildCoverageRules()
Expand All @@ -139,7 +143,11 @@ class DiffCoverageMojo : AbstractMojo() {
)
.filter { it.second > 0.0 }
.map { (entity, value) ->
ViolationRule { coverageEntity = entity; minCoverageRatio = value }
ViolationRule {
coverageEntity = entity
minCoverageRatio = value
entityCountThreshold = violations.entityCountThresholdOrNull
}
}
.toSet()

Expand Down
Loading