Fail build when coverage is too low 89/62089/4
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>
Thu, 23 Aug 2018 12:45:39 +0000 (14:45 +0200)
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>
Mon, 27 Aug 2018 12:53:20 +0000 (14:53 +0200)
Minimum coverage set to 60%. It works by parsing aggregated Jacoco
report as oposed to jacoco:check goal which checks coverage at most
on submodule level.

Change-Id: Ie6f50ce9b2f15e62ad84480611897a98321a7af2
Issue-ID: DCAEGEN2-681
Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
hv-collector-coverage/check-coverage.sh [new file with mode: 0755]
hv-collector-coverage/pom.xml
pom.xml

diff --git a/hv-collector-coverage/check-coverage.sh b/hv-collector-coverage/check-coverage.sh
new file mode 100755 (executable)
index 0000000..7a2f4c6
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+JACOCO_REPORT="$1"
+MIN_COVERAGE_PERCENT="$2"
+
+function coverage_from_report() {
+  local xpath_expr="string(/report/counter[@type='INSTRUCTION']/@$1)"
+  xpath -q -e "$xpath_expr" "$JACOCO_REPORT"
+}
+
+missed=`coverage_from_report missed`
+covered=`coverage_from_report covered`
+total=$(($missed + $covered))
+coverage=$((100 * $covered / $total))
+
+echo "Coverage: $coverage% (covered/total: $covered/$total)"
+
+if [[ $coverage -lt $MIN_COVERAGE_PERCENT ]]; then
+  echo "Coverage is too low. Minimum coverage: $MIN_COVERAGE_PERCENT%"
+  exit 1
+fi
+
index f988f8e..970d4b4 100644 (file)
                     </execution>
                 </executions>
             </plugin>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>exec-maven-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>check-coverage</id>
+                        <phase>verify</phase>
+                        <goals>
+                            <goal>exec</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <configuration>
+                    <executable>${project.basedir}/check-coverage.sh</executable>
+                    <workingDirectory>${project.basedir}</workingDirectory>
+                    <arguments>
+                        <argument>target/site/jacoco-aggregate/jacoco.xml</argument>
+                        <argument>${jacoco.minimum.coverage}</argument>
+                    </arguments>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
 
diff --git a/pom.xml b/pom.xml
index 2781799..86185e6 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -65,6 +65,7 @@
         <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
         <build-helper-maven-plugin.version>1.7</build-helper-maven-plugin.version>
         <jacoco.version>0.8.2</jacoco.version>
+        <jacoco.minimum.coverage>60</jacoco.minimum.coverage>
 
         <!-- Protocol buffers -->
         <protobuf.version>3.5.1</protobuf.version>
                         </dependency>
                     </dependencies>
                 </plugin>
+                <plugin>
+                    <groupId>org.codehaus.mojo</groupId>
+                    <artifactId>exec-maven-plugin</artifactId>
+                    <version>1.6.0</version>
+                </plugin>
             </plugins>
         </pluginManagement>
         <plugins>