diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml new file mode 100644 index 0000000..3887dca --- /dev/null +++ b/.github/workflows/coverage.yaml @@ -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 }} + diff --git a/tests/coverage.sh b/tests/coverage.sh index 2d79e66..5c2427b 100755 --- a/tests/coverage.sh +++ b/tests/coverage.sh @@ -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"