Skip to content

Commit

Permalink
Add code coverage (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntadej authored Nov 21, 2023
1 parent 4632ab3 commit 3b0db29
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 3 deletions.
43 changes: 42 additions & 1 deletion .github/workflows/Linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
qt_version: 6.6.0
qt_modules: qtlocation qtpositioning
compiler: gcc-13
gcov: gcov-13

steps:
- name: Checkout
Expand Down Expand Up @@ -89,6 +90,29 @@ jobs:
with:
compiler: ${{ matrix.compiler }}

- name: Install lcov
if: matrix.compiler != 'default'
run: |
sudo apt-get install \
libcapture-tiny-perl \
libdatetime-perl \
libdatetime-format-dateparse-perl \
libgd-perl \
libgd3 \
libjbig0 \
libjpeg-turbo8 \
libjpeg8 \
libjson-perl \
libperlio-gzip-perl \
libtiff5 \
libwebp7
git clone https://github.com/linux-test-project/lcov.git -b v2.0
pushd lcov
sudo make -j$(nproc) install
popd
rm -rf lcov
- name: Download Qt
uses: jurplel/install-qt-action@v3
with:
Expand Down Expand Up @@ -121,14 +145,17 @@ jobs:
env:
CC: ${{ steps.install_compiler.outputs.cc }}
CXX: ${{ steps.install_compiler.outputs.cxx }}
GCOV: ${{ matrix.gcov }}
run: |
mkdir build && cd build
qt-cmake ../source/ \
-G Ninja \
-DCMAKE_BUILD_TYPE="RelWithDebInfo" \
-DCMAKE_C_COMPILER_LAUNCHER="ccache" \
-DCMAKE_CXX_COMPILER_LAUNCHER="ccache" \
-DCMAKE_INSTALL_PREFIX="../install"
-DCMAKE_INSTALL_PREFIX="../install" \
-DMLN_QT_WITH_COVERAGE=ON \
-DGCOV_PATH="$(which ${GCOV})"
ninja
ninja install
Expand All @@ -139,6 +166,13 @@ jobs:
run: ctest --output-on-failure
working-directory: build

- name: Run code coverage
if: matrix.qt_series == 6 && matrix.compiler != 'default'
uses: coactions/setup-xvfb@v1
with:
run: ninja coverage
working-directory: build

- name: Build QtQuick Example (Qt6)
if: matrix.qt_series == 6 && matrix.compiler != 'default'
run: |
Expand Down Expand Up @@ -179,6 +213,13 @@ jobs:
name: QMapLibre_Linux_${{ matrix.qt_version }}
path: QMapLibre_Linux.tar.bz2

- name: Upload coverage reports to Codecov
if: matrix.qt_series == 6 && matrix.compiler != 'default'
uses: codecov/codecov-action@v3
with:
files: build/coverage.info
verbose: true

release:
name: Release QMapLibre
if: github.ref_type == 'tag'
Expand Down
24 changes: 22 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ set(MLN_QT_QML_PLUGIN declarative_locationplugin_maplibre)
include(cmake/helpers.cmake)

# Options
set(MLN_QT_WITH_LOCATION ON CACHE BOOL "Build QMapLibreLocation")
set(MLN_QT_WITH_WIDGETS ON CACHE BOOL "Build QMapLibreWidgets")
option(MLN_QT_WITH_LOCATION "Build QMapLibreLocation" ON)
option(MLN_QT_WITH_WIDGETS "Build QMapLibreWidgets" ON)
option(MLN_QT_WITH_COVERAGE "Build QMapLibre with code coverage collection" OFF)

# Find Qt
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
Expand Down Expand Up @@ -73,9 +74,28 @@ elseif(MSVC OR CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(CMAKE_DEBUG_POSTFIX "d")
endif()

# Common configuration
add_library(CompilerOptions INTERFACE)
target_compile_options(
CompilerOptions
INTERFACE
$<$<BOOL:${MLN_QT_WITH_COVERAGE}>:--coverage>
)
target_link_libraries(
CompilerOptions
INTERFACE
$<$<BOOL:${MLN_QT_WITH_COVERAGE}>:--coverage>
)

# Enable testing
enable_testing()

# Add subdirectories
add_subdirectory(src)
add_subdirectory(test)

# Coverage
if(MLN_QT_WITH_COVERAGE)
include(cmake/coverage.cmake)
setup_target_for_coverage(coverage ${CMAKE_CTEST_COMMAND} "${CMAKE_BINARY_DIR}/coverage" "-V")
endif()
65 changes: 65 additions & 0 deletions cmake/coverage.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
find_program(GCOV_PATH gcov)
find_program(LCOV_PATH lcov)
find_program(GENHTML_PATH genhtml)

# Param _targetname The name of new the custom make target
# Param _testrunner The name of the target which runs the tests.
# MUST return ZERO always, even on errors.
# If not, no coverage report will be created!
# Param _outputname lcov output is generated as _outputname.info
# HTML report is generated in _outputname/index.html
# Optional fourth parameter is passed as arguments to _testrunner
# Pass them in list form, e.g.: "-j;2" for -j 2
function(setup_target_for_coverage _targetname _testrunner _outputname)
if(NOT GCOV_PATH)
message(FATAL_ERROR "gcov not found! Aborting...")
endif()

if(NOT LCOV_PATH)
message(FATAL_ERROR "lcov not found! Aborting...")
endif()

if(NOT GENHTML_PATH)
message(FATAL_ERROR "genhtml not found! Aborting...")
endif()

# Setup target
add_custom_target(
${_targetname}
# Cleanup and prepare lcov
${LCOV_PATH} --directory src --zerocounters
COMMAND
${LCOV_PATH} --directory src --capture --initial
--ignore-errors inconsistent,inconsistent
--output-file ${_outputname}.info.initial
--gcov-tool ${GCOV_PATH}
# Run tests
COMMAND ${_testrunner} ${ARGV3}
# Capturing lcov counters and generating report
COMMAND
${LCOV_PATH} --directory src --capture
--ignore-errors inconsistent,inconsistent
--output-file ${_outputname}.info.full
--gcov-tool ${GCOV_PATH}
COMMAND
${LCOV_PATH} --remove ${_outputname}.info.full
'/usr/*' '*/Qt/*'
'*/examples/*' '*/vendor/*'
'${CMAKE_BINARY_DIR}*'
--ignore-errors inconsistent,inconsistent,unused
--output-file ${_outputname}.info
--gcov-tool ${GCOV_PATH}
COMMAND
${GENHTML_PATH} -t "${PROJECT_NAME}" -o ${_outputname}
${_outputname}.info -p "${CMAKE_SOURCE_DIR}"
--ignore-errors inconsistent,inconsistent
COMMAND
${LCOV_PATH} --summary ${_outputname}.info --ignore-errors inconsistent,inconsistent > ${_outputname}.summary
COMMAND
${CMAKE_COMMAND} -E remove ${_outputname}.info.initial ${_outputname}.info.full
WORKING_DIRECTORY
${CMAKE_BINARY_DIR}
COMMENT
"Resetting code coverage counters to zero. \nProcessing code coverage counters and generating report."
)
endfunction()
13 changes: 13 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ignore:
- "examples/"
- "test/"
- "vendor/"

coverage:
status:
project:
default:
threshold: 1%

fixes:
- "source/::"
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ target_link_libraries(
$<BUILD_INTERFACE:mbgl-compiler-options>
$<BUILD_INTERFACE:mbgl-core-interface>
$<BUILD_INTERFACE:mbgl-core>
$<BUILD_INTERFACE:CompilerOptions>
)
if (NOT MLN_QT_WITH_INTERNAL_SQLITE)
target_link_libraries(
Expand Down
1 change: 1 addition & 0 deletions src/location/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ target_link_libraries(
Qt${QT_VERSION_MAJOR}::Network
Qt${QT_VERSION_MAJOR}::LocationPrivate
$<BUILD_INTERFACE:mbgl-compiler-options>
$<BUILD_INTERFACE:CompilerOptions>
)

# Apple specifics
Expand Down
2 changes: 2 additions & 0 deletions src/location/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ target_link_libraries(
Qt${QT_VERSION_MAJOR}::Location
Qt${QT_VERSION_MAJOR}::LocationPrivate
$<BUILD_INTERFACE:mbgl-compiler-options>
$<BUILD_INTERFACE:CompilerOptions>
)

# QtLocation plugin installation
Expand Down Expand Up @@ -92,6 +93,7 @@ target_link_libraries(
Location
Qt${QT_VERSION_MAJOR}::LocationPrivate
$<BUILD_INTERFACE:mbgl-compiler-options>
$<BUILD_INTERFACE:CompilerOptions>
)

# QtLocation QML extension plugin installation
Expand Down
1 change: 1 addition & 0 deletions src/widgets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ target_link_libraries(
Core
PRIVATE
$<BUILD_INTERFACE:mbgl-compiler-options>
$<BUILD_INTERFACE:CompilerOptions>
)
if (Qt6_FOUND)
target_link_libraries(
Expand Down

0 comments on commit 3b0db29

Please sign in to comment.