coverage-in-disk.sh 3.8 KB

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