Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate gcovr and Coveralls #30

Merged
merged 16 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add Coveralls publish step; fix issues
  • Loading branch information
0xg0nz0 committed Mar 13, 2024
commit a43017bc7394b507bd3aa14e7e8b2b5f897987ad
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 56,13 @@ jobs:
- name: Run Coverage
run: |
cd build
ninja coverage
- name: Report code coverage
uses: andybelltree/lcov-reporter-action@v1.7.0
ninja coveralls
- name: Upload report to Coveralls
uses: coverallsapp/github-action@v2
with:
lcov-file: build/coverage.info
file: build/coveralls.json
measure: true
github-token: ${{ secrets.GITHUB_TOKEN }}
delete-old-comments: true

lint:
name: Lint
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 59,13 @@ repos:
always_run: true
- id: run-unit-tests
name: Run Unit Tests
entry: ./build/tests/iggy_cpp_test
entry: ./build/tests/Debug/iggy_cpp_test
language: system
pass_filenames: false
always_run: true
- id: e2e-tests
name: Run E2E Tests
entry: ./build/tests/iggy_e2e_test
entry: ./build/tests/Debug/iggy_e2e_test
language: system
pass_filenames: false
always_run: true
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 76,13 @@ target_link_libraries(
unofficial-sodium::sodium
)

# even though this is related to unit tests, to get a full report we need to ensure that
# all library files are compiled with --coverage so gcno is generated properly
if(ENABLE_CODE_COVERAGE)
include(cmake/modules/CodeCoverage.cmake)
append_coverage_compiler_flags()
endif()

if(BUILD_TESTS)
add_subdirectory(tests)
endif()
Expand Down
92 changes: 92 additions & 0 deletions cmake/modules/CodeCoverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 489,98 @@ function(setup_target_for_coverage_gcovr_xml)
)
endfunction() # setup_target_for_coverage_gcovr_xml

# Defines a target for running and collection code coverage information
# Builds dependencies, runs the given executable and outputs reports.
# NOTE! The executable should always have a ZERO as exit code otherwise
# the coverage generation will not complete.
#
# setup_target_for_coverage_gcovr_xml(
# NAME ctest_coverage # New target name
# EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
# DEPENDENCIES executable_target # Dependencies to build first
# BASE_DIRECTORY "../" # Base directory for report
# # (defaults to PROJECT_SOURCE_DIR)
# EXCLUDE "src/dir1/*" "src/dir2/*" # Patterns to exclude (can be relative
# # to BASE_DIRECTORY, with CMake 3.4 )
# )
# The user can set the variable GCOVR_ADDITIONAL_ARGS to supply additional flags to the
# GCVOR command.
function(setup_target_for_coverage_gcovr_coveralls)

set(options NONE)
set(oneValueArgs BASE_DIRECTORY NAME)
set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if(NOT GCOVR_PATH)
message(FATAL_ERROR "gcovr not found! Aborting...")
endif() # NOT GCOVR_PATH

# Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR
if(DEFINED Coverage_BASE_DIRECTORY)
get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE)
else()
set(BASEDIR ${PROJECT_SOURCE_DIR})
endif()

# Collect excludes (CMake 3.4 : Also compute absolute paths)
set(GCOVR_EXCLUDES "")
foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_GCOVR_EXCLUDES})
if(CMAKE_VERSION VERSION_GREATER 3.4)
get_filename_component(EXCLUDE ${EXCLUDE} ABSOLUTE BASE_DIR ${BASEDIR})
endif()
list(APPEND GCOVR_EXCLUDES "${EXCLUDE}")
endforeach()
list(REMOVE_DUPLICATES GCOVR_EXCLUDES)

# Combine excludes to several -e arguments
set(GCOVR_EXCLUDE_ARGS "")
foreach(EXCLUDE ${GCOVR_EXCLUDES})
list(APPEND GCOVR_EXCLUDE_ARGS "-e")
list(APPEND GCOVR_EXCLUDE_ARGS "${EXCLUDE}")
endforeach()

# Set up commands which will be run to generate coverage data
# Run tests
set(GCOVR_COVERALLS_EXEC_TESTS_CMD
${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS}
)
# Running gcovr
set(GCOVR_COVERALLS_CMD
${GCOVR_PATH} --coveralls-pretty -o ${Coverage_NAME}.json -r ${BASEDIR} ${GCOVR_ADDITIONAL_ARGS}
${GCOVR_EXCLUDE_ARGS} --object-directory=${PROJECT_BINARY_DIR}
)

if(CODE_COVERAGE_VERBOSE)
message(STATUS "Executed command report")

message(STATUS "Command to run tests: ")
string(REPLACE ";" " " GCOVR_COVERALLS_EXEC_TESTS_CMD_SPACED "${GCOVR_COVERALLS_EXEC_TESTS_CMD}")
message(STATUS "${GCOVR_COVERALLS_EXEC_TESTS_CMD_SPACED}")

message(STATUS "Command to generate gcovr Coveralls coverage data: ")
string(REPLACE ";" " " GCOVR_COVERALLS_CMD_SPACED "${GCOVR_COVERALLS_CMD}")
message(STATUS "${GCOVR_COVERALLS_CMD_SPACED}")
endif()

add_custom_target(${Coverage_NAME}
COMMAND ${GCOVR_COVERALLS_EXEC_TESTS_CMD}
COMMAND ${GCOVR_COVERALLS_CMD}

BYPRODUCTS ${Coverage_NAME}.json
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
DEPENDS ${Coverage_DEPENDENCIES}
VERBATIM # Protect arguments to commands
COMMENT "Running gcovr to produce Coveralls code coverage report."
)

# Show info where to find the report
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
COMMAND ;
COMMENT "Coveralls code coverage report saved in ${Coverage_NAME}.json."
)
endfunction() # setup_target_for_coverage_gcovr_coveralls

# Defines a target for running and collection code coverage information
# Builds dependencies, runs the given executable and outputs reports.
# NOTE! The executable should always have a ZERO as exit code otherwise
Expand Down
15 changes: 10 additions & 5 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 38,16 @@ if(BUILD_TESTS)
include(../cmake/modules/CodeCoverage.cmake)
append_coverage_compiler_flags()

setup_target_for_coverage_lcov(
NAME coverage
setup_target_for_coverage_gcovr_coveralls(
NAME coveralls
EXECUTABLE iggy_cpp_test
DEPENDENCIES iggy
EXCLUDE /usr/include/c /* ${CMAKE_CURRENT_SOURCE_DIR}/../tests/*
${CMAKE_CURRENT_SOURCE_DIR}/../build/*)
EXCLUDE "build/vcpkg_installed/*" "tests/*"
DEPENDENCIES iggy)

setup_target_for_coverage_gcovr_html(
NAME coverage-html
EXECUTABLE iggy_cpp_test
EXCLUDE "build/vcpkg_installed/*" "tests/*"
DEPENDENCIES iggy)
endif()
endif()