use grcov to get rust code coverage

This commit is contained in:
Aravinth Manivannan 2021-11-25 14:09:42 +05:30
parent c438ea3b2d
commit 5d46ef3a58
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
4 changed files with 98 additions and 18 deletions

View File

@ -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

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
/target
posts_cache
tmp/
*.profraw

View File

@ -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}'

88
scripts/coverage.sh Executable file
View File

@ -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