Prepare release 1.3.1
[dcaegen2/collectors/hv-ves.git] / build / hv-collector-coverage / check-coverage.sh
1 #!/usr/bin/env bash
2 set -euo pipefail
3
4 JACOCO_REPORT="$1"
5 MIN_COVERAGE_PERCENT="$2"
6 ROOT_SOURCES_MODULE_POM="$3"
7 LOG_FILE=target/check-coverage.log
8
9 function coverage_from_report() {
10   local xpath_expr="string(/report/counter[@type='INSTRUCTION']/@$1)"
11   xmllint --xpath "${xpath_expr}" "$JACOCO_REPORT" 2>> ${LOG_FILE}
12 }
13
14 function 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}))
18
19   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 }
26
27 function 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))
32
33   if [[ $(wc -c < ${LOG_FILE}) > 0 ]]; then
34     echo "Warnings from xpath evaluation:"
35     cat ${LOG_FILE}
36     echo
37   fi
38
39   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
47 check_preconditions || exit 1
48 check_coverage || exit 2