-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage.sh
More file actions
executable file
·128 lines (111 loc) · 4.03 KB
/
coverage.sh
File metadata and controls
executable file
·128 lines (111 loc) · 4.03 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
#!/usr/bin/env bash
set -euo pipefail
# Coverage runner for Rust workspaces using cargo-llvm-cov.
# - Supports both cli/ and protocol/ workspaces
# - Generates HTML report and LCOV file
# - Prints a summary to stdout
# Parse flags
FULL_CLEAN_FLAG=0
OPEN_HTML_FLAG=${OPEN_HTML:-0}
TARGET="cli"
for arg in "$@"; do
case "$arg" in
--full-clean|-c)
FULL_CLEAN_FLAG=1
;;
--open)
OPEN_HTML_FLAG=1
;;
--cli)
TARGET="cli"
;;
--protocol)
TARGET="protocol"
;;
--help|-h)
echo "Usage: $0 [--cli|--protocol] [--full-clean|-c] [--open]";
echo " --cli Run coverage on CLI workspace (default)";
echo " --protocol Run coverage on protocol workspace";
echo " --full-clean, -c Run cargo clean and remove coverage dirs before running";
echo " --open Open HTML report locally (no-op in CI)";
exit 0;
;;
*) ;;
esac
done
ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
cd "$ROOT_DIR/$TARGET"
echo "==> Running coverage for $TARGET workspace"
echo "==> Formatting and linting (like test.sh)"
cargo fmt
cargo clippy --all-targets --all-features -q || true
echo "==> Checking cargo-llvm-cov availability"
if ! cargo llvm-cov --version >/dev/null 2>&1; then
if [[ "${AUTO_INSTALL_LLVM_COV:-1}" == "1" ]]; then
echo "==> Installing cargo-llvm-cov (first run only)"
if ! cargo install cargo-llvm-cov; then
echo "Failed to install cargo-llvm-cov. Install manually with:" >&2
echo " cargo install cargo-llvm-cov" >&2
exit 1
fi
else
echo "cargo-llvm-cov is not installed. Install with:" >&2
echo " cargo install cargo-llvm-cov" >&2
exit 1
fi
fi
# Ensure llvm-tools only when running coverage, to avoid slowing CI in other jobs
if ! rustup component list --installed | grep -q '^llvm-tools-preview'; then
if [[ "${AUTO_INSTALL_LLVM_TOOLS:-1}" == "1" ]]; then
echo "==> Installing rustup component: llvm-tools-preview (first run only)"
if ! rustup component add llvm-tools-preview; then
echo "Failed to install llvm-tools-preview. Install manually with:" >&2
echo " rustup component add llvm-tools-preview" >&2
exit 1
fi
else
echo "llvm-tools-preview is missing. Enable auto-install via AUTO_INSTALL_LLVM_TOOLS=1 or run:" >&2
echo " rustup component add llvm-tools-preview" >&2
exit 1
fi
fi
mkdir -p target/coverage
if [[ "${FULL_CLEAN:-0}" == "1" || "$FULL_CLEAN_FLAG" == "1" ]]; then
echo "==> FULL_CLEAN=1: performing cargo clean and removing coverage dirs"
cargo clean
rm -rf target/llvm-cov target/coverage target/llvm-cov-target || true
fi
echo "==> Cleaning previous coverage artifacts"
cargo llvm-cov clean --workspace
# Ensure coverage output directory exists after cleaning steps
mkdir -p target/coverage
LCOV_OUT=${LCOV_OUT:-target/coverage/lcov.info}
HTML_FLAG="--html"
OPEN_FLAG=""
if [[ "$OPEN_HTML_FLAG" == "1" ]]; then
OPEN_FLAG="--open"
fi
echo "==> Running coverage (HTML via cargo alias)"
# Detect if cargo-llvm-cov supports --nextest
NEXTEST_FLAG=""
if cargo llvm-cov --help 2>/dev/null | grep -q -e "--nextest"; then
NEXTEST_FLAG="--nextest"
fi
# Use the alias defined in cli/.cargo/config.toml, append nextest if supported
cargo coverage $NEXTEST_FLAG $OPEN_FLAG
echo "==> Exporting LCOV (from existing coverage data)"
cargo llvm-cov report --lcov --output-path "$LCOV_OUT"
echo "==> Coverage summary (sorted by coverage %)"
# Capture the summary once to avoid broken pipe errors when truncating output.
SUMMARY_OUTPUT=$(cargo llvm-cov report --summary-only)
printf '%s\n' "$SUMMARY_OUTPUT" | head -n 3
printf '%s\n' "$SUMMARY_OUTPUT" | tail -n +4 | grep -v "^TOTAL" | sort -t'%' -k3 -n
printf '%s\n' "$SUMMARY_OUTPUT" | grep "^TOTAL"
# Best-effort path to HTML report (cargo-llvm-cov default)
HTML_DIR="target/llvm-cov/html"
if [[ -d "$HTML_DIR" ]]; then
echo "HTML report: $TARGET/$HTML_DIR/index.html"
else
echo "HTML report directory not found. cargo-llvm-cov typically writes to target/llvm-cov/html" >&2
fi
echo "LCOV file: $TARGET/$LCOV_OUT"