Skip to content

Commit

Permalink
Merge pull request #33 from Adamska1008/8-add-code-coverage-support
Browse files Browse the repository at this point in the history
8 add code coverage support
  • Loading branch information
Adamska1008 authored Sep 7, 2024
2 parents 667b1b6 + 2dd55d3 commit b0b1f02
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
24 changes: 22 additions & 2 deletions .github/workflows/cmake-single-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Install lcov
run: sudo apt-get install lcov

- name: Initialize Git Submodules
run: |
git submodule update --init
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DENABLE_COVERAGE=ON

- name: Build
# Build your program with the given configuration
Expand All @@ -40,7 +43,24 @@ jobs:
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest -C ${{env.BUILD_TYPE}}


- name: Coverage Info
working-directory: ${{github.workspace}}/build/
run: |
lcov --directory . --capture --output-file coverage.info
lcov --remove coverage.info '/usr/*' --output-file coverage.info
lcov --remove coverage.info '*/deps/*' --output-file coverage.info
lcov --remove coverage.info '*/tests/*' --output-file coverage.info
lcov --list coverage.info
- name: Codecov
uses: codecov/codecov-action@v3
with:
token : ${{ secrets.CODECOV_TOKEN }}
files: ${{github.workspace}}/build/coverage.info
flags: unittests
name: codecov-unittests

install:
runs-on: ubuntu-latest

Expand Down
17 changes: 15 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,31 @@ endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED on)

option(BUILD_TESTING "Build the testing tree." ON)
option(ENABLE_COVERAGE "Enable coverage reporting" ON)

# Code Coverage
if (ENABLE_COVERAGE)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
message(STATUS "Enable code coverage")
set(COVERAGE_FLAGS "--coverage")
else()
message(WARNING "Code coverage is only supported for GCC/Clang")
endif()
endif()

add_subdirectory(deps/fmt)
add_subdirectory(src)

# Installation
install(TARGETS myxml
EXPORT myxml
ARCHIVE DESTINATION lib)

install(DIRECTORY include/
DESTINATION include)

option(BUILD_TESTING "Build the testing tree." ON)

# Testing
if (BUILD_TESTING)
enable_testing()

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ int main()

For more examples, read [examples](./docs/examples.md) and test files [here](./tests/).

## Code Coverage

**myxml** support code coverage analysis via [Codecov](https://about.codecov.io/). Install Codecov browser extension to visualize which lines of code are covered and which are not.

## Inspiration

This repo is inspired by [tinyxml2](https://github.com/leethomason/tinyxml2), [pugixml](https://github.com/zeux/pugixml) and [nlohmann/json](https://github.com/nlohmann/json).
8 changes: 7 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ add_library(myxml ${sources})

target_include_directories(myxml PUBLIC ${PROJECT_SOURCE_DIR}/include/)

target_link_libraries(myxml PRIVATE fmt::fmt)
target_link_libraries(myxml PRIVATE fmt::fmt)

if (ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
message(STATUS "Adding cover flags ${COVERAGE_FLAGS}.")
target_compile_options(myxml PRIVATE ${COVERAGE_FLAGS})
target_link_options(myxml PRIVATE ${COVERAGE_FLAGS})
endif()
5 changes: 5 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ function(add_custom_test test_name)
target_compile_features(${test_name} PRIVATE cxx_std_17)
target_compile_features(${test_name} PRIVATE cxx_std_17)
add_test(NAME ${test_name} COMMAND ${test_name} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

if (ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(${test_name} PRIVATE ${COVERAGE_FLAGS})
target_link_options(${test_name} PRIVATE ${COVERAGE_FLAGS})
endif()
endfunction()

add_custom_test(element_test)
Expand Down

0 comments on commit b0b1f02

Please sign in to comment.