Piotr Jaszczyk | 851d470 | 2018-08-23 14:45:39 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | JACOCO_REPORT="$1" |
| 5 | MIN_COVERAGE_PERCENT="$2" |
Piotr Jaszczyk | a38f0f1 | 2019-03-11 14:16:39 +0100 | [diff] [blame] | 6 | ROOT_SOURCES_MODULE_POM="$3" |
Piotr Jaszczyk | b20f963 | 2018-09-06 08:20:03 +0200 | [diff] [blame] | 7 | LOG_FILE=target/check-coverage.log |
Piotr Jaszczyk | 851d470 | 2018-08-23 14:45:39 +0200 | [diff] [blame] | 8 | |
| 9 | function coverage_from_report() { |
| 10 | local xpath_expr="string(/report/counter[@type='INSTRUCTION']/@$1)" |
Izabela Zawadzka | 0c8e1f8 | 2019-07-25 17:36:42 +0200 | [diff] [blame] | 11 | xmllint --xpath "${xpath_expr}" "$JACOCO_REPORT" 2>> ${LOG_FILE} |
Piotr Jaszczyk | 851d470 | 2018-08-23 14:45:39 +0200 | [diff] [blame] | 12 | } |
| 13 | |
Piotr Jaszczyk | a38f0f1 | 2019-03-11 14:16:39 +0100 | [diff] [blame] | 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})) |
Piotr Jaszczyk | 851d470 | 2018-08-23 14:45:39 +0200 | [diff] [blame] | 18 | |
Piotr Jaszczyk | a38f0f1 | 2019-03-11 14:16:39 +0100 | [diff] [blame] | 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 | } |
Piotr Jaszczyk | b20f963 | 2018-09-06 08:20:03 +0200 | [diff] [blame] | 26 | |
Piotr Jaszczyk | a38f0f1 | 2019-03-11 14:16:39 +0100 | [diff] [blame] | 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)) |
Piotr Jaszczyk | 851d470 | 2018-08-23 14:45:39 +0200 | [diff] [blame] | 32 | |
Piotr Jaszczyk | a38f0f1 | 2019-03-11 14:16:39 +0100 | [diff] [blame] | 33 | if [[ $(wc -c < ${LOG_FILE}) > 0 ]]; then |
| 34 | echo "Warnings from xpath evaluation:" |
| 35 | cat ${LOG_FILE} |
| 36 | echo |
| 37 | fi |
Piotr Jaszczyk | 851d470 | 2018-08-23 14:45:39 +0200 | [diff] [blame] | 38 | |
Piotr Jaszczyk | a38f0f1 | 2019-03-11 14:16:39 +0100 | [diff] [blame] | 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 |