From 12743dbd9563551059c08b1c5663d423fd99eb22 Mon Sep 17 00:00:00 2001 From: adamska <2639980868@qq.com> Date: Sat, 7 Sep 2024 19:43:25 +0800 Subject: [PATCH 1/6] chore: add code coverage --- CMakeLists.txt | 17 +++++++++++++++-- src/CMakeLists.txt | 8 +++++++- tests/CMakeLists.txt | 5 +++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5589cc..7468f81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,9 +15,23 @@ 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) @@ -25,8 +39,7 @@ install(TARGETS myxml install(DIRECTORY include/ DESTINATION include) -option(BUILD_TESTING "Build the testing tree." ON) - +# Testing if (BUILD_TESTING) enable_testing() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9447d87..89d575f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,4 +4,10 @@ add_library(myxml ${sources}) target_include_directories(myxml PUBLIC ${PROJECT_SOURCE_DIR}/include/) -target_link_libraries(myxml PRIVATE fmt::fmt) \ No newline at end of file +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() \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 65da8e8..b3714cb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) From c1262a7e93fb6104695660542b3b006f07792a88 Mon Sep 17 00:00:00 2001 From: adamska <2639980868@qq.com> Date: Sat, 7 Sep 2024 19:55:11 +0800 Subject: [PATCH 2/6] ci: code coverage report --- .github/workflows/cmake-single-platform.yml | 22 +++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cmake-single-platform.yml b/.github/workflows/cmake-single-platform.yml index 93b4f62..339bcd5 100644 --- a/.github/workflows/cmake-single-platform.yml +++ b/.github/workflows/cmake-single-platform.yml @@ -22,6 +22,9 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Install lcov + run: sudo apt-get install lcov + - name: Initialize Git Submodules run: | git submodule update --init @@ -29,7 +32,7 @@ jobs: - 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 @@ -40,7 +43,22 @@ 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 + 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 --list coverage.info + + - name: Codecov + use: codecov/codecov-action@v3 + with: + token : ${{ secrets.CODECOV_TOKEN }} + files: build/coverage.info + flags: unittests + name: codecov-unittests + install: runs-on: ubuntu-latest From 2042b829300ebdf87745e6de4933248f4a99197b Mon Sep 17 00:00:00 2001 From: adamska <2639980868@qq.com> Date: Sat, 7 Sep 2024 19:55:53 +0800 Subject: [PATCH 3/6] fix: --- .github/workflows/cmake-single-platform.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cmake-single-platform.yml b/.github/workflows/cmake-single-platform.yml index 339bcd5..b33cff9 100644 --- a/.github/workflows/cmake-single-platform.yml +++ b/.github/workflows/cmake-single-platform.yml @@ -52,7 +52,7 @@ jobs: lcov --list coverage.info - name: Codecov - use: codecov/codecov-action@v3 + uses: codecov/codecov-action@v3 with: token : ${{ secrets.CODECOV_TOKEN }} files: build/coverage.info From 458c8ee38f7ab9265141b98feb6a7299ec150b72 Mon Sep 17 00:00:00 2001 From: adamska <2639980868@qq.com> Date: Sat, 7 Sep 2024 19:59:12 +0800 Subject: [PATCH 4/6] fix: --- .github/workflows/cmake-single-platform.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cmake-single-platform.yml b/.github/workflows/cmake-single-platform.yml index b33cff9..eb6ab8d 100644 --- a/.github/workflows/cmake-single-platform.yml +++ b/.github/workflows/cmake-single-platform.yml @@ -49,13 +49,14 @@ jobs: 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: build/coverage.info + files: ${{github.workspace}}/build/coverage.info flags: unittests name: codecov-unittests From 7ea9bb9406826fb8b2b2bbad2822ce9166fec2eb Mon Sep 17 00:00:00 2001 From: adamska <2639980868@qq.com> Date: Sat, 7 Sep 2024 20:02:44 +0800 Subject: [PATCH 5/6] fix: --- .github/workflows/cmake-single-platform.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/cmake-single-platform.yml b/.github/workflows/cmake-single-platform.yml index eb6ab8d..4e68e0d 100644 --- a/.github/workflows/cmake-single-platform.yml +++ b/.github/workflows/cmake-single-platform.yml @@ -45,6 +45,7 @@ jobs: 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 From 2dd55d31e2861760b1127382c10ad445e0dc3e17 Mon Sep 17 00:00:00 2001 From: adamska <2639980868@qq.com> Date: Sat, 7 Sep 2024 20:15:09 +0800 Subject: [PATCH 6/6] doc: update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 18e6202..c02cbc5 100644 --- a/README.md +++ b/README.md @@ -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).