0

I have a c++ project that uses CMake for generating makefiles. I have added a custom target to run the code coverage as follows:

    add_custom_target(
      coverage
      COMMAND
        lcov --rc lcov_branch_coverage=1 --capture --directory
        ${CMAKE_SOURCE_DIR} --output-file
        ${CMAKE_BINARY_DIR}/${PROJECT_NAME}_coverage.info --no-external
      COMMAND
        genhtml --branch-coverage
        ${CMAKE_BINARY_DIR}/${PROJECT_NAME}_coverage.info --output-directory
        ${CMAKE_BINARY_DIR}/coverage_report --title
        "${PROJECT_NAME} code coverage report" --show-details --legend
      COMMAND
        echo
        "Covverage report in ${CMAKE_BINARY_DIR}/coverage_report/index.html")

(earlier in the project I set the right compilation and linking flags and add different tests using add_test). When I run make, make test, make coverage I get the code coverage report properly. Now I am integrating this into a CI/CD and I want to check the coverage % of the resulting code coverage analysis to be above a threshold. If it is not above I want to fail the pipeline. Is there a way to run some process that will exit with an error code if the coverage is not above a given threshold?

apalomer
  • 1,895
  • 14
  • 36
  • Not sure what the format of the `.info` file is, but it it's a text file with reasonably simple structure, you may be able to use a cmake script file to check the values and simply use `message(FATAL_ERROR ...)` in that script, if it's to low... – fabian Dec 01 '22 at 08:45

1 Answers1

0

Changing the coverage from lcov to gcovr solves this issue. gcovr has --fail-under-line <min> and --fail-under-branch <min> options that will make the program exit with an error code (2 and 4 respectively) if the min is not reached (min is in the range 0-100).

apalomer
  • 1,895
  • 14
  • 36