-
Notifications
You must be signed in to change notification settings - Fork 70
/
coverage.sh
executable file
·142 lines (127 loc) · 3.32 KB
/
coverage.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env bash
export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]:+${FUNCNAME[0]}():} '
set -e
set -u
set -o pipefail
readonly progname="${0##*/}"
function print-help() {
cat <<EOH
Usage: $progname [OPTIONS] [-- ARGUMENTS]
Checks code coverage and optionally opens the report.
Use Maven for finding coverage. Gradle should be the same.
OPTIONS:
-f, --full-coverage=TYPE
force coverage check to 100%
all - both unit and mutation
mutation - mutation only
unit - unit only
-h, --help print this help and exit
-m, --with-mutation also check mutation coverage
-o, --open-report open coverage reports in a web browser
-x, --no-rerun only open coverage reports, do not rerun build
ARGUMENTS:
All arguments are passed to ./mvnw as options. The "--" is mandatory.
EOH
}
function bad-option() {
cat <<EOM
$progname: invalid option -- '$1'
Try '$progname --help' for more information.
EOM
}
function bad-option-argument() {
case $# in
1) cat <<EOM ;;
$progname: option '$1' requires an argument
Try '$progname --help' for more information.
EOM
2) cat <<EOM ;;
$progname: invalid "$2" for -$2 option
Try '$progname --help' for more information.
EOM
esac
}
function require-file() {
local file="$1"
shift
if [[ ! -f "$file" ]]; then
echo "$progname: $file: No such file or directory" >&2
return 1
fi
}
full=none
mutation=false
open=false
run=true
# shellcheck disable=SC2214
while getopts :f:hmox-: opt; do
[[ $opt == - ]] && opt=${OPTARG%%=*} OPTARG=${OPTARG#*=}
# shellcheck disable=SC2213
case $opt in
f | full-coverage)
case "$OPTARG" in
unit) full="$OPTARG" ;;
all | mutation) mutation=true full="$OPTARG" ;;
*)
bad-option-argument "-f" "$OPTARG"
exit 2
;;
esac
;;
h | help)
print-help
exit 0
;;
m | with-mutation) mutation=true ;;
o | open-report) open=true ;;
x | no-rerun) open=true run=false ;;
\?)
bad-option "$OPTARG"
exit 2
;;
:)
bad-option-argument "$OPTARG"
exit 2
;;
esac
done
shift $((OPTIND - 1))
case "$full" in
none) flags='' ;;
all)
flags='-Dcoverage.lines=100 -Dcoverage.branches=100 -Dcoverage.instructions=100 -Dcoverage.mutation=100'
;;
mutation)
flags='-Dcoverage.mutation=100'
;;
unit)
flags='-Dcoverage.lines=100 -Dcoverage.branches=100 -Dcoverage.instructions=100'
;;
esac
((rc = 0)) || true
if $run; then
if $mutation; then
targets='-DwithHistory clean verify'
else
targets='clean test jacoco:report jacoco:check'
fi
# shellcheck disable=SC2086
./mvnw $flags "$@" $targets || ((rc += $?)) || true
else
# Refresh coverage report when editing limits in pom.xml without rebuild
./mvnw $flags "$@" jacoco:report jacoco:check || ((rc += $?)) || true
fi
if $open; then
if require-file target/site/jacoco/index.html; then
(cd target/site/jacoco && open index.html)
else
((++rc))
fi
if $mutation && require-file target/pit-reports/index.html; then
(cd target/pit-reports && open index.html)
else
((++rc))
fi
fi
# shellcheck disable=SC2086
exit $rc