Repository navigation
Expand file tree
/
Copy pathcoverage.sh
More file actions
executable file
·168 lines (152 loc) · 4.56 KB
/
Copy pathcoverage.sh
File metadata and controls
executable file
·168 lines (152 loc) · 4.56 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env bash
set -eu -o pipefail
# set -x
export PS4='+ [${BASH_SOURCE[0]##*/}:${LINENO}${FUNCNAME[0]:+:${FUNCNAME[0]}}] '
export COVERAGE_FUNCTIONS=${COVERAGE_FUNCTIONS:-70}
export COVERAGE_LINES=${COVERAGE_LINES:-71}
export COVERAGE_REGIONS=${COVERAGE_REGIONS:-72}
usage() {
cat <<EOF
Usage: $0 [-h | --help] [<command>]
Commands:
check [TESTNAME] Run 'cargo llvm-cov test' and verify that code coverage statistics
are greater than or equal to the thresholds. Test name filtering
is supported.
help Show this help and exit.
html Generate coverage report in HTML format and open it in the default
browser.
show-env Show the coverage thresholds.
Omitted <command> defaults to 'check'.
EOF
}
_cov_test() {
cargo llvm-cov --version >/dev/null || cargo install cargo-llvm-cov
cargo nextest --version >/dev/null || cargo install cargo-nextest
cargo run -- init-db
cargo llvm-cov nextest \
--workspace \
--verbose \
--no-cfg-coverage \
--no-cfg-coverage-nightly \
--ignore-filename-regex 'job|.*generated.*' \
--test-threads=1 \
--no-fail-fast \
--retries 1 \
"$@"
}
cmd_html() {
_cov_test --html "$@"
open target/llvm-cov/html/index.html # HACK: `open` is not portable
}
cmd_show_env() {
cat <<EOF
COVERAGE_FUNCTIONS=$COVERAGE_FUNCTIONS
COVERAGE_LINES=$COVERAGE_LINES
COVERAGE_REGIONS=$COVERAGE_REGIONS
EOF
}
cmd_check() {
python3 <(
cat <<'EOF'
import json
import os
import subprocess
import sys
thresholds = {
'functions': float(os.environ['COVERAGE_FUNCTIONS']),
'lines': float(os.environ['COVERAGE_LINES']),
'regions': float(os.environ['COVERAGE_REGIONS']),
}
with open('report.json') as f:
report = json.load(f)
totals = report['data'][0]['totals']
exit_status = 0
failing_metrics = []
for k, threshold in thresholds.items():
actual = totals[k]['percent']
label = k.capitalize()
if actual >= threshold:
print(f'✅ {label} coverage: {actual:.2f}%', file=sys.stderr)
else:
print(
f'❌ {label} coverage is below threshold: {actual:.2f}% < {threshold}%',
file=sys.stderr,
)
failing_metrics.append(k)
exit_status = 1
if failing_metrics:
# Fetch per-file data from the existing profdata — no tests re-run
result = subprocess.run(
['cargo', 'llvm-cov', 'report', '--json',
'--ignore-filename-regex', 'job|.*generated.*'],
capture_output=True, text=True,
)
if result.returncode == 0:
full = json.loads(result.stdout)
files = full['data'][0].get('files', [])
metrics_label = ', '.join(failing_metrics)
print(f'\n── Worst files by {metrics_label} coverage (top 20) ──', file=sys.stderr)
scored = []
for entry in files:
fname = entry.get('filename', '')
summary = entry.get('summary', {})
for metric in failing_metrics:
m = summary.get(metric, {})
covered = m.get('covered', 0)
total = m.get('count', 0)
if total == 0:
continue
pct = covered / total * 100
missed = total - covered
scored.append((pct, missed, total, metric, fname))
# lowest coverage first, then most missed
scored.sort(key=lambda x: (x[0], -x[1]))
seen = set()
count = 0
for pct, missed, total, metric, fname in scored:
key = (metric, fname)
if key in seen:
continue
seen.add(key)
short = fname.split('/src/', 1)[-1] if '/src/' in fname else fname
print(f' {metric:10s} {pct:5.1f}% ({missed} uncovered / {total} total) {short}', file=sys.stderr)
count += 1
if count >= 20:
break
else:
print(f'(could not fetch per-file breakdown: {result.stderr.strip()})', file=sys.stderr)
sys.exit(exit_status)
EOF
)
}
main() {
case "${1:-}" in
'')
cmd_check
;;
check)
shift
cmd_check "$@"
;;
html)
shift
cmd_html "$@"
;;
run-cov)
shift
_cov_test --json --summary-only --output-path report.json "$@"
;;
show-env)
cmd_show_env
;;
-h | --help | help)
usage
;;
*)
echo >&2 "Invalid argument: $1"
echo >&2 "Type '$0 --help' for usage"
exit 1
;;
esac
}
main "$@"