Add workflow badges in README.md #173
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Code Coverage | |
on: [workflow_call, push, pull_request] | |
# on: | |
# workflow_call: | |
# push: | |
# branches: | |
# - main | |
# pull_request: | |
env: | |
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) | |
BUILD_TYPE: Debug | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Pull and Run Docker with GCC 11.4.0 and LCOV | |
run: | | |
docker pull ubuntu:22.04 | |
docker run --rm -v $(pwd):/workspace -w /workspace ubuntu:22.04 bash -c " | |
apt-get update && | |
apt-get install -y gcc-11 g++-11 lcov wget cmake zip bc curl && | |
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100 && | |
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100 && | |
update-alternatives --install /usr/bin/gcov gcov /usr/bin/gcov-11 100 && | |
gcc --version && | |
gcov --version && | |
lcov --version && | |
# Clean old coverage files | |
rm -rf cmake-build-unit-tests/*.gcda cmake-build-unit-tests/*.gcno && | |
# Configure and build the tests | |
rm -rf cmake-build-unit-tests && | |
cmake -B cmake-build-unit-tests -S executables/unitTest -DBUILD_UNIT_TESTS=ON -DCMAKE_BUILD_TYPE=${BUILD_TYPE} && | |
cmake --build cmake-build-unit-tests -j4 && | |
ctest --test-dir cmake-build-unit-tests -j4 && | |
# Capture code coverage | |
lcov --no-external --capture --directory . \ | |
--output-file cmake-build-unit-tests/coverage_unfiltered.info && | |
# Filter out 3rd party and mock files | |
lcov --remove cmake-build-unit-tests/coverage_unfiltered.info \ | |
'*libs/3rdparty/googletest/*' \ | |
'*/mock/*' \ | |
'*/gmock/*' \ | |
'*/gtest/*' \ | |
'*/test/*' \ | |
--output-file cmake-build-unit-tests/coverage.info && | |
# Generate HTML coverage report | |
genhtml cmake-build-unit-tests/coverage.info \ | |
--output-directory cmake-build-unit-tests/coverage && | |
# Zip the coverage report | |
mv cmake-build-unit-tests/coverage code_coverage | |
zip -r code_coverage.zip code_coverage && | |
# Extract coverage percentage and generate badge URL | |
lcov --summary cmake-build-unit-tests/coverage.info | grep lines | awk '{print $2}' > coverage_percentage.txt | |
cat coverage_percentage.txt | |
if [[ -s coverage_percentage.txt ]]; then | |
coverage_percentage=$(cat coverage_percentage.txt) | |
line_color="red" | |
if (( $(echo "${coverage_percentage%?} > 75" | bc -l) )); then line_color="yellow"; fi | |
if (( $(echo "${coverage_percentage%?} > 90" | bc -l) )); then line_color="green"; fi | |
encoded_percentage=${coverage_percentage//%/%25} && \ | |
badge_url="https://img.shields.io/badge/coverage-${encoded_percentage}-${line_color}" | |
echo "Badge URL: ${badge_url}" | |
curl -s "${badge_url}" -o coverage-badge.svg | |
else | |
echo "Error: Coverage percentage is missing or invalid." | |
exit 1 | |
fi | |
mv coverage-badge.svg coverage-badge && | |
zip -r coverage-badge.zip coverage-badge | |
" | |
- name: Upload code coverage artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: code_coverage | |
path: code_coverage.zip | |
- name: Upload coverage badge artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: coverage-badge | |
path: coverage-badge.zip |