From 5d46ef3a582b76145a049808b2a5a30b4607544b Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 25 Nov 2021 14:09:42 +0530 Subject: [PATCH] use grcov to get rust code coverage --- .github/workflows/coverage.yml | 20 +++----- .gitignore | 2 + Makefile | 6 +-- scripts/coverage.sh | 88 ++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 18 deletions(-) create mode 100755 scripts/coverage.sh diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 1b136ab..f15fa4c 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -13,7 +13,7 @@ jobs: fail-fast: false matrix: version: - - stable + - nightly name: ${{ matrix.version }} - x86_64-unknown-linux-gnu runs-on: ubuntu-latest @@ -36,18 +36,10 @@ jobs: profile: minimal override: true - - name: Generate coverage file - if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request') - uses: actions-rs/tarpaulin@v0.1 - with: - version: '0.18.2' - args: '-t 1200' - env: - # GIT_HASH is dummy value. I guess build.rs is skipped in tarpaulin - # execution so this value is required for preventing meta tests from - # panicking - GIT_HASH: 8e77345f1597e40c2e266cb4e6dee74888918a61 + - name: generate coverage + run: make coverage - name: Upload to Codecov - if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request') - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v2 + with: + files: ./target/lcov.info,coverage.xml diff --git a/.gitignore b/.gitignore index 6c19a0e..4e0a7f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target posts_cache +tmp/ +*.profraw diff --git a/Makefile b/Makefile index 84b65fe..fc5a18f 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ clean: ## Clean all build artifacts and dependencies @cargo clean coverage: ## Generate HTML code coverage - cargo tarpaulin -t 1200 --out Html + ./scripts/coverage.sh --coverage + dev-env: ## Download development dependencies cargo fetch @@ -33,8 +34,5 @@ run: default ## Run debug build test: ## Run tests cargo test --all-features --no-fail-fast -xml-test-coverage: ## Generate cobertura.xml test coverage - cargo tarpaulin -t 1200 --out Xml - help: ## Prints help for targets with comments @cat $(MAKEFILE_LIST) | grep -E '^[a-zA-Z_-]+:.*?## .*$$' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/scripts/coverage.sh b/scripts/coverage.sh new file mode 100755 index 0000000..bea11e6 --- /dev/null +++ b/scripts/coverage.sh @@ -0,0 +1,88 @@ +#!/bin/bash +readonly GRCOV_DOWNLOAD="https://github.com/mozilla/grcov/releases/download/v0.8.2/grcov-linux-x86_64.tar.bz2" +readonly TMP_DIR=$(pwd)/tmp +readonly PROJECT_ROOT=$(pwd) +readonly GRCOV_TARBAL="$TMP_DIR/grcov.tar.bz2" +readonly GRCOV="$TMP_DIR/grcov" + +clean_up() { + cd $PROJECT_ROOT + /bin/rm default.profraw lcov.info *.profraw || true + cd target + /bin/rm default.profraw lcov.info *.profraw || true + cargo clean +} + +check_arg(){ + if [ -z $1 ] + then + help + exit 1 + fi +} + +match_arg() { + if [ $1 == $2 ] || [ $1 == $3 ] + then + return 0 + else + return 1 + fi +} + + +download() { + if [ ! -e $GRCOV ]; + then + echo "[*] Downloading grcov" + wget --quiet --output-doc=$GRCOV_TARBAL $GRCOV_DOWNLOAD; + cd $TMP_DIR + tar -xf $GRCOV_TARBAL; + cd $PROJECT_ROOT + fi +} + +build_and_test() { + export RUSTFLAGS="-Zinstrument-coverage" + cd $PROJECT_ROOT + + echo "[*] Building project" + cargo build + + export LLVM_PROFILE_FILE="target/libmedium-%p-%m.profraw" + + echo "[*] Running tests" + cargo test + + echo "[*] Generating coverage" + $GRCOV target/ --binary-path \ + ./target/debug/ \ + -s . -t lcov --branch \ + --ignore-not-existing \ + --ignore "../*" -o target/lcov.info + + $GRCOV target/ --binary-path \ + ./target/debug/ \ + -s . -t html --branch \ + --ignore-not-existing \ + --ignore "../*" -o target/coverage.html + +} + +run_coverage() { + cd $PROJECT_ROOT + mkdir $TMP_DIR || true + clean_up + download + build_and_test +} + +check_arg $1 + +if match_arg $1 'c' '--coverage' +then + run_coverage +else + echo "undefined option" + exit 1 +fi