diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index edcbb3bb5..0f3f30b3a 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -30,7 +30,7 @@ jobs: cmake .. -DBUILD_TESTS=ON -DCOVERAGE=ON -DCMAKE_BUILD_TYPE=Debug - name: make - run: make jwt-cpp-test coverage + run: cd build && make jwt-cpp-test coverage - name: Coveralls GitHub Action uses: coverallsapp/github-action@v1.1.1 diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e7e45931..136056db3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ if(CMAKE_TOOLCHAIN_FILE) endif() option(BUILD_TESTS "Configure CMake to build tests (or not)" ON) +option(COVERAGE "Enable code coverage testing" OFF) option(EXTERNAL_PICOJSON "Use find_package() to locate the picojson header" OFF) option(DISABLE_JWT_CPP_BASE64 "Do not include the base64 implementation from this library" OFF) @@ -16,6 +17,8 @@ project(jwt-cpp) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") + find_package(OpenSSL 1.0.2 REQUIRED) if(EXTERNAL_PICOJSON) diff --git a/cmake/code-coverage.cmake b/cmake/code-coverage.cmake new file mode 100644 index 000000000..fd7da29ca --- /dev/null +++ b/cmake/code-coverage.cmake @@ -0,0 +1,15 @@ +set(COVERAGE_CMAKE "${CMAKE_BINARY_DIR}/cmake/CodeCoverage.cmake") +if(NOT EXISTS ${COVERAGE_CMAKE}) + set(COVERAGE_URL + "https://raw.githubusercontent.com/bilke/cmake-modules/master/CodeCoverage.cmake" + ) + file(DOWNLOAD ${COVERAGE_URL} ${COVERAGE_CMAKE}) +endif() + +include(${COVERAGE_CMAKE}) + +function(setup_coverage TARGET) + target_compile_options(${TARGET} PRIVATE -g -O0 -fprofile-arcs + -ftest-coverage) + target_link_libraries(${TARGET} PRIVATE gcov) +endfunction() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c0060b9d8..22e3b6f1f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -35,3 +35,12 @@ target_link_libraries(jwt-cpp-test PRIVATE jwt-cpp::jwt-cpp GTest::GTest GTest::Main pthread) gtest_add_tests(TARGET jwt-cpp-test) + +if(${COVERAGE}) + include("code-coverage") + setup_coverage(jwt-cpp-test) + + set(COVERAGE_EXCLUDES "/usr/**" "/home/*/.conan/**" "*test*" "*build*" "*json*") + setup_target_for_coverage_lcov(NAME coverage EXECUTABLE + ${CMAKE_CURRENT_BINARY_DIR}/jwt-cpp-test) +endif()