Skip to content

Commit dae2c7b

Browse files
committed
Add code coverage
1 parent 43abdae commit dae2c7b

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

.github/workflows/windows.yml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,24 @@ jobs:
2929
init-shell: bash
3030
cache-environment: true
3131

32+
- name: Enable tests coverage
33+
if: matrix.build_type == 'Debug'
34+
run: |
35+
choco install opencppcoverage
36+
echo "TEST_COVERAGE_ACTIVATION=' -DENABLE_COVERAGE=ON'" >> $GITHUB_ENV
37+
3238
- name: Configure using cmake
3339
run: |
40+
#if [[ "${{ matrix.build_type }}" == "Debug" ]]; then
41+
#export CMAKE_ARGS="-DENABLE_COVERAGE=ON"
42+
#fi
3443
cmake -S ./ -B ./build \
3544
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
3645
-DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX% \
3746
-DCMAKE_PREFIX_PATH=%CONDA_PREFIX% \
3847
-DSPARROW_IPC_BUILD_SHARED=${{ matrix.build_shared }} \
39-
-DSPARROW_IPC_BUILD_TESTS=ON
48+
-DSPARROW_IPC_BUILD_TESTS=ON \
49+
$TEST_COVERAGE_ACTIVATION
4050
4151
- name: Build sparrow-ipc
4252
working-directory: build
@@ -51,6 +61,23 @@ jobs:
5161
run: |
5262
cmake --build . --target run_tests
5363
64+
- name: Tests coverage
65+
if: matrix.build_type == 'Debug'
66+
working-directory: build
67+
run: |
68+
cmake --build . --config Debug --target generate_cobertura
69+
70+
- name: Upload coverage to Codecov
71+
if: matrix.build_type == 'Debug'
72+
uses: codecov/codecov-action@v5
73+
with:
74+
directory: ./build/coverage_reports/
75+
fail_ci_if_error: true
76+
files: ./cobertura.xml
77+
flags: unittests
78+
token: ${{ secrets.CODECOV_TOKEN }}
79+
verbose: true
80+
5481
windows_build_fetch_from_source:
5582
runs-on: windows-latest
5683
strategy:

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ MESSAGE(STATUS "🔧 Build tests: ${SPARROW_IPC_BUILD_TESTS}")
4747
OPTION(BUILD_DOCS "Build sparrow-ipc documentation" OFF)
4848
MESSAGE(STATUS "🔧 Build docs: ${BUILD_DOCS}")
4949

50+
OPTION(ENABLE_COVERAGE "Enable test coverage" OFF)
51+
MESSAGE(STATUS "🔧 Enable coverage: ${ENABLE_COVERAGE}")
52+
5053
set(SPARROW_IPC_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
5154
set(SPARROW_IPC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
5255

@@ -156,6 +159,10 @@ target_link_libraries(sparrow-ipc
156159

157160
add_dependencies(sparrow-ipc generate_flatbuffers_headers)
158161

162+
if(ENABLE_COVERAGE)
163+
include(code_coverage)
164+
enable_coverage(sparrow-ipc)
165+
endif()
159166
# Tests
160167
# =====
161168
if(SPARROW_IPC_BUILD_TESTS)

cmake/code_coverage.cmake

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
2+
message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
3+
endif()
4+
5+
set(COVERAGE_REPORT_PATH "${CMAKE_BINARY_DIR}/coverage_reports" CACHE PATH "Path to store coverage reports")
6+
set(COBERTURA_REPORT_PATH "${COVERAGE_REPORT_PATH}/cobertura.xml" CACHE PATH "Path to store cobertura report")
7+
set(COVERAGE_TARGETS_FOLDER "Tests utilities/Code Coverage")
8+
9+
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
10+
message(STATUS "==============> Using MSVC")
11+
find_program(OpenCPPCoverage OpenCppCoverage.exe opencppcoverage.exe REQUIRED
12+
PATHS "C:/Program Files/OpenCppCoverage" "C:/Program Files (x86)/OpenCppCoverage")
13+
14+
cmake_path(CONVERT ${CMAKE_SOURCE_DIR} TO_NATIVE_PATH_LIST OPENCPPCOVERAGE_SOURCES)
15+
set(OPENCPPCOVERAGE_COMMON_ARGS --sources=${OPENCPPCOVERAGE_SOURCES} --modules=${OPENCPPCOVERAGE_SOURCES} --excluded_sources=test*)
16+
17+
add_custom_target(generate_cobertura
18+
COMMAND ${OpenCPPCoverage}
19+
${OPENCPPCOVERAGE_COMMON_ARGS}
20+
--export_type=cobertura:${COBERTURA_REPORT_PATH}
21+
-- $<TARGET_FILE:test_sparrow_ipc_lib>
22+
DEPENDS test_sparrow_ipc_lib
23+
COMMENT "Generating coverage cobertura report with OpenCppCoverage: ${COBERTURA_REPORT_PATH}"
24+
)
25+
set(TARGET_PROPERTIES generate_cobertura PROPERTIES FOLDER ${COVERAGE_TARGETS_FOLDER})
26+
27+
add_custom_target(generate_html_coverage_report
28+
COMMAND ${OpenCPPCoverage}
29+
${OPENCPPCOVERAGE_COMMON_ARGS}
30+
--export_type=html:${COVERAGE_REPORT_PATH}
31+
-- $<TARGET_FILE:test_sparrow_ipc_lib>
32+
DEPENDS test_sparrow_ipc_lib
33+
COMMENT "Generating coverage report with OpenCppCoverage: ${COVERAGE_REPORT_PATH}"
34+
)
35+
set(TARGET_PROPERTIES generate_cobertura PROPERTIES FOLDER "Tests utilities/Code Coverage")
36+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
37+
message(STATUS "==============> Using Clang or GNU")
38+
endif()
39+
40+
#TODO this may not be useful if coverage only on Windows ci?
41+
function(enable_coverage target)
42+
set(CLANG_COVERAGE_FLAGS --coverage -fprofile-instr-generate -fcoverage-mapping -fno-inline -fno-elide-constructors)
43+
set(GCC_COVERAGE_FLAGS --coverage -fno-inline -fno-inline-small-functions -fno-default-inline)
44+
45+
target_compile_options(${target} PRIVATE
46+
$<$<CXX_COMPILER_ID:Clang>:${CLANG_COVERAGE_FLAGS}>
47+
$<$<CXX_COMPILER_ID:GNU>:${GCC_COVERAGE_FLAGS}>)
48+
target_link_options(${target} PRIVATE
49+
$<$<CXX_COMPILER_ID:Clang>:${CLANG_COVERAGE_FLAGS}>
50+
$<$<CXX_COMPILER_ID:GNU>:${GCC_COVERAGE_FLAGS}>)
51+
endfunction(enable_coverage target)

tests/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ set(
1313
)
1414

1515
add_executable(${test_target} ${SPARROW_IPC_TESTS_SRC})
16+
17+
if(ENABLE_COVERAGE)
18+
enable_coverage(${test_target})
19+
endif()
20+
1621
target_link_libraries(${test_target}
1722
PRIVATE
1823
sparrow-ipc

0 commit comments

Comments
 (0)