Skip to content

Commit

Permalink
Merge pull request #1 from nicbus/coverage
Browse files Browse the repository at this point in the history
Coverage
  • Loading branch information
nicbus authored Feb 23, 2024
2 parents e6b568f + caa92fd commit 82f61f3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 21 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Coverage

on:
push:
branches:
- master
- coverage
pull_request:
branches:
- master

jobs:
codecov:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup rust toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
components: llvm-tools-preview
toolchain: stable

- name: Install llvm-cov
run: cargo install cargo-llvm-cov

- name: Generate coverage report
run: cargo llvm-cov --lcov --output-path coverage.lcov

- name: Upload coverage report
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
file: coverage.lcov
flags: rust
token: ${{ secrets.CODECOV_TOKEN }}

53 changes: 32 additions & 21 deletions tests/coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,39 @@
#
# script to run projects tests and report code coverage
#
# uses tarpaulin (https://crates.io/crates/cargo-tarpaulin)
#
# other coverage solutions exist but all require rust nightly and the project
# does not build with nightly at the moment
# uses grcov (https://github.com/mozilla/grcov)

COVERAGE_DIR="target/coverage"

# install tarpaulin if missing
cargo tarpaulin --help >/dev/null 2>&1 || cargo install cargo-tarpaulin
_tit() {
echo
echo "========================================"
echo "$@"
echo "========================================"
}

_tit "installing requirements"
rustup component add llvm-tools-preview
cargo install grcov

_tit "gathering coverage info"
# enable code coverage instrumentation and set per-test profile file name
export RUSTFLAGS="-Cinstrument-coverage"
export RUSTDOCFLAGS="-Cinstrument-coverage"
export LLVM_PROFILE_FILE="$COVERAGE_DIR/%p-%m.profraw"
# run tests
# --skip-clean to avoid re-building everything each time
cargo tarpaulin \
--count \
--line \
--locked \
--skip-clean \
--ignore-tests \
--exclude-files rgb-lib-ffi/ \
--exclude-files tests/ \
--exclude-files src/wallet/test/ \
--out Html \
-- \
--test-threads=1
rm -rf $COVERAGE_DIR && mkdir -p $COVERAGE_DIR
cargo test --no-fail-fast || true

_tit "generating coverage report"
grcov $COVERAGE_DIR \
-s . \
--binary-path target/debug/ \
--output-types html \
--branch \
--ignore 'target/*' \
--ignore-not-existing \
-o $COVERAGE_DIR/

# open the html test report in the default browser
xdg-open tarpaulin-report.html
## show html report location
echo "generated html report: $COVERAGE_DIR/html/index.html"

0 comments on commit 82f61f3

Please sign in to comment.