Skip to content

Commit 72d0c8a

Browse files
authored
Add code coverage (#17)
* Add code coverage * Run coverage in windows from source * Remove leftover * Use another target name * Rename ENABLE_COVERAGE * Fix cmake warning * Fix cmake config warning * Add CTEST_OUTPUT_ON_FAILURE=1 * Remove unnecessary code * Change message * Add enable_coverage option to docs * Rename BUILD_DOCS to SPARROW_IPC_BUILD_DOCS for consistency * Run tests consistently * Change verbose mode * Remove CTEST_OUTPUT_ON_FAILURE
1 parent 43abdae commit 72d0c8a

File tree

6 files changed

+82
-15
lines changed

6 files changed

+82
-15
lines changed

.github/workflows/docs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
cache-downloads: true
2222

2323
- name: Configure using CMake
24-
run: cmake -G Ninja -Bbuild -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBUILD_DOCS=ON
24+
run: cmake -G Ninja -Bbuild -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DSPARROW_IPC_BUILD_DOCS=ON
2525

2626
- name: Build docs target
2727
run: cmake --build build --target docs

.github/workflows/windows.yml

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,23 @@ jobs:
3232
- name: Configure using cmake
3333
run: |
3434
cmake -S ./ -B ./build \
35-
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
3635
-DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX% \
3736
-DCMAKE_PREFIX_PATH=%CONDA_PREFIX% \
3837
-DSPARROW_IPC_BUILD_SHARED=${{ matrix.build_shared }} \
3938
-DSPARROW_IPC_BUILD_TESTS=ON
4039
4140
- name: Build sparrow-ipc
4241
working-directory: build
43-
run: cmake --build . --target sparrow-ipc
42+
run: cmake --build . --config ${{ matrix.build_type }} --target sparrow-ipc
4443

4544
- name: Build tests
4645
working-directory: build
47-
run: cmake --build . --target test_sparrow_ipc_lib
46+
run: cmake --build . --config ${{ matrix.build_type }} --target test_sparrow_ipc_lib
4847

4948
- name: Run tests
5049
working-directory: build
5150
run: |
52-
cmake --build . --target run_tests
51+
cmake --build . --config ${{ matrix.build_type }} --target run_tests_with_junit_report
5352
5453
windows_build_fetch_from_source:
5554
runs-on: windows-latest
@@ -61,22 +60,45 @@ jobs:
6160
- name: Checkout repository
6261
uses: actions/checkout@v4
6362

63+
- name: Enable tests coverage
64+
if: matrix.build_type == 'Debug'
65+
run: |
66+
choco install opencppcoverage
67+
echo "TEST_COVERAGE_ACTIVATION=-DSPARROW_IPC_ENABLE_COVERAGE=ON" >> $GITHUB_ENV
68+
6469
- name: Configure using cmake
6570
run: |
6671
cmake -S ./ -B ./build \
67-
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
6872
-DSPARROW_IPC_BUILD_SHARED=${{ matrix.build_shared }} \
6973
-DSPARROW_IPC_BUILD_TESTS=ON \
70-
-DFETCH_DEPENDENCIES_WITH_CMAKE=MISSING
74+
-DFETCH_DEPENDENCIES_WITH_CMAKE=MISSING \
75+
$TEST_COVERAGE_ACTIVATION
7176
7277
- name: Build sparrow-ipc
7378
working-directory: build
74-
run: cmake --build . --target sparrow-ipc
79+
run: cmake --build . --config ${{ matrix.build_type }} --target sparrow-ipc
7580

7681
- name: Build tests
7782
working-directory: build
78-
run: cmake --build . --target test_sparrow_ipc_lib
83+
run: cmake --build . --config ${{ matrix.build_type }} --target test_sparrow_ipc_lib
7984

8085
- name: Run tests
8186
working-directory: build
82-
run: cmake --build . --target run_tests_with_junit_report
87+
run: cmake --build . --config ${{ matrix.build_type }} --target run_tests_with_junit_report
88+
89+
- name: Tests coverage
90+
if: matrix.build_type == 'Debug'
91+
working-directory: build
92+
run: |
93+
cmake --build . --config Debug --target sparrow_ipc_generate_cobertura
94+
95+
- name: Upload coverage to Codecov
96+
if: matrix.build_type == 'Debug'
97+
uses: codecov/codecov-action@v5
98+
with:
99+
directory: ./build/coverage_reports/
100+
fail_ci_if_error: true
101+
files: ./cobertura.xml
102+
flags: unittests
103+
token: ${{ secrets.CODECOV_TOKEN }}
104+
verbose: true

CMakeLists.txt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,17 @@ endif()
4444
OPTION(SPARROW_IPC_BUILD_TESTS "Build sparrow-ipc test suite" OFF)
4545
MESSAGE(STATUS "🔧 Build tests: ${SPARROW_IPC_BUILD_TESTS}")
4646

47-
OPTION(BUILD_DOCS "Build sparrow-ipc documentation" OFF)
48-
MESSAGE(STATUS "🔧 Build docs: ${BUILD_DOCS}")
47+
OPTION(SPARROW_IPC_BUILD_DOCS "Build sparrow-ipc documentation" OFF)
48+
MESSAGE(STATUS "🔧 Build docs: ${SPARROW_IPC_BUILD_DOCS}")
49+
50+
# Code coverage
51+
# =============
52+
OPTION(SPARROW_IPC_ENABLE_COVERAGE "Enable sparrow-ipc test coverage" OFF)
53+
MESSAGE(STATUS "🔧 Enable coverage: ${SPARROW_IPC_ENABLE_COVERAGE}")
54+
55+
if(SPARROW_IPC_ENABLE_COVERAGE)
56+
include(code_coverage)
57+
endif()
4958

5059
set(SPARROW_IPC_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
5160
set(SPARROW_IPC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
@@ -166,7 +175,7 @@ endif()
166175

167176
# Docs
168177
# ====
169-
if(BUILD_DOCS)
178+
if(SPARROW_IPC_BUILD_DOCS)
170179
message(STATUS "📚 Create docs targets")
171180
add_subdirectory(docs)
172181
endif()

cmake/code_coverage.cmake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
find_program(OpenCPPCoverage OpenCppCoverage.exe opencppcoverage.exe REQUIRED
11+
PATHS "C:/Program Files/OpenCppCoverage" "C:/Program Files (x86)/OpenCppCoverage")
12+
13+
cmake_path(CONVERT ${CMAKE_SOURCE_DIR} TO_NATIVE_PATH_LIST OPENCPPCOVERAGE_SOURCES)
14+
set(OPENCPPCOVERAGE_COMMON_ARGS --sources=${OPENCPPCOVERAGE_SOURCES} --modules=${OPENCPPCOVERAGE_SOURCES} --excluded_sources=test*)
15+
16+
add_custom_target(sparrow_ipc_generate_cobertura
17+
COMMAND ${OpenCPPCoverage}
18+
${OPENCPPCOVERAGE_COMMON_ARGS}
19+
--export_type=cobertura:${COBERTURA_REPORT_PATH}
20+
-- $<TARGET_FILE:test_sparrow_ipc_lib>
21+
DEPENDS test_sparrow_ipc_lib
22+
COMMENT "Generating coverage cobertura report with OpenCppCoverage: ${COBERTURA_REPORT_PATH}"
23+
)
24+
set(TARGET_PROPERTIES sparrow_ipc_generate_cobertura PROPERTIES FOLDER ${COVERAGE_TARGETS_FOLDER})
25+
26+
add_custom_target(sparrow_ipc_generate_html_coverage_report
27+
COMMAND ${OpenCPPCoverage}
28+
${OPENCPPCOVERAGE_COMMON_ARGS}
29+
--export_type=html:${COVERAGE_REPORT_PATH}
30+
-- $<TARGET_FILE:test_sparrow_ipc_lib>
31+
DEPENDS test_sparrow_ipc_lib
32+
COMMENT "Generating coverage report with OpenCppCoverage: ${COVERAGE_REPORT_PATH}"
33+
)
34+
set(TARGET_PROPERTIES sparrow_ipc_generate_cobertura PROPERTIES FOLDER "Tests utilities/Code Coverage")
35+
endif()

conanfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def generate(self):
8888
tc = CMakeToolchain(self)
8989
tc.variables["SPARROW_IPC_BUILD_SHARED"] = self.options.shared
9090
tc.variables["SPARROW_IPC_BUILD_TESTS"] = self.options.build_tests
91-
tc.variables["BUILD_DOCS"] = self.options.generate_documentation
91+
tc.variables["SPARROW_IPC_BUILD_DOCS"] = self.options.generate_documentation
9292
tc.generate()
9393

9494
def build(self):

docs/source/dev_build.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ List of CMake options:
77
- `ACTIVATE_LINTER`: Create targets to run clang-format and clang-tidy (default: OFF)
88
- `ACTIVATE_LINTER_DURING_COMPILATION`: Run linter during the compilation (default: OFF),
99
requires `ACTIVATE_LINTER` to be ON
10-
- `BUILD_DOCS`: Build the documentation (default: OFF)
10+
- `SPARROW_IPC_BUILD_DOCS`: Build the documentation (default: OFF)
1111
- `SPARROW_IPC_BUILD_TESTS`: Build the tests (default: OFF)
1212
- `SPARROW_IPC_BUILD_SHARED`: Build sparrow-ipc as a shared library (default: ON)
13+
- `SPARROW_IPC_ENABLE_COVERAGE`: Enable coverage reporting (default: OFF)
1314

1415
## Building
1516

0 commit comments

Comments
 (0)