Skip to content

Commit 3048921

Browse files
Feature/add coverage (#10)
Added llvm-cov based coverage information for azure pipelines.
1 parent 6bdb9bf commit 3048921

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

CMakePresets.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": 6,
2+
"version": 8,
33
"cmakeMinimumRequired": {
44
"major": 3,
55
"minor": 25
@@ -14,7 +14,8 @@
1414
"CMAKE_INSTALL_PREFIX": "${sourceDir}/install/${presetName}",
1515
"CMAKE_C_COMPILER": "/opt/homebrew/opt/llvm/bin/clang",
1616
"CMAKE_CXX_COMPILER": "/opt/homebrew/opt/llvm/bin/clang++",
17-
"CMAKE_BUILD_TYPE": "Debug"
17+
"CMAKE_BUILD_TYPE": "Debug",
18+
"timethis_BUILD_TESTS": "ON"
1819
},
1920
"condition": {
2021
"type": "equals",
@@ -49,7 +50,8 @@
4950
"cacheVariables": {
5051
"CMAKE_BUILD_TYPE": "Debug",
5152
"CMAKE_C_COMPILER": "/usr/bin/clang",
52-
"CMAKE_CXX_COMPILER": "/usr/bin/clang++"
53+
"CMAKE_CXX_COMPILER": "/usr/bin/clang++",
54+
"timethis_BUILD_TESTS": "ON"
5355
},
5456
"condition": {
5557
"type": "equals",
@@ -112,7 +114,8 @@
112114
"strategy": "external"
113115
},
114116
"cacheVariables": {
115-
"CMAKE_BUILD_TYPE": "Debug"
117+
"CMAKE_BUILD_TYPE": "Debug",
118+
"timethis_BUILD_TESTS": "ON"
116119
}
117120
},
118121
{
@@ -221,7 +224,8 @@
221224
"configurations": [
222225
"Apple-Release"
223226
]
224-
}, {
227+
},
228+
{
225229
"name": "Linux-Release",
226230
"description": "Package for Linux",
227231
"displayName": "Linux-Package (Release)",

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ TimeThis : Simple stopwatch for scope
55
![](https://img.shields.io/nuget/v/SiddiqSoft.TimeThis)
66
![](https://img.shields.io/github/v/tag/SiddiqSoft/TimeThis)
77
![](https://img.shields.io/azure-devops/tests/siddiqsoft/siddiqsoft/11)
8-
8+
![Coverage](https://img.shields.io/azure-devops/coverage/siddiqsoft/siddiqsoft/11)
99

1010
# Objective
1111
Provide for a simple utility class where we can time the operation of a code block and allow

azure-pipelines.yml

+31-11
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,6 @@ stages:
7070
useConfigFile: true
7171
configFilePath: 'GitVersion.yml'
7272

73-
#- script: echo 'Set build version $(build.buildNumber)'
74-
# displayName: 'Set build version to $(GitVersion.MajorMinorPatch).$(GitVersion.CommitsSinceVersionSource)'
75-
# env:
76-
# Action: '##vso[build.updatebuildnumber]'
77-
# BuildVersion: $(GitVersion.MajorMinorPatch).$(GitVersion.CommitsSinceVersionSource)
78-
79-
#- script: |
80-
# echo "Updating the version for this build: BuildId:$(build.buildid) Build.buildNumber:$(build.buildNumber) build.sourceversion:$(Build.SourceVersion)"
81-
# echo "##vso[task.setvariable variable=buildFullSemVer;isoutput=true]$(GitVersion.MajorMinorPatch).$(GitVersion.CommitsSinceVersionSource)"
82-
# name: passOutput
83-
8473
# Set the flags for testing, build here.
8574
# timethis_BUILD_TESTS=ON
8675
# timethis_BUILD_PACKAGE=OFF
@@ -103,6 +92,31 @@ stages:
10392
condition: succeeded()
10493
continueOnError: false
10594

95+
# Try for coverage (but only on Linux build machines)
96+
- task: CMake@1
97+
displayName: 'CTest Coverage (Linux build only)'
98+
inputs:
99+
cmakeArgs: '-E chdir $(System.DefaultWorkingDirectory)/build/$(cmake.preset) ctest $(System.DefaultWorkingDirectory)/build/$(cmake.preset) --output-junit $(System.DefaultWorkingDirectory)/build/$(cmake.preset)/tests/results/test_detail.xml --output-on-failure -T Test -T Coverage'
100+
condition: and(succeeded(), startsWith(variables['Agent.OS'], 'Linux'))
101+
continueOnError: false
102+
103+
- task: CmdLine@2
104+
displayName: 'Coverage collection (Linux builds Only)'
105+
inputs:
106+
script: |
107+
#tree
108+
#gcovr -e $(System.DefaultWorkingDirectory)/build/$(cmake.preset)/_deps/ --cobertura-pretty --cobertura $(System.DefaultWorkingDirectory)/build/$(cmake.preset)/tests/results/coverage.xml
109+
echo "Profile data show..functions.."
110+
llvm-profdata show -all-functions -counts -ic-targets -output=tests/llvm_prof.log tests/default.profraw
111+
echo "Profile data merge............"
112+
llvm-profdata merge -output=tests/merge.out -instr tests/default.profraw
113+
echo "Profile data show..html......."
114+
llvm-cov show -format=html tests/timethis_tests --instr-profile=tests/merge.out -o tests/results/coverage.info
115+
echo "Profile data export..lcov....."
116+
llvm-cov export tests/timethis_tests --instr-profile=tests/merge.out -format=lcov > tests/results/coverage.lcov
117+
workingDirectory: "$(System.DefaultWorkingDirectory)/build/$(cmake.preset)"
118+
condition: and(succeeded(), startsWith(variables['Agent.OS'], 'Linux'))
119+
106120
- task: PublishTestResults@2
107121
displayName: 'Publish $(buildConfiguration) Test Results test_detail.xml'
108122
inputs:
@@ -113,6 +127,12 @@ stages:
113127
continueOnError: false
114128
condition: succeeded()
115129

130+
- task: PublishCodeCoverageResults@2
131+
displayName: 'Publish Coverage Results (Linux builds)'
132+
inputs:
133+
summaryFileLocation: '$(System.DefaultWorkingDirectory)/build/$(cmake.preset)/tests/results/coverage.*'
134+
condition: and(succeeded(), startsWith(variables['Agent.OS'], 'Linux'))
135+
116136
################################## STAGE ####################################
117137
# STAGE: Pack_n_Publish #
118138
#############################################################################

tests/CMakeLists.txt

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.25)
33
if(${${PROJECT_NAME}_BUILD_TESTS})
44
include(FetchContent)
55
include(GNUInstallDirs)
6+
include(GoogleTest)
67

78
set(TESTPROJ ${PROJECT_NAME}_tests)
89

@@ -19,17 +20,19 @@ if(${${PROJECT_NAME}_BUILD_TESTS})
1920
enable_testing()
2021

2122
target_compile_features(${TESTPROJ} PRIVATE cxx_std_20)
22-
2323
target_compile_options( ${TESTPROJ}
2424
PRIVATE
2525
$<$<CXX_COMPILER_ID:MSVC>:/std:c++20> )
26-
27-
include(GoogleTest)
28-
2926
target_link_libraries(${TESTPROJ} PRIVATE GTest::gtest_main)
27+
gtest_discover_tests(${TESTPROJ} XML_OUTPUT_DIR "${PROJECT_SOURCE_DIR}/tests/results")
3028

31-
include(CTest)
29+
# For GCC/Clang
30+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -fprofile-arcs -ftest-coverage -fprofile-instr-generate -fcoverage-mapping")
31+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -fprofile-arcs -ftest-coverage -fprofile-instr-generate -fcoverage-mapping")
32+
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -fprofile-instr-generate")
33+
message(STATUS "Setting compiler flags for coverage: ${CMAKE_CXX_FLAGS}")
34+
message(STATUS "Setting linker flags for coverage : ${CMAKE_LINKER_FLAGS}")
3235

33-
gtest_discover_tests(${TESTPROJ} XML_OUTPUT_DIR "${PROJECT_SOURCE_DIR}/tests/results")
36+
include(CTest)
3437
message(STATUS " Finished configuring for ${PROJECT_NAME}_BUILD_TESTS = ${${PROJECT_NAME}_BUILD_TESTS}")
3538
endif()

0 commit comments

Comments
 (0)