From 467f9d4781ce530e5f4d331f5f9b8e63732357a4 Mon Sep 17 00:00:00 2001 From: Uilian Ries Date: Tue, 2 Jul 2024 23:17:02 +0200 Subject: [PATCH] ci: Fix code coverage and publish on Codecov (#773) * Add codecov support Signed-off-by: Uilian Ries * Add codecov to analysis Signed-off-by: Uilian Ries * Check library coverage Signed-off-by: Uilian Ries * Add codecov to analysis Signed-off-by: Uilian Ries * update coverage option Signed-off-by: Uilian Ries * Fix code coverage steps Signed-off-by: Uilian Ries --------- Signed-off-by: Uilian Ries --- .github/workflows/linux-clang-build.yml | 5 +++++ .github/workflows/linux-static-analysis.yml | 17 +++++++++++++---- CMakeLists.txt | 20 +++++++++++--------- tests/CMakeLists.txt | 4 +++- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/.github/workflows/linux-clang-build.yml b/.github/workflows/linux-clang-build.yml index 32c1be699..041c5b35d 100644 --- a/.github/workflows/linux-clang-build.yml +++ b/.github/workflows/linux-clang-build.yml @@ -98,3 +98,8 @@ jobs: run: | cmake --build --preset=unixlike-clang-debug-${{ matrix.type }} --target run-faker-cxx-basic-example \ && cmake --build --preset=unixlike-clang-debug-${{ matrix.type }} --target run-faker-cxx-person-example + + - name: Code coverage + uses: codecov/codecov-action@v4.0.1 + with: + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/linux-static-analysis.yml b/.github/workflows/linux-static-analysis.yml index 17ed80a9e..e2024afa7 100644 --- a/.github/workflows/linux-static-analysis.yml +++ b/.github/workflows/linux-static-analysis.yml @@ -81,16 +81,25 @@ jobs: -DCMAKE_TOOLCHAIN_FILE=build/Debug/generators/conan_toolchain.cmake \ -DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=lld - - name: Build + - name: Build library and tests run: cmake --build --preset=unixlike-clang-debug-static - - name: Generate code coverage + - name: Build code coverage + run: cmake --build --preset=unixlike-clang-debug-static --target=faker-ccov-all + + + - name: Generate code coverage report working-directory: ${{github.workspace}}/build/unixlike-clang-debug-static run: | - ninja faker-ccov-all \ - && llvm-cov-18 show `cat ccov/binaries.list` -instr-profile=ccov/all-merged.profdata > coverage.txt \ + llvm-cov-18 show `cat ccov/binaries.list` -instr-profile=ccov/all-merged.profdata > coverage.txt \ && llvm-cov-18 report `cat ccov/binaries.list` -instr-profile=ccov/all-merged.profdata + - name: Upload code coverage report to Codecov + uses: codecov/codecov-action@v4.0.1 + with: + token: ${{ secrets.CODECOV_TOKEN }} + directory: ${{github.workspace}}/build/unixlike-clang-debug-static + - name: Store build artifacts uses: actions/upload-artifact@v4 with: diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d1da5be7..0ee940c59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,10 +6,12 @@ project(faker-cxx HOMEPAGE_URL "https://github.com/cieslarmichal/faker-cxx") include(cmake/CompilerWarnings.cmake) +include(CMakeDependentOption) option(USE_SYSTEM_DEPENDENCIES "Use fmt and GTest from system" OFF) option(BUILD_EXAMPLES "Build examples" OFF) option(BUILD_TESTING "Build tests" ON) +option(CODE_COVERAGE "Build faker-cxx with coverage support" OFF) if (MSVC) set(CMAKE_REQUIRED_FLAGS /std:c++20) @@ -23,32 +25,32 @@ if (NOT HAS_STD_FORMAT) message(STATUS "Compiler has no support for std::format. Need to use fmt library as dependency.") endif() -include(CMakeDependentOption) cmake_dependent_option(USE_STD_FORMAT "Use std::format when available" ON "HAS_STD_FORMAT" OFF) -if (BUILD_TESTING) - enable_testing() - set(target_code_coverage_ALL 1) - include("cmake/cmake-coverage.cmake") - add_code_coverage_all_targets() -endif () - add_library(${CMAKE_PROJECT_NAME}) if (USE_SYSTEM_DEPENDENCIES) find_package(fmt CONFIG REQUIRED) elseif (NOT USE_STD_FORMAT) + add_subdirectory("${CMAKE_SOURCE_DIR}/externals/fmt") find_library(FMT_LIBRARY fmt) if (NOT FMT_LIBRARY) message(FATAL_ERROR "Could not find fmt library. Please, read the contribution guide.") endif() - add_subdirectory("${CMAKE_SOURCE_DIR}/externals/fmt") set(FMT_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/externals/fmt/include") endif () add_subdirectory(src) +if (CODE_COVERAGE) + set(target_code_coverage_ALL 1) + include("cmake/cmake-coverage.cmake") + add_code_coverage_all_targets(EXCLUDE tests/*) + target_code_coverage(${CMAKE_PROJECT_NAME} ALL) +endif () + if (BUILD_TESTING) + enable_testing() add_subdirectory(tests) endif () diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 19c69d9e3..3427e6125 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -83,4 +83,6 @@ add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_ set_tests_properties(${PROJECT_NAME} PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<$:$>") -target_code_coverage(${PROJECT_NAME} ALL) +if (CODE_COVERAGE) + target_code_coverage(${PROJECT_NAME} ALL) +endif () \ No newline at end of file