From 1c85e742ef7d5fc892c255397025b0d9b78a2d79 Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Sun, 4 Feb 2024 18:45:59 +0100 Subject: [PATCH 01/15] add some unit tests and code coverage --- .github/workflows/code_coverage.yml | 82 +++++++++++++++++++++++++++++ CMakeLists.txt | 39 ++++++++++++++ cmake/coverage.cmake | 40 ++++++++++++++ tests/test_circle.cpp | 54 +++++++++++++++++++ 4 files changed, 215 insertions(+) create mode 100644 .github/workflows/code_coverage.yml create mode 100644 cmake/coverage.cmake create mode 100644 tests/test_circle.cpp diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml new file mode 100644 index 0000000..d00ff23 --- /dev/null +++ b/.github/workflows/code_coverage.yml @@ -0,0 +1,82 @@ +name: CMake Build Ryzen Monitor ImGui + +on: + push: + branches: [ "main" ] + + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-22.04 +# 22 has gcc 11 and clang 14 + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install dependencies + run: | + sudo apt-get install -y cmake ninja-build \ + gcovr lcov + +# Do we need a very modern compiler? +# - name: Install clang18 +# run: | +# wget https://apt.llvm.org/llvm.sh +# chmod a+x llvm.sh +# echo ""|sudo ./llvm.sh 18 + + - name: Create build directory + run: | + #rm -rf build + mkdir build + du -hs build + +# This can be used to login to the runner and debug +# - name: Setup tmate session +# uses: mxschmitt/action-tmate@v3 + + - name: Configure CMake + run: | + # The following settings are needed for clang-18 + #export CC=/usr/bin/clang-18 + #export CXX=/usr/bin/clang++-18 + #export CXXFLAGS="-std=c++20" + cmake -S . -B build -G Ninja \ + -DCMAKE_BUILD_TYPE=Debug \ + -DENABLE_GEOM2D_TESTS=ON \ + -DBUILD_GMOCK=OFF \ + -DINSTALL_GTEST=OFF + + - name: Build with Ninja + run: ninja -C build + + - name: Run tests + run: | + ninja -C build test + + - name: Collect coverage + run: | + ninja -C build cov + lcov --list build/coverage.info + + - name: Generate JSON coverage report + working-directory: ./build + run: | + gcovr -r .. . --branches --cobertura > coverage.xml + + - name: Upload build artifact + uses: actions/upload-artifact@v3 + with: + name: TEST_Geometry2D_linux_amd64_artifact1 + path: build/bin/TEST_Geometry2D + + # - name: Upload Release Asset + # uses: actions/upload-release-asset@v1 + # env: + # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # with: + # upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing its `id` + # asset_path: ./build/ryzen_mon_glgui + # asset_name: ryzen_mon_glgui_artifact1.zip # Name of the asset to display on the release page + # asset_content_type: application/zip \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index f093044..887d9b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -175,3 +175,42 @@ if (EXISTS "${CMAKE_SOURCE_DIR}/${C_CXX_HEADERS_DIR}" AND IS_DIRECTORY "${CMAKE_ endif (EXISTS "${CMAKE_SOURCE_DIR}/${C_CXX_HEADERS_DIR}" AND IS_DIRECTORY "${CMAKE_SOURCE_DIR}/${C_CXX_HEADERS_DIR}") include_directories("${CMAKE_SOURCE_DIR}/third_party") + + +# Define an option to enable/disable unit tests +option(ENABLE_GEOM2D_UNIT_TESTS "Enable unit tests for /olcUTIL_Geometry2D" OFF) + +# unit tests +# I use this with cmake -G Ninja -DBUILD_GMOCK=OFF -DINSTALL_GTEST=OFF -DENABLE_GEOM2D_UNIT_TESTS=ON .. +if(ENABLE_GEOM2D_UNIT_TESTS) + enable_testing() + + # Code Coverage + set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${PROJECT_SOURCE_DIR}/cmake") + include(coverage) + add_coverage_target("*/tests/*") + + # Fetch Google Test + include(FetchContent) + FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG v1.14.0 + ) + FetchContent_MakeAvailable(googletest) + + add_executable(unit_tests + tests/test_circle.cpp # TODO: use cmake to find all test files + ) + + # Disable optimization for coverage + # TODO: only tested with gcc + target_compile_options(unit_tests PRIVATE + -O0 -fprofile-arcs -ftest-coverage --coverage + ) + target_link_options(unit_tests PRIVATE --coverage) + + target_link_libraries(unit_tests gtest_main) + + add_test(NAME unit_tests COMMAND unit_tests) +endif() diff --git a/cmake/coverage.cmake b/cmake/coverage.cmake new file mode 100644 index 0000000..d8475b1 --- /dev/null +++ b/cmake/coverage.cmake @@ -0,0 +1,40 @@ +function(add_coverage_target exclude) + + find_program(GCOV gcov) + if (NOT GCOV) + message(WARNING "program gcov not found") + endif() + + find_program(LCOV lcov) + if (NOT LCOV) + message(WARNING "program lcov not found") + endif() + + find_program(GENHTML genhtml) + if (NOT GENHTML) + message(WARNING "program genhtml not found") + endif() + + if (LCOV AND GCOV AND GENHTML) + set(covname cov.info) + # the original file set all compile options here, but I prefer to set them in the CMakeLists.txt + #add_compile_options(-fprofile-arcs -ftest-coverage) + #add_link_options(--coverage) + add_custom_target(cov DEPENDS ${covname}) + add_custom_command( + OUTPUT ${covname} + COMMAND ${LCOV} -c -o ${covname} -d . -b . --gcov-tool ${GCOV} --ignore-errors mismatch + # In the following I explicitly exclude system headers from the coverage report + COMMAND ${LCOV} -r ${covname} -o ${covname} ${exclude} "\*/googletest/\*" "\*/g++\*/bits/\*" "'*/g++-v13/*'" + COMMAND ${LCOV} -l ${covname} + COMMAND ${GENHTML} ${covname} -output coverage + COMMAND ${LCOV} -l ${covname} 2>/dev/null | grep Total | sed 's/|//g' | sed 's/Total://g' | awk '{print $1}' | sed s/%//g > coverage/total + ) + set_directory_properties(PROPERTIES + ADDITIONAL_CLEAN_FILES ${covname} + ) + else() + message(WARNING "unable to add target `cov`: missing coverage tools") + endif() + +endfunction() \ No newline at end of file diff --git a/tests/test_circle.cpp b/tests/test_circle.cpp new file mode 100644 index 0000000..b0b0cac --- /dev/null +++ b/tests/test_circle.cpp @@ -0,0 +1,54 @@ +#include +#include "../olcUTIL_Geometry2D.h" + +using namespace olc::utils::geom2d; + +class CircleTest : public ::testing::Test +{ +protected: + void SetUp() override + { + // Initialize the circle + c = circle({{0.0f, 0.0f}, 10.0f}); + } + + circle c; +}; + +// The test name consists of the module that is tested, the scenario under which it is tested and the expected behavior + +TEST_F(CircleTest, circle_contains_PointOnCircleBoundary_True) +{ + // Create a point on the circle boundary + olc::v_2d p(0.0f, 10.0f); + + // Check if the circle contains the point + bool result = contains(c, p); + + // Assert that the result is true + ASSERT_TRUE(result); +} + +TEST_F(CircleTest, circleContains_PointIsInside_True) +{ + // Create a point inside the circle + olc::v_2d p(1.0f, 1.0f); + + // Check if the circle contains the point + bool result = contains(c, p); + + // Assert that the result is true + ASSERT_TRUE(result); +} + +TEST_F(CircleTest, circleContains_PointIsOutside_False) +{ + // Create a point outside the circle + olc::v_2d p(12.0f, 12.0f); + + // Check if the circle contains the point + bool result = contains(c, p); + + // Assert that the result is false + ASSERT_FALSE(result); +} From b257415e536f9870be7be23db7e21691935217f0 Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Sun, 4 Feb 2024 18:46:22 +0100 Subject: [PATCH 02/15] a comment i forgot to save --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 887d9b1..7d9cacf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -188,6 +188,7 @@ if(ENABLE_GEOM2D_UNIT_TESTS) # Code Coverage set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${PROJECT_SOURCE_DIR}/cmake") include(coverage) + # the following pattern excludes all tests from coverage report add_coverage_target("*/tests/*") # Fetch Google Test From 8bef80710ea9a8698987b5cf38a1a7e2ae59506d Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Sun, 4 Feb 2024 18:48:11 +0100 Subject: [PATCH 03/15] yml has wrong name --- .github/workflows/code_coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index d00ff23..bde8bb5 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -1,4 +1,4 @@ -name: CMake Build Ryzen Monitor ImGui +name: Unit Tests and Code Coverage on: push: From 6c530fc3b3ad8fc5209d4d4223ee9df6f03b4cc0 Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Sun, 4 Feb 2024 18:52:23 +0100 Subject: [PATCH 04/15] comment int cmake --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d9cacf..93f7e8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -181,7 +181,7 @@ include_directories("${CMAKE_SOURCE_DIR}/third_party") option(ENABLE_GEOM2D_UNIT_TESTS "Enable unit tests for /olcUTIL_Geometry2D" OFF) # unit tests -# I use this with cmake -G Ninja -DBUILD_GMOCK=OFF -DINSTALL_GTEST=OFF -DENABLE_GEOM2D_UNIT_TESTS=ON .. +# I use this with: cmake -G Ninja -DBUILD_GMOCK=OFF -DINSTALL_GTEST=OFF -DENABLE_GEOM2D_UNIT_TESTS=ON .. if(ENABLE_GEOM2D_UNIT_TESTS) enable_testing() From 912ed3a0a9f446aa3d1271120db81231e1ccb2ce Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Sun, 4 Feb 2024 18:54:33 +0100 Subject: [PATCH 05/15] cmake doesnt configure on github build agent -- Detecting CXX compile features - done 24 CMake Error at /usr/local/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:230 (message): 25 Could NOT find OpenGL (missing: OPENGL_opengl_LIBRARY OPENGL_glx_LIBRARY 26 OPENGL_INCLUDE_DIR) 27 Call Stack (most recent call first): 28 /usr/local/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE) 29 /usr/local/share/cmake-3.28/Modules/FindOpenGL.cmake:545 (FIND_PACKAGE_HANDLE_STANDARD_ARGS) 30 CMakeLists.txt:45 (find_package) 31 -- Configuring incomplete, errors occurred! 32 Error: Process completed with exit code 1. Build with Ninja --- .github/workflows/code_coverage.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index bde8bb5..efba112 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -17,6 +17,8 @@ jobs: - name: Install dependencies run: | sudo apt-get install -y cmake ninja-build \ + libxrandr-dev libxinerama-dev \ + libxcursor-dev libxi-dev libgl1-mesa-dev \ gcovr lcov # Do we need a very modern compiler? From d0493e4b49d99931a0d00080c0c0a0bddea69654 Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Sun, 4 Feb 2024 18:57:01 +0100 Subject: [PATCH 06/15] unit tests were not on 0s 1 Run ninja -C build test 2 ninja -C build test 3 shell: /usr/bin/bash -e {0} 4 ninja: Entering directory `build' 5 ninja: error: unknown target 'test', did you mean 'help'? 6 Error: Process completed with exit code 1. Collect coverage --- .github/workflows/code_coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index efba112..591e671 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -46,7 +46,7 @@ jobs: #export CXXFLAGS="-std=c++20" cmake -S . -B build -G Ninja \ -DCMAKE_BUILD_TYPE=Debug \ - -DENABLE_GEOM2D_TESTS=ON \ + -DENABLE_GEOM2D_UNIT_TESTS=ON \ -DBUILD_GMOCK=OFF \ -DINSTALL_GTEST=OFF From 37764c8847044f9324e4f4c0c3becf37bc9fc1b4 Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Sun, 4 Feb 2024 18:59:16 +0100 Subject: [PATCH 07/15] try to fix this error on build agent: Run ninja -C build cov 2 ninja -C build cov 3 lcov --list build/coverage.info 4 shell: /usr/bin/bash -e {0} 5 ninja: Entering directory `build' 6 [1/1] Generating cov.info 7 FAILED: cov.info /home/runner/work/olcUTIL_Geometry2D/olcUTIL_Geometry2D/build/cov.info 8 cd /home/runner/work/olcUTIL_Geometry2D/olcUTIL_Geometry2D/build && /usr/bin/lcov -c -o cov.info -d . -b . --gcov-tool /usr/bin/gcov --ignore-errors mismatch && /usr/bin/lcov -r cov.info -o cov.info */tests/* */googletest/* */g++*/bits/* '*/g++-v13/*' && /usr/bin/lcov -l cov.info && /usr/bin/genhtml cov.info -output coverage && /usr/bin/lcov -l cov.info 2>/dev/null | grep Total | sed 's/|//g' | sed 's/Total://g' | awk '{print }' | sed s/%//g > coverage/total 9 Capturing coverage data from . 10 Subroutine read_intermediate_text redefined at /usr/bin/geninfo line 2623. 11 Subroutine read_intermediate_json redefined at /usr/bin/geninfo line 2655. 12 Subroutine intermediate_text_to_info redefined at /usr/bin/geninfo line 2703. 13 Subroutine intermediate_json_to_info redefined at /usr/bin/geninfo line 2792. 14 Subroutine get_output_fd redefined at /usr/bin/geninfo line 2872. 15 Subroutine print_gcov_warnings redefined at /usr/bin/geninfo line 2900. 16 Subroutine process_intermediate redefined at /usr/bin/geninfo line 2930. 17 Found gcov version: 11.4.0 18 geninfo: ERROR: unknown argument for --ignore-errors: mismatch 19 Using intermediate gcov format 20 ninja: build stopped: subcommand failed. 21 Error: Process completed with exit code 1. Generate JSON coverage report --- .github/workflows/code_coverage.yml | 1 - cmake/coverage.cmake | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index 591e671..6718e52 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -30,7 +30,6 @@ jobs: - name: Create build directory run: | - #rm -rf build mkdir build du -hs build diff --git a/cmake/coverage.cmake b/cmake/coverage.cmake index d8475b1..7ef218b 100644 --- a/cmake/coverage.cmake +++ b/cmake/coverage.cmake @@ -23,7 +23,7 @@ function(add_coverage_target exclude) add_custom_target(cov DEPENDS ${covname}) add_custom_command( OUTPUT ${covname} - COMMAND ${LCOV} -c -o ${covname} -d . -b . --gcov-tool ${GCOV} --ignore-errors mismatch + COMMAND ${LCOV} -c -o ${covname} -d . -b . --gcov-tool ${GCOV} #--ignore-errors mismatch # In the following I explicitly exclude system headers from the coverage report COMMAND ${LCOV} -r ${covname} -o ${covname} ${exclude} "\*/googletest/\*" "\*/g++\*/bits/\*" "'*/g++-v13/*'" COMMAND ${LCOV} -l ${covname} From 237c15b8992177c46db2e01f9d5ec1da4be4d0fb Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Sun, 4 Feb 2024 19:01:19 +0100 Subject: [PATCH 08/15] lcov command was wrong lcov: ERROR: cannot read file build/coverage.info! 85 Reading tracefile build/coverage.info 86 Error: Process completed with exit code 9. Generate JSON coverage report --- .github/workflows/code_coverage.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index 6718e52..d0ccd5c 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -59,7 +59,6 @@ jobs: - name: Collect coverage run: | ninja -C build cov - lcov --list build/coverage.info - name: Generate JSON coverage report working-directory: ./build From ada205d490ece53c1da62732dbc21f2e44eaedf2 Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Sun, 4 Feb 2024 19:04:56 +0100 Subject: [PATCH 09/15] publish code coverage --- .github/workflows/code_coverage.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index d0ccd5c..f375068 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -71,6 +71,13 @@ jobs: name: TEST_Geometry2D_linux_amd64_artifact1 path: build/bin/TEST_Geometry2D + - name: Publish coverage artifact + uses: actions/upload-artifact@v3 + with: + name: code-coverage + path: build/coverage + + # - name: Upload Release Asset # uses: actions/upload-release-asset@v1 # env: From ac69af9f0a7c485f88ef232c57cb37a32364fb92 Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Sun, 4 Feb 2024 19:07:43 +0100 Subject: [PATCH 10/15] comment --- .github/workflows/code_coverage.yml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index f375068..351b8a4 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -60,10 +60,13 @@ jobs: run: | ninja -C build cov - - name: Generate JSON coverage report - working-directory: ./build - run: | - gcovr -r .. . --branches --cobertura > coverage.xml +# The following article describes how to publish code coverage to codecov.io: +# https://dev.to/askrodney/cmake-coverage-example-with-github-actions-and-codecovio-5bjp +# I can't get it to work, though. We can just download the html, for now. +# - name: Generate JSON coverage report +# working-directory: ./build +# run: | +# gcovr -r .. . --branches --cobertura > coverage.xml - name: Upload build artifact uses: actions/upload-artifact@v3 From 9376605c37d0c20adb920ce8106c70f4d94d0a3e Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Sun, 4 Feb 2024 19:28:08 +0100 Subject: [PATCH 11/15] pesky warning about Node 16 and Node 20 --- .github/workflows/code_coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index 351b8a4..81daa4e 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -12,7 +12,7 @@ jobs: # 22 has gcc 11 and clang 14 steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Install dependencies run: | From 9a85748dd6b071d913cb80bbb9c4b10da01c9f1a Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Mon, 5 Feb 2024 20:59:47 +0100 Subject: [PATCH 12/15] enable branch-coverage function and line coverage is 100% with only 3 tests --- cmake/coverage.cmake | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmake/coverage.cmake b/cmake/coverage.cmake index 7ef218b..bb3300a 100644 --- a/cmake/coverage.cmake +++ b/cmake/coverage.cmake @@ -23,12 +23,12 @@ function(add_coverage_target exclude) add_custom_target(cov DEPENDS ${covname}) add_custom_command( OUTPUT ${covname} - COMMAND ${LCOV} -c -o ${covname} -d . -b . --gcov-tool ${GCOV} #--ignore-errors mismatch + COMMAND ${LCOV} -c -o ${covname} --branch-coverage -d . -b . --gcov-tool ${GCOV} --ignore-errors mismatch # In the following I explicitly exclude system headers from the coverage report - COMMAND ${LCOV} -r ${covname} -o ${covname} ${exclude} "\*/googletest/\*" "\*/g++\*/bits/\*" "'*/g++-v13/*'" - COMMAND ${LCOV} -l ${covname} - COMMAND ${GENHTML} ${covname} -output coverage - COMMAND ${LCOV} -l ${covname} 2>/dev/null | grep Total | sed 's/|//g' | sed 's/Total://g' | awk '{print $1}' | sed s/%//g > coverage/total + COMMAND ${LCOV} -r ${covname} --branch-coverage -o ${covname} ${exclude} "\*/googletest/\*" "\*/g++\*/bits/\*" "'*/g++-v13/*'" + COMMAND ${LCOV} -l ${covname} --branch-coverage + COMMAND ${GENHTML} ${covname} --branch-coverage -output coverage + COMMAND ${LCOV} -l ${covname} --branch-coverage 2>/dev/null | grep Total | sed 's/|//g' | sed 's/Total://g' | awk '{print $1}' | sed s/%//g > coverage/total ) set_directory_properties(PROPERTIES ADDITIONAL_CLEAN_FILES ${covname} From 98fb42cabfac750f846bfc2adffec27d7112daa2 Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Mon, 5 Feb 2024 21:05:09 +0100 Subject: [PATCH 13/15] lcov on ubuntu 22 doesn't support new options --- cmake/coverage.cmake | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cmake/coverage.cmake b/cmake/coverage.cmake index bb3300a..5a03eec 100644 --- a/cmake/coverage.cmake +++ b/cmake/coverage.cmake @@ -21,14 +21,20 @@ function(add_coverage_target exclude) #add_compile_options(-fprofile-arcs -ftest-coverage) #add_link_options(--coverage) add_custom_target(cov DEPENDS ${covname}) + + # lcov on ubuntu 22 doesn't support --branch-coverage + set(COV_OPT + #"--branch-coverage" + "--rc lcov_branch_coverage=1" + ) add_custom_command( OUTPUT ${covname} - COMMAND ${LCOV} -c -o ${covname} --branch-coverage -d . -b . --gcov-tool ${GCOV} --ignore-errors mismatch + COMMAND ${LCOV} -c -o ${covname} ${COV_OPT} -d . -b . --gcov-tool ${GCOV} --ignore-errors mismatch # In the following I explicitly exclude system headers from the coverage report - COMMAND ${LCOV} -r ${covname} --branch-coverage -o ${covname} ${exclude} "\*/googletest/\*" "\*/g++\*/bits/\*" "'*/g++-v13/*'" - COMMAND ${LCOV} -l ${covname} --branch-coverage + COMMAND ${LCOV} -r ${covname} ${COV_OPT} -o ${covname} ${exclude} "\*/googletest/\*" "\*/g++\*/bits/\*" "'*/g++-v13/*'" + COMMAND ${LCOV} -l ${covname} ${COV_OPT} COMMAND ${GENHTML} ${covname} --branch-coverage -output coverage - COMMAND ${LCOV} -l ${covname} --branch-coverage 2>/dev/null | grep Total | sed 's/|//g' | sed 's/Total://g' | awk '{print $1}' | sed s/%//g > coverage/total + COMMAND ${LCOV} -l ${covname} ${COV_OPT} 2>/dev/null | grep Total | sed 's/|//g' | sed 's/Total://g' | awk '{print $1}' | sed s/%//g > coverage/total ) set_directory_properties(PROPERTIES ADDITIONAL_CLEAN_FILES ${covname} From 4ad6f7abd19b14daa4c245c7d6ef10916f4bb852 Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Mon, 5 Feb 2024 21:14:23 +0100 Subject: [PATCH 14/15] is branch coverage working on ubuntu now? --- .github/workflows/code_coverage.yml | 5 +++++ cmake/coverage.cmake | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index 81daa4e..257af46 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -56,6 +56,11 @@ jobs: run: | ninja -C build test + - name: Configure lcov to capture branch coverage + run: | + cp /etc/lcovrc /dev/shm + cat /dev/shm/lcovrc |sed 's/branch_coverage = 0/branch_coverage = 1/g' > /etc/lcovrc + - name: Collect coverage run: | ninja -C build cov diff --git a/cmake/coverage.cmake b/cmake/coverage.cmake index 5a03eec..26e1114 100644 --- a/cmake/coverage.cmake +++ b/cmake/coverage.cmake @@ -24,16 +24,19 @@ function(add_coverage_target exclude) # lcov on ubuntu 22 doesn't support --branch-coverage set(COV_OPT + "" # i modify /etc/lcovrc in the github action #"--branch-coverage" - "--rc lcov_branch_coverage=1" + #"--rc lcov_branch_coverage=1" # this doesn't work with -l option ) + # option for genhtml --rc genhtml_branch_coverage=1 + add_custom_command( OUTPUT ${covname} - COMMAND ${LCOV} -c -o ${covname} ${COV_OPT} -d . -b . --gcov-tool ${GCOV} --ignore-errors mismatch + COMMAND ${LCOV} -c -o ${covname} ${COV_OPT} -d . -b . --gcov-tool ${GCOV} #--ignore-errors mismatch # In the following I explicitly exclude system headers from the coverage report COMMAND ${LCOV} -r ${covname} ${COV_OPT} -o ${covname} ${exclude} "\*/googletest/\*" "\*/g++\*/bits/\*" "'*/g++-v13/*'" COMMAND ${LCOV} -l ${covname} ${COV_OPT} - COMMAND ${GENHTML} ${covname} --branch-coverage -output coverage + COMMAND ${GENHTML} ${covname} --rc lcov_branch_coverage=1 -output coverage COMMAND ${LCOV} -l ${covname} ${COV_OPT} 2>/dev/null | grep Total | sed 's/|//g' | sed 's/Total://g' | awk '{print $1}' | sed s/%//g > coverage/total ) set_directory_properties(PROPERTIES From 1b1c903d8b742c9cb2fcd8d8d81dedd73e4b1c09 Mon Sep 17 00:00:00 2001 From: Martin Kielhorn Date: Mon, 5 Feb 2024 21:18:07 +0100 Subject: [PATCH 15/15] unit testing header only libraries takes effort --- .github/workflows/code_coverage.yml | 2 +- cmake/coverage.cmake | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index 257af46..bf657d6 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -59,7 +59,7 @@ jobs: - name: Configure lcov to capture branch coverage run: | cp /etc/lcovrc /dev/shm - cat /dev/shm/lcovrc |sed 's/branch_coverage = 0/branch_coverage = 1/g' > /etc/lcovrc + cat /dev/shm/lcovrc |sed 's/branch_coverage = 0/branch_coverage = 1/g' > ~/.lcovrc - name: Collect coverage run: | diff --git a/cmake/coverage.cmake b/cmake/coverage.cmake index 26e1114..8ecfd61 100644 --- a/cmake/coverage.cmake +++ b/cmake/coverage.cmake @@ -16,6 +16,8 @@ function(add_coverage_target exclude) endif() if (LCOV AND GCOV AND GENHTML) + # the following link describes how to write unit tests for header only libraries + # https://stackoverflow.com/questions/9666800/getting-useful-gcov-results-for-header-only-libraries set(covname cov.info) # the original file set all compile options here, but I prefer to set them in the CMakeLists.txt #add_compile_options(-fprofile-arcs -ftest-coverage)