Skip to content

Commit c8d830f

Browse files
author
Samuel Rodriguez Sevilla
committed
Adding coverage check
1 parent ddb7270 commit c8d830f

File tree

7 files changed

+86
-4
lines changed

7 files changed

+86
-4
lines changed

.devcontainer/Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ FROM gcc:13.2.0
22
RUN \
33
apt update && \
44
apt upgrade -y && \
5-
apt install -y cmake gdb valgrind vim
5+
apt install -y cmake gdb valgrind vim lcov python3-pip python3-venv && \
6+
python3 -m venv /tmp/gcovr && \
7+
/tmp/gcovr/bin/pip install gcovr lxml
68

79
ARG USERNAME=devcontainer
810
ARG USER_UID=1000
@@ -13,4 +15,5 @@ ARG USER_GID=$USER_UID
1315
RUN groupadd --gid ${USER_GID} ${USERNAME} \
1416
&& useradd --uid ${USER_UID} --gid ${USER_GID} -m ${USERNAME}
1517

18+
ENV PATH="/tmp/gcovr/bin:${PATH}"
1619
ENTRYPOINT /bin/bash

.devcontainer/devcontainer.json

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
"source=${localEnv:HOME}/.gitconfig,target=/home/${localEnv:USER}/.gitconfig,type=bind"
3232
],
3333

34+
"containerEnv": {
35+
"SHELL": "/bin/bash"
36+
},
3437
// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
3538
"remoteUser": "${localEnv:USER}"
3639

.github/workflows/cmake-ubuntu-g++13.yml

+22-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ jobs:
2222
steps:
2323
- uses: actions/checkout@v3
2424

25+
- name: install
26+
run: sudo apt-get update && sudo apt-get install lcov #libxrandr-dev libxcursor-dev libudev-dev libopenal-dev libflac-dev libvorbis-dev libgl1-mesa-dev libegl1-mesa-dev freeglut3-dev # OpenGL dependencies only needed for SFML
27+
2528
- name: Configure CMake
2629
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
2730
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
28-
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_CXX_COMPILER=g++-13
31+
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_CXX_COMPILER=g++-13 -DENABLE_TEST -DENABLE_COVERAGE
2932

3033
- name: Build
3134
# Build your program with the given configuration
@@ -37,3 +40,21 @@ jobs:
3740
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
3841
run: ctest -C ${{env.BUILD_TYPE}}
3942

43+
- name: generate coverage
44+
working-directory: ./build
45+
run: |
46+
make cov
47+
48+
- name: Install gcovr
49+
run: |
50+
pip install -r requirements.txt
51+
52+
- name: Generate JSON coverage report
53+
working-directory: ./build
54+
run: |
55+
gcovr -r .. . --branches --cobertura --filter ../include > coverage.xml
56+
57+
- name: Upload coverage reports to Codecov
58+
uses: codecov/codecov-action@v3
59+
env:
60+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@
3030
*.exe
3131
*.out
3232
*.app
33+
34+
# Visual Studio Code personal configuration
35+
.vscode
36+
37+
# Build directory
38+
build
39+
Testing

CMakeLists.txt

+10-2
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,13 @@ install(TARGETS lzc-coro
3535
# INSTALL_DESTINATION
3636
# ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
3737

38-
enable_testing()
39-
add_subdirectory(tests)
38+
option(ENABLE_TESTS "Build tests" OFF)
39+
option(ENABLE_COVERAGE "If ENABLE_TESTS is ON then coverage could be produced" OFF)
40+
if(ENABLE_TESTS)
41+
enable_testing()
42+
if(ENABLE_COVERAGE)
43+
include(cmake/coverage.cmake)
44+
add_coverage_target("*/tests/*")
45+
endif()
46+
add_subdirectory(tests)
47+
endif()

cmake/coverage.cmake

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function(add_coverage_target exclude)
2+
3+
find_program(GCOV gcov)
4+
if (NOT GCOV)
5+
message(WARNING "program gcov not found")
6+
endif()
7+
8+
find_program(LCOV lcov)
9+
if (NOT LCOV)
10+
message(WARNING "program lcov not found")
11+
endif()
12+
13+
find_program(GENHTML genhtml)
14+
if (NOT GENHTML)
15+
message(WARNING "program genhtml not found")
16+
endif()
17+
18+
if (LCOV AND GCOV AND GENHTML)
19+
set(covname cov.info)
20+
add_compile_options(-fprofile-arcs -ftest-coverage)
21+
add_link_options(--coverage)
22+
add_custom_target(cov DEPENDS ${covname})
23+
add_custom_command(
24+
OUTPUT ${covname}
25+
COMMAND ${LCOV} -c -o ${covname} -d . -b . --gcov-tool ${GCOV}
26+
COMMAND ${LCOV} -r ${covname} -o ${covname} ${exclude}
27+
COMMAND ${LCOV} -l ${covname}
28+
COMMAND ${GENHTML} ${covname} -output coverage
29+
COMMAND ${LCOV} -l ${covname} 2>/dev/null | grep Total | sed 's/|//g' | sed 's/Total://g' | awk '{print $1}' | sed s/%//g > coverage/total
30+
)
31+
set_directory_properties(PROPERTIES
32+
ADDITIONAL_CLEAN_FILES ${covname}
33+
)
34+
else()
35+
message(WARNING "unable to add target `cov`: missing coverage tools")
36+
endif()
37+
38+
endfunction()

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gcovr==6.0
2+
lxml==5.1.0

0 commit comments

Comments
 (0)