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
#!/bin/bash
# https://doc.rust-lang.org/rustc/instrument-coverage.html
readonly RUSTUP="$HOME/.rustup"
readonly NAME="fqs"
# ----------
# Functions.
function setup() {
rustup component add llvm-tools-preview
LLVMCOV=$(find "${RUSTUP}" -name "llvm-cov")
[ -f "${LLVMCOV}" ] || { \
echo "Could not find llvm-cov"; exit 1; }
LLVMPROF=$(find "${RUSTUP}" -name "llvm-profdata")
[ -f "${LLVMPROF}" ] || { \
echo "Could not find llvm-profdata"; exit 1; }
}
function clean() {
### Remove old files if any.
rm -f "${NAME}".profdata
rm -f default_*
}
function run() {
### Run tests with instrumentation for coverage.
RUSTFLAGS="-C instrument-coverage" cargo test --tests
}
function merge() {
### Merge raw files.
"${LLVMPROF}" \
merge \
-sparse \
default_*.profraw \
-o "${NAME}".profdata
}
function report_and_show() {
time "${LLVMCOV}" report \
--use-color --ignore-filename-regex='/.cargo/registry' \
--instr-profile="${NAME}".profdata \
$( \
for file in \
$( \
RUSTFLAGS="-C instrument-coverage" \
cargo test --tests --no-run --message-format=json \
| jq -r "select(.profile.test == true) | .filenames[]" \
| grep -v dSYM - \
); \
do \
printf "%s %s " -object $file; \
done \
)
time "${LLVMCOV}" show \
--use-color --ignore-filename-regex='/.cargo/registry' \
--instr-profile="${NAME}".profdata \
$( \
for file in \
$( \
RUSTFLAGS="-C instrument-coverage" \
cargo test --tests --no-run --message-format=json \
| jq -r "select(.profile.test == true) | .filenames[]" \
| grep -v dSYM - \
); \
do \
printf "%s %s " -object $file; \
done \
) \
--show-instantiations --show-line-counts-or-regions
}
function report() {
### Report coverage summary.
time "${LLVMCOV}" report \
$( \
for file in \
$( \
RUSTFLAGS="-C instrument-coverage" \
cargo test --tests --no-run --message-format=json \
| jq -r "select(.profile.test == true) | .filenames[]" \
| grep -v dSYM - \
); \
do \
printf "%s %s " -object $file; \
done \
) \
--instr-profile="${NAME}".profdata --summary-only
}
function cov() {
setup
clean
run
merge
report
#_report_and_show
}
cov