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 fail-fast: false
matrix: matrix:
version: version:
- stable - nightly
name: ${{ matrix.version }} - x86_64-unknown-linux-gnu name: ${{ matrix.version }} - x86_64-unknown-linux-gnu
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -36,18 +36,10 @@ jobs:
profile: minimal profile: minimal
override: true override: true
- name: Generate coverage file - name: generate coverage
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request') run: make coverage
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: Upload to Codecov - name: Upload to Codecov
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request') uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v1 with:
files: ./target/lcov.info,coverage.xml

2
.gitignore vendored
View file

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

View file

@ -5,7 +5,8 @@ clean: ## Clean all build artifacts and dependencies
@cargo clean @cargo clean
coverage: ## Generate HTML code coverage coverage: ## Generate HTML code coverage
cargo tarpaulin -t 1200 --out Html ./scripts/coverage.sh --coverage
dev-env: ## Download development dependencies dev-env: ## Download development dependencies
cargo fetch cargo fetch
@ -33,8 +34,5 @@ run: default ## Run debug build
test: ## Run tests test: ## Run tests
cargo test --all-features --no-fail-fast 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 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}' @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