From 9c8928dd291b644fd2c4e84a6893ad1a4076d386 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Mon, 24 Jun 2024 14:47:53 +0200 Subject: [PATCH 1/3] Add: Coverage CMake targets and Codecov upload This adds new targets for measuring unit test coverage that can be enabled with the CMake option ENABLE_COVERAGE. The GitHub Actions workflow now also generates the coverage report and uploads it to Codecov. --- .github/workflows/ci-c.yml | 7 ++++++- CMakeLists.txt | 10 ++++++++++ src/CMakeLists.txt | 16 +++++++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-c.yml b/.github/workflows/ci-c.yml index 58ee17c82..a8820ebe5 100644 --- a/.github/workflows/ci-c.yml +++ b/.github/workflows/ci-c.yml @@ -42,7 +42,12 @@ jobs: - 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 }} diff --git a/CMakeLists.txt b/CMakeLists.txt index d6e4852ff..f612143a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c7e3b5c1f..b45182001 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) @@ -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 From 6a5a5f251d5d078fdc973f48d0437f6371777ee4 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Mon, 24 Jun 2024 16:27:04 +0200 Subject: [PATCH 2/3] Install git for Codecov uploader --- .github/workflows/ci-c.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ci-c.yml b/.github/workflows/ci-c.yml index a8820ebe5..83638a55d 100644 --- a/.github/workflows/ci-c.yml +++ b/.github/workflows/ci-c.yml @@ -39,6 +39,11 @@ 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: | From d90eb34927d732ec56f52052932fa5576f963eed Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Tue, 25 Jun 2024 13:57:04 +0200 Subject: [PATCH 3/3] Add "unittests" flag for Codecov upload --- .github/workflows/ci-c.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci-c.yml b/.github/workflows/ci-c.yml index 83638a55d..e7a37cb18 100644 --- a/.github/workflows/ci-c.yml +++ b/.github/workflows/ci-c.yml @@ -56,3 +56,4 @@ jobs: with: file: build/coverage/coverage.xml token: ${{ secrets.CODECOV_TOKEN }} + flags: unittests