Skip to content

Commit 28d18fa

Browse files
Improve code coverage results (#154)
1 parent 9eff97f commit 28d18fa

File tree

19 files changed

+216
-47
lines changed

19 files changed

+216
-47
lines changed

.github/workflows/gcc.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,14 @@ jobs:
9292

9393
- name: Test
9494
timeout-minutes: 2
95-
working-directory: build/tests
96-
run: ctest --output-on-failure -E "dsl/UDP"
95+
working-directory: build
96+
run: ninja run_all_tests -k 0
97+
98+
- name: Test Summary
99+
if: ${{ !cancelled() }}
100+
uses: test-summary/action@v2
101+
with:
102+
paths: "build/reports/tests/*.junit.xml"
97103

98104
- name: Upload Traces
99105
if: ${{ !cancelled() }}

.github/workflows/macos.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,14 @@ jobs:
6060

6161
- name: Test
6262
timeout-minutes: 5
63-
working-directory: build/tests
64-
run: ctest --output-on-failure
63+
working-directory: build
64+
run: ninja run_all_tests -k 0
65+
66+
- name: Test Summary
67+
if: ${{ !cancelled() }}
68+
uses: test-summary/action@v2
69+
with:
70+
paths: "build/reports/tests/*.junit.xml"
6571

6672
- name: Upload Traces
6773
if: ${{ !cancelled() }}

.github/workflows/sonarcloud.yaml

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ jobs:
3333
uses: SonarSource/sonarcloud-github-c-cpp@v3
3434

3535
- name: Install CMake
36-
uses: lukka/get-cmake@latest
36+
uses: lukka/get-cmake@v3.30.5
3737
with:
38-
cmakeVersion: 3.27.1
38+
cmakeVersion: 3.30.5
3939
ninjaVersion: 1.11.1
4040

4141
- name: Setup CCache
@@ -45,14 +45,16 @@ jobs:
4545
max-size: 100M
4646

4747
- name: Configure CMake
48+
env:
49+
CXXFLAGS: -DNUCLEAR_TEST_TIME_UNIT_DEN=10
4850
run: |
4951
cmake -E make_directory build
5052
cmake -S . -B build \
5153
-GNinja \
5254
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
5355
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
54-
-DBUILD_TESTS=ON \
5556
-DCMAKE_BUILD_TYPE=Debug \
57+
-DBUILD_TESTS=ON \
5658
-DCI_BUILD=ON \
5759
-DENABLE_COVERAGE=ON \
5860
-DENABLE_CLANG_TIDY=OFF
@@ -63,12 +65,28 @@ jobs:
6365

6466
- name: Run tests to generate coverage statistics
6567
timeout-minutes: 10
66-
working-directory: build/tests
67-
run: ctest --output-on-failure
68+
working-directory: build
69+
run: ninja run_all_tests -j1 -k 0
70+
71+
- name: Test Summary
72+
if: ${{ !cancelled() }}
73+
uses: test-summary/action@v2
74+
with:
75+
paths: "build/reports/tests/*.junit.xml"
6876

6977
- name: Collect coverage into one XML report
7078
if: ${{ !cancelled() }}
71-
run: gcovr --gcov-ignore-parse-errors=negative_hits.warn_once_per_file --exclude-unreachable-branches --exclude-noncode-lines --sonarqube > coverage.xml
79+
run: |
80+
gcovr \
81+
--root . \
82+
--object-directory build \
83+
--force-color \
84+
--no-markers \
85+
--decisions \
86+
--calls \
87+
--exclude-noncode-lines \
88+
--gcov-ignore-parse-errors negative_hits.warn \
89+
--sonarqube "coverage.xml"
7290
7391
- name: Upload coverage report
7492
if: ${{ !cancelled() }}
@@ -86,6 +104,10 @@ jobs:
86104
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
87105
run: |
88106
sonar-scanner \
107+
--define sonar.projectKey=Fastcode_NUClear \
108+
--define sonar.organization=fastcode \
109+
--define sonar.sources=src \
110+
--define sonar.tests=tests \
89111
--define sonar.cfamily.compile-commands=build/compile_commands.json \
90112
--define sonar.coverageReportPaths=coverage.xml
91113

.github/workflows/windows.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,14 @@ jobs:
8181

8282
- name: Test
8383
timeout-minutes: 5
84-
working-directory: build/tests
85-
run: ctest --output-on-failure
84+
working-directory: build
85+
run: ninja run_all_tests -k 0
86+
87+
- name: Test Summary
88+
if: ${{ !cancelled() }}
89+
uses: test-summary/action@v2
90+
with:
91+
paths: "build/reports/tests/*.junit.xml"
8692

8793
- name: Upload Traces
8894
if: ${{ !cancelled() }}

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ option(BUILD_TESTS "Builds all of the NUClear unit tests." ON)
8989
if(BUILD_TESTS)
9090
enable_testing()
9191
add_subdirectory(tests)
92+
include(TestRunner)
9293
endif()
9394

9495
# Add the documentation subdirectory

cmake/TestRunner.cmake

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Collect all currently added targets in all subdirectories
2+
#
3+
# Parameters:
4+
# - _result the list containing all found targets
5+
# - _dir root directory to start looking from
6+
function(get_all_catch_test_targets _result _dir)
7+
get_property(
8+
_subdirs
9+
DIRECTORY "${_dir}"
10+
PROPERTY SUBDIRECTORIES
11+
)
12+
foreach(_subdir IN LISTS _subdirs)
13+
get_all_catch_test_targets(${_result} "${_subdir}")
14+
endforeach()
15+
16+
unset(catch_targets)
17+
get_directory_property(_sub_targets DIRECTORY "${_dir}" BUILDSYSTEM_TARGETS)
18+
foreach(target ${_sub_targets})
19+
get_target_property(target_type ${target} TYPE)
20+
if(target_type STREQUAL "EXECUTABLE")
21+
get_target_property(target_link_libraries ${target} INTERFACE_LINK_LIBRARIES)
22+
if(target_link_libraries MATCHES "Catch2::Catch2")
23+
list(APPEND catch_targets ${target})
24+
endif()
25+
endif()
26+
endforeach()
27+
28+
set(${_result}
29+
${${_result}} ${catch_targets}
30+
PARENT_SCOPE
31+
)
32+
endfunction()
33+
34+
# Find all executable targets that link to Catch2::Catch2WithMain || Catch2::Catch2
35+
get_all_catch_test_targets(all_targets ${PROJECT_SOURCE_DIR})
36+
37+
# Create a custom command for each test target to run it
38+
# Make sure that coverage data is written with paths relative to the source directory
39+
unset(reports)
40+
foreach(target ${all_targets})
41+
42+
set(sonarqube_report_file "${PROJECT_BINARY_DIR}/reports/tests/${target}.sonarqube.xml")
43+
set(junit_report_file "${PROJECT_BINARY_DIR}/reports/tests/${target}.junit.xml")
44+
list(APPEND reports ${sonarqube_report_file} ${junit_report_file})
45+
add_custom_command(
46+
OUTPUT ${sonarqube_report_file} ${junit_report_file}
47+
COMMAND $<TARGET_FILE:${target}> --reporter console --reporter SonarQube::out=${sonarqube_report_file} --reporter
48+
JUnit::out=${junit_report_file}
49+
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
50+
USES_TERMINAL
51+
COMMENT "Running test ${target}"
52+
)
53+
endforeach()
54+
55+
# Create a custom target that depends on all test targets
56+
add_custom_target(
57+
run_all_tests
58+
DEPENDS ${reports}
59+
COMMENT "Running all Catch2 tests"
60+
)

sonar-project.properties

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ target_link_libraries(nuclear ${CMAKE_THREAD_LIBS_INIT})
3737
set_target_properties(nuclear PROPERTIES POSITION_INDEPENDENT_CODE ON)
3838
target_compile_features(nuclear PUBLIC cxx_std_14)
3939

40-
# When enabling coverage, just set it on the nuclear target so it doesn't end up on catch2
41-
option(ENABLE_COVERAGE "Enable coverage support" OFF)
40+
option(ENABLE_COVERAGE "Compile with coverage support enabled.")
4241
if(ENABLE_COVERAGE)
43-
target_compile_options(nuclear PUBLIC "-fprofile-arcs" "-ftest-coverage" "-fprofile-abs-path")
44-
target_link_options(nuclear PUBLIC "-fprofile-arcs" "-ftest-coverage" "-fprofile-abs-path")
45-
endif()
42+
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
43+
message(WARNING "Coverage is enabled but not build in debug mode. Coverage results may be misleading.")
44+
endif()
45+
target_compile_options(nuclear PUBLIC -fprofile-arcs -ftest-coverage -fprofile-abs-path -fprofile-update=atomic)
46+
target_link_options(nuclear PUBLIC -fprofile-arcs)
47+
endif(ENABLE_COVERAGE)
4648

4749
# Enable warnings, and all warnings are errors
4850
if(MSVC)

src/util/platform.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,6 @@
9696

9797
#endif // _WIN32
9898

99-
/*******************************************
100-
* SHIM FOR THREAD LOCAL STORAGE *
101-
*******************************************/
102-
#if defined(__GNUC__)
103-
#define ATTRIBUTE_TLS __thread
104-
#elif defined(_WIN32)
105-
#define ATTRIBUTE_TLS __declspec(thread)
106-
#else // !__GNUC__ && !_MSC_VER
107-
#error "Define a thread local storage qualifier for your compiler/platform!"
108-
#endif
109-
11099
/*******************************************
111100
* SHIM FOR NETWORKING *
112101
*******************************************/

tests/test_util/TestBase.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <utility>
2929

3030
#include "nuclear"
31+
#include "test_util/TimeUnit.hpp" // IWYU pragma: export
3132
#include "test_util/diff_string.hpp" // IWYU pragma: export
3233

3334
namespace test_util {
@@ -57,7 +58,7 @@ class TestBase : public NUClear::Reactor {
5758
private:
5859
explicit TestBase(std::unique_ptr<NUClear::Environment> environment,
5960
const bool& shutdown_on_idle = true,
60-
const std::chrono::milliseconds& timeout = std::chrono::milliseconds(1000))
61+
const std::chrono::milliseconds& timeout = TimeUnit(20))
6162
: Reactor(std::move(environment)) {
6263

6364
// Shutdown if the system is idle

0 commit comments

Comments
 (0)