Skip to content

Commit

Permalink
Merge pull request #176 from greenbone/add-coverage
Browse files Browse the repository at this point in the history
Add: Coverage CMake targets and Codecov upload
  • Loading branch information
a-h-abdelsalam authored Jul 1, 2024
2 parents 171d94f + d90eb34 commit 2c83747
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/ci-c.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,21 @@ jobs:
runs-on: ubuntu-latest
container: greenbone/gsad-build:stable
steps:
- name: Install git for Codecov uploader
run: |
apt update
apt install --no-install-recommends -y ca-certificates git
rm -rf /var/lib/apt/lists/*
- uses: actions/checkout@v4
- name: Configure and compile gsad
run: |
cmake -B build -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=1 .
cmake -B build -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=1 -DENABLE_COVERAGE=1 .
cmake --build build
- name: Configure and run tests
run: CTEST_OUTPUT_ON_FAILURE=1 cmake --build build -- tests test
- name: Upload test coverage to Codecov
uses: codecov/codecov-action@v4
with:
file: build/coverage/coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
flags: unittests
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ set (GSAD_VERSION "${PROJECT_VERSION_STRING}")

message (STATUS "Building gsad version ${GSAD_VERSION}")

## Code coverage

OPTION (ENABLE_COVERAGE "Enable support for coverage analysis" OFF)
if (ENABLE_COVERAGE)
set (COVERAGE_FLAGS "--coverage -ftest-coverage -fprofile-arcs")
set (COVERAGE_DIR "${CMAKE_BINARY_DIR}/coverage")
file (MAKE_DIRECTORY ${COVERAGE_DIR})
message ("-- Code Coverage enabled")
endif (ENABLE_COVERAGE)

## Files generated on installation

# generate compile_commands.json file
Expand Down
16 changes: 15 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ endif (NOT LIBMICROHTTPD_FOUND OR NOT LIBXML_FOUND OR NOT GLIB_FOUND OR
set (HARDENING_FLAGS "-D_FORTIFY_SOURCE=2 -fstack-protector")
set (LINKER_HARDENING_FLAGS "-Wl,-z,relro -Wl,-z,now")

set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wformat -Wformat-security")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wformat -Wformat-security ${COVERAGE_FLAGS}")
if (BROTLI_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_BROTLI=1")
endif (BROTLI_FOUND)
Expand Down Expand Up @@ -210,4 +210,18 @@ if (BUILD_TESTING)
)
endif(BUILD_TESTING)

if (ENABLE_COVERAGE)
add_custom_target (coverage-html
COMMAND gcovr --html-details ${COVERAGE_DIR}/coverage.html
-r ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
add_custom_target (coverage-xml
COMMAND gcovr --xml ${COVERAGE_DIR}/coverage.xml
-r ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
add_custom_target (coverage DEPENDS coverage-xml coverage-html)
endif (ENABLE_COVERAGE)

add_custom_target (clean-coverage
COMMAND find . -name *.gcda -delete -or -name *.gcno -delete
COMMAND rm -f ${COVERAGE_DIR}/*)

## End

0 comments on commit 2c83747

Please sign in to comment.