diff --git a/.github/workflows/docs.yaml b/.github/workflows/docs.yaml index 8307c7a..b47098f 100644 --- a/.github/workflows/docs.yaml +++ b/.github/workflows/docs.yaml @@ -21,7 +21,7 @@ jobs: cache-downloads: true - name: Configure using CMake - run: cmake -G Ninja -Bbuild -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBUILD_DOCS=ON + run: cmake -G Ninja -Bbuild -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DSPARROW_IPC_BUILD_DOCS=ON - name: Build docs target run: cmake --build build --target docs diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 73768b2..7b043c0 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -32,7 +32,6 @@ jobs: - name: Configure using cmake run: | cmake -S ./ -B ./build \ - -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \ -DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX% \ -DCMAKE_PREFIX_PATH=%CONDA_PREFIX% \ -DSPARROW_IPC_BUILD_SHARED=${{ matrix.build_shared }} \ @@ -40,16 +39,16 @@ jobs: - name: Build sparrow-ipc working-directory: build - run: cmake --build . --target sparrow-ipc + run: cmake --build . --config ${{ matrix.build_type }} --target sparrow-ipc - name: Build tests working-directory: build - run: cmake --build . --target test_sparrow_ipc_lib + run: cmake --build . --config ${{ matrix.build_type }} --target test_sparrow_ipc_lib - name: Run tests working-directory: build run: | - cmake --build . --target run_tests + cmake --build . --config ${{ matrix.build_type }} --target run_tests_with_junit_report windows_build_fetch_from_source: runs-on: windows-latest @@ -61,22 +60,45 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 + - name: Enable tests coverage + if: matrix.build_type == 'Debug' + run: | + choco install opencppcoverage + echo "TEST_COVERAGE_ACTIVATION=-DSPARROW_IPC_ENABLE_COVERAGE=ON" >> $GITHUB_ENV + - name: Configure using cmake run: | cmake -S ./ -B ./build \ - -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \ -DSPARROW_IPC_BUILD_SHARED=${{ matrix.build_shared }} \ -DSPARROW_IPC_BUILD_TESTS=ON \ - -DFETCH_DEPENDENCIES_WITH_CMAKE=MISSING + -DFETCH_DEPENDENCIES_WITH_CMAKE=MISSING \ + $TEST_COVERAGE_ACTIVATION - name: Build sparrow-ipc working-directory: build - run: cmake --build . --target sparrow-ipc + run: cmake --build . --config ${{ matrix.build_type }} --target sparrow-ipc - name: Build tests working-directory: build - run: cmake --build . --target test_sparrow_ipc_lib + run: cmake --build . --config ${{ matrix.build_type }} --target test_sparrow_ipc_lib - name: Run tests working-directory: build - run: cmake --build . --target run_tests_with_junit_report + run: cmake --build . --config ${{ matrix.build_type }} --target run_tests_with_junit_report + + - name: Tests coverage + if: matrix.build_type == 'Debug' + working-directory: build + run: | + cmake --build . --config Debug --target sparrow_ipc_generate_cobertura + + - name: Upload coverage to Codecov + if: matrix.build_type == 'Debug' + uses: codecov/codecov-action@v5 + with: + directory: ./build/coverage_reports/ + fail_ci_if_error: true + files: ./cobertura.xml + flags: unittests + token: ${{ secrets.CODECOV_TOKEN }} + verbose: true diff --git a/CMakeLists.txt b/CMakeLists.txt index a30fa81..153ae70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,8 +44,17 @@ endif() OPTION(SPARROW_IPC_BUILD_TESTS "Build sparrow-ipc test suite" OFF) MESSAGE(STATUS "🔧 Build tests: ${SPARROW_IPC_BUILD_TESTS}") -OPTION(BUILD_DOCS "Build sparrow-ipc documentation" OFF) -MESSAGE(STATUS "🔧 Build docs: ${BUILD_DOCS}") +OPTION(SPARROW_IPC_BUILD_DOCS "Build sparrow-ipc documentation" OFF) +MESSAGE(STATUS "🔧 Build docs: ${SPARROW_IPC_BUILD_DOCS}") + +# Code coverage +# ============= +OPTION(SPARROW_IPC_ENABLE_COVERAGE "Enable sparrow-ipc test coverage" OFF) +MESSAGE(STATUS "🔧 Enable coverage: ${SPARROW_IPC_ENABLE_COVERAGE}") + +if(SPARROW_IPC_ENABLE_COVERAGE) + include(code_coverage) +endif() set(SPARROW_IPC_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) set(SPARROW_IPC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) @@ -166,7 +175,7 @@ endif() # Docs # ==== -if(BUILD_DOCS) +if(SPARROW_IPC_BUILD_DOCS) message(STATUS "📚 Create docs targets") add_subdirectory(docs) endif() diff --git a/cmake/code_coverage.cmake b/cmake/code_coverage.cmake new file mode 100644 index 0000000..c05d8a4 --- /dev/null +++ b/cmake/code_coverage.cmake @@ -0,0 +1,35 @@ +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") + message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading") +endif() + +set(COVERAGE_REPORT_PATH "${CMAKE_BINARY_DIR}/coverage_reports" CACHE PATH "Path to store coverage reports") +set(COBERTURA_REPORT_PATH "${COVERAGE_REPORT_PATH}/cobertura.xml" CACHE PATH "Path to store cobertura report") +set(COVERAGE_TARGETS_FOLDER "Tests utilities/Code Coverage") + +if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + find_program(OpenCPPCoverage OpenCppCoverage.exe opencppcoverage.exe REQUIRED + PATHS "C:/Program Files/OpenCppCoverage" "C:/Program Files (x86)/OpenCppCoverage") + + cmake_path(CONVERT ${CMAKE_SOURCE_DIR} TO_NATIVE_PATH_LIST OPENCPPCOVERAGE_SOURCES) + set(OPENCPPCOVERAGE_COMMON_ARGS --sources=${OPENCPPCOVERAGE_SOURCES} --modules=${OPENCPPCOVERAGE_SOURCES} --excluded_sources=test*) + + add_custom_target(sparrow_ipc_generate_cobertura + COMMAND ${OpenCPPCoverage} + ${OPENCPPCOVERAGE_COMMON_ARGS} + --export_type=cobertura:${COBERTURA_REPORT_PATH} + -- $ + DEPENDS test_sparrow_ipc_lib + COMMENT "Generating coverage cobertura report with OpenCppCoverage: ${COBERTURA_REPORT_PATH}" + ) + set(TARGET_PROPERTIES sparrow_ipc_generate_cobertura PROPERTIES FOLDER ${COVERAGE_TARGETS_FOLDER}) + + add_custom_target(sparrow_ipc_generate_html_coverage_report + COMMAND ${OpenCPPCoverage} + ${OPENCPPCOVERAGE_COMMON_ARGS} + --export_type=html:${COVERAGE_REPORT_PATH} + -- $ + DEPENDS test_sparrow_ipc_lib + COMMENT "Generating coverage report with OpenCppCoverage: ${COVERAGE_REPORT_PATH}" + ) + set(TARGET_PROPERTIES sparrow_ipc_generate_cobertura PROPERTIES FOLDER "Tests utilities/Code Coverage") +endif() diff --git a/conanfile.py b/conanfile.py index fc1ce51..59916f8 100644 --- a/conanfile.py +++ b/conanfile.py @@ -88,7 +88,7 @@ def generate(self): tc = CMakeToolchain(self) tc.variables["SPARROW_IPC_BUILD_SHARED"] = self.options.shared tc.variables["SPARROW_IPC_BUILD_TESTS"] = self.options.build_tests - tc.variables["BUILD_DOCS"] = self.options.generate_documentation + tc.variables["SPARROW_IPC_BUILD_DOCS"] = self.options.generate_documentation tc.generate() def build(self): diff --git a/docs/source/dev_build.md b/docs/source/dev_build.md index 5daac90..4916fa1 100644 --- a/docs/source/dev_build.md +++ b/docs/source/dev_build.md @@ -7,9 +7,10 @@ List of CMake options: - `ACTIVATE_LINTER`: Create targets to run clang-format and clang-tidy (default: OFF) - `ACTIVATE_LINTER_DURING_COMPILATION`: Run linter during the compilation (default: OFF), requires `ACTIVATE_LINTER` to be ON -- `BUILD_DOCS`: Build the documentation (default: OFF) +- `SPARROW_IPC_BUILD_DOCS`: Build the documentation (default: OFF) - `SPARROW_IPC_BUILD_TESTS`: Build the tests (default: OFF) - `SPARROW_IPC_BUILD_SHARED`: Build sparrow-ipc as a shared library (default: ON) +- `SPARROW_IPC_ENABLE_COVERAGE`: Enable coverage reporting (default: OFF) ## Building