|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Runs all tests and collects code coverage |
| 4 | +# |
| 5 | +# Warning: this process is a little slow |
| 6 | +# |
| 7 | + |
| 8 | +if ! command -v grcov; then |
| 9 | + echo "Error: grcov not found. Try |cargo install grcov|" |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +if [[ ! "$(grcov --version)" =~ 0.8.[0-9] ]]; then |
| 14 | + echo Error: Required grcov version not installed |
| 15 | + |
| 16 | + echo "Installed version: $(grcov --version)" |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +set -e |
| 21 | +cd "$(dirname "$0")/.." |
| 22 | +source ci/_ |
| 23 | + |
| 24 | +cargo="$(readlink -f "./cargo")" |
| 25 | + |
| 26 | +: "${CI_COMMIT:=local}" |
| 27 | +reportName="lcov-${CI_COMMIT:0:9}" |
| 28 | + |
| 29 | +if [[ -z $1 ]]; then |
| 30 | + packages=(--lib --all --exclude solana-local-cluster) |
| 31 | +else |
| 32 | + packages=("$@") |
| 33 | +fi |
| 34 | + |
| 35 | +coverageFlags=() |
| 36 | +coverageFlags+=(-Zprofile) # Enable coverage |
| 37 | +coverageFlags+=("-Aincomplete_features") # Supress warnings due to frozen abi, which is harmless for it |
| 38 | +if [[ $(uname) != Darwin ]]; then # macOS skipped due to https://github.com/rust-lang/rust/issues/63047 |
| 39 | + coverageFlags+=("-Clink-dead-code") # Dead code should appear red in the report |
| 40 | +fi |
| 41 | +coverageFlags+=("-Ccodegen-units=1") # Disable code generation parallelism which is unsupported under -Zprofile (see [rustc issue #51705]). |
| 42 | +coverageFlags+=("-Cinline-threshold=0") # Disable inlining, which complicates control flow. |
| 43 | +coverageFlags+=("-Copt-level=0") |
| 44 | +coverageFlags+=("-Coverflow-checks=off") # Disable overflow checks, which create unnecessary branches. |
| 45 | + |
| 46 | +export RUSTFLAGS="${coverageFlags[*]} $RUSTFLAGS" |
| 47 | +export CARGO_INCREMENTAL=0 |
| 48 | +export RUST_BACKTRACE=1 |
| 49 | +export RUST_MIN_STACK=8388608 |
| 50 | +export SOLANA_TEST_ACCOUNTS_INDEX_MEMORY_LIMIT_MB=10000 |
| 51 | + |
| 52 | +echo "--- remove old coverage results" |
| 53 | +if [[ -d target/cov ]]; then |
| 54 | + find target/cov -type f -name '*.gcda' -delete |
| 55 | +fi |
| 56 | +rm -rf target/cov/$reportName |
| 57 | +mkdir -p target/cov |
| 58 | + |
| 59 | +# Mark the base time for a clean room dir |
| 60 | +touch target/cov/before-test |
| 61 | + |
| 62 | +# Force rebuild of possibly-cached proc macro crates and build.rs because |
| 63 | +# we always want stable coverage for them |
| 64 | +# Don't support odd file names in our repo ever |
| 65 | +if [[ -n $CI || -z $1 ]]; then |
| 66 | + # shellcheck disable=SC2046 |
| 67 | + touch \ |
| 68 | + $(git ls-files :**/build.rs) \ |
| 69 | + $(git grep -l "proc-macro.*true" :**/Cargo.toml | sed 's|Cargo.toml|src/lib.rs|') |
| 70 | +fi |
| 71 | + |
| 72 | +# limit jobs to 4gb/thread |
| 73 | +JOBS=$(grep MemTotal /proc/meminfo | awk '{printf "%.0f", ($2 / (4 * 1024 * 1024))}') |
| 74 | +NPROC=$(nproc) |
| 75 | +JOBS=$((JOBS>NPROC ? NPROC : JOBS)) |
| 76 | + |
| 77 | +RUST_LOG=solana=trace _ "$cargo" nightly test --jobs "$JOBS" --target-dir target/cov --no-run "${packages[@]}" |
| 78 | +if RUST_LOG=solana=trace _ "$cargo" nightly test --jobs "$JOBS" --target-dir target/cov "${packages[@]}" 2> target/cov/coverage-stderr.log; then |
| 79 | + test_status=0 |
| 80 | +else |
| 81 | + test_status=$? |
| 82 | + echo "Failed: $test_status" |
| 83 | + echo "^^^ +++" |
| 84 | + if [[ -n $CI ]]; then |
| 85 | + exit $test_status |
| 86 | + fi |
| 87 | +fi |
| 88 | +touch target/cov/after-test |
| 89 | + |
| 90 | +echo "--- grcov" |
| 91 | + |
| 92 | +# Create a clean room dir only with updated gcda/gcno files for this run, |
| 93 | +# because our cached target dir is full of other builds' coverage files |
| 94 | +rm -rf target/cov/tmp |
| 95 | +mkdir -p target/cov/tmp |
| 96 | + |
| 97 | +# Can't use a simpler construct under the condition of SC2044 and bash 3 |
| 98 | +# (macOS's default). See: https://github.com/koalaman/shellcheck/wiki/SC2044 |
| 99 | +find target/cov -type f -name '*.gcda' -newer target/cov/before-test ! -newer target/cov/after-test -print0 | |
| 100 | + (while IFS= read -r -d '' gcda_file; do |
| 101 | + gcno_file="${gcda_file%.gcda}.gcno" |
| 102 | + ln -sf "../../../$gcda_file" "target/cov/tmp/$(basename "$gcda_file")" |
| 103 | + ln -sf "../../../$gcno_file" "target/cov/tmp/$(basename "$gcno_file")" |
| 104 | + done) |
| 105 | + |
| 106 | +( |
| 107 | + grcov_args=( |
| 108 | + target/cov/tmp |
| 109 | + --llvm |
| 110 | + --ignore \*.cargo\* |
| 111 | + --ignore \*build.rs |
| 112 | + --ignore bench-tps\* |
| 113 | + --ignore upload-perf\* |
| 114 | + --ignore bench-streamer\* |
| 115 | + --ignore local-cluster\* |
| 116 | + ) |
| 117 | + |
| 118 | + set -x |
| 119 | + grcov "${grcov_args[@]}" -t html -o target/cov/$reportName |
| 120 | + grcov "${grcov_args[@]}" -t lcov -o target/cov/lcov.info |
| 121 | + |
| 122 | + cd target/cov |
| 123 | + tar zcf report.tar.gz $reportName |
| 124 | +) |
| 125 | + |
| 126 | +ls -l target/cov/$reportName/index.html |
| 127 | +ln -sfT $reportName target/cov/LATEST |
| 128 | + |
| 129 | +exit $test_status |
0 commit comments