-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcoverage.sh
executable file
·119 lines (88 loc) · 2.97 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
#!/bin/bash
# coverage.sh
echo_err () {
echo "$@" 1>&2
}
usage () {
echo_err 'coverage.sh'
echo_err ''
echo_err 'Generate code coverage reports for the repository.'
echo_err 'This generates target/debug/coverage/lcov.info (for editors) and target/debug/coverage/index.html.'
}
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
usage
exit 0
fi
if ! command -v rustup &> /dev/null
then
echo_err "rustup is not found!"
exit 1
fi
if ! command -v cargo &> /dev/null
then
echo_err "cargo is not found!"
exit 1
fi
if ! command -v jq &> /dev/null
then
echo_err "jq is not found!"
exit 1
fi
# Setup - enter rust project
cargo locate-project &>/dev/null || { echo_err "Cannot find a rust project"; usage; exit 1; }
PROJECT_ROOT=$(dirname $(cargo locate-project | jq -r .root 2> /dev/null))
TARGET_DIR=$(cargo metadata 2> /dev/null | jq -r .target_directory 2>/dev/null)
cd "$PROJECT_ROOT"
export RUSTUP_TOOLCHAIN="nightly" # fixes https://github.com/mozilla/grcov/issues/677
# Install required tools
echo_err "info: installing nightly rust"
rustup install nightly
echo_err "info: installing llvm-tools-preview"
rustup component add llvm-tools-preview
echo_err "info: installing grcov"
rustup run nightly cargo install grcov
# Uses rust's source code instrument-based coverage, processed by grcov.
# See:
# https://doc.rust-lang.org/rustc/instrument-coverage.html
# https://github.com/mozilla/grcov
rm -rf target/debug/coverage
export CARGO_INCREMENTAL=0
export RUSTFLAGS="$RUSTFLAGS -Z unstable-options -Cinstrument-coverage -Zlinker-features=-lld"
export RUSTDOCFLAGS="$RUSTDOCFLAGS -C instrument-coverage -Z unstable-options --persist-doctests target/debug/doctestbins -Zlinker-features=-lld"
export LLVM_PROFILE_FILE='conjure-oxide-%p-%m.profraw'
# regex patterns to ignore
GRCOV_EXCLUDE_LINES=(
'consider covered'
'bug!'
'#\[register_rule'
'register_rule_set!'
)
# construct an or regex
GRCOV_EXCLUDE_FLAG="--excl-line=$(echo ${GRCOV_EXCLUDE_LINES[@]} | tr ' ' '|'})"
GRCOV_IGNORE_FLAGS=(
'--ignore-not-existing'
'--ignore'
"${HOME}"'/.cargo/**/*.rs'
'--ignore'
'target/**/*.rs'
'--ignore'
'**/examples/*.rs'
'--ignore'
'**/build.rs'
'--ignore'
'conjure_oxide/tests/generated_tests.rs'
)
echo_err "info: building with nightly"
cargo +nightly build --workspace
echo_err "info: running tests"
cargo +nightly test --workspace
echo_err "info: generating coverage reports"
grcov . -s . --binary-path ./target/debug -t html\
"${GRCOV_IGNORE_FLAGS[@]}" ${GRCOV_EXCLUDE_FLAG}\
-o ./target/debug/coverage || { echo_err "fatal: html coverage generation failed" ; exit 1; }
echo_err "info: html coverage report generated to target/debug/coverage/index.html"
grcov . -s . --binary-path ./target/debug -t lcov\
"${GRCOV_IGNORE_FLAGS[@]}" ${GRCOV_EXCLUDE_FLAG}\
-o ./target/debug/lcov.info || { echo_err "fatal: lcov coverage generation failed" ; exit 1; }
echo_err "info: lcov coverage report generated to target/debug/lcov.info"
rm -rf **/*.profraw *.profraw