Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code coverage results #154

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .github/workflows/gcc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@ jobs:

- name: Test
timeout-minutes: 2
working-directory: build/tests
run: ctest --output-on-failure -E "dsl/UDP"
working-directory: build
run: ninja run_all_tests -k 0

- name: Test Summary
if: ${{ !cancelled() }}
uses: test-summary/action@v2
with:
paths: "build/reports/tests/*.junit.xml"

- name: Upload Traces
if: always()
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: traces-gcc-${{ matrix.toolchain.version }}
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ jobs:

- name: Test
timeout-minutes: 5
working-directory: build/tests
run: ctest --output-on-failure
working-directory: build
run: ninja run_all_tests -k 0

- name: Test Summary
if: ${{ !cancelled() }}
uses: test-summary/action@v2
with:
paths: "build/reports/tests/*.junit.xml"

- name: Upload Traces
if: always()
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: traces-macos
Expand Down
42 changes: 31 additions & 11 deletions .github/workflows/sonarcloud.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ jobs:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

- name: Install gcovr
run: pip install gcovr==7.2
run: pip install gcovr==8.2

- name: Install sonar-scanner and build-wrapper
uses: SonarSource/sonarcloud-github-c-cpp@v3

- name: Install CMake
uses: lukka/get-cmake@latest
uses: lukka/get-cmake@v3.30.5
with:
cmakeVersion: 3.27.1
cmakeVersion: 3.30.5
ninjaVersion: 1.11.1

- name: Setup CCache
Expand All @@ -49,12 +49,12 @@ jobs:
cmake -E make_directory build
cmake -S . -B build \
-GNinja \
-DCMAKE_CXX_FLAGS="--coverage" \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-DBUILD_TESTS=ON \
-DCMAKE_BUILD_TYPE=Debug \
-DBUILD_TESTS=ON \
-DCI_BUILD=ON \
-DENABLE_COVERAGE=ON \
-DENABLE_CLANG_TIDY=OFF

- name: Build the code in debug mode
Expand All @@ -63,25 +63,45 @@ jobs:

- name: Run tests to generate coverage statistics
timeout-minutes: 10
working-directory: build/tests
run: ctest --output-on-failure
working-directory: build
run: ninja run_all_tests -j1 -k 0

- name: Test Summary
if: ${{ !cancelled() }}
uses: test-summary/action@v2
with:
paths: "build/reports/tests/*.junit.xml"

- name: Collect coverage into one XML report
if: always()
run: gcovr --gcov-ignore-parse-errors=negative_hits.warn_once_per_file --exclude-unreachable-branches --exclude-noncode-lines --sonarqube > coverage.xml
if: ${{ !cancelled() }}
run: |
gcovr \
--root . \
--object-directory build \
--force-color \
--no-markers \
--decisions \
--calls \
--exclude-noncode-lines \
--gcov-ignore-parse-errors negative_hits.warn \
--sonarqube "coverage.xml"

- name: Run sonar-scanner
if: always()
if: ${{ !cancelled() }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: |
sonar-scanner \
--define sonar.projectKey=Fastcode_NUClear \
--define sonar.organization=fastcode \
--define sonar.sources=src \
--define sonar.tests=tests \
--define sonar.cfamily.compile-commands=build/compile_commands.json \
--define sonar.coverageReportPaths=coverage.xml

- name: Upload Traces
if: always()
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: traces-sonar
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ jobs:

- name: Test
timeout-minutes: 5
working-directory: build/tests
run: ctest --output-on-failure
working-directory: build
run: ninja run_all_tests -k 0

- name: Test Summary
if: ${{ !cancelled() }}
uses: test-summary/action@v2
with:
paths: "build/reports/tests/*.junit.xml"

- name: Upload Traces
if: always()
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: traces-windows-${{ matrix.toolchain.name }}
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ option(BUILD_TESTS "Builds all of the NUClear unit tests." ON)
if(BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
include(TestRunner)
endif()

# Add the documentation subdirectory
Expand Down
59 changes: 59 additions & 0 deletions cmake/TestRunner.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Collect all currently added targets in all subdirectories
#
# Parameters:
# - _result the list containing all found targets
# - _dir root directory to start looking from
function(get_all_catch_test_targets _result _dir)
get_property(
_subdirs
DIRECTORY "${_dir}"
PROPERTY SUBDIRECTORIES
)
foreach(_subdir IN LISTS _subdirs)
get_all_catch_test_targets(${_result} "${_subdir}")
endforeach()

unset(catch_targets)
get_directory_property(_sub_targets DIRECTORY "${_dir}" BUILDSYSTEM_TARGETS)
foreach(target ${_sub_targets})
get_target_property(target_type ${target} TYPE)
if(target_type STREQUAL "EXECUTABLE")
get_target_property(target_link_libraries ${target} INTERFACE_LINK_LIBRARIES)
if(target_link_libraries MATCHES "Catch2::Catch2")
list(APPEND catch_targets ${target})
endif()
endif()
endforeach()

set(${_result}
${${_result}} ${catch_targets}
PARENT_SCOPE
)
endfunction()

# Find all executable targets that link to Catch2::Catch2WithMain || Catch2::Catch2
get_all_catch_test_targets(all_targets ${PROJECT_SOURCE_DIR})

# Create a custom command for each test target to run it
# Make sure that coverage data is written with paths relative to the source directory
unset(reports)
foreach(target ${all_targets})

set(sonarqube_report_file "${PROJECT_BINARY_DIR}/reports/tests/${target}.sonarqube.xml")
set(junit_report_file "${PROJECT_BINARY_DIR}/reports/tests/${target}.junit.xml")
list(APPEND reports ${sonarqube_report_file} ${junit_report_file})
add_custom_command(
OUTPUT ${sonarqube_report_file} ${junit_report_file}
COMMAND $<TARGET_FILE:${target}> --reporter console --reporter SonarQube::out=${sonarqube_report_file} --reporter
JUnit::out=${junit_report_file}
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
COMMENT "Running test ${target}"
)
endforeach()

# Create a custom target that depends on all test targets
add_custom_target(
run_all_tests
DEPENDS ${reports}
COMMENT "Running all Catch2 tests"
)
9 changes: 0 additions & 9 deletions sonar-project.properties

This file was deleted.

9 changes: 9 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ target_link_libraries(nuclear ${CMAKE_THREAD_LIBS_INIT})
set_target_properties(nuclear PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_compile_features(nuclear PUBLIC cxx_std_14)

option(ENABLE_COVERAGE "Compile with coverage support enabled.")
if(ENABLE_COVERAGE)
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
message(WARNING "Coverage is enabled but not build in debug mode. Coverage results may be misleading.")
endif()
target_compile_options(nuclear PUBLIC -fprofile-arcs -ftest-coverage -fprofile-abs-path -fprofile-update=atomic)
target_link_options(nuclear PUBLIC -fprofile-arcs)
endif(ENABLE_COVERAGE)

# Enable warnings, and all warnings are errors
if(MSVC)
target_compile_options(nuclear PRIVATE /W4 /WX)
Expand Down
Loading