Skip to content

Commit 2cfc863

Browse files
Try to do test coverage using clang and see what happens
1 parent 0855a7d commit 2cfc863

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

.github/workflows/sonarcloud.yaml

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ jobs:
2626
with:
2727
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
2828

29-
- name: Install gcovr
30-
run: pip install gcovr==8.2
31-
3229
- name: Install sonar-scanner and build-wrapper
3330
uses: SonarSource/sonarcloud-github-c-cpp@v3
3431

@@ -61,41 +58,27 @@ jobs:
6158
-DENABLE_COVERAGE=ON \
6259
-DENABLE_CLANG_TIDY=OFF
6360
64-
- name: Build the code in debug mode
61+
- name: Build the code
6562
timeout-minutes: 30
66-
run: cmake --build build/ --config Debug
63+
run: cmake --build build/ --config Release
6764

6865
- name: Run tests to generate coverage statistics
6966
timeout-minutes: 10
7067
working-directory: build
71-
run: ninja run_all_tests -j1 -k 0
68+
run: ninja coverage -k 0
7269

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

79-
- name: Collect coverage into one XML report
80-
if: ${{ !cancelled() }}
81-
run: |
82-
gcovr \
83-
--root . \
84-
--object-directory build \
85-
--force-color \
86-
--no-markers \
87-
--decisions \
88-
--calls \
89-
--exclude-noncode-lines \
90-
--gcov-ignore-parse-errors negative_hits.warn \
91-
--sonarqube "coverage.xml"
92-
9376
- name: Upload coverage report
9477
if: ${{ !cancelled() }}
9578
uses: actions/upload-artifact@v4
9679
with:
97-
name: coverage-sonar
98-
path: coverage.xml
80+
name: coverage
81+
path: build/reports/coverage.txt
9982
retention-days: 1 # This sets the artifact TTL to 1 day (minimum is 1 day)
10083
overwrite: true
10184

@@ -111,7 +94,7 @@ jobs:
11194
--define sonar.sources=src \
11295
--define sonar.tests=tests \
11396
--define sonar.cfamily.compile-commands=build/compile_commands.json \
114-
--define sonar.coverageReportPaths=coverage.xml
97+
--define sonar.cfamily.llvm-cov.reportPath=build/reports/coverage.txt
11598
11699
- name: Upload Traces
117100
if: ${{ !cancelled() }}

cmake/TestRunner.cmake

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,68 @@ get_all_catch_test_targets(all_targets ${PROJECT_SOURCE_DIR})
3636

3737
# Create a custom command for each test target to run it
3838
# Make sure that coverage data is written with paths relative to the source directory
39-
unset(reports)
39+
unset(report_outputs)
40+
unset(coverage_reports)
4041
foreach(target ${all_targets})
4142

42-
set(sonarqube_report_file "${PROJECT_BINARY_DIR}/reports/tests/${target}.sonarqube.xml")
43+
unset(command_prefix)
44+
if(ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
45+
# Extra output files for the profile data
46+
set(raw_coverage "${PROJECT_BINARY_DIR}/reports/tests/${target}.profraw")
47+
set(indexed_coverage "${PROJECT_BINARY_DIR}/reports/tests/${target}.profdata")
48+
set(coverage_report "${PROJECT_BINARY_DIR}/reports/tests/${target}.txt")
49+
set(command_prefix "cmake" "-E" "env" "LLVM_PROFILE_FILE=${raw_coverage}")
50+
list(APPEND coverage_reports ${coverage_report})
51+
endif()
52+
4353
set(junit_report_file "${PROJECT_BINARY_DIR}/reports/tests/${target}.junit.xml")
44-
list(APPEND reports ${sonarqube_report_file} ${junit_report_file})
54+
list(APPEND report_outputs ${junit_report_file})
4555
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}
56+
OUTPUT ${junit_report_file} ${raw_coverage}
57+
COMMAND ${command_prefix} $<TARGET_FILE:${target}> --reporter console --reporter JUnit::out=${junit_report_file}
4958
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
59+
DEPENDS ${target}
5060
USES_TERMINAL
5161
COMMENT "Running test ${target}"
5262
)
63+
64+
if(ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
65+
add_custom_command(
66+
OUTPUT ${indexed_coverage}
67+
COMMAND llvm-profdata merge -sparse ${raw_coverage} -o ${indexed_coverage}
68+
DEPENDS ${raw_coverage}
69+
)
70+
71+
add_custom_command(
72+
OUTPUT ${coverage_report}
73+
COMMAND llvm-cov show --show-branches=count -Xdemangler c++filt
74+
-instr-profile=${CMAKE_CURRENT_BINARY_DIR}/coverage.profdata $<TARGET_FILE:${target}> > ${coverage_report}
75+
DEPENDS ${indexed_coverage}
76+
)
77+
endif()
78+
5379
endforeach()
5480

5581
# Create a custom target that depends on all test targets
5682
add_custom_target(
5783
run_all_tests
58-
DEPENDS ${reports}
84+
DEPENDS ${report_outputs} ${all_targets}
5985
COMMENT "Running all Catch2 tests"
6086
)
87+
88+
# Concatenate all the coverage reports together
89+
if(ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
90+
91+
add_custom_command(
92+
OUTPUT ${PROJECT_BINARY_DIR}/reports/coverage.txt
93+
COMMAND cat ${coverage_reports} > ${PROJECT_BINARY_DIR}/reports/coverage.txt
94+
DEPENDS ${coverage_reports}
95+
)
96+
97+
add_custom_target(
98+
coverage
99+
DEPENDS ${PROJECT_BINARY_DIR}/reports/coverage.txt
100+
COMMENT "Running all Catch2 tests with coverage"
101+
)
102+
103+
endif()

0 commit comments

Comments
 (0)