Skip to content

Commit 61956f9

Browse files
add basic coverage
1 parent 54e87f0 commit 61956f9

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

.gdbinit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
skip make_frame_base

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
*.hex
2121
*.dSYM/
2222
*build/
23+
build*/
2324
cmake-build-*/
24-
build-avr/
2525
.cache/
2626
.metadata
2727
.settings
2828
.project
2929
.cproject
3030
.pydevproject
31-
.gdbinit
3231
.scannerwork/
3332
.vscode/
3433
**/.idea/*

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Consult with the CI workflow files for the required tools and build & test instr
2727
You may want to use the [toolshed](https://github.com/OpenCyphal/docker_toolchains/pkgs/container/toolshed)
2828
container for this.
2929

30+
To run tests with coverage reports, refer to the instructions in `tests/CMakeLists.txt`.
31+
3032
## Releasing
3133

3234
Simply create a new release & tag on GitHub.

tests/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ enable_testing()
1515

1616
set(CTEST_OUTPUT_ON_FAILURE ON)
1717
set(NO_STATIC_ANALYSIS OFF CACHE BOOL "disable udpard static analysis")
18+
set(ENABLE_COVERAGE OFF CACHE BOOL "enable code coverage measurement")
1819

1920
set(library_dir "${CMAKE_SOURCE_DIR}/libudpard")
2021
set(unity_root "${CMAKE_SOURCE_DIR}/submodules/unity")
@@ -53,6 +54,13 @@ function(gen_test name files compile_definitions compile_flags link_flags c_stan
5354
target_include_directories(${name} PUBLIC ${library_dir})
5455
target_compile_definitions(${name} PUBLIC ${compile_definitions})
5556
target_link_libraries(${name} "${name}_unity")
57+
58+
# Apply coverage flags if coverage is enabled
59+
if (ENABLE_COVERAGE)
60+
target_compile_options(${name} PRIVATE --coverage)
61+
target_link_options(${name} PRIVATE --coverage)
62+
endif()
63+
5664
set_target_properties(
5765
${name}
5866
PROPERTIES
@@ -82,3 +90,31 @@ gen_test_matrix(test_intrusive_header "src/test_intrusive_header.c")
8290
gen_test_matrix(test_intrusive_misc "src/test_intrusive_misc.c")
8391
gen_test_matrix(test_intrusive_tx "src/test_intrusive_tx.c")
8492
gen_test_matrix(test_intrusive_rx "src/test_intrusive_rx.c")
93+
94+
# Coverage targets. Usage:
95+
# cmake -DENABLE_COVERAGE=ON ..
96+
# make -j16
97+
# make test # Run the tests
98+
# make coverage # Generate coverage reports
99+
# xdg-open coverage-html/index.html
100+
if (ENABLE_COVERAGE)
101+
find_program(LCOV_PATH lcov REQUIRED)
102+
find_program(GENHTML_PATH genhtml REQUIRED)
103+
104+
# Target to reset coverage counters
105+
add_custom_target(coverage-reset
106+
COMMAND ${LCOV_PATH} --zerocounters --directory .
107+
COMMAND ${LCOV_PATH} --capture --initial --directory . --output-file coverage-base.info
108+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
109+
COMMENT "Resetting coverage counters"
110+
)
111+
112+
# Target to generate coverage report
113+
add_custom_target(coverage
114+
COMMAND ${LCOV_PATH} --capture --directory . --output-file coverage-total.info
115+
COMMAND ${LCOV_PATH} --extract coverage-total.info '*/libudpard/udpard.c' --output-file coverage-udpard.info
116+
COMMAND ${GENHTML_PATH} coverage-udpard.info --output-directory coverage-html --title "libudpard Coverage" --legend --demangle-cpp
117+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
118+
COMMENT "Generating coverage HTML report in coverage-html/"
119+
)
120+
endif()

0 commit comments

Comments
 (0)