blob: 192eac70882bb13a981ca32c6b95d54159cc56df [file] [log] [blame]
Piotr Jaszczyk851d4702018-08-23 14:45:39 +02001#!/usr/bin/env bash
2set -euo pipefail
3
4JACOCO_REPORT="$1"
5MIN_COVERAGE_PERCENT="$2"
Piotr Jaszczyka38f0f12019-03-11 14:16:39 +01006ROOT_SOURCES_MODULE_POM="$3"
Piotr Jaszczykb20f9632018-09-06 08:20:03 +02007LOG_FILE=target/check-coverage.log
Piotr Jaszczyk851d4702018-08-23 14:45:39 +02008
9function coverage_from_report() {
10 local xpath_expr="string(/report/counter[@type='INSTRUCTION']/@$1)"
Izabela Zawadzka0c8e1f82019-07-25 17:36:42 +020011 xmllint --xpath "${xpath_expr}" "$JACOCO_REPORT" 2>> ${LOG_FILE}
Piotr Jaszczyk851d4702018-08-23 14:45:39 +020012}
13
Piotr Jaszczyka38f0f12019-03-11 14:16:39 +010014function check_preconditions() {
15 local num_deps=$(grep -c 'project\.parent\.groupId' pom.xml)
16 local num_submodules=$(grep -c '<module>' ${ROOT_SOURCES_MODULE_POM})
17 local difference=$((${num_submodules}-${num_deps}))
Piotr Jaszczyk851d4702018-08-23 14:45:39 +020018
Piotr Jaszczyka38f0f12019-03-11 14:16:39 +010019 if [[ ${difference} -ne 0 ]]; then
20 echo "Not all modules are included in the coverage report."
21 echo "Verify if all submodules of hv-collector-sources module are included as a dependency to hv-collector-coverage module."
22 echo "Number of missing modules: ${difference}"
23 exit 1
24 fi
25}
Piotr Jaszczykb20f9632018-09-06 08:20:03 +020026
Piotr Jaszczyka38f0f12019-03-11 14:16:39 +010027function check_coverage() {
28 local missed=$(coverage_from_report missed)
29 local covered=$(coverage_from_report covered)
30 local total=$(($missed + $covered))
31 local coverage=$((100 * $covered / $total))
Piotr Jaszczyk851d4702018-08-23 14:45:39 +020032
Piotr Jaszczyka38f0f12019-03-11 14:16:39 +010033 if [[ $(wc -c < ${LOG_FILE}) > 0 ]]; then
34 echo "Warnings from xpath evaluation:"
35 cat ${LOG_FILE}
36 echo
37 fi
Piotr Jaszczyk851d4702018-08-23 14:45:39 +020038
Piotr Jaszczyka38f0f12019-03-11 14:16:39 +010039 echo "Coverage: $coverage% (covered/total: $covered/$total)"
40
41 if [[ ${coverage} -lt ${MIN_COVERAGE_PERCENT} ]]; then
42 echo "Coverage is too low. Minimum coverage: $MIN_COVERAGE_PERCENT%"
43 exit 1
44 fi
45}
46
47check_preconditions || exit 1
48check_coverage || exit 2